]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Replace util::Blob alias
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 3 Sep 2022 17:47:11 +0000 (19:47 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 6 Sep 2022 06:13:23 +0000 (08:13 +0200)
I want to introduce more use of nonstd::span<const uint8_t> and I think
that it makes more sense to use unaliased types.

13 files changed:
src/AtomicFile.cpp
src/AtomicFile.hpp
src/storage/Storage.cpp
src/storage/Storage.hpp
src/storage/secondary/FileStorage.cpp
src/storage/secondary/HttpStorage.cpp
src/storage/secondary/RedisStorage.cpp
src/storage/secondary/SecondaryStorage.hpp
src/util/file.cpp
src/util/file.hpp
src/util/types.hpp
unittest/test_AtomicFile.cpp
unittest/test_util_file.cpp

index 9d0753b4a1471ef14a56c68f01a3e985c69d03b3..f171dd2fbc3636f5e22bc6274710d85bf85dad55 100644 (file)
@@ -51,7 +51,7 @@ AtomicFile::write(const std::string& data)
 }
 
 void
-AtomicFile::write(const util::Blob& data)
+AtomicFile::write(nonstd::span<const uint8_t> data)
 {
   if (fwrite(data.data(), data.size(), 1, m_stream) != 1) {
     throw core::Error(
index 2dffb25ffc38ea8386c6d2fa2aa0b2d3ce818438..3c9768a3361a8c169665a9ae0cf1b8b25c7523d1 100644 (file)
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include <util/types.hpp>
+#include <third_party/nonstd/span.hpp>
 
 #include <cstdint>
 #include <cstdio>
@@ -37,7 +37,7 @@ public:
   FILE* stream();
 
   void write(const std::string& data);
-  void write(const util::Blob& data);
+  void write(nonstd::span<const uint8_t> 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
index af8c11518cbf178d82c8a80ba5156ac989eb694a..8d26a34aa271c50115bad977f397fd17384b4ae9 100644 (file)
@@ -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<util::Blob>(*path);
+          const auto value = util::read_file<std::vector<uint8_t>>(*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<util::Blob>(*path);
+    const auto value = util::read_file<std::vector<uint8_t>>(*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<util::Blob>
+std::optional<std::vector<uint8_t>>
 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<uint8_t>& value,
                                   bool only_if_missing)
 {
   MTR_SCOPE("secondary_storage", "put");
index 6810fe1300735c598c1340eaba5158e5c3b8e3b7..4eac498fc015093c5e721b2361d7c7cba76a96b6 100644 (file)
@@ -22,7 +22,6 @@
 #include <storage/primary/PrimaryStorage.hpp>
 #include <storage/secondary/SecondaryStorage.hpp>
 #include <storage/types.hpp>
-#include <util/types.hpp>
 
 #include <functional>
 #include <memory>
@@ -81,10 +80,12 @@ private:
               const Digest& key,
               std::string_view operation_description,
               const bool for_writing);
-  std::optional<util::Blob> get_from_secondary_storage(const Digest& key);
+
+  std::optional<std::vector<uint8_t>>
+  get_from_secondary_storage(const Digest& key);
 
   void put_in_secondary_storage(const Digest& key,
-                                const util::Blob& value,
+                                const std::vector<uint8_t>& value,
                                 bool only_if_missing);
 
   void remove_from_secondary_storage(const Digest& key);
index 16580a6a12b3aea89f0260da6629aeb7033b741f..38c0f8b1917530a0dc8d7e090b0e5cc45758aa33 100644 (file)
@@ -43,11 +43,11 @@ class FileStorageBackend : public SecondaryStorage::Backend
 public:
   FileStorageBackend(const Params& params);
 
-  nonstd::expected<std::optional<util::Blob>, Failure>
+  nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
   get(const Digest& key) override;
 
   nonstd::expected<bool, Failure> put(const Digest& key,
-                                      const util::Blob& value,
+                                      const std::vector<uint8_t>& value,
                                       bool only_if_missing) override;
 
   nonstd::expected<bool, Failure> remove(const Digest& key) override;
@@ -103,7 +103,8 @@ FileStorageBackend::FileStorageBackend(const Params& params)
   }
 }
 
-nonstd::expected<std::optional<util::Blob>, SecondaryStorage::Backend::Failure>
+nonstd::expected<std::optional<std::vector<uint8_t>>,
+                 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<util::Blob>(path);
+  auto value = util::read_file<std::vector<uint8_t>>(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<bool, SecondaryStorage::Backend::Failure>
 FileStorageBackend::put(const Digest& key,
-                        const util::Blob& value,
+                        const std::vector<uint8_t>& value,
                         const bool only_if_missing)
 {
   const auto path = get_entry_path(key);
index 5e11cfe6a37f93cb81d7875cc0faf478fb510a98..ba99485d897f38df794bcf8ac7fbbf5c17d2ae94 100644 (file)
@@ -41,11 +41,11 @@ class HttpStorageBackend : public SecondaryStorage::Backend
 public:
   HttpStorageBackend(const Params& params);
 
-  nonstd::expected<std::optional<util::Blob>, Failure>
+  nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
   get(const Digest& key) override;
 
   nonstd::expected<bool, Failure> put(const Digest& key,
-                                      const util::Blob& value,
+                                      const std::vector<uint8_t>& value,
                                       bool only_if_missing) override;
 
   nonstd::expected<bool, Failure> remove(const Digest& key) override;
@@ -144,7 +144,8 @@ HttpStorageBackend::HttpStorageBackend(const Params& params)
   m_http_client.set_write_timeout(operation_timeout);
 }
 
-nonstd::expected<std::optional<util::Blob>, SecondaryStorage::Backend::Failure>
+nonstd::expected<std::optional<std::vector<uint8_t>>,
+                 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<uint8_t>(result->body.begin(), result->body.end());
 }
 
 nonstd::expected<bool, SecondaryStorage::Backend::Failure>
 HttpStorageBackend::put(const Digest& key,
-                        const util::Blob& value,
+                        const std::vector<uint8_t>& value,
                         const bool only_if_missing)
 {
   const auto url_path = get_entry_path(key);
index cb985a688ebdcf8a1f2528f6e000d5b02e3c7142..5f03f1e60d348def91318e55d2b57c69df202c4f 100644 (file)
@@ -60,11 +60,11 @@ class RedisStorageBackend : public SecondaryStorage::Backend
 public:
   RedisStorageBackend(const SecondaryStorage::Backend::Params& params);
 
-  nonstd::expected<std::optional<util::Blob>, Failure>
+  nonstd::expected<std::optional<std::vector<uint8_t>>, Failure>
   get(const Digest& key) override;
 
   nonstd::expected<bool, Failure> put(const Digest& key,
-                                      const util::Blob& value,
+                                      const std::vector<uint8_t>& value,
                                       bool only_if_missing) override;
 
   nonstd::expected<bool, Failure> remove(const Digest& key) override;
@@ -157,7 +157,8 @@ is_timeout(int err)
 #endif
 }
 
-nonstd::expected<std::optional<util::Blob>, SecondaryStorage::Backend::Failure>
+nonstd::expected<std::optional<std::vector<uint8_t>>,
+                 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<uint8_t>((*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<bool, SecondaryStorage::Backend::Failure>
 RedisStorageBackend::put(const Digest& key,
-                         const util::Blob& value,
+                         const std::vector<uint8_t>& value,
                          bool only_if_missing)
 {
   const auto key_string = get_key_string(key);
index 72911418adbdf1c2169a0bb9e20adaad849ff213..08110517e8aa1e6837f3a16359fa7c1c2dd91d62 100644 (file)
@@ -19,7 +19,6 @@
 #pragma once
 
 #include <storage/types.hpp>
-#include <util/types.hpp>
 
 #include <third_party/nonstd/expected.hpp>
 #include <third_party/url.hpp>
@@ -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<std::optional<util::Blob>, Failure>
+    virtual nonstd::expected<std::optional<std::vector<uint8_t>>, 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<bool, Failure>
     put(const Digest& key,
-        const util::Blob& value,
+        const std::vector<uint8_t>& value,
         bool only_if_missing = false) = 0;
 
     // Remove `key` and its associated value. Returns true if the entry was
index 77b24b539984daf646bbd4338319df0397aad79a..e7a2763d6002fb786a0d1a0d1f4b4244992141c5 100644 (file)
@@ -181,7 +181,7 @@ read_file(const std::string& path, size_t size_hint)
 template nonstd::expected<std::string, std::string>
 read_file(const std::string& path, size_t size_hint);
 
-template nonstd::expected<util::Blob, std::string>
+template nonstd::expected<std::vector<uint8_t>, std::string>
 read_file(const std::string& path, size_t size_hint);
 
 void
index dc6596c862f53ac27956886ca34797b4828fc193..fd812cd61951f12bc4ea5a97281a611594aef109 100644 (file)
@@ -38,10 +38,10 @@ void create_cachedir_tag(const std::string& dir);
 nonstd::expected<void, std::string> 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<uint8_t>` 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<typename T>
 nonstd::expected<T, std::string> read_file(const std::string& path,
                                            size_t size_hint = 0);
index 8c9d19662449cf6065214cf4824f6fc675e4189f..e09a105240801bf0b7642373055548933382a1a8 100644 (file)
@@ -25,7 +25,6 @@
 
 namespace util {
 
-using Blob = std::vector<uint8_t>;
 using DataReceiver = std::function<void(const void* data, size_t size)>;
 
 } // namespace util
index 7bd38ed24650b115a0f76794231fac262280e04b..f4005ba5418f07e76786f1c7cebac716d26390bb 100644 (file)
@@ -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<uint8_t>{0x65, 0x6c});
   fputs("lo", atomic_file.stream());
   atomic_file.commit();
   CHECK(*util::read_file<std::string>("test") == "hello");
index dbbf666770b4328123fb1523e80c2700024a2385..eec2de43e95720df7ec78f5105ee0871197bfee4 100644 (file)
@@ -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<util::Blob>("test");
+  auto bin_data = util::read_file<std::vector<uint8_t>>("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<uint8_t>(expected_bin_data.begin(),
+                                expected_bin_data.end()));
 
   REQUIRE(util::write_file("size_hint_test", std::string(8192, '\0')));
   data = util::read_file<std::string>("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<uint8_t> 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<util::Blob>("test");
+  auto actual = util::read_file<std::vector<uint8_t>>("test");
   REQUIRE(actual);
   CHECK(*actual == expected);
 
-  REQUIRE(util::write_file("size_hint_test", util::Blob(8192, 0)));
-  auto data = util::read_file<util::Blob>("size_hint_test", 8191 /*size_hint*/);
+  REQUIRE(util::write_file("size_hint_test", std::vector<uint8_t>(8192, 0)));
+  auto data =
+    util::read_file<std::vector<uint8_t>>("size_hint_test", 8191 /*size_hint*/);
   REQUIRE(data);
   CHECK(data->size() == 8192);
-  data = util::read_file<util::Blob>("size_hint_test", 8193 /*size_hint*/);
+  data =
+    util::read_file<std::vector<uint8_t>>("size_hint_test", 8193 /*size_hint*/);
   REQUIRE(data);
   CHECK(data->size() == 8192);
 }