Skip to content

Commit cc64450

Browse files
committed
docs(examples): add transfer-collection example
1 parent 4645452 commit cc64450

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

examples/transfer-collection.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use anyhow::Result;
2+
use iroh::{protocol::Router, Endpoint, NodeId, Watcher};
3+
use iroh_blobs::{
4+
api::{blobs::Blobs, downloader::Shuffled, Store},
5+
format::collection::Collection,
6+
store::mem::MemStore,
7+
BlobsProtocol, Hash, HashAndFormat,
8+
};
9+
10+
struct Node {
11+
store: Store,
12+
blobs: Blobs,
13+
router: Router,
14+
}
15+
16+
impl Node {
17+
async fn new() -> Result<Self> {
18+
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
19+
20+
// We initialize an in-memory backing store for iroh-blobs
21+
let store = MemStore::new();
22+
// Then we initialize a struct that can accept blobs requests over iroh connections
23+
let blobs = BlobsProtocol::new(&store, endpoint.clone(), None);
24+
25+
let router = Router::builder(endpoint)
26+
.accept(iroh_blobs::ALPN, blobs.clone())
27+
.spawn();
28+
29+
Ok(Self {
30+
store,
31+
blobs: blobs.blobs().clone(),
32+
router,
33+
})
34+
}
35+
36+
async fn node_id(&self) -> Result<NodeId> {
37+
let addr = self.router.endpoint().node_addr().initialized().await;
38+
Ok(addr.node_id)
39+
}
40+
41+
async fn list_hashes(&self) -> Result<Vec<Hash>> {
42+
todo!();
43+
}
44+
45+
async fn create_collection(&self) -> Result<Hash> {
46+
let tx = self.store.batch().await?;
47+
let a = tx.add_slice(b"a").await?;
48+
let b = tx.add_slice(b"b").await?;
49+
let c = tx.add_slice(b"c").await?;
50+
51+
let collection = Collection::from_iter([
52+
("a", a.hash().clone()),
53+
("b", b.hash().clone()),
54+
("c", c.hash().clone()),
55+
]);
56+
57+
let collection = collection.store(&self.store).await?;
58+
let tag = self.store.tags().create(collection).await?;
59+
Ok(tag.hash())
60+
}
61+
62+
async fn get_collection(&self, hash: Hash, provider: NodeId) -> Result<()> {
63+
let req = HashAndFormat::hash_seq(hash);
64+
let addrs = Shuffled::new(vec![provider]);
65+
self.store
66+
.downloader(self.router.endpoint())
67+
.download(req, addrs)
68+
.await?;
69+
Ok(())
70+
}
71+
}
72+
73+
#[tokio::main]
74+
async fn main() -> anyhow::Result<()> {
75+
let send_node = Node::new().await?;
76+
let send_node_id = send_node.node_id().await?;
77+
let hash = send_node.create_collection().await?;
78+
79+
let recv_node = Node::new().await?;
80+
recv_node.get_collection(hash, send_node_id).await?;
81+
82+
let send_hashes = send_node.list_hashes().await?;
83+
let recv_hashes = recv_node.list_hashes().await?;
84+
assert_eq!(send_hashes, recv_hashes);
85+
86+
Ok(())
87+
}

src/api/blobs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ pub struct AddPathOptions {
620620
/// stream directly can be inconvenient, so this struct provides some convenience
621621
/// methods to work with the result.
622622
///
623-
/// It also implements [`IntoFuture`], so you can await it to get the [`TempTag`] that
623+
/// It also implements [`IntoFuture`], so you can await it to get the [`TagInfo`] that
624624
/// contains the hash of the added content and also protects the content.
625625
///
626626
/// If you want access to the stream, you can use the [`AddProgress::stream`] method.

0 commit comments

Comments
 (0)