mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-22 07:35:08 +00:00
Move LoadCachedFileContents into ICacheManager
This commit is contained in:
parent
71591d7805
commit
6048eb6237
@ -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<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.
|
||||
struct RealCacheManager : ICacheManager {
|
||||
explicit RealCacheManager(Config* config) : config_(config) {}
|
||||
@ -41,6 +69,14 @@ struct RealCacheManager : ICacheManager {
|
||||
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 {
|
||||
for (const auto& it : caches) {
|
||||
assert(it.second);
|
||||
@ -81,46 +117,6 @@ std::unique_ptr<IndexFile> 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<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) {
|
||||
if (!config->enableCacheWrite)
|
||||
return;
|
||||
|
@ -34,13 +34,11 @@ struct ICacheManager {
|
||||
// exists.
|
||||
std::unique_ptr<IndexFile> TakeOrLoad(const std::string& path);
|
||||
|
||||
virtual optional<std::string> LoadCachedFileContents(
|
||||
const std::string& filename) = 0;
|
||||
|
||||
// Iterate over all loaded caches.
|
||||
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);
|
@ -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<Index_DoIdMap> 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<ICacheManager> 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<ICacheManager> 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<std::string> 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
|
||||
|
@ -32,10 +32,11 @@ struct TextDocumentDidOpenHandler
|
||||
if (ShouldIgnoreFileForIndexing(path))
|
||||
return;
|
||||
|
||||
std::unique_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
|
||||
WorkingFile* working_file =
|
||||
working_files->OnOpen(request->params.textDocument);
|
||||
optional<std::string> cached_file_contents =
|
||||
LoadCachedFileContents(config, path);
|
||||
cache_manager->LoadCachedFileContents(path);
|
||||
if (cached_file_contents)
|
||||
working_file->SetIndexContent(*cached_file_contents);
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user