co_storage/storage/
store_params.rs1use crate::{BlockStat, BlockStorage, BlockStorageContentMapping, ExtendedBlock, ExtendedBlockStorage, StorageError};
5use async_trait::async_trait;
6use cid::Cid;
7use co_primitives::{Block, BlockStorageCloneSettings, CloneWithBlockStorageSettings, MappedCid};
8use std::collections::BTreeSet;
9
10#[derive(Debug, Clone)]
13pub struct StoreParamsBlockStorage<S>
14where
15 S: Clone,
16{
17 next: S,
18 checked: bool,
19 max_block_size: usize,
20}
21impl<S> StoreParamsBlockStorage<S>
22where
23 S: Clone,
24{
25 pub fn new(next: S, checked: bool, max_block_size: usize) -> Self {
26 Self { next, checked, max_block_size }
27 }
28}
29#[async_trait]
30impl<S> BlockStorage for StoreParamsBlockStorage<S>
31where
32 S: BlockStorage + Send + Sync + Clone,
33{
34 async fn get(&self, cid: &Cid) -> Result<Block, StorageError> {
35 let (cid, data) = self.next.get(cid).await?.with_block_max_size(self.max_block_size)?.into_inner();
36 match self.checked {
37 true => Ok(Block::new(cid, data)?),
38 false => Ok(Block::new_unchecked(cid, data)),
39 }
40 }
41
42 async fn set(&self, block: Block) -> Result<Cid, StorageError> {
43 self.next
44 .set(if self.checked { block.with_block_max_size(self.max_block_size)? } else { block })
45 .await
46 }
47
48 async fn remove(&self, cid: &Cid) -> Result<(), StorageError> {
49 self.next.remove(cid).await
50 }
51
52 async fn stat(&self, cid: &Cid) -> Result<BlockStat, StorageError> {
53 self.next.stat(cid).await
54 }
55
56 fn max_block_size(&self) -> usize {
57 self.max_block_size
58 }
59}
60#[async_trait]
61impl<S> ExtendedBlockStorage for StoreParamsBlockStorage<S>
62where
63 S: ExtendedBlockStorage + Send + Sync + Clone,
64{
65 async fn set_extended(&self, block: ExtendedBlock) -> Result<Cid, StorageError> {
66 let next_block = ExtendedBlock {
67 block: if self.checked { block.block.with_block_max_size(self.max_block_size)? } else { block.block },
68 options: block.options,
69 };
70 self.next.set_extended(next_block).await
71 }
72
73 async fn exists(&self, cid: &Cid) -> Result<bool, StorageError> {
74 self.next.exists(cid).await
75 }
76
77 async fn clear(&self) -> Result<(), StorageError> {
78 self.next.clear().await
79 }
80}
81impl<S> CloneWithBlockStorageSettings for StoreParamsBlockStorage<S>
82where
83 S: BlockStorage + CloneWithBlockStorageSettings,
84{
85 fn clone_with_settings(&self, settings: BlockStorageCloneSettings) -> Self {
86 Self {
87 next: self.next.clone_with_settings(settings),
88 checked: self.checked,
89 max_block_size: self.max_block_size,
90 }
91 }
92}
93#[async_trait]
94impl<S> BlockStorageContentMapping for StoreParamsBlockStorage<S>
95where
96 S: BlockStorage + CloneWithBlockStorageSettings + BlockStorageContentMapping + 'static,
97{
98 async fn is_content_mapped(&self) -> bool {
99 self.next.is_content_mapped().await
100 }
101
102 async fn to_plain(&self, mapped: &Cid) -> Option<Cid> {
103 self.next.to_plain(mapped).await
104 }
105
106 async fn to_mapped(&self, plain: &Cid) -> Option<Cid> {
107 self.next.to_mapped(plain).await
108 }
109
110 async fn insert_mappings(&self, mappings: BTreeSet<MappedCid>) {
111 self.next.insert_mappings(mappings).await
112 }
113}