From ef4c6f8d93e416f9508f2eb7904cd98411950651 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 17 Jan 2024 16:42:25 +0100 Subject: [PATCH] add tags_to_delete --- iroh/src/client.rs | 21 +++++++++++++++++++-- iroh/src/node.rs | 10 +++++++++- iroh/src/rpc_protocol.rs | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/iroh/src/client.rs b/iroh/src/client.rs index 97916bb586..de22cdd516 100644 --- a/iroh/src/client.rs +++ b/iroh/src/client.rs @@ -281,14 +281,22 @@ where } /// Create a collection from already existing blobs. + /// + /// For automtically clearing the tags for the passed in blobs you can set + /// `tags_to_delete` to those tags, and they will be deleted once the collection is created. pub async fn create_collection( &self, collection: Collection, tag: SetTagOption, + tags_to_delete: Vec, ) -> anyhow::Result<(Hash, Tag)> { let CreateCollectionResponse { hash, tag } = self .rpc - .rpc(CreateCollectionRequest { collection, tag }) + .rpc(CreateCollectionRequest { + collection, + tag, + tags_to_delete, + }) .await??; Ok((hash, tag)) } @@ -1267,6 +1275,7 @@ mod tests { let client = node.client(); let mut collection = Collection::default(); + let mut tags = Vec::new(); // import files for path in &paths { let import_outcome = client @@ -1287,11 +1296,12 @@ mod tests { path.file_name().unwrap().to_str().unwrap().to_string(), import_outcome.hash, ); + tags.push(import_outcome.tag); } let (hash, tag) = client .blobs - .create_collection(collection, SetTagOption::Auto) + .create_collection(collection, SetTagOption::Auto, tags) .await?; let collections: Vec<_> = client.blobs.list_collections().await?.try_collect().await?; @@ -1302,6 +1312,13 @@ mod tests { // 5 blobs + 1 meta assert_eq!(collections[0].total_blobs_count, Some(5 + 1)); + // check that "temp" tags have been deleted + let tags: Vec<_> = client.tags.list().await?.try_collect().await?; + assert_eq!(tags.len(), 1); + assert_eq!(tags[0].hash, hash); + assert_eq!(tags[0].name, tag); + assert_eq!(tags[0].format, BlobFormat::HashSeq); + Ok(()) } } diff --git a/iroh/src/node.rs b/iroh/src/node.rs index b46f2b41b5..b6ffe671ff 100644 --- a/iroh/src/node.rs +++ b/iroh/src/node.rs @@ -1517,7 +1517,11 @@ impl RpcHandler { self, req: CreateCollectionRequest, ) -> RpcResult { - let CreateCollectionRequest { collection, tag } = req; + let CreateCollectionRequest { + collection, + tag, + tags_to_delete, + } = req; let temp_tag = collection.store(&self.inner.db).await?; let hash_and_format = temp_tag.inner(); @@ -1533,6 +1537,10 @@ impl RpcHandler { SetTagOption::Auto => self.inner.db.create_tag(*hash_and_format).await?, }; + for tag in tags_to_delete { + self.inner.db.set_tag(tag, None).await?; + } + Ok(CreateCollectionResponse { hash, tag }) } } diff --git a/iroh/src/rpc_protocol.rs b/iroh/src/rpc_protocol.rs index 57515e85b5..8464b94dc5 100644 --- a/iroh/src/rpc_protocol.rs +++ b/iroh/src/rpc_protocol.rs @@ -281,6 +281,8 @@ pub struct CreateCollectionRequest { pub collection: Collection, /// Tag option. pub tag: SetTagOption, + /// Tags that should be deleted after creation. + pub tags_to_delete: Vec, } /// A response to a create collection request