Skip to content

Commit 387c68c

Browse files
authored
feat: Richer tags api (#69)
## Description This makes the anemic tags api much more rich - Tag deletion - delete by range, by prefix, single item, or all 💣 - Tag listing - list by range, prefix or get single item - Tag setting - set a tag to a value - Tag renaming - atomic rename, just like for files ## Breaking Changes Breaks the RPC protocol and the store trait RPC protocol: - rpc::proto::tags::DeleteRequest now has from and to field - rpc::proto::tags::DeleteRequest no longer has name field - rpc::proto::tags::ListRequest now has from and to field - rpc::proto::tags::ListRequest::all removed - rpc::proto::tags::ListRequest::raw removed - rpc::proto::tags::ListRequest::hash_seq removed Store trait: - store::traits::ReadableStore::tags now takes from and to ## Notes & open questions Question: should we enable the rpc feature flag by default? Given that the only way to get the pleasant API at this time is the rpc, and we want beginners to have a good experience? In my big refactor you will be able to get this API without rpc enabled, but for now this is just how it is. Implements #65 ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 56b0695 commit 387c68c

File tree

12 files changed

+755
-115
lines changed

12 files changed

+755
-115
lines changed

src/hash.rs

+6
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ pub struct HashAndFormat {
256256
pub format: BlobFormat,
257257
}
258258

259+
impl From<Hash> for HashAndFormat {
260+
fn from(hash: Hash) -> Self {
261+
Self::raw(hash)
262+
}
263+
}
264+
259265
#[cfg(feature = "redb")]
260266
mod redb_support {
261267
use postcard::experimental::max_size::MaxSize;

src/rpc.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use proto::{
3030
},
3131
tags::{
3232
CreateRequest as TagsCreateRequest, DeleteRequest as TagDeleteRequest,
33-
ListRequest as TagListRequest, SetRequest as TagsSetRequest, SyncMode,
33+
ListRequest as TagListRequest, RenameRequest, SetRequest as TagsSetRequest, SyncMode,
3434
},
3535
Request, RpcError, RpcResult, RpcService,
3636
};
@@ -158,6 +158,7 @@ impl<D: crate::store::Store> Handler<D> {
158158
Set(msg) => chan.rpc(msg, self, Self::tags_set).await,
159159
DeleteTag(msg) => chan.rpc(msg, self, Self::blob_delete_tag).await,
160160
ListTags(msg) => chan.server_streaming(msg, self, Self::blob_list_tags).await,
161+
Rename(msg) => chan.rpc(msg, self, Self::tags_rename).await,
161162
}
162163
}
163164

@@ -295,7 +296,7 @@ impl<D: crate::store::Store> Handler<D> {
295296

296297
async fn blob_delete_tag(self, msg: TagDeleteRequest) -> RpcResult<()> {
297298
self.store()
298-
.set_tag(msg.name, None)
299+
.delete_tags(msg.from, msg.to)
299300
.await
300301
.map_err(|e| RpcError::new(&e))?;
301302
Ok(())
@@ -313,7 +314,7 @@ impl<D: crate::store::Store> Handler<D> {
313314
tracing::info!("blob_list_tags");
314315
let blobs = self;
315316
Gen::new(|co| async move {
316-
let tags = blobs.store().tags().await.unwrap();
317+
let tags = blobs.store().tags(msg.from, msg.to).await.unwrap();
317318
#[allow(clippy::manual_flatten)]
318319
for item in tags {
319320
if let Ok((name, HashAndFormat { hash, format })) = item {
@@ -382,6 +383,16 @@ impl<D: crate::store::Store> Handler<D> {
382383
rx.map(AddPathResponse)
383384
}
384385

386+
async fn tags_rename(self, msg: RenameRequest) -> RpcResult<()> {
387+
let blobs = self;
388+
blobs
389+
.store()
390+
.rename_tag(msg.from, msg.to)
391+
.await
392+
.map_err(|e| RpcError::new(&e))?;
393+
Ok(())
394+
}
395+
385396
async fn tags_set(self, msg: TagsSetRequest) -> RpcResult<()> {
386397
let blobs = self;
387398
blobs
@@ -393,13 +404,11 @@ impl<D: crate::store::Store> Handler<D> {
393404
blobs.store().sync().await.map_err(|e| RpcError::new(&e))?;
394405
}
395406
if let Some(batch) = msg.batch {
396-
if let Some(content) = msg.value.as_ref() {
397-
blobs
398-
.batches()
399-
.await
400-
.remove_one(batch, content)
401-
.map_err(|e| RpcError::new(&*e))?;
402-
}
407+
blobs
408+
.batches()
409+
.await
410+
.remove_one(batch, &msg.value)
411+
.map_err(|e| RpcError::new(&*e))?;
403412
}
404413
Ok(())
405414
}
@@ -572,10 +581,7 @@ impl<D: crate::store::Store> Handler<D> {
572581
let HashAndFormat { hash, format } = *hash_and_format;
573582
let tag = match tag {
574583
SetTagOption::Named(tag) => {
575-
blobs
576-
.store()
577-
.set_tag(tag.clone(), Some(*hash_and_format))
578-
.await?;
584+
blobs.store().set_tag(tag.clone(), *hash_and_format).await?;
579585
tag
580586
}
581587
SetTagOption::Auto => blobs.store().create_tag(*hash_and_format).await?,
@@ -764,10 +770,7 @@ impl<D: crate::store::Store> Handler<D> {
764770
let HashAndFormat { hash, format } = hash_and_format;
765771
let tag = match msg.tag {
766772
SetTagOption::Named(tag) => {
767-
blobs
768-
.store()
769-
.set_tag(tag.clone(), Some(hash_and_format))
770-
.await?;
773+
blobs.store().set_tag(tag.clone(), hash_and_format).await?;
771774
tag
772775
}
773776
SetTagOption::Auto => blobs.store().create_tag(hash_and_format).await?,
@@ -907,7 +910,7 @@ impl<D: crate::store::Store> Handler<D> {
907910
SetTagOption::Named(tag) => {
908911
blobs
909912
.store()
910-
.set_tag(tag.clone(), Some(*hash_and_format))
913+
.set_tag(tag.clone(), *hash_and_format)
911914
.await
912915
.map_err(|e| RpcError::new(&e))?;
913916
tag
@@ -922,7 +925,7 @@ impl<D: crate::store::Store> Handler<D> {
922925
for tag in tags_to_delete {
923926
blobs
924927
.store()
925-
.set_tag(tag, None)
928+
.delete_tags(Some(tag.clone()), Some(tag.successor()))
926929
.await
927930
.map_err(|e| RpcError::new(&e))?;
928931
}
@@ -959,7 +962,7 @@ impl<D: crate::store::Store> Handler<D> {
959962
progress.send(DownloadProgress::AllDone(stats)).await.ok();
960963
match tag {
961964
SetTagOption::Named(tag) => {
962-
self.store().set_tag(tag, Some(hash_and_format)).await?;
965+
self.store().set_tag(tag, hash_and_format).await?;
963966
}
964967
SetTagOption::Auto => {
965968
self.store().create_tag(hash_and_format).await?;

src/rpc/client/blobs/batch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ where
441441
.rpc
442442
.rpc(tags::SetRequest {
443443
name: tag,
444-
value: Some(tt.hash_and_format()),
444+
value: tt.hash_and_format(),
445445
batch: Some(self.0.batch),
446446
sync: SyncMode::Full,
447447
})

0 commit comments

Comments
 (0)