2017-04-09 02:24:32 +00:00
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
#include "indexer.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string GetCachedFileName(const std::string& cache_directory, std::string source_file) {
|
|
|
|
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(), ':', '_');
|
|
|
|
std::replace(source_file.begin(), source_file.end(), '.', '_');
|
2017-04-18 02:59:48 +00:00
|
|
|
return cache_directory + source_file + ".json";
|
2017-04-09 02:24:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-19 07:32:59 +00:00
|
|
|
std::unique_ptr<IndexedFile> LoadCachedFile(IndexerConfig* config, const std::string& filename) {
|
|
|
|
if (!config->enableCacheRead)
|
|
|
|
return nullptr;
|
2017-04-11 07:29:36 +00:00
|
|
|
|
2017-04-19 07:32:59 +00:00
|
|
|
optional<std::string> file_content = ReadContent(GetCachedFileName(config->cacheDirectory, filename));
|
2017-04-17 07:06:01 +00:00
|
|
|
if (!file_content)
|
2017-04-09 02:24:32 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
optional<IndexedFile> indexed = Deserialize(filename, *file_content);
|
2017-04-09 02:24:32 +00:00
|
|
|
if (indexed)
|
|
|
|
return MakeUnique<IndexedFile>(indexed.value());
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-19 07:32:59 +00:00
|
|
|
void WriteToCache(IndexerConfig* config, const std::string& filename, IndexedFile& file) {
|
|
|
|
if (!config->enableCacheWrite)
|
|
|
|
return;
|
|
|
|
|
2017-04-09 02:24:32 +00:00
|
|
|
std::string indexed_content = Serialize(file);
|
|
|
|
|
|
|
|
std::ofstream cache;
|
2017-04-19 07:32:59 +00:00
|
|
|
cache.open(GetCachedFileName(config->cacheDirectory, filename));
|
2017-04-09 02:24:32 +00:00
|
|
|
assert(cache.good());
|
|
|
|
cache << indexed_content;
|
|
|
|
cache.close();
|
2017-04-19 00:05:14 +00:00
|
|
|
}
|