From 32c865dce37ff18d796caa9f3cd760eb22edd8f5 Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Fri, 20 Dec 2024 12:47:39 +0100 Subject: Pack BackendDescription to a class ...to let it be stored as an independent instance. --- src/buildtool/storage/backend_description.cpp | 41 +++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'src/buildtool/storage/backend_description.cpp') diff --git a/src/buildtool/storage/backend_description.cpp b/src/buildtool/storage/backend_description.cpp index a290dc22..0f1a3401 100644 --- a/src/buildtool/storage/backend_description.cpp +++ b/src/buildtool/storage/backend_description.cpp @@ -19,11 +19,24 @@ #include "fmt/core.h" #include "nlohmann/json.hpp" +#include "src/buildtool/common/artifact_digest_factory.hpp" +#include "src/buildtool/file_system/object_type.hpp" -auto DescribeBackend(std::optional const& address, - ExecutionProperties const& properties, - std::vector const& dispatch) noexcept - -> expected { +BackendDescription::BackendDescription() noexcept { + if (auto dummy = Describe( + /*address=*/std::nullopt, /*properties=*/{}, /*dispatch=*/{})) { + description_ = dummy->description_; + } + else { + description_ = std::make_shared(); + } +} + +auto BackendDescription::Describe( + std::optional const& address, + ExecutionProperties const& properties, + std::vector const& dispatch) noexcept + -> expected { nlohmann::json description; try { description["remote_address"] = @@ -54,16 +67,32 @@ auto DescribeBackend(std::optional const& address, "Failed to serialize endpoint dispatch list:\n{}", e.what())}; } } + + std::shared_ptr result; + std::shared_ptr sha256; try { // json::dump with json::error_handler_t::replace will not throw an // exception if invalid UTF-8 sequences are detected in the input. // Instead, it will replace them with the UTF-8 replacement // character, but still it needs to be inside a try-catch clause to // ensure the noexcept modifier of the enclosing function. - return description.dump( - 2, ' ', false, nlohmann::json::error_handler_t::replace); + result = std::make_shared(description.dump( + 2, ' ', false, nlohmann::json::error_handler_t::replace)); + + std::string hash = + ArtifactDigestFactory::HashDataAs( + HashFunction(HashFunction::Type::PlainSHA256), *result) + .hash(); + sha256 = std::make_shared(std::move(hash)); } catch (std::exception const& e) { return unexpected{fmt::format( "Failed to dump backend description to JSON:\n{}", e.what())}; } + return BackendDescription(std::move(result), std::move(sha256)); +} + +auto BackendDescription::HashContent(HashFunction hash_function) const noexcept + -> ArtifactDigest { + return ArtifactDigestFactory::HashDataAs( + hash_function, GetDescription()); } -- cgit v1.2.3