this repo has no description
1use crate::repo::PostgresBlockStore;
2use bytes::Bytes;
3use cid::Cid;
4use jacquard_repo::error::RepoError;
5use jacquard_repo::repo::CommitData;
6use jacquard_repo::storage::BlockStore;
7use std::sync::{Arc, Mutex};
8
9#[derive(Clone)]
10pub struct TrackingBlockStore {
11 inner: PostgresBlockStore,
12 written_cids: Arc<Mutex<Vec<Cid>>>,
13}
14
15impl TrackingBlockStore {
16 pub fn new(store: PostgresBlockStore) -> Self {
17 Self {
18 inner: store,
19 written_cids: Arc::new(Mutex::new(Vec::new())),
20 }
21 }
22
23 pub fn get_written_cids(&self) -> Vec<Cid> {
24 self.written_cids.lock().unwrap().clone()
25 }
26}
27
28impl BlockStore for TrackingBlockStore {
29 async fn get(&self, cid: &Cid) -> Result<Option<Bytes>, RepoError> {
30 self.inner.get(cid).await
31 }
32
33 async fn put(&self, data: &[u8]) -> Result<Cid, RepoError> {
34 let cid = self.inner.put(data).await?;
35 self.written_cids.lock().unwrap().push(cid.clone());
36 Ok(cid)
37 }
38
39 async fn has(&self, cid: &Cid) -> Result<bool, RepoError> {
40 self.inner.has(cid).await
41 }
42
43 async fn put_many(
44 &self,
45 blocks: impl IntoIterator<Item = (Cid, Bytes)> + Send,
46 ) -> Result<(), RepoError> {
47 let blocks: Vec<_> = blocks.into_iter().collect();
48 let cids: Vec<Cid> = blocks.iter().map(|(cid, _)| cid.clone()).collect();
49 self.inner.put_many(blocks).await?;
50 self.written_cids.lock().unwrap().extend(cids);
51 Ok(())
52 }
53
54 async fn get_many(&self, cids: &[Cid]) -> Result<Vec<Option<Bytes>>, RepoError> {
55 self.inner.get_many(cids).await
56 }
57
58 async fn apply_commit(&self, commit: CommitData) -> Result<(), RepoError> {
59 self.put_many(commit.blocks).await?;
60 Ok(())
61 }
62}