From 6048eb62375107825d217c49a81ba207dbffb28a Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Fri, 29 Dec 2017 12:19:39 -0600 Subject: [PATCH] Move LoadCachedFileContents into ICacheManager --- src/cache_manager.cc | 76 ++++++++++++-------------- src/cache_manager.h | 8 +-- src/import_pipeline.cc | 10 ++-- src/messages/text_document_did_open.cc | 3 +- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/cache_manager.cc b/src/cache_manager.cc index 462efd44..a276f765 100644 --- a/src/cache_manager.cc +++ b/src/cache_manager.cc @@ -12,6 +12,34 @@ namespace { +std::string GetCachedBaseFileName(Config* config, + const std::string& source_file, + bool create_dir = false) { + assert(!config->cacheDirectory.empty()); + std::string cache_file; + size_t len = config->projectRoot.size(); + if (StartsWith(source_file, config->projectRoot)) { + cache_file = EscapeFileName(config->projectRoot) + '/' + + EscapeFileName(source_file.substr(len)); + } else + cache_file = EscapeFileName(source_file); + + return config->cacheDirectory + cache_file; +} + +std::unique_ptr LoadCachedIndex(Config* config, + const std::string& filename) { + if (!config->enableCacheRead) + return nullptr; + + optional file_content = + ReadContent(GetCachedBaseFileName(config, filename) + ".json"); + if (!file_content) + return nullptr; + + return Deserialize(filename, *file_content, IndexFile::kCurrentVersion); +} + // Manages loading caches from file paths for the indexer process. struct RealCacheManager : ICacheManager { explicit RealCacheManager(Config* config) : config_(config) {} @@ -41,6 +69,14 @@ struct RealCacheManager : ICacheManager { return LoadCachedIndex(config_, path); } + optional LoadCachedFileContents( + const std::string& filename) override { + if (!config_->enableCacheRead) + return nullopt; + + return ReadContent(GetCachedBaseFileName(config_, filename)); + } + void IterateLoadedCaches(std::function fn) override { for (const auto& it : caches) { assert(it.second); @@ -81,46 +117,6 @@ std::unique_ptr ICacheManager::TakeOrLoad(const std::string& path) { return result; } -namespace { - -std::string GetCachedBaseFileName(Config* config, - const std::string& source_file, - bool create_dir = false) { - assert(!config->cacheDirectory.empty()); - std::string cache_file; - size_t len = config->projectRoot.size(); - if (StartsWith(source_file, config->projectRoot)) { - cache_file = EscapeFileName(config->projectRoot) + '/' + - EscapeFileName(source_file.substr(len)); - } else - cache_file = EscapeFileName(source_file); - - return config->cacheDirectory + cache_file; -} - -} // namespace - -std::unique_ptr LoadCachedIndex(Config* config, - const std::string& filename) { - if (!config->enableCacheRead) - return nullptr; - - optional file_content = - ReadContent(GetCachedBaseFileName(config, filename) + ".json"); - if (!file_content) - return nullptr; - - return Deserialize(filename, *file_content, IndexFile::kCurrentVersion); -} - -optional LoadCachedFileContents(Config* config, - const std::string& filename) { - if (!config->enableCacheRead) - return nullopt; - - return ReadContent(GetCachedBaseFileName(config, filename)); -} - void WriteToCache(Config* config, IndexFile& file) { if (!config->enableCacheWrite) return; diff --git a/src/cache_manager.h b/src/cache_manager.h index d7fffa5e..886e2bdf 100644 --- a/src/cache_manager.h +++ b/src/cache_manager.h @@ -34,13 +34,11 @@ struct ICacheManager { // exists. std::unique_ptr TakeOrLoad(const std::string& path); + virtual optional LoadCachedFileContents( + const std::string& filename) = 0; + // Iterate over all loaded caches. virtual void IterateLoadedCaches(std::function fn) = 0; }; -// FIXME: only use ICacheLoader, not these functions. -std::unique_ptr LoadCachedIndex(Config* config, - const std::string& filename); -optional LoadCachedFileContents(Config* config, - const std::string& filename); void WriteToCache(Config* config, IndexFile& file); \ No newline at end of file diff --git a/src/import_pipeline.cc b/src/import_pipeline.cc index 0ca6b235..a8e34a50 100644 --- a/src/import_pipeline.cc +++ b/src/import_pipeline.cc @@ -330,13 +330,13 @@ bool IndexMain_DoCreateIndexUpdate(Config* config, return true; } -bool IndexMain_LoadPreviousIndex(Config* config) { +bool IndexMain_LoadPreviousIndex(ICacheManager* cache_manager) { auto* queue = QueueManager::instance(); optional response = queue->load_previous_index.TryDequeue(); if (!response) return false; - response->previous = LoadCachedIndex(config, response->current->path); + response->previous = cache_manager->TryTakeOrLoad(response->current->path); LOG_IF_S(ERROR, !response->previous) << "Unable to load previous index for already imported index " << response->current->path; @@ -417,6 +417,7 @@ void Indexer_Main(Config* config, Project* project, WorkingFiles* working_files, MultiQueueWaiter* waiter) { + std::unique_ptr cache_manager = ICacheManager::Make(config); auto* queue = QueueManager::instance(); // Build one index per-indexer, as building the index acquires a global lock. ClangIndex index; @@ -441,7 +442,7 @@ void Indexer_Main(Config* config, bool did_create_update = IndexMain_DoCreateIndexUpdate(config, timestamp_manager); - bool did_load_previous = IndexMain_LoadPreviousIndex(config); + bool did_load_previous = IndexMain_LoadPreviousIndex(cache_manager.get()); // Nothing to index and no index updates to create, so join some already // created index updates to reduce work on querydb thread. @@ -464,6 +465,7 @@ bool QueryDb_ImportMain(Config* config, ImportManager* import_manager, SemanticHighlightSymbolCache* semantic_cache, WorkingFiles* working_files) { + std::unique_ptr cache_manager = ICacheManager::Make(config); auto* queue = QueueManager::instance(); EmitProgress(config); @@ -538,7 +540,7 @@ bool QueryDb_ImportMain(Config* config, working_files->GetFileByFilename(updated_file.path); if (working_file) { optional cached_file_contents = - LoadCachedFileContents(config, updated_file.path); + cache_manager->LoadCachedFileContents(updated_file.path); if (cached_file_contents) working_file->SetIndexContent(*cached_file_contents); else diff --git a/src/messages/text_document_did_open.cc b/src/messages/text_document_did_open.cc index ac983af4..c1ede582 100644 --- a/src/messages/text_document_did_open.cc +++ b/src/messages/text_document_did_open.cc @@ -32,10 +32,11 @@ struct TextDocumentDidOpenHandler if (ShouldIgnoreFileForIndexing(path)) return; + std::unique_ptr cache_manager = ICacheManager::Make(config); WorkingFile* working_file = working_files->OnOpen(request->params.textDocument); optional cached_file_contents = - LoadCachedFileContents(config, path); + cache_manager->LoadCachedFileContents(path); if (cached_file_contents) working_file->SetIndexContent(*cached_file_contents); else