Skip to content

Commit

Permalink
add tags_to_delete
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jan 17, 2024
1 parent 73f2f34 commit ef4c6f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
21 changes: 19 additions & 2 deletions iroh/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag>,
) -> 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))
}
Expand Down Expand Up @@ -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
Expand All @@ -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?;
Expand All @@ -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(())
}
}
10 changes: 9 additions & 1 deletion iroh/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,11 @@ impl<D: BaoStore> RpcHandler<D> {
self,
req: CreateCollectionRequest,
) -> RpcResult<CreateCollectionResponse> {
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();
Expand All @@ -1533,6 +1537,10 @@ impl<D: BaoStore> RpcHandler<D> {
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 })
}
}
Expand Down
2 changes: 2 additions & 0 deletions iroh/src/rpc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag>,
}

/// A response to a create collection request
Expand Down

0 comments on commit ef4c6f8

Please sign in to comment.