/build/source/nativelink-service/src/remote_asset_proto.rs
Line | Count | Source |
1 | | // Copyright 2026 Trace Machina, Inc. All rights reserved. |
2 | | // |
3 | | // Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // See LICENSE file for details |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | use bytes::Bytes; |
16 | | use nativelink_proto::build::bazel::remote::asset::v1::Qualifier; |
17 | | use nativelink_proto::build::bazel::remote::execution::v2::Digest; |
18 | | use nativelink_util::common::DigestInfo; |
19 | | use nativelink_util::digest_hasher::{DigestHasher, DigestHasherFunc}; |
20 | | use prost::Message; |
21 | | use prost_types::Timestamp; |
22 | | |
23 | | #[derive(Debug, Clone, PartialEq, Eq)] |
24 | | pub struct RemoteAssetQuery { |
25 | | pub uri: String, |
26 | | pub qualifiers: Vec<Qualifier>, |
27 | | } |
28 | | |
29 | | impl RemoteAssetQuery { |
30 | 95 | pub const fn new(uri: String, qualifiers: Vec<Qualifier>) -> Self { |
31 | 95 | Self { uri, qualifiers } |
32 | 95 | } |
33 | | |
34 | 95 | pub fn digest(&self) -> DigestInfo { |
35 | 95 | let mut hasher = DigestHasherFunc::Blake3.hasher(); |
36 | 95 | hasher.update(self.uri.as_bytes()); |
37 | 95 | for qualifier57 in &self.qualifiers { |
38 | 57 | hasher.update(qualifier.name.as_bytes()); |
39 | 57 | hasher.update(qualifier.value.as_bytes()); |
40 | 57 | } |
41 | 95 | hasher.finalize_digest() |
42 | 95 | } |
43 | | } |
44 | | |
45 | | #[derive(Clone, PartialEq, Eq, prost::Message)] |
46 | | pub struct RemoteAssetArtifact { |
47 | | #[prost(string, tag = "1")] |
48 | | pub uri: String, |
49 | | |
50 | | #[prost(message, repeated, tag = "2")] |
51 | | pub qualifiers: Vec<Qualifier>, |
52 | | |
53 | | #[prost(message, tag = "3")] |
54 | | pub blob_digest: Option<Digest>, |
55 | | |
56 | | #[prost(message, optional, tag = "4")] |
57 | | pub expire_at: Option<Timestamp>, |
58 | | |
59 | | #[prost( |
60 | | enumeration = "nativelink_proto::build::bazel::remote::execution::v2::digest_function::Value", |
61 | | tag = "5" |
62 | | )] |
63 | | pub digest_function: i32, |
64 | | |
65 | | /// When retrieval from origin started. Older records have no timestamp. |
66 | | #[prost(message, optional, tag = "6")] |
67 | | pub fetched_at: Option<Timestamp>, |
68 | | } |
69 | | |
70 | | impl RemoteAssetArtifact { |
71 | 54 | pub const fn new( |
72 | 54 | uri: String, |
73 | 54 | qualifiers: Vec<Qualifier>, |
74 | 54 | blob_digest: Digest, |
75 | 54 | expire_at: Option<Timestamp>, |
76 | 54 | digest_function: i32, |
77 | 54 | ) -> Self { |
78 | 54 | Self { |
79 | 54 | uri, |
80 | 54 | qualifiers, |
81 | 54 | blob_digest: Some(blob_digest), |
82 | 54 | expire_at, |
83 | 54 | digest_function, |
84 | 54 | fetched_at: None, |
85 | 54 | } |
86 | 54 | } |
87 | | |
88 | 54 | pub fn digest(&self) -> DigestInfo { |
89 | 54 | let mut hasher = DigestHasherFunc::Blake3.hasher(); |
90 | 54 | hasher.update(self.uri.as_bytes()); |
91 | 54 | for qualifier30 in &self.qualifiers { |
92 | 30 | hasher.update(qualifier.name.as_bytes()); |
93 | 30 | hasher.update(qualifier.value.as_bytes()); |
94 | 30 | } |
95 | 54 | hasher.finalize_digest() |
96 | 54 | } |
97 | | |
98 | 54 | pub fn as_bytes(&self) -> Bytes { |
99 | 54 | let encoded_asset = self.encode_to_vec(); |
100 | 54 | Bytes::from_owner(encoded_asset) |
101 | 54 | } |
102 | | } |