mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-29 11:01:57 +00:00
Fix race condition when saving indexed file contents to cache.
This commit is contained in:
parent
671a54c7af
commit
59a077d8a9
14
src/cache.cc
14
src/cache.cc
@ -47,14 +47,24 @@ optional<std::string> LoadCachedFileContents(IndexerConfig* config,
|
|||||||
|
|
||||||
void WriteToCache(IndexerConfig* config,
|
void WriteToCache(IndexerConfig* config,
|
||||||
const std::string& filename,
|
const std::string& filename,
|
||||||
IndexFile& file) {
|
IndexFile& file,
|
||||||
|
const optional<std::string>& indexed_file_content) {
|
||||||
if (!config->enableCacheWrite)
|
if (!config->enableCacheWrite)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::string cache_basename =
|
std::string cache_basename =
|
||||||
GetCachedBaseFileName(config->cacheDirectory, filename);
|
GetCachedBaseFileName(config->cacheDirectory, filename);
|
||||||
|
|
||||||
CopyFileTo(cache_basename, 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::string indexed_content = Serialize(file);
|
||||||
std::ofstream cache;
|
std::ofstream cache;
|
||||||
|
@ -18,4 +18,5 @@ optional<std::string> LoadCachedFileContents(IndexerConfig* config,
|
|||||||
|
|
||||||
void WriteToCache(IndexerConfig* config,
|
void WriteToCache(IndexerConfig* config,
|
||||||
const std::string& filename,
|
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.
|
// Forward file content, but only for the primary file.
|
||||||
optional<std::string> content;
|
optional<std::string> content;
|
||||||
if (new_index->path == entry.filename)
|
if (new_index->path == entry.filename)
|
||||||
content = indexed_content;
|
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.
|
// Dispatch IdMap creation request, which will happen on querydb thread.
|
||||||
Index_DoIdMap response(std::move(cached_index), std::move(new_index), content);
|
Index_DoIdMap response(std::move(cached_index), std::move(new_index), content);
|
||||||
queue_do_id_map->Enqueue(std::move(response));
|
queue_do_id_map->Enqueue(std::move(response));
|
||||||
|
Loading…
Reference in New Issue
Block a user