Skip to content

Commit a9ca505

Browse files
committed
Add a fn to list all tags with a certain prefix
1 parent 900d169 commit a9ca505

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/rpc/client/tags.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//!
1212
//! [`Client::delete`] can be used to delete a tag.
1313
use anyhow::Result;
14+
use bytes::Bytes;
1415
use futures_lite::{Stream, StreamExt};
1516
use quic_rpc::{client::BoxedConnector, Connector, RpcClient};
1617
use serde::{Deserialize, Serialize};
@@ -48,6 +49,15 @@ where
4849
Ok(stream.next().await.transpose()?)
4950
}
5051

52+
/// Lists all tags.
53+
pub async fn list_prefix(&self, prefix: &[u8]) -> Result<impl Stream<Item = Result<TagInfo>>> {
54+
let stream = self
55+
.rpc
56+
.server_streaming(ListRequest::prefix(Bytes::copy_from_slice(prefix).into()))
57+
.await?;
58+
Ok(stream.map(|res| res.map_err(anyhow::Error::from)))
59+
}
60+
5161
/// Lists all tags.
5262
pub async fn list(&self) -> Result<impl Stream<Item = Result<TagInfo>>> {
5363
let stream = self.rpc.server_streaming(ListRequest::all()).await?;

src/rpc/proto/tags.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use serde::{Deserialize, Serialize};
66

77
use super::{RpcResult, RpcService};
88
use crate::{
9-
net_protocol::BatchId, rpc::client::tags::TagInfo, util::increment_vec, HashAndFormat, Tag,
9+
net_protocol::BatchId,
10+
rpc::client::tags::TagInfo,
11+
util::{increment_vec, next_prefix},
12+
HashAndFormat, Tag,
1013
};
1114

1215
#[allow(missing_docs)]
@@ -83,6 +86,22 @@ pub struct ListRequest {
8386
}
8487

8588
impl ListRequest {
89+
/// List tags with a prefix
90+
pub fn prefix(prefix: Tag) -> Self {
91+
let mut to = prefix.0.to_vec();
92+
let to = if next_prefix(&mut to) {
93+
Some(Bytes::from(to).into())
94+
} else {
95+
None
96+
};
97+
Self {
98+
raw: true,
99+
hash_seq: true,
100+
from: Some(prefix),
101+
to,
102+
}
103+
}
104+
86105
/// List a single tag
87106
pub fn single(name: Tag) -> Self {
88107
let mut next = name.0.to_vec();

src/util.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,21 @@ pub(crate) fn raw_outboard_size(size: u64) -> u64 {
302302
BaoTree::new(size, IROH_BLOCK_SIZE).outboard_size()
303303
}
304304

305+
/// Given a prefix, increment it lexographically.
306+
///
307+
/// If the prefix is all FF, this will return false because there is no
308+
/// higher prefix than that.
309+
pub(crate) fn next_prefix(bytes: &mut [u8]) -> bool {
310+
for byte in bytes.iter_mut().rev() {
311+
if *byte < 255 {
312+
*byte += 1;
313+
return true;
314+
}
315+
*byte = 0;
316+
}
317+
false
318+
}
319+
305320
/// Increment a byte vector, lexographically.
306321
pub(crate) fn increment_vec(bytes: &mut Vec<u8>) {
307322
for byte in bytes.iter_mut().rev() {

0 commit comments

Comments
 (0)