]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Improve name and type of only_if_missing parameter
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 6 Aug 2025 14:55:35 +0000 (16:55 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 6 Aug 2025 14:55:35 +0000 (16:55 +0200)
src/ccache/ccache.cpp
src/ccache/storage/local/localstorage.cpp
src/ccache/storage/local/localstorage.hpp
src/ccache/storage/remote/filestorage.cpp
src/ccache/storage/remote/httpstorage.cpp
src/ccache/storage/remote/redisstorage.cpp
src/ccache/storage/remote/remotestorage.hpp
src/ccache/storage/storage.cpp
src/ccache/storage/storage.hpp
src/ccache/storage/types.hpp

index 50a7de2b0acca32a36f19511ebb3e9c2fcf8db05..053c5d6a65e8ebcc7f449e144364e6ab409dbc3f 100644 (file)
@@ -2279,7 +2279,8 @@ get_result_key_from_manifest(Context& ctx, const Hash::Digest& manifest_key)
     core::CacheEntry::Header header(ctx.config, core::CacheEntryType::manifest);
     ctx.storage.local.put(manifest_key,
                           core::CacheEntryType::manifest,
-                          core::CacheEntry::serialize(header, ctx.manifest));
+                          core::CacheEntry::serialize(header, ctx.manifest),
+                          storage::Overwrite::yes);
   }
 
   return result_key;
