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;
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;
#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>
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);
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;
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;
}
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;
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);
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;
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()));
-// 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.
//
#pragma once
#include <ccache/hash.hpp>
+#include <ccache/storage/types.hpp>
#include <ccache/util/bytes.hpp>
#include <cxxurl/url.hpp>
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.
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;
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));
});
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
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",
}
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.
-// 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.
//
#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>
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);
};
-// 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.
//
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