summaryrefslogtreecommitdiff
path: root/src/buildtool/build_engine/target_map/target_cache.hpp
blob: ef25c0580bfeac0de360e3ac81986ed4e2f63303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 <filesystem>
#include <functional>
#include <optional>
#include <string>
#include <utility>

#include <gsl-lite/gsl-lite.hpp>
#include <nlohmann/json.hpp>

#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<std::pair<TargetCacheEntry, Artifact::ObjectInfo>>;

#ifndef BOOTSTRAP_BUILD_TOOL
    auto SetLocalApi(gsl::not_null<IExecutionApi*> const& api) noexcept
        -> void {
        local_api_ = api;
    };

    auto SetRemoteApi(gsl::not_null<IExecutionApi*> const& api) noexcept
        -> void {
        remote_api_ = api;
    };
#endif

  private:
    Logger logger_{"TargetCache"};
    FileStorage<ObjectType::File,
                StoreMode::LastWins,
                /*kSetEpochTime=*/false>
        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<ObjectType::File>& {
        return LocalCAS<ObjectType::File>::Instance();
    }
    [[nodiscard]] static auto ComputeCacheDir() -> std::filesystem::path;
    [[nodiscard]] static auto ExecutionBackendId() -> std::string;
};

namespace std {
template <>
struct hash<TargetCacheKey> {
    [[nodiscard]] auto operator()(TargetCacheKey const& k) const {
        return std::hash<Artifact::ObjectInfo>{}(k.Id());
    }
};
}  // namespace std

#endif  // INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_TARGET_CACHE_HPP