From 6f071cfeb85cbe7ed0034caf9ad1bbf9f8f984b5 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Fri, 7 Apr 2017 01:01:58 -0700 Subject: [PATCH] wip IdMap --- src/command_line.cc | 8 +-- src/query.cc | 153 +++++++++++++++++++++++++++++++++++++++----- src/query.h | 58 +++++++++-------- 3 files changed, 174 insertions(+), 45 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index 653c77a4..6c76bd99 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -279,7 +279,7 @@ void IndexMain(IndexRequestQueue* requests, IndexResponseQueue* responses) { // TODO/FIXME/TODO // TODO/FIXME/TODO // TODO: We need to create IdMap on QueryDb thread. - IdMap old_id_map(old_index->id_cache); + IdMap old_id_map(nullptr, old_index->id_cache); // TODO/FIXME/TODO // TODO/FIXME/TODO // TODO/FIXME/TODO @@ -314,8 +314,8 @@ void IndexMain(IndexRequestQueue* requests, IndexResponseQueue* responses) { // TODO/FIXME/TODO // TODO/FIXME/TODO // TODO: We need to create IdMap on QueryDb thread. - IdMap old_id_map(old_index->id_cache); - IdMap new_id_map(new_index.id_cache); + IdMap old_id_map(nullptr, old_index->id_cache); + IdMap new_id_map(nullptr, new_index.id_cache); // TODO/FIXME/TODO // TODO/FIXME/TODO // TODO/FIXME/TODO @@ -336,7 +336,7 @@ void IndexMain(IndexRequestQueue* requests, IndexResponseQueue* responses) { // TODO/FIXME/TODO // TODO/FIXME/TODO // TODO: We need to create IdMap on QueryDb thread. - IdMap new_id_map(new_index.id_cache); + IdMap new_id_map(nullptr, new_index.id_cache); // TODO/FIXME/TODO // TODO/FIXME/TODO // TODO/FIXME/TODO diff --git a/src/query.cc b/src/query.cc index b9375bce..cc7cefaf 100644 --- a/src/query.cc +++ b/src/query.cc @@ -345,6 +345,139 @@ void CompareGroups( +// TODO: consider having separate lookup maps so they are smaller (maybe +// lookups will go faster). + +QueryFileId GetQueryFileIdFromUsr(QueryableDatabase* query_db, const Usr& usr) { + auto it = query_db->usr_to_symbol.find(usr); + if (it != query_db->usr_to_symbol.end()) { + assert(it->second.kind == SymbolKind::File); + return it->second.idx; + } + + int idx = query_db->files.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(SymbolKind::File, idx); + query_db->files.push_back(QueryableFile(usr)); + return idx; +} + +QueryTypeId GetQueryTypeIdFromUsr(QueryableDatabase* query_db, const Usr& usr) { + auto it = query_db->usr_to_symbol.find(usr); + if (it != query_db->usr_to_symbol.end()) { + assert(it->second.kind == SymbolKind::Type); + return it->second.idx; + } + + int idx = query_db->types.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(SymbolKind::Type, idx); + query_db->types.push_back(QueryableTypeDef(usr)); + return idx; +} + +QueryFuncId GetQueryFuncIdFromUsr(QueryableDatabase* query_db, const Usr& usr) { + auto it = query_db->usr_to_symbol.find(usr); + if (it != query_db->usr_to_symbol.end()) { + assert(it->second.kind == SymbolKind::Func); + return it->second.idx; + } + + int idx = query_db->funcs.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(SymbolKind::Func, idx); + query_db->funcs.push_back(QueryableFuncDef(usr)); + return idx; +} + +QueryVarId GetQueryVarIdFromUsr(QueryableDatabase* query_db, const Usr& usr) { + auto it = query_db->usr_to_symbol.find(usr); + if (it != query_db->usr_to_symbol.end()) { + assert(it->second.kind == SymbolKind::Var); + return it->second.idx; + } + + int idx = query_db->vars.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(SymbolKind::Var, idx); + query_db->vars.push_back(QueryableVarDef(usr)); + return idx; +} + + +#if false +int GetOrAddSymbol(QueryableDatabase* query_db, SymbolKind kind, const Usr& usr) { + // TODO: consider having separate lookup maps so they are smaller (maybe + // lookups will go faster). + auto it = query_db->usr_to_symbol.find(usr); + + // Found; return existing symbol. + if (it != query_db->usr_to_symbol.end()) { + assert(it->second.kind == kind); + return it->second.idx; + } + + // Not found; add a new symbol. + switch (kind) { + case SymbolKind::File: { + int idx = query_db->files.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(kind, idx); + query_db->files.push_back(QueryableFile(usr)); + return idx; + } + case SymbolKind::Type: { + int idx = query_db->types.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(kind, idx); + query_db->types.push_back(QueryableTypeDef(usr)); + return idx; + } + case SymbolKind::Func: { + int idx = query_db->funcs.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(kind, idx); + query_db->funcs.push_back(QueryableFuncDef(usr)); + return idx; + } + case SymbolKind::Var: { + int idx = query_db->vars.size(); + query_db->usr_to_symbol[usr] = SymbolIdx(kind, idx); + query_db->vars.push_back(QueryableVarDef(usr)); + return idx; + } + case SymbolKind::Invalid: { + assert(false); + return -1; + } + } + + assert(false); + return -1; +} +#endif + +IdMap::IdMap(QueryableDatabase* query_db, const IdCache& local_ids) + : local_ids(local_ids) { + assert(query_db); // TODO: remove after testing. + + index_file_id = GetQueryFileIdFromUsr(query_db, local_ids.primary_file); + + cached_type_ids_.reserve(local_ids.type_id_to_usr.size()); + for (const auto& entry : local_ids.type_id_to_usr) + cached_type_ids_[entry.first] = GetQueryTypeIdFromUsr(query_db, entry.second); + + cached_func_ids_.reserve(local_ids.func_id_to_usr.size()); + for (const auto& entry : local_ids.func_id_to_usr) + cached_func_ids_[entry.first] = GetQueryFuncIdFromUsr(query_db, entry.second); + + cached_var_ids_.reserve(local_ids.var_id_to_usr.size()); + for (const auto& entry : local_ids.var_id_to_usr) + cached_var_ids_[entry.first] = GetQueryVarIdFromUsr(query_db, entry.second); +} + + + + + + + + + + @@ -525,10 +658,7 @@ void QueryableDatabase::ImportOrUpdate(const std::vectorsecond.idx]; @@ -544,10 +674,7 @@ void QueryableDatabase::ImportOrUpdate(const std::vectorsecond.idx]; @@ -564,10 +691,7 @@ void QueryableDatabase::ImportOrUpdate(const std::vectorsecond.idx]; @@ -584,10 +708,7 @@ void QueryableDatabase::ImportOrUpdate(const std::vectorsecond.idx]; diff --git a/src/query.h b/src/query.h index ee325a52..a7915b95 100644 --- a/src/query.h +++ b/src/query.h @@ -20,26 +20,7 @@ using QueryVarId = Id; -struct IdMap { - // TODO threading model - // - [querydb] Create IdMap mapping from every id registered in local_ids - // - [indexer] Create IndexUpdate using IdMap cached state - // - [querydb] Apply IndexUpdate - // - // Then lookup in cached_* should *never* fail. - - const IdCache& local_ids; - - IdMap(const IdCache& local_ids) : local_ids(local_ids) {} - - // TODO -private: - QueryFileId index_file_id; - std::unordered_map cached_type_ids_; - std::unordered_map cached_func_ids_; - std::unordered_map cached_var_ids_; -}; - +struct IdMap; @@ -136,7 +117,8 @@ struct QueryableFile { DefUpdate def; - QueryableFile() {} + QueryableFile(const Usr& usr) { def.usr = usr; } + QueryableFile(const Def& def) : def(def) {} QueryableFile(const IdMap& id_map, const IndexedFile& indexed); }; @@ -151,7 +133,8 @@ struct QueryableTypeDef { std::vector instantiations; std::vector uses; - QueryableTypeDef() : def("") {} + QueryableTypeDef(const Usr& usr) : def(usr) {} + QueryableTypeDef(const DefUpdate& def) : def(def) {} QueryableTypeDef(const IdMap& id_map, const IndexedTypeDef& indexed); }; @@ -168,7 +151,8 @@ struct QueryableFuncDef { std::vector callers; std::vector uses; - QueryableFuncDef() : def("") {} + QueryableFuncDef(const Usr& usr) : def(usr) {} + QueryableFuncDef(const DefUpdate& def) : def(def) {} QueryableFuncDef(const IdMap& id_map, const IndexedFuncDef& indexed); }; @@ -179,7 +163,8 @@ struct QueryableVarDef { DefUpdate def; std::vector uses; - QueryableVarDef() : def("") {} + QueryableVarDef(const Usr& usr) : def(usr) {} + QueryableVarDef(const DefUpdate& def) : def(def) {} QueryableVarDef(const IdMap& id_map, const IndexedVarDef& indexed); }; @@ -259,4 +244,27 @@ struct QueryableDatabase { void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); -}; \ No newline at end of file +}; + + + + +struct IdMap { + // TODO threading model + // - [querydb] Create IdMap mapping from every id registered in local_ids + // - [indexer] Create IndexUpdate using IdMap cached state + // - [querydb] Apply IndexUpdate + // + // Then lookup in cached_* should *never* fail. + + const IdCache& local_ids; + + IdMap(QueryableDatabase* query_db, const IdCache& local_ids); + + // TODO +private: + QueryFileId index_file_id; + std::unordered_map cached_type_ids_; + std::unordered_map cached_func_ids_; + std::unordered_map cached_var_ids_; +};