Group cache files by projectRoot

This commit is contained in:
Fangrui Song 2017-12-01 21:07:30 -08:00 committed by Jacob Dufault
parent 82643dc79d
commit 697968b15f
4 changed files with 30 additions and 11 deletions

View File

@ -10,14 +10,19 @@
namespace { namespace {
std::string GetCachedBaseFileName(const std::string& cache_directory, std::string GetCachedBaseFileName(Config* config,
std::string source_file) { const std::string& source_file,
assert(!cache_directory.empty()); bool create_dir = false) {
std::replace(source_file.begin(), source_file.end(), '\\', '_'); assert(!config->cacheDirectory.empty());
std::replace(source_file.begin(), source_file.end(), '/', '_'); std::string cache_file;
std::replace(source_file.begin(), source_file.end(), ':', '_'); 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 cache_directory + source_file; return config->cacheDirectory + cache_file;
} }
} // namespace } // namespace
@ -28,7 +33,7 @@ std::unique_ptr<IndexFile> LoadCachedIndex(Config* config,
return nullptr; return nullptr;
optional<std::string> file_content = ReadContent( optional<std::string> file_content = ReadContent(
GetCachedBaseFileName(config->cacheDirectory, filename) + ".json"); GetCachedBaseFileName(config, filename) + ".json");
if (!file_content) if (!file_content)
return nullptr; return nullptr;
@ -40,7 +45,7 @@ optional<std::string> LoadCachedFileContents(Config* config,
if (!config->enableCacheRead) if (!config->enableCacheRead)
return nullopt; return nullopt;
return ReadContent(GetCachedBaseFileName(config->cacheDirectory, filename)); return ReadContent(GetCachedBaseFileName(config, filename));
} }
void WriteToCache(Config* config, IndexFile& file) { void WriteToCache(Config* config, IndexFile& file) {
@ -48,7 +53,7 @@ void WriteToCache(Config* config, IndexFile& file) {
return; return;
std::string cache_basename = std::string cache_basename =
GetCachedBaseFileName(config->cacheDirectory, file.path); GetCachedBaseFileName(config, file.path);
if (file.file_contents_.empty()) { if (file.file_contents_.empty()) {
LOG_S(ERROR) << "No cached file contents; performing potentially stale " LOG_S(ERROR) << "No cached file contents; performing potentially stale "

View File

@ -1474,7 +1474,6 @@ bool QueryDbMainLoop(Config* config,
config->cacheDirectory = NormalizePath(config->cacheDirectory); config->cacheDirectory = NormalizePath(config->cacheDirectory);
EnsureEndsInSlash(config->cacheDirectory); EnsureEndsInSlash(config->cacheDirectory);
MakeDirectoryRecursive(config->cacheDirectory);
// Ensure there is a resource directory. // Ensure there is a resource directory.
if (config->resourceDirectory.empty()) { if (config->resourceDirectory.empty()) {
@ -1553,6 +1552,8 @@ bool QueryDbMainLoop(Config* config,
config->projectRoot = config->projectRoot =
NormalizePath(request->params.rootUri->GetPath()); NormalizePath(request->params.rootUri->GetPath());
EnsureEndsInSlash(config->projectRoot); EnsureEndsInSlash(config->projectRoot);
MakeDirectoryRecursive(
config->cacheDirectory + EscapeFileName(config->projectRoot));
// Start indexer threads. // Start indexer threads.
if (config->indexerCount == 0) { if (config->indexerCount == 0) {

View File

@ -195,6 +195,15 @@ void EnsureEndsInSlash(std::string& path) {
path += '/'; path += '/';
} }
std::string EscapeFileName(std::string path) {
if (path.size() && path.back() == '/')
path.pop_back();
std::replace(path.begin(), path.end(), '\\', '_');
std::replace(path.begin(), path.end(), '/', '_');
std::replace(path.begin(), path.end(), ':', '_');
return path;
}
// http://stackoverflow.com/a/6089413 // http://stackoverflow.com/a/6089413
std::istream& SafeGetline(std::istream& is, std::string& t) { std::istream& SafeGetline(std::istream& is, std::string& t) {
t.clear(); t.clear();

View File

@ -69,6 +69,10 @@ void GetFilesInFolder(std::string folder,
// Ensures that |path| ends in a slash. // Ensures that |path| ends in a slash.
void EnsureEndsInSlash(std::string& path); void EnsureEndsInSlash(std::string& path);
// Converts a file path to one that can be used as filename.
// e.g. foo/bar.c => foo_bar.c
std::string EscapeFileName(std::string path);
optional<std::string> ReadContent(const std::string& filename); optional<std::string> ReadContent(const std::string& filename);
std::vector<std::string> ReadLines(std::string filename); std::vector<std::string> ReadLines(std::string filename);
std::vector<std::string> ToLines(const std::string& content, std::vector<std::string> ToLines(const std::string& content,