mirror of
https://github.com/MaskRay/ccls.git
synced 2025-02-16 21:58:08 +00:00
Move LoadCachedFileContents into ICacheManager
This commit is contained in:
parent
71591d7805
commit
6048eb6237
@ -12,6 +12,34 @@
|
|||||||
|
|
||||||
namespace {
|
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<IndexFile> LoadCachedIndex(Config* config,
|
||||||
|
const std::string& filename) {
|
||||||
|
if (!config->enableCacheRead)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
optional<std::string> 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.
|
// Manages loading caches from file paths for the indexer process.
|
||||||
struct RealCacheManager : ICacheManager {
|
struct RealCacheManager : ICacheManager {
|
||||||
explicit RealCacheManager(Config* config) : config_(config) {}
|
explicit RealCacheManager(Config* config) : config_(config) {}
|
||||||
@ -41,6 +69,14 @@ struct RealCacheManager : ICacheManager {
|
|||||||
return LoadCachedIndex(config_, path);
|
return LoadCachedIndex(config_, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
optional<std::string> LoadCachedFileContents(
|
||||||
|
const std::string& filename) override {
|
||||||
|
if (!config_->enableCacheRead)
|
||||||
|
return nullopt;
|
||||||
|
|
||||||
|
return ReadContent(GetCachedBaseFileName(config_, filename));
|
||||||
|
}
|
||||||
|
|
||||||
void IterateLoadedCaches(std::function<void(IndexFile*)> fn) override {
|
void IterateLoadedCaches(std::function<void(IndexFile*)> fn) override {
|
||||||
for (const auto& it : caches) {
|
for (const auto& it : caches) {
|
||||||
assert(it.second);
|
assert(it.second);
|
||||||
@ -81,46 +117,6 @@ std::unique_ptr<IndexFile> ICacheManager::TakeOrLoad(const std::string& path) {
|
|||||||
return result;
|
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<IndexFile> LoadCachedIndex(Config* config,
|
|
||||||
const std::string& filename) {
|
|
||||||
if (!config->enableCacheRead)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
optional<std::string> file_content =
|
|
||||||
ReadContent(GetCachedBaseFileName(config, filename) + ".json");
|
|
||||||
if (!file_content)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
return Deserialize(filename, *file_content, IndexFile::kCurrentVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
optional<std::string> LoadCachedFileContents(Config* config,
|
|
||||||
const std::string& filename) {
|
|
||||||
if (!config->enableCacheRead)
|
|
||||||
return nullopt;
|
|
||||||
|
|
||||||
return ReadContent(GetCachedBaseFileName(config, filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
void WriteToCache(Config* config, IndexFile& file) {
|
void WriteToCache(Config* config, IndexFile& file) {
|
||||||
if (!config->enableCacheWrite)
|
if (!config->enableCacheWrite)
|
||||||
return;
|
return;
|
||||||
|
@ -34,13 +34,11 @@ struct ICacheManager {
|
|||||||
// exists.
|
// exists.
|
||||||
std::unique_ptr<IndexFile> TakeOrLoad(const std::string& path);
|
std::unique_ptr<IndexFile> TakeOrLoad(const std::string& path);
|
||||||
|
|
||||||
|
virtual optional<std::string> LoadCachedFileContents(
|
||||||
|
const std::string& filename) = 0;
|
||||||
|
|
||||||
// Iterate over all loaded caches.
|
// Iterate over all loaded caches.
|
||||||
virtual void IterateLoadedCaches(std::function<void(IndexFile*)> fn) = 0;
|
virtual void IterateLoadedCaches(std::function<void(IndexFile*)> fn) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: only use ICacheLoader, not these functions.
|
|
||||||
std::unique_ptr<IndexFile> LoadCachedIndex(Config* config,
|
|
||||||
const std::string& filename);
|
|
||||||
optional<std::string> LoadCachedFileContents(Config* config,
|
|
||||||
const std::string& filename);
|
|
||||||
void WriteToCache(Config* config, IndexFile& file);
|
void WriteToCache(Config* config, IndexFile& file);
|
@ -330,13 +330,13 @@ bool IndexMain_DoCreateIndexUpdate(Config* config,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IndexMain_LoadPreviousIndex(Config* config) {
|
bool IndexMain_LoadPreviousIndex(ICacheManager* cache_manager) {
|
||||||
auto* queue = QueueManager::instance();
|
auto* queue = QueueManager::instance();
|
||||||
optional<Index_DoIdMap> response = queue->load_previous_index.TryDequeue();
|
optional<Index_DoIdMap> response = queue->load_previous_index.TryDequeue();
|
||||||
if (!response)
|
if (!response)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
response->previous = LoadCachedIndex(config, response->current->path);
|
response->previous = cache_manager->TryTakeOrLoad(response->current->path);
|
||||||
LOG_IF_S(ERROR, !response->previous)
|
LOG_IF_S(ERROR, !response->previous)
|
||||||
<< "Unable to load previous index for already imported index "
|
<< "Unable to load previous index for already imported index "
|
||||||
<< response->current->path;
|
<< response->current->path;
|
||||||
@ -417,6 +417,7 @@ void Indexer_Main(Config* config,
|
|||||||
Project* project,
|
Project* project,
|
||||||
WorkingFiles* working_files,
|
WorkingFiles* working_files,
|
||||||
MultiQueueWaiter* waiter) {
|
MultiQueueWaiter* waiter) {
|
||||||
|
std::unique_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
|
||||||
auto* queue = QueueManager::instance();
|
auto* queue = QueueManager::instance();
|
||||||
// Build one index per-indexer, as building the index acquires a global lock.
|
// Build one index per-indexer, as building the index acquires a global lock.
|
||||||
ClangIndex index;
|
ClangIndex index;
|
||||||
@ -441,7 +442,7 @@ void Indexer_Main(Config* config,
|
|||||||
bool did_create_update =
|
bool did_create_update =
|
||||||
IndexMain_DoCreateIndexUpdate(config, timestamp_manager);
|
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
|
// Nothing to index and no index updates to create, so join some already
|
||||||
// created index updates to reduce work on querydb thread.
|
// created index updates to reduce work on querydb thread.
|
||||||
@ -464,6 +465,7 @@ bool QueryDb_ImportMain(Config* config,
|
|||||||
ImportManager* import_manager,
|
ImportManager* import_manager,
|
||||||
SemanticHighlightSymbolCache* semantic_cache,
|
SemanticHighlightSymbolCache* semantic_cache,
|
||||||
WorkingFiles* working_files) {
|
WorkingFiles* working_files) {
|
||||||
|
std::unique_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
|
||||||
auto* queue = QueueManager::instance();
|
auto* queue = QueueManager::instance();
|
||||||
EmitProgress(config);
|
EmitProgress(config);
|
||||||
|
|
||||||
@ -538,7 +540,7 @@ bool QueryDb_ImportMain(Config* config,
|
|||||||
working_files->GetFileByFilename(updated_file.path);
|
working_files->GetFileByFilename(updated_file.path);
|
||||||
if (working_file) {
|
if (working_file) {
|
||||||
optional<std::string> cached_file_contents =
|
optional<std::string> cached_file_contents =
|
||||||
LoadCachedFileContents(config, updated_file.path);
|
cache_manager->LoadCachedFileContents(updated_file.path);
|
||||||
if (cached_file_contents)
|
if (cached_file_contents)
|
||||||
working_file->SetIndexContent(*cached_file_contents);
|
working_file->SetIndexContent(*cached_file_contents);
|
||||||
else
|
else
|
||||||
|
@ -32,10 +32,11 @@ struct TextDocumentDidOpenHandler
|
|||||||
if (ShouldIgnoreFileForIndexing(path))
|
if (ShouldIgnoreFileForIndexing(path))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
std::unique_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
|
||||||
WorkingFile* working_file =
|
WorkingFile* working_file =
|
||||||
working_files->OnOpen(request->params.textDocument);
|
working_files->OnOpen(request->params.textDocument);
|
||||||
optional<std::string> cached_file_contents =
|
optional<std::string> cached_file_contents =
|
||||||
LoadCachedFileContents(config, path);
|
cache_manager->LoadCachedFileContents(path);
|
||||||
if (cached_file_contents)
|
if (cached_file_contents)
|
||||||
working_file->SetIndexContent(*cached_file_contents);
|
working_file->SetIndexContent(*cached_file_contents);
|
||||||
else
|
else
|
||||||
|
Loading…
Reference in New Issue
Block a user