mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-19 12:05:50 +00:00
Fix delta update.
Previous file was being loaded after the current file is written to disk.
This commit is contained in:
parent
59851c06e0
commit
3ab4d0455d
30
src/cache.cc
30
src/cache.cc
@ -4,6 +4,8 @@
|
||||
#include "platform.h"
|
||||
#include "language_server_api.h"
|
||||
|
||||
#include <loguru/loguru.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace {
|
||||
@ -30,7 +32,9 @@ std::unique_ptr<IndexFile> LoadCachedIndex(Config* config,
|
||||
if (!file_content)
|
||||
return nullptr;
|
||||
|
||||
return Deserialize(filename, *file_content, IndexFile::kCurrentVersion);
|
||||
auto result = Deserialize(filename, *file_content, IndexFile::kCurrentVersion);
|
||||
result->is_loaded_from_cache_ = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
optional<std::string> LoadCachedFileContents(Config* config,
|
||||
@ -42,25 +46,21 @@ optional<std::string> LoadCachedFileContents(Config* config,
|
||||
}
|
||||
|
||||
void WriteToCache(Config* config,
|
||||
const std::string& filename,
|
||||
IndexFile& file,
|
||||
const optional<std::string>& indexed_file_content) {
|
||||
IndexFile& file) {
|
||||
if (!config->enableCacheWrite)
|
||||
return;
|
||||
|
||||
std::string cache_basename =
|
||||
GetCachedBaseFileName(config->cacheDirectory, filename);
|
||||
GetCachedBaseFileName(config->cacheDirectory, file.path);
|
||||
|
||||
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);
|
||||
}
|
||||
LOG_IF_S(ERROR, file.file_contents_.empty()) << "Writing " << file.path << " to cache but it has no contents";
|
||||
|
||||
assert(!file.file_contents_.empty());
|
||||
std::ofstream cache_content;
|
||||
cache_content.open(cache_basename);
|
||||
assert(cache_content.good());
|
||||
cache_content << file.file_contents_;
|
||||
cache_content.close();
|
||||
|
||||
std::string indexed_content = Serialize(file);
|
||||
std::ofstream cache;
|
||||
|
@ -17,6 +17,4 @@ optional<std::string> LoadCachedFileContents(Config* config,
|
||||
const std::string& filename);
|
||||
|
||||
void WriteToCache(Config* config,
|
||||
const std::string& filename,
|
||||
IndexFile& file,
|
||||
const optional<std::string>& indexed_file_contents);
|
||||
IndexFile& file);
|
@ -583,6 +583,7 @@ struct Index_OnIdMapped {
|
||||
|
||||
std::unique_ptr<File> previous;
|
||||
std::unique_ptr<File> current;
|
||||
|
||||
PerformanceImportFile perf;
|
||||
bool is_interactive;
|
||||
|
||||
@ -638,8 +639,6 @@ struct Index_OnIndexed {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -896,11 +895,6 @@ std::vector<Index_DoIdMap> DoParseFile(
|
||||
// Note: we are reusing the parent perf.
|
||||
perf.index_load_cached = time.ElapsedMicrosecondsAndReset();
|
||||
|
||||
// Write new cache to disk. Add file to list of paths that need to be reimported.
|
||||
time.Reset();
|
||||
WriteToCache(config, new_index->path, *new_index, new_index->file_contents_);
|
||||
perf.index_save_to_disk = time.ElapsedMicrosecondsAndReset();
|
||||
|
||||
// TODO/FIXME: real is_interactive
|
||||
bool is_interactive = false;
|
||||
LOG_S(INFO) << "Emitting index result for " << new_index->path;
|
||||
@ -952,6 +946,7 @@ bool IndexMain_DoParse(
|
||||
}
|
||||
|
||||
bool IndexMain_DoCreateIndexUpdate(
|
||||
Config* config,
|
||||
QueueManager* queue) {
|
||||
// TODO: Index_OnIdMapped dtor is failing because it seems that its contents have already been destroyed.
|
||||
optional<Index_OnIdMapped> response = queue->on_id_mapped.TryDequeue();
|
||||
@ -968,11 +963,19 @@ bool IndexMain_DoCreateIndexUpdate(
|
||||
previous_index = response->previous->file.get();
|
||||
}
|
||||
|
||||
// Build delta update.
|
||||
IndexUpdate update = IndexUpdate::CreateDelta(
|
||||
previous_id_map, response->current->ids.get(),
|
||||
previous_index, response->current->file.get());
|
||||
response->perf.index_make_delta = time.ElapsedMicrosecondsAndReset();
|
||||
|
||||
// Write new cache to disk if it is a fresh index.
|
||||
if (!response->current->file->is_loaded_from_cache_) {
|
||||
time.Reset();
|
||||
WriteToCache(config, *response->current->file);
|
||||
response->perf.index_save_to_disk = time.ElapsedMicrosecondsAndReset();
|
||||
}
|
||||
|
||||
#if false
|
||||
#define PRINT_SECTION(name) \
|
||||
if (response->perf.name) {\
|
||||
@ -1061,8 +1064,8 @@ void IndexMain(Config* config,
|
||||
bool did_parse = IndexMain_DoParse(config, queue, file_consumer_shared, &index);
|
||||
|
||||
bool did_create_update =
|
||||
IndexMain_DoCreateIndexUpdate(queue);
|
||||
|
||||
IndexMain_DoCreateIndexUpdate(config, queue);
|
||||
|
||||
bool did_load_previous = IndexMain_LoadPreviousIndex(config, queue);
|
||||
|
||||
// Nothing to index and no index updates to create, so join some already
|
||||
|
@ -356,10 +356,6 @@ std::string IndexFile::ToString() {
|
||||
return Serialize(*this);
|
||||
}
|
||||
|
||||
void IndexFile::ClearLargeState() {
|
||||
file_contents_ = "";
|
||||
}
|
||||
|
||||
IndexType::IndexType(IndexTypeId id, const std::string& usr)
|
||||
: def(usr), id(id) {
|
||||
assert(usr.size() > 0);
|
||||
|
@ -480,6 +480,8 @@ struct IndexFile {
|
||||
std::vector<IndexFunc> funcs;
|
||||
std::vector<IndexVar> vars;
|
||||
|
||||
// True if this file was loaded from cache. Not serialized.
|
||||
bool is_loaded_from_cache_ = false;
|
||||
// Diagnostics found when indexing this file. Not serialized.
|
||||
NonElidedVector<lsDiagnostic> diagnostics_;
|
||||
// File contents at the time of index. Not serialized.
|
||||
@ -498,7 +500,6 @@ struct IndexFile {
|
||||
IndexVar* Resolve(IndexVarId id);
|
||||
|
||||
std::string ToString();
|
||||
void ClearLargeState();
|
||||
};
|
||||
|
||||
struct FileContents {
|
||||
|
Loading…
Reference in New Issue
Block a user