// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_TARGET_CACHE_HPP #define INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_TARGET_CACHE_HPP #include #include #include #include #include #include #include #include "src/buildtool/build_engine/target_map/target_cache_entry.hpp" #include "src/buildtool/build_engine/target_map/target_cache_key.hpp" #include "src/buildtool/common/artifact.hpp" #include "src/buildtool/execution_api/local/local_cas.hpp" #include "src/buildtool/file_system/file_storage.hpp" #include "src/buildtool/file_system/object_type.hpp" #include "src/buildtool/logging/logger.hpp" #ifndef BOOTSTRAP_BUILD_TOOL #include "src/buildtool/execution_api/common/execution_api.hpp" #endif class TargetCache { public: TargetCache() = default; TargetCache(TargetCache const&) = delete; TargetCache(TargetCache&&) = delete; auto operator=(TargetCache const&) -> TargetCache& = delete; auto operator=(TargetCache&&) -> TargetCache& = delete; ~TargetCache() noexcept = default; [[nodiscard]] static auto Instance() -> TargetCache& { static TargetCache instance; return instance; } // Store new key entry pair in the target cache. [[nodiscard]] auto Store(TargetCacheKey const& key, TargetCacheEntry const& value) const noexcept -> bool; // Read existing entry and object info for given key from the target cache. [[nodiscard]] auto Read(TargetCacheKey const& key) const noexcept -> std::optional>; #ifndef BOOTSTRAP_BUILD_TOOL auto SetLocalApi(gsl::not_null const& api) noexcept -> void { local_api_ = api; }; auto SetRemoteApi(gsl::not_null const& api) noexcept -> void { remote_api_ = api; }; #endif private: Logger logger_{"TargetCache"}; FileStorage file_store_{ComputeCacheDir()}; #ifndef BOOTSTRAP_BUILD_TOOL IExecutionApi* local_api_{}; IExecutionApi* remote_api_{}; #endif [[nodiscard]] auto DownloadKnownArtifacts( TargetCacheEntry const& value) const noexcept -> bool; [[nodiscard]] static auto CAS() noexcept -> LocalCAS& { return LocalCAS::Instance(); } [[nodiscard]] static auto ComputeCacheDir() -> std::filesystem::path; [[nodiscard]] static auto ExecutionBackendId() -> std::string; }; namespace std { template <> struct hash { [[nodiscard]] auto operator()(TargetCacheKey const& k) const { return std::hash{}(k.Id()); } }; } // namespace std #endif // INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_TARGET_CACHE_HPP