Fix race condition when saving indexed file contents to cache.

This commit is contained in:
Jacob Dufault 2017-05-16 23:01:51 -07:00
parent 671a54c7af
commit 59a077d8a9
3 changed files with 19 additions and 8 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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));