Skip to content

Commit cb8e8c0

Browse files
committed
Add a basic differencing blockstore layer
1 parent 5da91d0 commit cb8e8c0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Diff for: atrium-repo/src/blockstore.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use std::future::Future;
33
use ipld_core::cid::Cid;
44

55
mod car;
6+
mod diff;
67
mod memory;
78

89
pub use car::{CarStore, Error as CarError};
10+
pub use diff::DiffBlockStore;
911
pub use memory::MemoryBlockStore;
1012

1113
/// DAG-PB multicodec code

Diff for: atrium-repo/src/blockstore/diff.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::collections::HashSet;
2+
3+
use ipld_core::cid::Cid;
4+
5+
use super::{AsyncBlockStoreRead, AsyncBlockStoreWrite, Error};
6+
7+
/// An extremely simple differencing blockstore layer. This tracks all CIDs that are created.
8+
pub struct DiffBlockStore<S> {
9+
inner: S,
10+
blocks: HashSet<Cid>,
11+
}
12+
13+
impl<S> DiffBlockStore<S> {
14+
pub fn wrap(inner: S) -> Self {
15+
Self { inner, blocks: HashSet::new() }
16+
}
17+
18+
pub fn into_inner(self) -> S {
19+
self.inner
20+
}
21+
22+
/// Return the CIDs of the blocks that have been written so far.
23+
pub fn blocks<'a>(&'a self) -> impl Iterator<Item = Cid> + 'a {
24+
self.blocks.iter().cloned()
25+
}
26+
}
27+
28+
impl<S: AsyncBlockStoreRead> AsyncBlockStoreRead for DiffBlockStore<S> {
29+
async fn read_block_into(&mut self, cid: &Cid, contents: &mut Vec<u8>) -> Result<(), Error> {
30+
self.inner.read_block_into(cid, contents).await
31+
}
32+
}
33+
34+
impl<S: AsyncBlockStoreWrite> AsyncBlockStoreWrite for DiffBlockStore<S> {
35+
async fn write_block(&mut self, codec: u64, hash: u64, contents: &[u8]) -> Result<Cid, Error> {
36+
let cid = self.inner.write_block(codec, hash, contents).await?;
37+
self.blocks.insert(cid);
38+
Ok(cid)
39+
}
40+
}

0 commit comments

Comments
 (0)