From: Joel Rosdahl Date: Sat, 3 Sep 2022 17:47:11 +0000 (+0200) Subject: refactor: Replace util::Blob alias X-Git-Tag: v4.7~83 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3934faf7c7b80f91ec17b5f61007e55400841d3;p=thirdparty%2Fccache.git refactor: Replace util::Blob alias I want to introduce more use of nonstd::span and I think that it makes more sense to use unaliased types. --- diff --git a/src/AtomicFile.cpp b/src/AtomicFile.cpp index 9d0753b4a..f171dd2fb 100644 --- a/src/AtomicFile.cpp +++ b/src/AtomicFile.cpp @@ -51,7 +51,7 @@ AtomicFile::write(const std::string& data) } void -AtomicFile::write(const util::Blob& data) +AtomicFile::write(nonstd::span data) { if (fwrite(data.data(), data.size(), 1, m_stream) != 1) { throw core::Error( diff --git a/src/AtomicFile.hpp b/src/AtomicFile.hpp index 2dffb25ff..3c9768a33 100644 --- a/src/AtomicFile.hpp +++ b/src/AtomicFile.hpp @@ -18,7 +18,7 @@ #pragma once -#include +#include #include #include @@ -37,7 +37,7 @@ public: FILE* stream(); void write(const std::string& data); - void write(const util::Blob& data); + void write(nonstd::span data); // Close the temporary file and rename it to the destination file. Note: The // destructor will not do this automatically to avoid half-written data in the diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index af8c11518..8d26a34aa 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -246,7 +246,7 @@ Storage::get(const Digest& key, m_secondary_storages.end(), [](const auto& entry) { return !entry->config.read_only; }); if (should_put_in_secondary_storage) { - const auto value = util::read_file(*path); + const auto value = util::read_file>(*path); if (!value) { LOG("Failed to read {}: {}", *path, value.error()); return path; // Don't indicate failure since primary storage was OK. @@ -309,7 +309,7 @@ Storage::put(const Digest& key, m_secondary_storages.end(), [](const auto& entry) { return !entry->config.read_only; }); if (should_put_in_secondary_storage) { - const auto value = util::read_file(*path); + const auto value = util::read_file>(*path); if (!value) { LOG("Failed to read {}: {}", *path, value.error()); return true; // Don't indicate failure since primary storage was OK. @@ -469,7 +469,7 @@ Storage::get_backend(SecondaryStorageEntry& entry, } } -std::optional +std::optional> Storage::get_from_secondary_storage(const Digest& key) { MTR_SCOPE("secondary_storage", "get"); @@ -510,7 +510,7 @@ Storage::get_from_secondary_storage(const Digest& key) void Storage::put_in_secondary_storage(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing) { MTR_SCOPE("secondary_storage", "put"); diff --git a/src/storage/Storage.hpp b/src/storage/Storage.hpp index 6810fe130..4eac498fc 100644 --- a/src/storage/Storage.hpp +++ b/src/storage/Storage.hpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -81,10 +80,12 @@ private: const Digest& key, std::string_view operation_description, const bool for_writing); - std::optional get_from_secondary_storage(const Digest& key); + + std::optional> + get_from_secondary_storage(const Digest& key); void put_in_secondary_storage(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing); void remove_from_secondary_storage(const Digest& key); diff --git a/src/storage/secondary/FileStorage.cpp b/src/storage/secondary/FileStorage.cpp index 16580a6a1..38c0f8b19 100644 --- a/src/storage/secondary/FileStorage.cpp +++ b/src/storage/secondary/FileStorage.cpp @@ -43,11 +43,11 @@ class FileStorageBackend : public SecondaryStorage::Backend public: FileStorageBackend(const Params& params); - nonstd::expected, Failure> + nonstd::expected>, Failure> get(const Digest& key) override; nonstd::expected put(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing) override; nonstd::expected remove(const Digest& key) override; @@ -103,7 +103,8 @@ FileStorageBackend::FileStorageBackend(const Params& params) } } -nonstd::expected, SecondaryStorage::Backend::Failure> +nonstd::expected>, + SecondaryStorage::Backend::Failure> FileStorageBackend::get(const Digest& key) { const auto path = get_entry_path(key); @@ -120,7 +121,7 @@ FileStorageBackend::get(const Digest& key) util::set_timestamps(path); } - auto value = util::read_file(path); + auto value = util::read_file>(path); if (!value) { LOG("Failed to read {}: {}", path, value.error()); return nonstd::make_unexpected(Failure::error); @@ -130,7 +131,7 @@ FileStorageBackend::get(const Digest& key) nonstd::expected FileStorageBackend::put(const Digest& key, - const util::Blob& value, + const std::vector& value, const bool only_if_missing) { const auto path = get_entry_path(key); diff --git a/src/storage/secondary/HttpStorage.cpp b/src/storage/secondary/HttpStorage.cpp index 5e11cfe6a..ba99485d8 100644 --- a/src/storage/secondary/HttpStorage.cpp +++ b/src/storage/secondary/HttpStorage.cpp @@ -41,11 +41,11 @@ class HttpStorageBackend : public SecondaryStorage::Backend public: HttpStorageBackend(const Params& params); - nonstd::expected, Failure> + nonstd::expected>, Failure> get(const Digest& key) override; nonstd::expected put(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing) override; nonstd::expected remove(const Digest& key) override; @@ -144,7 +144,8 @@ HttpStorageBackend::HttpStorageBackend(const Params& params) m_http_client.set_write_timeout(operation_timeout); } -nonstd::expected, SecondaryStorage::Backend::Failure> +nonstd::expected>, + SecondaryStorage::Backend::Failure> HttpStorageBackend::get(const Digest& key) { const auto url_path = get_entry_path(key); @@ -163,12 +164,12 @@ HttpStorageBackend::get(const Digest& key) return std::nullopt; } - return util::Blob(result->body.begin(), result->body.end()); + return std::vector(result->body.begin(), result->body.end()); } nonstd::expected HttpStorageBackend::put(const Digest& key, - const util::Blob& value, + const std::vector& value, const bool only_if_missing) { const auto url_path = get_entry_path(key); diff --git a/src/storage/secondary/RedisStorage.cpp b/src/storage/secondary/RedisStorage.cpp index cb985a688..5f03f1e60 100644 --- a/src/storage/secondary/RedisStorage.cpp +++ b/src/storage/secondary/RedisStorage.cpp @@ -60,11 +60,11 @@ class RedisStorageBackend : public SecondaryStorage::Backend public: RedisStorageBackend(const SecondaryStorage::Backend::Params& params); - nonstd::expected, Failure> + nonstd::expected>, Failure> get(const Digest& key) override; nonstd::expected put(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing) override; nonstd::expected remove(const Digest& key) override; @@ -157,7 +157,8 @@ is_timeout(int err) #endif } -nonstd::expected, SecondaryStorage::Backend::Failure> +nonstd::expected>, + SecondaryStorage::Backend::Failure> RedisStorageBackend::get(const Digest& key) { const auto key_string = get_key_string(key); @@ -166,7 +167,7 @@ RedisStorageBackend::get(const Digest& key) if (!reply) { return nonstd::make_unexpected(reply.error()); } else if ((*reply)->type == REDIS_REPLY_STRING) { - return util::Blob((*reply)->str, (*reply)->str + (*reply)->len); + return std::vector((*reply)->str, (*reply)->str + (*reply)->len); } else if ((*reply)->type == REDIS_REPLY_NIL) { return std::nullopt; } else { @@ -177,7 +178,7 @@ RedisStorageBackend::get(const Digest& key) nonstd::expected RedisStorageBackend::put(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing) { const auto key_string = get_key_string(key); diff --git a/src/storage/secondary/SecondaryStorage.hpp b/src/storage/secondary/SecondaryStorage.hpp index 72911418a..08110517e 100644 --- a/src/storage/secondary/SecondaryStorage.hpp +++ b/src/storage/secondary/SecondaryStorage.hpp @@ -19,7 +19,6 @@ #pragma once #include -#include #include #include @@ -79,7 +78,7 @@ public: // Get the value associated with `key`. Returns the value on success or // std::nullopt if the entry is not present. - virtual nonstd::expected, Failure> + virtual nonstd::expected>, Failure> get(const Digest& key) = 0; // Put `value` associated to `key` in the storage. A true `only_if_missing` @@ -87,7 +86,7 @@ public: // Returns true if the entry was stored, otherwise false. virtual nonstd::expected put(const Digest& key, - const util::Blob& value, + const std::vector& value, bool only_if_missing = false) = 0; // Remove `key` and its associated value. Returns true if the entry was diff --git a/src/util/file.cpp b/src/util/file.cpp index 77b24b539..e7a2763d6 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -181,7 +181,7 @@ read_file(const std::string& path, size_t size_hint) template nonstd::expected read_file(const std::string& path, size_t size_hint); -template nonstd::expected +template nonstd::expected, std::string> read_file(const std::string& path, size_t size_hint); void diff --git a/src/util/file.hpp b/src/util/file.hpp index dc6596c86..fd812cd61 100644 --- a/src/util/file.hpp +++ b/src/util/file.hpp @@ -38,10 +38,10 @@ void create_cachedir_tag(const std::string& dir); nonstd::expected read_fd(int fd, DataReceiver data_receiver); // Return data from `path`, where `T` is `std::string` for text data and -// `util::Blob` for binary data. If `T` is `std::string` and the content starts -// with a UTF-16 little-endian BOM on Windows then it will be converted to -// UTF-8. If `size_hint` is not 0 then it is assumed that `path` has this size -// (this saves system calls). +// `std::vector` for binary data. If `T` is `std::string` and the +// content starts with a UTF-16 little-endian BOM on Windows then it will be +// converted to UTF-8. If `size_hint` is not 0 then it is assumed that `path` +// has this size (this saves system calls). template nonstd::expected read_file(const std::string& path, size_t size_hint = 0); diff --git a/src/util/types.hpp b/src/util/types.hpp index 8c9d19662..e09a10524 100644 --- a/src/util/types.hpp +++ b/src/util/types.hpp @@ -25,7 +25,6 @@ namespace util { -using Blob = std::vector; using DataReceiver = std::function; } // namespace util diff --git a/unittest/test_AtomicFile.cpp b/unittest/test_AtomicFile.cpp index 7bd38ed24..f4005ba54 100644 --- a/unittest/test_AtomicFile.cpp +++ b/unittest/test_AtomicFile.cpp @@ -34,7 +34,7 @@ TEST_CASE("Base case") AtomicFile atomic_file("test", AtomicFile::Mode::text); atomic_file.write("h"); - atomic_file.write(util::Blob{0x65, 0x6c}); + atomic_file.write(std::vector{0x65, 0x6c}); fputs("lo", atomic_file.stream()); atomic_file.commit(); CHECK(*util::read_file("test") == "hello"); diff --git a/unittest/test_util_file.cpp b/unittest/test_util_file.cpp index dbbf66677..eec2de43e 100644 --- a/unittest/test_util_file.cpp +++ b/unittest/test_util_file.cpp @@ -42,7 +42,7 @@ TEST_CASE("util::read_file and util::write_file, text data") // Newline handling REQUIRE(util::write_file("test", "foo\r\nbar\n")); - auto bin_data = util::read_file("test"); + auto bin_data = util::read_file>("test"); REQUIRE(bin_data); #ifdef _WIN32 const std::string expected_bin_data = "foo\r\r\nbar\r\n"; @@ -50,7 +50,8 @@ TEST_CASE("util::read_file and util::write_file, text data") const std::string expected_bin_data = "foo\r\nbar\n"; #endif CHECK(*bin_data - == util::Blob(expected_bin_data.begin(), expected_bin_data.end())); + == std::vector(expected_bin_data.begin(), + expected_bin_data.end())); REQUIRE(util::write_file("size_hint_test", std::string(8192, '\0'))); data = util::read_file("size_hint_test", 8191 /*size_hint*/); @@ -77,21 +78,23 @@ TEST_CASE("util::read_file and util::write_file, binary data") { TestContext test_context; - util::Blob expected; + std::vector expected; for (size_t i = 0; i < 512; ++i) { expected.push_back((32 + i) % 256); } CHECK(util::write_file("test", expected)); - auto actual = util::read_file("test"); + auto actual = util::read_file>("test"); REQUIRE(actual); CHECK(*actual == expected); - REQUIRE(util::write_file("size_hint_test", util::Blob(8192, 0))); - auto data = util::read_file("size_hint_test", 8191 /*size_hint*/); + REQUIRE(util::write_file("size_hint_test", std::vector(8192, 0))); + auto data = + util::read_file>("size_hint_test", 8191 /*size_hint*/); REQUIRE(data); CHECK(data->size() == 8192); - data = util::read_file("size_hint_test", 8193 /*size_hint*/); + data = + util::read_file>("size_hint_test", 8193 /*size_hint*/); REQUIRE(data); CHECK(data->size() == 8192); }