From 3b9db9cc3242524da98e17b77acc5c9559f3f40b Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Tue, 23 Jul 2024 12:17:51 +0200 Subject: Pass to ObjectCAS an optional ExistsFunc using std::optional ...since this is more readable. And adjust the LocalCAS's uplinker creator. --- src/buildtool/file_system/TARGETS | 1 + src/buildtool/file_system/object_cas.hpp | 44 +++++++++++++++++--------------- src/buildtool/storage/local_cas.hpp | 8 +++--- 3 files changed, 30 insertions(+), 23 deletions(-) (limited to 'src') 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 -#include -#include +#include +#include +#include +#include #include // 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; - /// 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> 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 file_store_; - ExistsFunc exists_; + gsl::not_null 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 { @@ -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 #include +#include #include #include @@ -277,8 +278,7 @@ class LocalCAS { /// \brief Provides uplink via "exists callback" for physical object CAS. template [[nodiscard]] static auto MakeUplinker( - gsl::not_null const*> const& uplinker) -> - typename ObjectCAS::ExistsFunc { + gsl::not_null 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::kDefaultExists; + else { + return std::nullopt; + } } /// \brief Try to sync blob between file CAS and executable CAS. -- cgit v1.2.3