mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-29 02:51:57 +00:00
Fix race condition when saving indexed file contents to cache.
This commit is contained in:
parent
671a54c7af
commit
59a077d8a9
12
src/cache.cc
12
src/cache.cc
@ -47,14 +47,24 @@ optional<std::string> LoadCachedFileContents(IndexerConfig* config,
|
||||
|
||||
void WriteToCache(IndexerConfig* config,
|
||||
const std::string& filename,
|
||||
IndexFile& file) {
|
||||
IndexFile& file,
|
||||
const optional<std::string>& indexed_file_content) {
|
||||
if (!config->enableCacheWrite)
|
||||
return;
|
||||
|
||||
std::string cache_basename =
|
||||
GetCachedBaseFileName(config->cacheDirectory, filename);
|
||||
|
||||
if (indexed_file_content) {
|
||||
std::ofstream cache_content;
|
||||
cache_content.open(cache_basename);
|
||||
assert(cache_content.good());
|
||||
cache_content << *indexed_file_content;
|
||||
cache_content.close();
|
||||
}
|
||||
else {
|
||||
CopyFileTo(cache_basename, filename);
|
||||
}
|
||||
|
||||
std::string indexed_content = Serialize(file);
|
||||
std::ofstream cache;
|
||||
|
@ -18,4 +18,5 @@ optional<std::string> LoadCachedFileContents(IndexerConfig* config,
|
||||
|
||||
void WriteToCache(IndexerConfig* config,
|
||||
const std::string& filename,
|
||||
IndexFile& file);
|
||||
IndexFile& file,
|
||||
const optional<std::string>& indexed_file_contents);
|
@ -1045,16 +1045,16 @@ void ParseFile(IndexerConfig* config,
|
||||
}
|
||||
}
|
||||
|
||||
// Cache the newly indexed file. This replaces the existing cache.
|
||||
// TODO: Run this as another import pipeline stage.
|
||||
WriteToCache(config, new_index->path, *new_index);
|
||||
time.ResetAndPrint("Cache index update to disk");
|
||||
|
||||
// Forward file content, but only for the primary file.
|
||||
optional<std::string> content;
|
||||
if (new_index->path == entry.filename)
|
||||
content = indexed_content;
|
||||
|
||||
// Cache the newly indexed file. This replaces the existing cache.
|
||||
// TODO: Run this as another import pipeline stage.
|
||||
WriteToCache(config, new_index->path, *new_index, content);
|
||||
time.ResetAndPrint("Cache index update to disk");
|
||||
|
||||
// Dispatch IdMap creation request, which will happen on querydb thread.
|
||||
Index_DoIdMap response(std::move(cached_index), std::move(new_index), content);
|
||||
queue_do_id_map->Enqueue(std::move(response));
|
||||
|
Loading…
Reference in New Issue
Block a user