mqdb_core/storage/
backend.rs1use crate::error::Result;
5use std::future::Future;
6
7pub trait StorageBackend: Send + Sync {
9 fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
14
15 fn insert(&self, key: &[u8], value: &[u8]) -> Result<()>;
20
21 fn remove(&self, key: &[u8]) -> Result<()>;
26
27 fn prefix_scan(&self, prefix: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
32
33 fn prefix_count(&self, prefix: &[u8]) -> Result<usize>;
38
39 fn prefix_scan_keys(&self, prefix: &[u8]) -> Result<Vec<Vec<u8>>>;
44
45 fn prefix_scan_batch(
53 &self,
54 prefix: &[u8],
55 batch_size: usize,
56 after_key: Option<&[u8]>,
57 ) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
58
59 fn range_scan(&self, start: &[u8], end: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
64
65 fn batch(&self) -> Box<dyn BatchOperations>;
67
68 fn flush(&self) -> Result<()>;
73}
74
75pub trait BatchOperations: Send {
77 fn insert(&mut self, key: Vec<u8>, value: Vec<u8>);
79
80 fn remove(&mut self, key: Vec<u8>);
82
83 fn expect_value(&mut self, key: Vec<u8>, expected_value: Vec<u8>);
85
86 fn commit(self: Box<Self>) -> Result<()>;
91}
92
93pub trait AsyncStorageBackend {
94 type Batch: AsyncBatchOperations;
95
96 fn get(&self, key: &[u8]) -> impl Future<Output = Result<Option<Vec<u8>>>>;
97 fn insert(&self, key: &[u8], value: &[u8]) -> impl Future<Output = Result<()>>;
98 fn remove(&self, key: &[u8]) -> impl Future<Output = Result<()>>;
99 fn prefix_scan(&self, prefix: &[u8]) -> impl Future<Output = Result<Vec<(Vec<u8>, Vec<u8>)>>>;
100 fn prefix_count(&self, prefix: &[u8]) -> impl Future<Output = Result<usize>>;
101 fn prefix_scan_keys(&self, prefix: &[u8]) -> impl Future<Output = Result<Vec<Vec<u8>>>>;
102 fn prefix_scan_batch(
103 &self,
104 prefix: &[u8],
105 batch_size: usize,
106 after_key: Option<&[u8]>,
107 ) -> impl Future<Output = Result<Vec<(Vec<u8>, Vec<u8>)>>>;
108 fn range_scan(
109 &self,
110 start: &[u8],
111 end: &[u8],
112 ) -> impl Future<Output = Result<Vec<(Vec<u8>, Vec<u8>)>>>;
113 fn batch(&self) -> Self::Batch;
114 fn flush(&self) -> impl Future<Output = Result<()>>;
115}
116
117pub trait AsyncBatchOperations {
118 fn insert(&mut self, key: Vec<u8>, value: Vec<u8>);
119 fn remove(&mut self, key: Vec<u8>);
120 fn expect_value(&mut self, key: Vec<u8>, expected_value: Vec<u8>);
121 fn commit(self) -> impl Future<Output = Result<()>>;
122}