Use INVALID_ID instead of optional and clean up

This commit is contained in:
Fangrui Song 2018-01-19 23:55:48 +08:00
parent b6539a2639
commit c5bb14e86b
4 changed files with 29 additions and 30 deletions

View File

@ -352,7 +352,7 @@ void ParseFile(Config* config,
if (!indexes) { if (!indexes) {
if (config->enableIndexing && if (config->enableIndexing &&
std::holds_alternative<int64_t>(request.id)) { !std::holds_alternative<std::monostate>(request.id)) {
Out_Error out; Out_Error out;
out.id = request.id; out.id = request.id;
out.error.code = lsErrorCodes::InternalError; out.error.code = lsErrorCodes::InternalError;

View File

@ -187,11 +187,10 @@ struct CqueryCallTreeExpandHandler
Out_CqueryCallTree out; Out_CqueryCallTree out;
out.id = request->id; out.id = request->id;
// FIXME // FIXME Change VSCode plugin to use number representation of USR hash
optional<QueryFuncId> func_id = QueryFuncId func_id = db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr));
db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr)); if (func_id.id != QueryFuncId::INVALID_ID)
if (func_id) out.result = BuildExpandCallTree(db, working_files, func_id);
out.result = BuildExpandCallTree(db, working_files, func_id.value());
QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out); QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out);
} }

View File

