]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Refactor away Result::Writer knowledge about counter_updates
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 13 Jun 2021 18:41:32 +0000 (20:41 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 13 Jun 2021 18:47:39 +0000 (20:47 +0200)
src/Result.cpp
src/Result.hpp
src/ccache.cpp

index 8408d2a7b7fd3037e8caf45fd853795e8697f638..7a6701409b626623daf55040ac2c914762448fde 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2021 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -196,6 +196,14 @@ gcno_file_in_unmangled_form(const Context& ctx)
   return Util::change_extension(ctx.args_info.output_obj, ".gcno");
 }
 
+FileSizeAndCountDiff&
+FileSizeAndCountDiff::operator+=(const FileSizeAndCountDiff& other)
+{
+  size_kibibyte += other.size_kibibyte;
+  count += other.count;
+  return *this;
+}
+
 Result::Reader::Reader(const std::string& result_path)
   : m_result_path(result_path)
 {
@@ -311,20 +319,20 @@ Writer::write(FileType file_type, const std::string& file_path)
   m_entries_to_write.emplace_back(file_type, file_path);
 }
 
-optional<std::string>
+nonstd::expected<FileSizeAndCountDiff, std::string>
 Writer::finalize()
 {
   try {
-    do_finalize();
-    return nullopt;
+    return do_finalize();
   } catch (const Error& e) {
-    return e.what();
+    return nonstd::make_unexpected(e.what());
   }
 }
 
-void
+FileSizeAndCountDiff
 Writer::do_finalize()
 {
+  FileSizeAndCountDiff file_size_and_count_diff{0, 0};
   uint64_t payload_size = 0;
   payload_size += 1; // n_entries
   for (const auto& pair : m_entries_to_write) {
@@ -369,7 +377,7 @@ Writer::do_finalize()
     writer.write(file_size);
 
     if (store_raw) {
-      write_raw_file_entry(path, entry_number);
+      file_size_and_count_diff += write_raw_file_entry(path, entry_number);
     } else {
       write_embedded_file_entry(writer, path, file_size);
     }
@@ -379,6 +387,8 @@ Writer::do_finalize()
 
   writer.finalize();
   atomic_result_file.commit();
+
+  return file_size_and_count_diff;
 }
 
 void
@@ -410,7 +420,7 @@ Result::Writer::write_embedded_file_entry(CacheEntryWriter& writer,
   }
 }
 
-void
+FileSizeAndCountDiff
 Result::Writer::write_raw_file_entry(const std::string& path,
                                      uint32_t entry_number)
 {
@@ -423,11 +433,10 @@ Result::Writer::write_raw_file_entry(const std::string& path,
       "Failed to store {} as raw file {}: {}", path, raw_file, e.what());
   }
   const auto new_stat = Stat::stat(raw_file);
-  m_ctx.counter_updates.increment(
-    Statistic::cache_size_kibibyte,
-    Util::size_change_kibibyte(old_stat, new_stat));
-  m_ctx.counter_updates.increment(Statistic::files_in_cache,
-                                  (new_stat ? 1 : 0) - (old_stat ? 1 : 0));
+  return {
+    Util::size_change_kibibyte(old_stat, new_stat),
+    (new_stat ? 1 : 0) - (old_stat ? 1 : 0),
+  };
 }
 
 } // namespace Result
index 01068c9e6f06e25d3fccc249038b85c8df17177d..7fcfc95b6cd712a6374fcd222a9b2b40c039bae3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2021 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -20,6 +20,7 @@
 
 #include "system.hpp"
 
+#include "third_party/nonstd/expected.hpp"
 #include "third_party/nonstd/optional.hpp"
 
 #include <map>
@@ -79,6 +80,14 @@ const char* file_type_to_string(FileType type);
 std::string gcno_file_in_mangled_form(const Context& ctx);
 std::string gcno_file_in_unmangled_form(const Context& ctx);
 
+struct FileSizeAndCountDiff
+{
+  int64_t size_kibibyte;
+  int64_t count;
+
+  FileSizeAndCountDiff& operator+=(const FileSizeAndCountDiff& other);
+};
+
 // This class knows how to read a result cache entry.
 class Reader
 {
@@ -121,18 +130,19 @@ public:
   void write(FileType file_type, const std::string& file_path);
 
   // Write registered files to the result. Returns an error message on error.
-  nonstd::optional<std::string> finalize();
+  nonstd::expected<FileSizeAndCountDiff, std::string> finalize();
 
 private:
   Context& m_ctx;
   const std::string m_result_path;
   std::vector<std::pair<FileType, std::string>> m_entries_to_write;
 
-  void do_finalize();
+  FileSizeAndCountDiff do_finalize();
   static void write_embedded_file_entry(CacheEntryWriter& writer,
                                         const std::string& path,
                                         uint64_t file_size);
-  void write_raw_file_entry(const std::string& path, uint32_t entry_number);
+  FileSizeAndCountDiff write_raw_file_entry(const std::string& path,
+                                            uint32_t entry_number);
 };
 
 } // namespace Result
index 9be789b9b74d543f9fff4af4279144874016c7a8..87e4f94e7cae04ad187a3d9de95862534637be87 100644 (file)
@@ -1134,11 +1134,15 @@ to_cache(Context& ctx,
                         ctx.args_info.output_dwo);
   }
 
-  auto error = result_writer.finalize();
-  if (error) {
-    LOG("Error: {}", *error);
-  } else {
+  const auto file_size_and_count_diff = result_writer.finalize();
+  if (file_size_and_count_diff) {
     LOG("Stored in cache: {}", result_file.path);
+    ctx.counter_updates.increment(Statistic::cache_size_kibibyte,
+                                  file_size_and_count_diff->size_kibibyte);
+    ctx.counter_updates.increment(Statistic::files_in_cache,
+                                  file_size_and_count_diff->count);
+  } else {
+    LOG("Error: {}", file_size_and_count_diff.error());
   }
 
   auto new_result_stat = Stat::stat(result_file.path, Stat::OnError::log);