diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-07-23 12:17:51 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-08-07 14:43:19 +0200 |
commit | 3b9db9cc3242524da98e17b77acc5c9559f3f40b (patch) | |
tree | ae45563d9cec9b3cdce9392bf1fc82e3a56dfdfb | |
parent | ed6f31f4c9939d6cc8d4d317d561a94545750b0b (diff) | |
download | justbuild-3b9db9cc3242524da98e17b77acc5c9559f3f40b.tar.gz |
Pass to ObjectCAS an optional ExistsFunc using std::optional
...since this is more readable. And adjust the LocalCAS's uplinker creator.
-rw-r--r-- | src/buildtool/file_system/TARGETS | 1 | ||||
-rw-r--r-- | src/buildtool/file_system/object_cas.hpp | 44 | ||||
-rw-r--r-- | src/buildtool/storage/local_cas.hpp | 8 |
3 files changed, 30 insertions, 23 deletions
diff --git a/src/buildtool/file_system/TARGETS b/src/buildtool/file_system/TARGETS index b67ac0cf..724d6b8d 100644 --- a/src/buildtool/file_system/TARGETS +++ b/src/buildtool/file_system/TARGETS @@ -29,6 +29,7 @@ , ["src/buildtool/logging", "log_level"] , ["src/buildtool/logging", "logging"] , ["src/buildtool/crypto", "hash_function"] + , ["@", "gsl", "", "gsl"] ] , "stage": ["src", "buildtool", "file_system"] } diff --git a/src/buildtool/file_system/object_cas.hpp b/src/buildtool/file_system/object_cas.hpp index cf960110..95091773 100644 --- a/src/buildtool/file_system/object_cas.hpp +++ b/src/buildtool/file_system/object_cas.hpp @@ -15,11 +15,13 @@ #ifndef INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_OBJECT_CAS_HPP #define INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_OBJECT_CAS_HPP -#include <memory> -#include <sstream> -#include <thread> +#include <filesystem> +#include <functional> +#include <optional> +#include <string> #include <utility> // std::move +#include "gsl/gsl" #include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/common/bazel_types.hpp" #include "src/buildtool/crypto/hash_function.hpp" @@ -44,24 +46,20 @@ class ObjectCAS { using ExistsFunc = std::function<bool(bazel_re::Digest const&, std::filesystem::path const&)>; - /// Default callback for checking blob existence. - static inline ExistsFunc const kDefaultExists = [](auto /*digest*/, - auto path) { - return FileSystemManager::IsFile(path); - }; - /// \brief Create new object CAS in store_path directory. /// The optional "exists callback" is used to check blob existence before /// every read and write operation. It promises that a blob for the given /// digest exists at the given path if true was returned. /// \param store_path The path to use for storing blobs. /// \param exists (optional) Function for checking blob existence. - explicit ObjectCAS(HashFunction hash_function, - std::filesystem::path const& store_path, - ExistsFunc exists = kDefaultExists) - : hash_function_{hash_function}, - file_store_{store_path}, - exists_{std::move(exists)} {} + explicit ObjectCAS( + HashFunction hash_function, + std::filesystem::path const& store_path, + std::optional<gsl::not_null<ExistsFunc>> exists = std::nullopt) + : file_store_{store_path}, + exists_{exists.has_value() ? std::move(exists)->get() + : kDefaultExists}, + hash_function_{hash_function} {} ObjectCAS(ObjectCAS const&) = delete; ObjectCAS(ObjectCAS&&) = delete; @@ -109,15 +107,21 @@ class ObjectCAS { private: // For `Tree` the underlying storage type is non-executable file. - static constexpr auto kStorageType = - kType == ObjectType::Tree ? ObjectType::File : kType; + static constexpr ObjectType kStorageType = + IsTreeObject(kType) ? ObjectType::File : kType; - HashFunction const hash_function_; Logger logger_{std::string{"ObjectCAS"} + ToChar(kType)}; FileStorage<kStorageType, StoreMode::FirstWins, /*kSetEpochTime=*/true> file_store_; - ExistsFunc exists_; + gsl::not_null<ExistsFunc> exists_; + HashFunction const hash_function_; + + /// Default callback for checking blob existence. + static inline ExistsFunc const kDefaultExists = [](auto const& /*digest*/, + auto const& path) { + return FileSystemManager::IsFile(path); + }; [[nodiscard]] auto CreateDigest(std::string const& bytes) const noexcept -> std::optional<bazel_re::Digest> { @@ -133,7 +137,7 @@ class ObjectCAS { bazel_re::Digest const& digest, std::filesystem::path const& path) const noexcept -> bool { try { - return exists_ and exists_(digest, path); + return std::invoke(exists_.get(), digest, path); } catch (...) { return false; } diff --git a/src/buildtool/storage/local_cas.hpp b/src/buildtool/storage/local_cas.hpp index 1f6a74ed..287b1d7b 100644 --- a/src/buildtool/storage/local_cas.hpp +++ b/src/buildtool/storage/local_cas.hpp @@ -17,6 +17,7 @@ #include <filesystem> #include <optional> +#include <string> #include <unordered_set> #include <vector> @@ -277,8 +278,7 @@ class LocalCAS { /// \brief Provides uplink via "exists callback" for physical object CAS. template <ObjectType kType> [[nodiscard]] static auto MakeUplinker( - gsl::not_null<Uplinker<kDoGlobalUplink> const*> const& uplinker) -> - typename ObjectCAS<kType>::ExistsFunc { + gsl::not_null<Uplinker<kDoGlobalUplink> const*> const& uplinker) { if constexpr (kDoGlobalUplink) { return [uplinker](auto const& digest, auto const& /*path*/) { if constexpr (IsTreeObject(kType)) { @@ -291,7 +291,9 @@ class LocalCAS { return uplinker->UplinkBlob(digest, IsExecutableObject(kType)); }; } - return ObjectCAS<kType>::kDefaultExists; + else { + return std::nullopt; + } } /// \brief Try to sync blob between file CAS and executable CAS. |