2017-04-09 02:24:32 +00:00
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
#include "indexer.h"
|
2017-04-21 06:32:18 +00:00
|
|
|
#include "platform.h"
|
|
|
|
#include "language_server_api.h"
|
2017-04-09 02:24:32 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-04-22 07:42:57 +00:00
|
|
|
std::string GetCachedBaseFileName(const std::string& cache_directory,
|
|
|
|
std::string source_file) {
|
2017-04-18 02:59:48 +00:00
|
|
|
assert(!cache_directory.empty());
|
2017-04-09 02:24:32 +00:00
|
|
|
std::replace(source_file.begin(), source_file.end(), '\\', '_');
|
|
|
|
std::replace(source_file.begin(), source_file.end(), '/', '_');
|
|
|
|
std::replace(source_file.begin(), source_file.end(), ':', '_');
|
2017-04-24 01:24:09 +00:00
|
|
|
|
2017-04-21 06:32:18 +00:00
|
|
|
return cache_directory + source_file;
|
2017-04-09 02:24:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
std::unique_ptr<IndexFile> LoadCachedIndex(IndexerConfig* config,
|
2017-04-22 07:42:57 +00:00
|
|
|
const std::string& filename) {
|
2017-04-19 07:32:59 +00:00
|
|
|
if (!config->enableCacheRead)
|
|
|
|
return nullptr;
|
2017-04-11 07:29:36 +00:00
|
|
|
|
2017-04-22 07:42:57 +00:00
|
|
|
optional<std::string> file_content = ReadContent(
|
|
|
|
GetCachedBaseFileName(config->cacheDirectory, filename) + ".json");
|
2017-04-17 07:06:01 +00:00
|
|
|
if (!file_content)
|
2017-04-09 02:24:32 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
optional<IndexFile> indexed = Deserialize(filename, *file_content);
|
|
|
|
if (indexed && indexed->version == IndexFile::kCurrentVersion)
|
|
|
|
return MakeUnique<IndexFile>(indexed.value());
|
2017-04-09 02:24:32 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-22 07:42:57 +00:00
|
|
|
optional<std::string> LoadCachedFileContents(IndexerConfig* config,
|
|
|
|
const std::string& filename) {
|
2017-04-21 06:32:18 +00:00
|
|
|
if (!config->enableCacheRead)
|
2017-04-21 17:00:36 +00:00
|
|
|
return nullopt;
|
2017-04-21 06:32:18 +00:00
|
|
|
|
2017-04-24 01:24:09 +00:00
|
|
|
return ReadContent(GetCachedBaseFileName(config->cacheDirectory, filename));
|
2017-04-21 06:32:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 07:42:57 +00:00
|
|
|
void WriteToCache(IndexerConfig* config,
|
|
|
|
const std::string& filename,
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFile& file) {
|
2017-04-19 07:32:59 +00:00
|
|
|
if (!config->enableCacheWrite)
|
|
|
|
return;
|
|
|
|
|
2017-04-22 07:42:57 +00:00
|
|
|
std::string cache_basename =
|
|
|
|
GetCachedBaseFileName(config->cacheDirectory, filename);
|
2017-04-09 02:24:32 +00:00
|
|
|
|
2017-04-24 01:24:09 +00:00
|
|
|
CopyFileTo(cache_basename, filename);
|
2017-04-21 06:32:18 +00:00
|
|
|
|
|
|
|
std::string indexed_content = Serialize(file);
|
2017-04-09 02:24:32 +00:00
|
|
|
std::ofstream cache;
|
2017-04-21 06:32:18 +00:00
|
|
|
cache.open(cache_basename + ".json");
|
2017-04-09 02:24:32 +00:00
|
|
|
assert(cache.good());
|
|
|
|
cache << indexed_content;
|
|
|
|
cache.close();
|
2017-04-19 00:05:14 +00:00
|
|
|
}
|