Simplify Index_OnIdMapped

This commit is contained in:
Jacob Dufault 2017-07-30 11:49:24 -07:00
parent 1f3f69b221
commit 50a726f59e

View File

@ -547,7 +547,13 @@ void FilterCompletionResponse(Out_TextDocumentComplete* complete_response,
struct MappedIndexFile {
std::shared_ptr<IndexFile> file;
std::shared_ptr<IdMap> ids;
MappedIndexFile(std::shared_ptr<IndexFile> file, std::shared_ptr<IdMap> ids)
: file(file), ids(ids) {}
};
@ -568,10 +574,8 @@ struct Index_DoIdMap {
}; };
struct Index_OnIdMapped { struct Index_OnIdMapped {
std::shared_ptr<IndexFile> previous_index; std::shared_ptr<MappedIndexFile> previous;
std::shared_ptr<IndexFile> current_index; std::shared_ptr<MappedIndexFile> current;
std::shared_ptr<IdMap> previous_id_map;
std::shared_ptr<IdMap> current_id_map;
PerformanceImportFile perf; PerformanceImportFile perf;
bool is_interactive; bool is_interactive;
@ -817,38 +821,25 @@ struct CacheLoader {
// Maintains the currently indexed file cache for the querydb process. This is // Maintains the currently indexed file cache for the querydb process. This is
// needed for delta index updates. // needed for delta index updates.
struct CacheManager { struct CacheManager {
struct Entry { std::shared_ptr<MappedIndexFile> UpdateAndReturnOldFile(
std::shared_ptr<IndexFile> file; std::shared_ptr<MappedIndexFile> entry) {
std::shared_ptr<IdMap> ids; // Fetch previous value, if any.
std::shared_ptr<MappedIndexFile> previous_value;
Entry(std::shared_ptr<IndexFile> file, std::shared_ptr<IdMap> ids)
: file(std::move(file)), ids(std::move(ids)) {}
};
Entry* TryGet(const std::string& path) {
auto it = files_.find(path);
if (it != files_.end())
return it->second.get();
return nullptr;
}
std::shared_ptr<Entry> UpdateAndReturnOldFile(std::shared_ptr<Entry> entry) {
entry->file->ClearLargeState();
const auto it = files_.find(entry->file->path); const auto it = files_.find(entry->file->path);
if (it != files_.end()) { if (it != files_.end()) {
std::shared_ptr<Entry> old = std::move(it->second); previous_value = it->second;
files_[entry->file->path] = std::move(entry);
return old;
} }
files_[entry->file->path] = std::move(entry); // Update new value.
return nullptr; entry->file->ClearLargeState();
files_[entry->file->path] = entry;
// Return previous value.
return previous_value;
} }
Config* config_; Config* config_;
std::unordered_map<std::string, std::shared_ptr<Entry>> files_; std::unordered_map<std::string, std::shared_ptr<MappedIndexFile>> files_;
}; };
struct IndexManager { struct IndexManager {
@ -914,9 +905,17 @@ bool IndexMain_DoCreateIndexUpdate(
return false; return false;
Timer time; Timer time;
IdMap* previous_id_map = nullptr;
IndexFile* previous_index = nullptr;
if (response->previous) {
previous_id_map = response->previous->ids.get();
previous_index = response->previous->file.get();
}
IndexUpdate update = IndexUpdate::CreateDelta( IndexUpdate update = IndexUpdate::CreateDelta(
response->previous_id_map.get(), response->current_id_map.get(), previous_id_map, response->current->ids.get(),
response->previous_index.get(), response->current_index.get()); previous_index, response->current->file.get());
response->perf.index_make_delta = time.ElapsedMicrosecondsAndReset(); response->perf.index_make_delta = time.ElapsedMicrosecondsAndReset();
#if false #if false
@ -2878,17 +2877,8 @@ bool QueryDbMainLoop(
assert(request->current); assert(request->current);
// Build IdMap for the new instance. Replace the value in the cache. // Build IdMap for the new instance. Replace the value in the cache.
auto id_map_current = std::make_shared<IdMap>(db, request->current->id_cache); auto id_map_current = std::make_shared<IdMap>(db, request->current->id_cache);
std::shared_ptr<CacheManager::Entry> current = std::make_shared<CacheManager::Entry>(request->current, id_map_current); response.current = std::make_shared<MappedIndexFile>(request->current, id_map_current);
std::shared_ptr<CacheManager::Entry> previous = db_cache->UpdateAndReturnOldFile(current); response.previous = db_cache->UpdateAndReturnOldFile(response.current);
// Let response know about previous id map, if available.
if (previous) {
response.previous_id_map = previous->ids;
response.previous_index = previous->file;
}
// Let response know about current id map, if available.
response.current_id_map = current->ids;
response.current_index = current->file;
response.perf.querydb_id_map = time.ElapsedMicrosecondsAndReset(); response.perf.querydb_id_map = time.ElapsedMicrosecondsAndReset();
queue_on_id_mapped->Enqueue(std::move(response)); queue_on_id_mapped->Enqueue(std::move(response));