From: Joel Rosdahl Date: Sat, 12 Sep 2020 10:58:46 +0000 (+0200) Subject: Refactor away stats_{add_cleanup,set_sizes} X-Git-Tag: v4.0~100 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0378eb68713c4a908414cf917ffb7b11f3183a49;p=thirdparty%2Fccache.git Refactor away stats_{add_cleanup,set_sizes} --- diff --git a/src/cleanup.cpp b/src/cleanup.cpp index 5849fe6b1..cff7d22b2 100644 --- a/src/cleanup.cpp +++ b/src/cleanup.cpp @@ -22,6 +22,7 @@ #include "CacheFile.hpp" #include "Config.hpp" #include "Context.hpp" +#include "Lockfile.hpp" #include "Logging.hpp" #include "Util.hpp" #include "stats.hpp" @@ -53,6 +54,25 @@ delete_file(const std::string& path, } } +static void +update_counters(const std::string& dir, + uint64_t files_in_cache, + uint64_t cache_size, + bool cleanup_performed) +{ + const std::string stats_file = dir + "/stats"; + Lockfile lock(stats_file); + if (lock.acquired()) { + Counters counters = Statistics::read(stats_file); + if (cleanup_performed) { + counters.increment(Statistic::cleanups_performed); + } + counters.set(Statistic::files_in_cache, files_in_cache); + counters.set(Statistic::cache_size_kibibyte, cache_size / 1024); + Statistics::write(stats_file, counters); + } +} + void clean_old(const Context& ctx, const Util::ProgressReceiver& progress_receiver, @@ -164,10 +184,9 @@ clean_up_dir(const std::string& subdir, if (cleaned) { log("Cleaned up cache directory {}", subdir); - stats_add_cleanup(subdir, 1); } - stats_set_sizes(subdir, files_in_cache, cache_size); + update_counters(subdir, files_in_cache, cache_size, cleaned); } // Clean up all cache subdirectories. @@ -204,12 +223,11 @@ wipe_dir(const std::string& subdir, progress_receiver(0.5 + 0.5 * i / files.size()); } - if (!files.empty()) { + const bool cleared = !files.empty(); + if (cleared) { log("Cleared out cache directory {}", subdir); - stats_add_cleanup(subdir, 1); } - - stats_set_sizes(subdir, 0, 0); + update_counters(subdir, 0, 0, cleared); } // Wipe all cached files in all subdirectories. diff --git a/src/stats.cpp b/src/stats.cpp index 70539beed..273dbb453 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -298,30 +298,6 @@ stats_zero(const Context& ctx) } } -// Set the per-directory sizes. -void -stats_set_sizes(const std::string& dir, uint64_t num_files, uint64_t total_size) -{ - std::string statsfile = dir + "/stats"; - Lockfile lock(statsfile); - if (lock.acquired()) { - Counters counters = Statistics::read(statsfile); - counters.set(Statistic::files_in_cache, num_files); - counters.set(Statistic::cache_size_kibibyte, total_size / 1024); - Statistics::write(statsfile, counters); - } -} - -// Count directory cleanup run. -void -stats_add_cleanup(const std::string& dir, uint64_t count) -{ - std::string statsfile = dir + "/stats"; - Counters updates; - updates.increment(Statistic::cleanups_performed, count); - Statistics::increment(statsfile, updates); -} - optional stats_get_result(const Counters& counters) { diff --git a/src/stats.hpp b/src/stats.hpp index b4220f8b2..69d5a001a 100644 --- a/src/stats.hpp +++ b/src/stats.hpp @@ -33,9 +33,4 @@ void stats_zero(const Context& ctx); void stats_summary(const Context& ctx); void stats_print(const Config& config); -void stats_set_sizes(const std::string& dir, - uint64_t num_files, - uint64_t total_size); -void stats_add_cleanup(const std::string& dir, uint64_t count); - nonstd::optional stats_get_result(const Counters& counters);