index ccf2cf4c15ba41c44794f13eaeca1b56c7d9fb73..d21410b90329582c436590ef6a9674ea5d011344 100644 (file)
@@ -527,10 +527,10 @@ void
 LocalStorage::put(const Hash::Digest& key,
                   const core::CacheEntryType type,
                   nonstd::span<const uint8_t> value,
-                  bool only_if_missing)
+                  Overwrite overwrite)
 {
   const auto cache_file = look_up_cache_file(key, type);
-  if (only_if_missing && cache_file.dir_entry.exists()) {
+  if (overwrite == Overwrite::no && cache_file.dir_entry.exists()) {
     LOG("Not storing {} in local storage since it already exists",
         cache_file.path);
     return;
index 962cfed08f052d2963ef983191b8d3f43fa235fb..58366f9f73ef51ecdff14265aa981c50f6f0805a 100644 (file)
@@ -25,6 +25,7 @@
 #include <ccache/hash.hpp>
 #include <ccache/storage/local/statsfile.hpp>
 #include <ccache/storage/local/util.hpp>
+#include <ccache/storage/types.hpp>
 #include <ccache/util/bytes.hpp>
 #include <ccache/util/direntry.hpp>
 #include <ccache/util/lockfile.hpp>
@@ -76,7 +77,7 @@ public:
   void put(const Hash::Digest& key,
            core::CacheEntryType type,
            nonstd::span<const uint8_t> value,
-           bool only_if_missing = false);
+           Overwrite overwrite);
 
   void remove(const Hash::Digest& key, core::CacheEntryType type);
 
index 26f6af4aadc36ce8f7b254bba25ef0a65abd6fdc..eca30a4dce31bc3b421e537e24dfc3cf2522823c 100644 (file)
@@ -54,7 +54,7 @@ public:
 
   tl::expected<bool, Failure> put(const Hash::Digest& key,
                                   nonstd::span<const uint8_t> value,
-                                  bool only_if_missing) override;
+                                  Overwrite overwrite) override;
 
   tl::expected<bool, Failure> remove(const Hash::Digest& key) override;
 
@@ -140,11 +140,11 @@ FileStorageBackend::get(const Hash::Digest& key)
 tl::expected<bool, RemoteStorage::Backend::Failure>
 FileStorageBackend::put(const Hash::Digest& key,
                         const nonstd::span<const uint8_t> value,
-                        const bool only_if_missing)
+                        const Overwrite overwrite)
 {
   const auto path = get_entry_path(key);
 
-  if (only_if_missing && DirEntry(path).exists()) {
+  if (overwrite == Overwrite::no && DirEntry(path).exists()) {
     LOG("{} already in cache", path);
     return false;
   }
index 8ed1cb967ef2c5ee437c4fc6875a3bfa3bc1edc9..dfca496461e197976222d0b6ddd249f914997f59 100644 (file)
@@ -49,7 +49,7 @@ public:
 
   tl::expected<bool, Failure> put(const Hash::Digest& key,
                                   nonstd::span<const uint8_t> value,
-                                  bool only_if_missing) override;
+                                  Overwrite overwrite) override;
 
   tl::expected<bool, Failure> remove(const Hash::Digest& key) override;
 
@@ -193,11 +193,11 @@ HttpStorageBackend::get(const Hash::Digest& key)
 tl::expected<bool, RemoteStorage::Backend::Failure>
 HttpStorageBackend::put(const Hash::Digest& key,
                         const nonstd::span<const uint8_t> value,
-                        const bool only_if_missing)
+                        const Overwrite overwrite)
 {
   const auto url_path = get_entry_path(key);
 
-  if (only_if_missing) {
+  if (overwrite == Overwrite::no) {
     const auto result = m_http_client.Head(url_path);
     LOG("HEAD {}{} -> {}", m_url.str(), url_path, result->status);
 
index bc891139793a3823c5a4d5b7b4eceb14271c0929..73df93cec53949324bc2eea0e0b8acd2a04541c3 100644 (file)
@@ -73,7 +73,7 @@ public:
 
   tl::expected<bool, Failure> put(const Hash::Digest& key,
                                   nonstd::span<const uint8_t> value,
-                                  bool only_if_missing) override;
+                                  Overwrite overwrite) override;
 
   tl::expected<bool, Failure> remove(const Hash::Digest& key) override;
 
@@ -187,11 +187,11 @@ RedisStorageBackend::get(const Hash::Digest& key)
 tl::expected<bool, RemoteStorage::Backend::Failure>
 RedisStorageBackend::put(const Hash::Digest& key,
                          nonstd::span<const uint8_t> value,
-                         bool only_if_missing)
+                         Overwrite overwrite)
 {
   const auto key_string = get_key_string(key);
 
-  if (only_if_missing) {
+  if (overwrite == Overwrite::no) {
     LOG("Redis EXISTS {}", key_string);
     TRY_ASSIGN(const auto reply,
                redis_command("EXISTS %s", key_string.c_str()));
index c47cb53c8fa1731ea6758a56836e551fe449ac92..60f03b8ae872ab700114751221b6f094ecad5c86 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -19,6 +19,7 @@
 #pragma once
 
 #include <ccache/hash.hpp>
+#include <ccache/storage/types.hpp>
 #include <ccache/util/bytes.hpp>
 
 #include <cxxurl/url.hpp>
@@ -75,12 +76,11 @@ public:
     virtual tl::expected<std::optional<util::Bytes>, Failure>
     get(const Hash::Digest& key) = 0;
 
-    // Put `value` associated to `key` in the storage. A true `only_if_missing`
-    // is a hint that the value does not have to be set if already present.
-    // Returns true if the entry was stored, otherwise false.
+    // Put `value` associated to `key` in the storage. Returns true if the entry
+    // was stored, otherwise false.
     virtual tl::expected<bool, Failure> put(const Hash::Digest& key,
                                             nonstd::span<const uint8_t> value,
-                                            bool only_if_missing = false) = 0;
+                                            Overwrite overwrite) = 0;
 
     // Remove `key` and its associated value. Returns true if the entry was
     // removed, otherwise false.
index e3c140ffedecdbb6fa7659c17f5a4ac57abd2239..cbcb967d10390ca84db200c5edf5b62649c9ef49 100644 (file)
@@ -276,7 +276,7 @@ Storage::get(const Hash::Hash::Digest& key,
     auto value = local.get(key, type);
     if (value) {
       if (m_config.reshare()) {
-        put_in_remote_storage(key, *value, true);
+        put_in_remote_storage(key, *value, Overwrite::no);
       }
       if (entry_receiver(std::move(*value))) {
         return;
@@ -286,7 +286,7 @@ Storage::get(const Hash::Hash::Digest& key,
 
   get_from_remote_storage(key, type, [&](util::Bytes&& data) {
     if (!m_config.remote_only()) {
-      local.put(key, type, data, true);
+      local.put(key, type, data, Overwrite::no);
     }
     return entry_receiver(std::move(data));
   });
@@ -298,9 +298,9 @@ Storage::put(const Hash::Digest& key,
              nonstd::span<const uint8_t> value)
 {
   if (!m_config.remote_only()) {
-    local.put(key, type, value);
+    local.put(key, type, value, Overwrite::yes);
   }
-  put_in_remote_storage(key, value, false);
+  put_in_remote_storage(key, value, Overwrite::yes);
 }
 
 void
@@ -502,7 +502,7 @@ Storage::get_from_remote_storage(const Hash::Digest& key,
 void
 Storage::put_in_remote_storage(const Hash::Digest& key,
                                nonstd::span<const uint8_t> value,
-                               bool only_if_missing)
+                               Overwrite overwrite)
 {
   if (!core::CacheEntry::Header(value).self_contained) {
     LOG("Not putting {} in remote storage since it's not self-contained",
@@ -517,7 +517,7 @@ Storage::put_in_remote_storage(const Hash::Digest& key,
     }
 
     Timer timer;
-    const auto result = backend->impl->put(key, value, only_if_missing);
+    const auto result = backend->impl->put(key, value, overwrite);
     const auto ms = timer.measure_ms();
     if (!result) {
       // The backend is expected to log details about the error.
index a0c5a971207560ed6511bb1005ce6c90729383f4..4525ee910528ad97ced116d9fe3365a3b9e757b5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -22,6 +22,7 @@
 #include <ccache/hash.hpp>
 #include <ccache/storage/local/localstorage.hpp>
 #include <ccache/storage/remote/remotestorage.hpp>
+#include <ccache/storage/types.hpp>
 #include <ccache/util/bytes.hpp>
 
 #include <nonstd/span.hpp>
@@ -88,7 +89,7 @@ private:
 
   void put_in_remote_storage(const Hash::Digest& key,
                              nonstd::span<const uint8_t> value,
-                             bool only_if_missing);
+                             Overwrite overwrite);
 
   void remove_from_remote_storage(const Hash::Digest& key);
 };
index 16e70cd765a71ceaf6e320a94ce0744ec4732ee9..392ea8a50c86d62f67810106a5d5e0fe470a0380 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -25,4 +25,9 @@ namespace storage {
 
 using EntryWriter = std::function<bool(const std::string& path)>;
 
+enum class Overwrite {
+  yes, // Overwrite any preexisting value
+  no,  // OK to not overwrite any preexisting value (but OK to overwrite anyway)
+};
+
 } // namespace storage