1use async_trait::async_trait;
4use ipfrs_core::{Block, Cid, Result};
5use std::sync::Arc;
6
7#[async_trait]
9pub trait BlockStore: Send + Sync {
10 async fn put(&self, block: &Block) -> Result<()>;
12
13 async fn put_many(&self, blocks: &[Block]) -> Result<()> {
15 for block in blocks {
17 self.put(block).await?;
18 }
19 Ok(())
20 }
21
22 async fn get(&self, cid: &Cid) -> Result<Option<Block>>;
24
25 async fn get_many(&self, cids: &[Cid]) -> Result<Vec<Option<Block>>> {
27 let mut results = Vec::with_capacity(cids.len());
29 for cid in cids {
30 results.push(self.get(cid).await?);
31 }
32 Ok(results)
33 }
34
35 async fn has(&self, cid: &Cid) -> Result<bool>;
37
38 async fn has_many(&self, cids: &[Cid]) -> Result<Vec<bool>> {
40 let mut results = Vec::with_capacity(cids.len());
42 for cid in cids {
43 results.push(self.has(cid).await?);
44 }
45 Ok(results)
46 }
47
48 async fn delete(&self, cid: &Cid) -> Result<()>;
50
51 async fn delete_many(&self, cids: &[Cid]) -> Result<()> {
53 for cid in cids {
55 self.delete(cid).await?;
56 }
57 Ok(())
58 }
59
60 fn list_cids(&self) -> Result<Vec<Cid>>;
62
63 fn len(&self) -> usize;
65
66 fn is_empty(&self) -> bool {
68 self.len() == 0
69 }
70
71 async fn flush(&self) -> Result<()> {
73 Ok(())
75 }
76
77 async fn close(&self) -> Result<()> {
79 self.flush().await
81 }
82}
83
84#[async_trait]
87impl<S: BlockStore> BlockStore for Arc<S> {
88 async fn put(&self, block: &Block) -> Result<()> {
89 (**self).put(block).await
90 }
91
92 async fn put_many(&self, blocks: &[Block]) -> Result<()> {
93 (**self).put_many(blocks).await
94 }
95
96 async fn get(&self, cid: &Cid) -> Result<Option<Block>> {
97 (**self).get(cid).await
98 }
99
100 async fn get_many(&self, cids: &[Cid]) -> Result<Vec<Option<Block>>> {
101 (**self).get_many(cids).await
102 }
103
104 async fn has(&self, cid: &Cid) -> Result<bool> {
105 (**self).has(cid).await
106 }
107
108 async fn has_many(&self, cids: &[Cid]) -> Result<Vec<bool>> {
109 (**self).has_many(cids).await
110 }
111
112 async fn delete(&self, cid: &Cid) -> Result<()> {
113 (**self).delete(cid).await
114 }
115
116 async fn delete_many(&self, cids: &[Cid]) -> Result<()> {
117 (**self).delete_many(cids).await
118 }
119
120 fn list_cids(&self) -> Result<Vec<Cid>> {
121 (**self).list_cids()
122 }
123
124 fn len(&self) -> usize {
125 (**self).len()
126 }
127
128 fn is_empty(&self) -> bool {
129 (**self).is_empty()
130 }
131
132 async fn flush(&self) -> Result<()> {
133 (**self).flush().await
134 }
135
136 async fn close(&self) -> Result<()> {
137 (**self).close().await
138 }
139}