summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/common/bytestream_utils.hpp
blob: 672ae017e894d04af7453cd4de841ece27b9e2a1 (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
// 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 INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_BYTESTREAM_UTILS_HPP
#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_BYTESTREAM_UTILS_HPP

#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>

namespace build::bazel::remote::execution::v2 {
class Digest;
}
namespace bazel_re = build::bazel::remote::execution::v2;

class ByteStreamUtils final {
    static constexpr auto* kBlobs = "blobs";
    static constexpr auto* kUploads = "uploads";

  public:
    // Chunk size for uploads (default size used by BuildBarn)
    static constexpr std::size_t kChunkSize = 64 * 1024;

    /// \brief Create a read request for the bytestream service to be
    /// transferred over the net. Handles serialization/deserialization on its
    /// own. The pattern is:
    /// "{instance_name}/{kBlobs}/{digest.hash()}/{digest.size_bytes()}".
    /// "instance_name_example/blobs/62183d7a696acf7e69e218efc82c93135f8c85f895/4424712"
    class ReadRequest final {
      public:
        explicit ReadRequest(std::string instance_name,
                             bazel_re::Digest const& digest) noexcept;

        [[nodiscard]] auto ToString() && noexcept -> std::string;

        [[nodiscard]] static auto FromString(
            std::string const& request) noexcept -> std::optional<ReadRequest>;

        [[nodiscard]] auto GetInstanceName() const noexcept
            -> std::string const& {
            return instance_name_;
        }

        [[nodiscard]] auto GetDigest() const noexcept -> bazel_re::Digest;

      private:
        std::string instance_name_;
        std::string hash_;
        std::int64_t size_ = 0;

        ReadRequest() = default;
    };

    /// \brief Create a write request for the bytestream service to be
    /// transferred over the net. Handles serialization/deserialization on its
    /// own. The pattern is:
    /// "{instance_name}/{kUploads}/{uuid}/{kBlobs}/{digest.hash()}/{digest.size_bytes()}".
    /// "instance_name_example/uploads/c4f03510-7d56-4490-8934-01bce1b1288e/blobs/62183d7a696acf7e69e218efc82c93135f8c85f895/4424712"
    class WriteRequest final {
      public:
        explicit WriteRequest(std::string instance_name,
                              std::string uuid,
                              bazel_re::Digest const& digest) noexcept;

        [[nodiscard]] auto ToString() && noexcept -> std::string;

        [[nodiscard]] static auto FromString(
            std::string const& request) noexcept -> std::optional<WriteRequest>;

        [[nodiscard]] auto GetInstanceName() const noexcept
            -> std::string const& {
            return instance_name_;
        }

        [[nodiscard]] auto GetUUID() const noexcept -> std::string const& {
            return uuid_;
        }

        [[nodiscard]] auto GetDigest() const noexcept -> bazel_re::Digest;

      private:
        std::string instance_name_;
        std::string uuid_;
        std::string hash_;
        std::int64_t size_ = 0;

        WriteRequest() = default;
    };
};

#endif  // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_BYTESTREAM_UTILS_HPP