Skip to content

Commit 900d169

Browse files
committed
Add API to get the content of a single tag
1 parent d739d52 commit 900d169

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

src/rpc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ impl<D: crate::store::Store> Handler<D> {
317317
#[allow(clippy::manual_flatten)]
318318
for item in tags {
319319
if let Ok((name, HashAndFormat { hash, format })) = item {
320+
if let Some(from) = msg.from.as_ref() {
321+
if &name < from {
322+
continue;
323+
}
324+
}
325+
if let Some(to) = msg.to.as_ref() {
326+
if &name >= to {
327+
break;
328+
}
329+
}
320330
if (format.is_raw() && msg.raw) || (format.is_hash_seq() && msg.hash_seq) {
321331
co.yield_(TagInfo { name, hash, format }).await;
322332
}

src/rpc/client/tags.rs

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ where
4242
Self { rpc }
4343
}
4444

45+
/// Get the value of a single tag
46+
pub async fn get(&self, name: Tag) -> Result<Option<TagInfo>> {
47+
let mut stream = self.rpc.server_streaming(ListRequest::single(name)).await?;
48+
Ok(stream.next().await.transpose()?)
49+
}
50+
4551
/// Lists all tags.
4652
pub async fn list(&self) -> Result<impl Stream<Item = Result<TagInfo>>> {
4753
let stream = self.rpc.server_streaming(ListRequest::all()).await?;

src/rpc/proto/tags.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Tags RPC protocol
2+
use bytes::Bytes;
23
use nested_enum_utils::enum_conversions;
34
use quic_rpc_derive::rpc_requests;
45
use serde::{Deserialize, Serialize};
56

67
use super::{RpcResult, RpcService};
7-
use crate::{net_protocol::BatchId, rpc::client::tags::TagInfo, HashAndFormat, Tag};
8+
use crate::{
9+
net_protocol::BatchId, rpc::client::tags::TagInfo, util::increment_vec, HashAndFormat, Tag,
10+
};
811

912
#[allow(missing_docs)]
1013
#[derive(strum::Display, Debug, Serialize, Deserialize)]
@@ -73,14 +76,33 @@ pub struct ListRequest {
7376
pub raw: bool,
7477
/// List hash seq tags
7578
pub hash_seq: bool,
79+
/// From tag
80+
pub from: Option<Tag>,
81+
/// To tag (exclusive)
82+
pub to: Option<Tag>,
7683
}
7784

7885
impl ListRequest {
86+
/// List a single tag
87+
pub fn single(name: Tag) -> Self {
88+
let mut next = name.0.to_vec();
89+
increment_vec(&mut next);
90+
let next = Bytes::from(next).into();
91+
Self {
92+
raw: true,
93+
hash_seq: true,
94+
from: Some(name),
95+
to: Some(next),
96+
}
97+
}
98+
7999
/// List all tags
80100
pub fn all() -> Self {
81101
Self {
82102
raw: true,
83103
hash_seq: true,
104+
from: None,
105+
to: None,
84106
}
85107
}
86108

@@ -89,6 +111,8 @@ impl ListRequest {
89111
Self {
90112
raw: true,
91113
hash_seq: false,
114+
from: None,
115+
to: None,
92116
}
93117
}
94118

@@ -97,6 +121,8 @@ impl ListRequest {
97121
Self {
98122
raw: false,
99123
hash_seq: true,
124+
from: None,
125+
to: None,
100126
}
101127
}
102128
}

src/util.rs

+13
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,19 @@ pub(crate) fn raw_outboard_size(size: u64) -> u64 {
302302
BaoTree::new(size, IROH_BLOCK_SIZE).outboard_size()
303303
}
304304

305+
/// Increment a byte vector, lexographically.
306+
pub(crate) fn increment_vec(bytes: &mut Vec<u8>) {
307+
for byte in bytes.iter_mut().rev() {
308+
if *byte < 255 {
309+
*byte += 1;
310+
return;
311+
}
312+
*byte = 0;
313+
}
314+
315+
bytes.push(0);
316+
}
317+
305318
/// Synchronously compute the outboard of a file, and return hash and outboard.
306319
///
307320
/// It is assumed that the file is not modified while this is running.

0 commit comments

Comments
 (0)