@ -281,14 +281,14 @@ QueryFile::Def BuildFileDef(const IdMap& id_map, const IndexFile& indexed) {
} // namespace } // namespace
optional<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db, QueryFileId GetQueryFileIdFromPath(QueryDatabase* query_db,
const std::string& path, const std::string& path,
bool create_if_missing) { bool create_if_missing) {
auto it = query_db->usr_to_file.find(LowerPathIfCaseInsensitive(path)); auto it = query_db->usr_to_file.find(LowerPathIfCaseInsensitive(path));
if (it != query_db->usr_to_file.end()) if (it != query_db->usr_to_file.end())
return QueryFileId(it->second.id); return QueryFileId(it->second.id);
if (!create_if_missing) if (!create_if_missing)
return {}; return QueryFileId(QueryFileId::INVALID_ID);
size_t idx = query_db->files.size(); size_t idx = query_db->files.size();
query_db->usr_to_file[LowerPathIfCaseInsensitive(path)] = QueryFileId(idx); query_db->usr_to_file[LowerPathIfCaseInsensitive(path)] = QueryFileId(idx);
@ -296,14 +296,14 @@ optional<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db,
return QueryFileId(idx); return QueryFileId(idx);
} }
optional<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db, QueryTypeId GetQueryTypeIdFromUsr(QueryDatabase* query_db,
Usr usr, Usr usr,
bool create_if_missing) { bool create_if_missing) {
auto it = query_db->usr_to_type.find(usr); auto it = query_db->usr_to_type.find(usr);
if (it != query_db->usr_to_type.end()) if (it != query_db->usr_to_type.end())
return QueryTypeId(it->second.id); return QueryTypeId(it->second.id);
if (!create_if_missing) if (!create_if_missing)
return {}; return QueryTypeId(QueryTypeId::INVALID_ID);
size_t idx = query_db->types.size(); size_t idx = query_db->types.size();
query_db->usr_to_type[usr] = QueryTypeId(idx); query_db->usr_to_type[usr] = QueryTypeId(idx);
@ -311,29 +311,28 @@ optional<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db,
return QueryTypeId(idx); return QueryTypeId(idx);
} }
optional<QueryFuncId> GetQueryFuncIdFromUsr(QueryDatabase* query_db, QueryFuncId GetQueryFuncIdFromUsr(QueryDatabase* query_db,
Usr usr, Usr usr,
bool create_if_missing) { bool create_if_missing) {
auto it = query_db->usr_to_func.find(usr); auto it = query_db->usr_to_func.find(usr);
if (it != query_db->usr_to_func.end()) if (it != query_db->usr_to_func.end())
return QueryFuncId(it->second.id); return QueryFuncId(it->second.id);
if (!create_if_missing) if (!create_if_missing)
return {}; return QueryFuncId(QueryFuncId::INVALID_ID);
size_t idx = query_db->funcs.size(); size_t idx = query_db->funcs.size();
query_db->usr_to_func[usr] = QueryFuncId(idx); query_db->usr_to_func[usr] = QueryFuncId(idx);
query_db->funcs.push_back(QueryFunc(usr)); query_db->funcs.push_back(QueryFunc(usr));
return QueryFuncId(idx); return QueryFuncId(idx);
} }
optional<QueryVarId> GetQueryVarIdFromUsr(QueryDatabase* query_db, QueryVarId GetQueryVarIdFromUsr(QueryDatabase* query_db,
Usr usr, Usr usr,
bool create_if_missing) { bool create_if_missing) {
auto it = query_db->usr_to_var.find(usr); auto it = query_db->usr_to_var.find(usr);
if (it != query_db->usr_to_var.end()) if (it != query_db->usr_to_var.end())
return QueryVarId(it->second.id); return QueryVarId(it->second.id);
if (!create_if_missing) if (!create_if_missing)
return {}; return QueryVarId(QueryVarId::INVALID_ID);
size_t idx = query_db->vars.size(); size_t idx = query_db->vars.size();
query_db->usr_to_var[usr] = QueryVarId(idx); query_db->usr_to_var[usr] = QueryVarId(idx);
@ -341,43 +340,42 @@ optional<QueryVarId> GetQueryVarIdFromUsr(QueryDatabase* query_db,
return QueryVarId(idx); return QueryVarId(idx);
} }
optional<QueryFileId> QueryDatabase::GetQueryFileIdFromPath( QueryFileId QueryDatabase::GetQueryFileIdFromPath(
const std::string& path) { const std::string& path) {
return ::GetQueryFileIdFromPath(this, path, false); return ::GetQueryFileIdFromPath(this, path, false);
} }
optional<QueryTypeId> QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) { QueryTypeId QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) {
return ::GetQueryTypeIdFromUsr(this, usr, false); return ::GetQueryTypeIdFromUsr(this, usr, false);
} }
optional<QueryFuncId> QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) { QueryFuncId QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) {
return ::GetQueryFuncIdFromUsr(this, usr, false); return ::GetQueryFuncIdFromUsr(this, usr, false);
} }
optional<QueryVarId> QueryDatabase::GetQueryVarIdFromUsr(Usr usr) { QueryVarId QueryDatabase::GetQueryVarIdFromUsr(Usr usr) {
return ::GetQueryVarIdFromUsr(this, usr, false); return ::GetQueryVarIdFromUsr(this, usr, false);
} }
IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids) IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids)
: local_ids(local_ids) { : local_ids(local_ids) {
// LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file; // LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file;
primary_file = primary_file = GetQueryFileIdFromPath(query_db, local_ids.primary_file, true);
GetQueryFileIdFromPath(query_db, local_ids.primary_file, true).value();
cached_type_ids_.resize(local_ids.type_id_to_usr.size()); cached_type_ids_.resize(local_ids.type_id_to_usr.size());
for (const auto& entry : local_ids.type_id_to_usr) for (const auto& entry : local_ids.type_id_to_usr)
cached_type_ids_[entry.first] = cached_type_ids_[entry.first] =
GetQueryTypeIdFromUsr(query_db, entry.second, true).value(); GetQueryTypeIdFromUsr(query_db, entry.second, true);
cached_func_ids_.resize(local_ids.func_id_to_usr.size()); cached_func_ids_.resize(local_ids.func_id_to_usr.size());
for (const auto& entry : local_ids.func_id_to_usr) for (const auto& entry : local_ids.func_id_to_usr)
cached_func_ids_[entry.first] = cached_func_ids_[entry.first] =
GetQueryFuncIdFromUsr(query_db, entry.second, true).value(); GetQueryFuncIdFromUsr(query_db, entry.second, true);
cached_var_ids_.resize(local_ids.var_id_to_usr.size()); cached_var_ids_.resize(local_ids.var_id_to_usr.size());
for (const auto& entry : local_ids.var_id_to_usr) for (const auto& entry : local_ids.var_id_to_usr)
cached_var_ids_[entry.first] = cached_var_ids_[entry.first] =
GetQueryVarIdFromUsr(query_db, entry.second, true).value(); GetQueryVarIdFromUsr(query_db, entry.second, true);
} }
QueryLocation IdMap::ToQuery(Range range) const { QueryLocation IdMap::ToQuery(Range range) const {

View File

@ -368,10 +368,12 @@ struct QueryDatabase {
const std::string& detailed_name); const std::string& detailed_name);
// Query the indexing structure to look up symbol id for given Usr. // Query the indexing structure to look up symbol id for given Usr.
optional<QueryFileId> GetQueryFileIdFromPath(const std::string& path); // In case the given Usr does not exist in the indexing structure,
optional<QueryTypeId> GetQueryTypeIdFromUsr(Usr usr); // |INVALID_ID| will be returned
optional<QueryFuncId> GetQueryFuncIdFromUsr(Usr usr); QueryFileId GetQueryFileIdFromPath(const std::string& path);
optional<QueryVarId> GetQueryVarIdFromUsr(Usr usr); QueryTypeId GetQueryTypeIdFromUsr(Usr usr);
QueryFuncId GetQueryFuncIdFromUsr(Usr usr);
QueryVarId GetQueryVarIdFromUsr(Usr usr);
}; };
struct IdMap { struct IdMap {