2017-12-29 17:27:56 +00:00
|
|
|
#include "cache_manager.h"
|
2017-04-09 02:24:32 +00:00
|
|
|
|
2017-12-29 17:27:56 +00:00
|
|
|
#include "config.h"
|
2017-04-09 02:24:32 +00:00
|
|
|
#include "indexer.h"
|
2017-04-21 06:32:18 +00:00
|
|
|
#include "language_server_api.h"
|
2017-09-22 01:14:57 +00:00
|
|
|
#include "platform.h"
|
|
|
|
|
2017-08-15 07:46:21 +00:00
|
|
|
#include <loguru/loguru.hpp>
|
|
|
|
|
2017-04-09 02:24:32 +00:00
|
|
|
#include <algorithm>
|
2017-12-29 17:27:56 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Manages loading caches from file paths for the indexer process.
|
|
|
|
struct RealCacheManager : ICacheManager {
|
|
|
|
explicit RealCacheManager(Config* config) : config_(config) {}
|
|
|
|
~RealCacheManager() override = default;
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
void WriteToCache(IndexFile& file) override {
|
|
|
|
if (!config_->enableCacheWrite)
|
|
|
|
return;
|
2017-12-29 17:27:56 +00:00
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
std::string cache_path = GetCachePath(file.path);
|
2017-12-29 17:27:56 +00:00
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
if (file.file_contents_.empty()) {
|
|
|
|
LOG_S(ERROR) << "No cached file contents; performing potentially stale "
|
|
|
|
<< "file-copy for " << file.path;
|
|
|
|
CopyFileTo(cache_path, file.path);
|
|
|
|
} else {
|
|
|
|
WriteToFile(cache_path, file.file_contents_);
|
2017-12-29 17:27:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
std::string indexed_content = Serialize(file);
|
|
|
|
WriteToFile(AppendSerializationFormat(cache_path), indexed_content);
|
2017-12-29 17:27:56 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 18:19:39 +00:00
|
|
|
optional<std::string> LoadCachedFileContents(
|
2018-01-07 00:20:37 +00:00
|
|
|
const std::string& path) override {
|
2017-12-29 18:19:39 +00:00
|
|
|
if (!config_->enableCacheRead)
|
|
|
|
return nullopt;
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
return ReadContent(GetCachePath(path));
|
|
|
|
}
|
|
|
|
|
2018-01-07 00:22:46 +00:00
|
|
|
std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) override {
|
2018-01-07 00:20:37 +00:00
|
|
|
if (!config_->enableCacheRead)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::string cache_path = GetCachePath(path);
|
|
|
|
optional<std::string> file_content =
|
|
|
|
ReadContent(AppendSerializationFormat(cache_path));
|
|
|
|
if (!file_content)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return Deserialize(config_->cacheFormat, path, *file_content,
|
|
|
|
IndexFile::kCurrentVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetCachePath(const std::string& source_file) {
|
|
|
|
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;
|
2017-12-29 18:19:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
std::string AppendSerializationFormat(const std::string& base) {
|
|
|
|
switch (config_->cacheFormat) {
|
|
|
|
case SerializeFormat::Json:
|
|
|
|
return base + ".json";
|
|
|
|
case SerializeFormat::MessagePack:
|
|
|
|
return base + ".mpack";
|
2017-12-29 17:27:56 +00:00
|
|
|
}
|
2018-01-07 00:20:37 +00:00
|
|
|
assert(false);
|
|
|
|
return ".json";
|
2017-12-29 17:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Config* config_;
|
|
|
|
};
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
struct FakeCacheManager : ICacheManager {
|
|
|
|
explicit FakeCacheManager(const std::vector<FakeCacheEntry>& entries)
|
|
|
|
: entries_(entries) {}
|
|
|
|
|
|
|
|
void WriteToCache(IndexFile& file) override { assert(false); }
|
|
|
|
|
|
|
|
optional<std::string> LoadCachedFileContents(
|
|
|
|
const std::string& path) override {
|
|
|
|
for (const FakeCacheEntry& entry : entries_) {
|
|
|
|
if (entry.path == path) {
|
|
|
|
return entry.content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2018-01-07 00:22:46 +00:00
|
|
|
std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) override {
|
2018-01-07 00:20:37 +00:00
|
|
|
for (const FakeCacheEntry& entry : entries_) {
|
|
|
|
if (entry.path == path) {
|
|
|
|
return Deserialize(SerializeFormat::Json, path, entry.json, nullopt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<FakeCacheEntry> entries_;
|
|
|
|
};
|
2017-12-29 17:27:56 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
std::unique_ptr<ICacheManager> ICacheManager::Make(Config* config) {
|
|
|
|
return MakeUnique<RealCacheManager>(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
std::unique_ptr<ICacheManager> ICacheManager::MakeFake(
|
|
|
|
const std::vector<FakeCacheEntry>& entries) {
|
2018-01-07 00:20:37 +00:00
|
|
|
return MakeUnique<FakeCacheManager>(entries);
|
2017-12-29 17:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ICacheManager::~ICacheManager() = default;
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
IndexFile* ICacheManager::TryLoad(const std::string& path) {
|
|
|
|
auto it = caches_.find(path);
|
|
|
|
if (it != caches_.end())
|
|
|
|
return it->second.get();
|
|
|
|
|
|
|
|
std::unique_ptr<IndexFile> cache = RawCacheLoad(path);
|
|
|
|
if (!cache)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
caches_[path] = std::move(cache);
|
|
|
|
return caches_[path].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IndexFile> ICacheManager::TryTakeOrLoad(
|
|
|
|
const std::string& path) {
|
|
|
|
auto it = caches_.find(path);
|
|
|
|
if (it != caches_.end()) {
|
|
|
|
auto result = std::move(it->second);
|
|
|
|
caches_.erase(it);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RawCacheLoad(path);
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:27:56 +00:00
|
|
|
std::unique_ptr<IndexFile> ICacheManager::TakeOrLoad(const std::string& path) {
|
|
|
|
auto result = TryTakeOrLoad(path);
|
|
|
|
assert(result);
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-09 02:24:32 +00:00
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
void ICacheManager::IterateLoadedCaches(std::function<void(IndexFile*)> fn) {
|
|
|
|
for (const auto& cache : caches_) {
|
|
|
|
assert(cache.second);
|
|
|
|
fn(cache.second.get());
|
2017-09-22 07:39:59 +00:00
|
|
|
}
|
2017-04-19 00:05:14 +00:00
|
|
|
}
|