summaryrefslogtreecommitdiff
path: root/src/buildtool/storage/uplinker.cpp
blob: f9e5c33f3c8849f54a1b5806d9c6c650424b7e11 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2024 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 BOOTSTRAP_BUILD_TOOL

#include "src/buildtool/storage/uplinker.hpp"

#include <algorithm>
#include <cstddef>

#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/storage/storage.hpp"
#include "src/buildtool/storage/target_cache.hpp"
#include "src/buildtool/storage/target_cache_key.hpp"

namespace {
[[nodiscard]] auto CreateGenerations(
    gsl::not_null<StorageConfig const*> const& storage_config) noexcept
    -> std::vector<Generation> {
    std::vector<Generation> generations;
    generations.reserve(storage_config->num_generations);
    for (std::size_t i = 0; i < storage_config->num_generations; ++i) {
        generations.emplace_back(Generation::Create(storage_config,
                                                    /*generation=*/i));
    }
    return generations;
}
}  // namespace

GlobalUplinker::GlobalUplinker(
    gsl::not_null<StorageConfig const*> const& storage_config) noexcept
    : storage_config_{*storage_config},
      generations_{CreateGenerations(&storage_config_)} {}

GlobalUplinker::~GlobalUplinker() = default;

auto GlobalUplinker::UplinkBlob(ArtifactDigest const& digest,
                                bool is_executable) const noexcept -> bool {
    // Try to find blob in all generations.
    auto const& latest = generations_[Generation::kYoungest].CAS();
    return std::any_of(
        generations_.begin(),
        generations_.end(),
        [&latest, &digest, is_executable](Generation const& generation) {
            return generation.CAS().LocalUplinkBlob(latest,
                                                    digest,
                                                    is_executable,
                                                    /*skip_sync=*/true,
                                                    /*splice_result=*/true);
        });
}

auto GlobalUplinker::UplinkTree(ArtifactDigest const& digest) const noexcept
    -> bool {
    // Try to find blob in all generations.
    auto const& latest = generations_[Generation::kYoungest].CAS();
    return std::any_of(generations_.begin(),
                       generations_.end(),
                       [&latest, &digest](Generation const& generation) {
                           return generation.CAS().LocalUplinkTree(
                               latest, digest, /*splice_result=*/true);
                       });
}

auto GlobalUplinker::UplinkLargeBlob(
    ArtifactDigest const& digest) const noexcept -> bool {
    // Try to find large entry in all generations.
    auto const& latest = generations_[Generation::kYoungest].CAS();
    return std::any_of(
        generations_.begin(),
        generations_.end(),
        [&latest, &digest](Generation const& generation) {
            return generation.CAS().LocalUplinkLargeObject<ObjectType::File>(
                latest, digest);
        });
}

auto GlobalUplinker::UplinkActionCacheEntry(
    ArtifactDigest const& action_id) const noexcept -> bool {
    // Try to find action-cache entry in all generations.
    auto const& latest = generations_[Generation::kYoungest].ActionCache();
    return std::any_of(generations_.begin(),
                       generations_.end(),
                       [&latest, &action_id](Generation const& generation) {
                           return generation.ActionCache().LocalUplinkEntry(
                               latest, action_id);
                       });
}

auto GlobalUplinker::UplinkTargetCacheEntry(
    TargetCacheKey const& key,
    BackendDescription const& backend_description) const noexcept -> bool {
    // Try to find target-cache entry in all generations.
    auto const& latest =
        generations_[Generation::kYoungest].TargetCache().WithShard(
            backend_description);
    return std::any_of(
        generations_.begin(),
        generations_.end(),
        [&latest, &key, &backend_description](Generation const& generation) {
            return generation.TargetCache()
                .WithShard(backend_description)
                .LocalUplinkEntry(latest, key);
        });
}

#endif  // BOOTSTRAP_BUILD_TOOL