From: Joel Rosdahl Date: Sun, 18 Jul 2021 10:52:23 +0000 (+0200) Subject: Move Util::get_path_in_cache to PrimaryStorage X-Git-Tag: v4.4~112 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b126cc8217285a3706aa730922efe97f417ae05d;p=thirdparty%2Fccache.git Move Util::get_path_in_cache to PrimaryStorage PrimaryStorage is now the sole user of get_path_in_cache. --- diff --git a/src/Util.cpp b/src/Util.cpp index d5173bdbb..ab0041da8 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -776,27 +776,6 @@ get_relative_path(string_view dir, string_view path) return result.empty() ? "." : result; } -std::string -get_path_in_cache(string_view cache_dir, uint8_t level, string_view name) -{ - ASSERT(level >= 1 && level <= 8); - ASSERT(name.length() >= level); - - std::string path(cache_dir); - path.reserve(path.size() + level * 2 + 1 + name.length() - level); - - for (uint8_t i = 0; i < level; ++i) { - path.push_back('/'); - path.push_back(name.at(i)); - } - - path.push_back('/'); - string_view name_remaining = name.substr(level); - path.append(name_remaining.data(), name_remaining.length()); - - return path; -} - void hard_link(const std::string& oldpath, const std::string& newpath) { diff --git a/src/Util.hpp b/src/Util.hpp index f41707bf1..06ea153b4 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -220,13 +220,6 @@ const char* get_hostname(); std::string get_relative_path(nonstd::string_view dir, nonstd::string_view path); -// Join `cache_dir`, a '/' and `name` into a single path and return it. -// Additionally, `level` single-character, '/'-separated subpaths are split from -// the beginning of `name` before joining them all. -std::string get_path_in_cache(nonstd::string_view cache_dir, - uint8_t level, - nonstd::string_view name); - // Hard-link `oldpath` to `newpath`. Throws `Error` on error. void hard_link(const std::string& oldpath, const std::string& newpath); diff --git a/src/storage/primary/PrimaryStorage.cpp b/src/storage/primary/PrimaryStorage.cpp index 9a6e3a7f1..a4dd77906 100644 --- a/src/storage/primary/PrimaryStorage.cpp +++ b/src/storage/primary/PrimaryStorage.cpp @@ -290,16 +290,15 @@ PrimaryStorage::look_up_cache_file(const Digest& key, for (uint8_t level = k_min_cache_levels; level <= k_max_cache_levels; ++level) { - const auto path = - Util::get_path_in_cache(m_config.cache_dir(), level, key_string); + const auto path = get_path_in_cache(level, key_string); const auto stat = Stat::stat(path); if (stat) { return {path, stat, level}; } } - const auto shallowest_path = Util::get_path_in_cache( - m_config.cache_dir(), k_min_cache_levels, key_string); + const auto shallowest_path = + get_path_in_cache(k_min_cache_levels, key_string); return {shallowest_path, Stat(), k_min_cache_levels}; } @@ -370,9 +369,7 @@ PrimaryStorage::update_stats_and_maybe_move_cache_file( const auto wanted_level = calculate_wanted_cache_level(counters->get(Statistic::files_in_cache)); const auto wanted_path = - Util::get_path_in_cache(m_config.cache_dir(), - wanted_level, - key.to_string() + suffix_from_type(type)); + get_path_in_cache(wanted_level, key.to_string() + suffix_from_type(type)); if (current_path != wanted_path) { Util::ensure_dir_exists(Util::dir_name(wanted_path)); LOG("Moving {} to {}", current_path, wanted_path); @@ -387,5 +384,27 @@ PrimaryStorage::update_stats_and_maybe_move_cache_file( return counters; } +std::string +PrimaryStorage::get_path_in_cache(const uint8_t level, + const nonstd::string_view name) const +{ + ASSERT(level >= 1 && level <= 8); + ASSERT(name.length() >= level); + + std::string path(m_config.cache_dir()); + path.reserve(path.size() + level * 2 + 1 + name.length() - level); + + for (uint8_t i = 0; i < level; ++i) { + path.push_back('/'); + path.push_back(name.at(i)); + } + + path.push_back('/'); + const nonstd::string_view name_remaining = name.substr(level); + path.append(name_remaining.data(), name_remaining.length()); + + return path; +} + } // namespace primary } // namespace storage diff --git a/src/storage/primary/PrimaryStorage.hpp b/src/storage/primary/PrimaryStorage.hpp index 1bc97ab59..097002e0b 100644 --- a/src/storage/primary/PrimaryStorage.hpp +++ b/src/storage/primary/PrimaryStorage.hpp @@ -96,6 +96,11 @@ private: const std::string& current_path, const Counters& counter_updates, core::CacheEntryType type); + + // Join the cache directory, a '/' and `name` into a single path and return + // it. Additionally, `level` single-character, '/'-separated subpaths are + // split from the beginning of `name` before joining them all. + std::string get_path_in_cache(uint8_t level, nonstd::string_view name) const; }; } // namespace primary diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 32e14687a..ae57fd80b 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -429,14 +429,6 @@ TEST_CASE("Util::get_relative_path") #endif } -TEST_CASE("Util::get_path_in_cache") -{ - CHECK(Util::get_path_in_cache("/zz/ccache", 1, "ABCDEF.suffix") - == "/zz/ccache/A/BCDEF.suffix"); - CHECK(Util::get_path_in_cache("/zz/ccache", 4, "ABCDEF.suffix") - == "/zz/ccache/A/B/C/D/EF.suffix"); -} - TEST_CASE("Util::hard_link") { TestContext test_context;