storey_storage/
storage.rs

1use std::ops::Bound;
2
3/// A read interface for binary key-value storage.
4pub trait Storage {
5    /// Get the value of the key.
6    fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
7
8    /// Check if the key exists.
9    fn has(&self, key: &[u8]) -> bool {
10        self.get(key).is_some()
11    }
12
13    /// Get the value of the key in the metadata namespace.
14    fn get_meta(&self, _key: &[u8]) -> Option<Vec<u8>>;
15
16    /// Check if the key exists in the metadata namespace.
17    fn has_meta(&self, key: &[u8]) -> bool {
18        self.get_meta(key).is_some()
19    }
20}
21
22pub trait IntoStorage<O>: Sized {
23    fn into_storage(self) -> O;
24}
25
26impl<'a, T: Storage> IntoStorage<&'a T> for (&'a T,) {
27    fn into_storage(self) -> &'a T {
28        self.0
29    }
30}
31
32impl<'a, T: Storage> IntoStorage<&'a mut T> for (&'a mut T,) {
33    fn into_storage(self) -> &'a mut T {
34        self.0
35    }
36}
37
38/// A write interface for binary key-value storage.
39pub trait StorageMut {
40    /// Set the value of the key.
41    fn set(&mut self, key: &[u8], value: &[u8]);
42
43    /// Remove the key.
44    fn remove(&mut self, key: &[u8]);
45
46    /// Set the value of the key in the metadata namespace.
47    fn set_meta(&mut self, _key: &[u8], _value: &[u8]);
48
49    /// Remove the key in the metadata namespace.
50    fn remove_meta(&mut self, _key: &[u8]);
51}
52
53/// Iteration interface for binary key-value storage.
54///
55/// The iterator should iterate over key-value pairs in lexicographical order of keys.
56pub trait IterableStorage {
57    /// The type of the iterator returned by [`keys`](Self::keys).
58    type KeysIterator<'a>: Iterator<Item = Vec<u8>>
59    where
60        Self: 'a;
61
62    /// The type of the iterator returned by [`values`](Self::values).
63    type ValuesIterator<'a>: Iterator<Item = Vec<u8>>
64    where
65        Self: 'a;
66
67    /// The type of the iterator returned by [`pairs`](Self::pairs).
68    type PairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)>
69    where
70        Self: 'a;
71
72    /// Get an iterator over keys.
73    ///
74    /// The iterator walks keys in lexicographical order.
75    ///
76    /// The [`Bound`] type is used to specify either end of the range - whether it should be
77    /// bounded at all, and if so, whether it should be inclusive or exclusive. See the
78    /// [`Bound`] documentation for more details.
79    fn keys<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::KeysIterator<'a>;
80
81    /// Get an iterator over values.
82    ///
83    /// The iterator walks values corresponding to keys in lexicographical order.
84    ///
85    /// The [`Bound`] type is used to specify either end of the range - whether it should be
86    /// bounded at all, and if so, whether it should be inclusive or exclusive. See the
87    /// [`Bound`] documentation for more details.
88    fn values<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::ValuesIterator<'a>;
89
90    /// Get an iterator over key-value pairs.
91    ///
92    /// The iterator walks key-value pairs in lexicographical order.
93    ///
94    /// The [`Bound`] type is used to specify either end of the range - whether it should be
95    /// bounded at all, and if so, whether it should be inclusive or exclusive. See the
96    /// [`Bound`] documentation for more details.
97    fn pairs<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::PairsIterator<'a>;
98}
99
100impl<T: IterableStorage> IterableStorage for &T {
101    type KeysIterator<'a> = T::KeysIterator<'a> where Self: 'a;
102    type ValuesIterator<'a> = T::ValuesIterator<'a> where Self: 'a;
103    type PairsIterator<'a> = T::PairsIterator<'a> where Self: 'a;
104
105    fn keys<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::KeysIterator<'a> {
106        (**self).keys(start, end)
107    }
108
109    fn values<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::ValuesIterator<'a> {
110        (**self).values(start, end)
111    }
112
113    fn pairs<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::PairsIterator<'a> {
114        (**self).pairs(start, end)
115    }
116}
117
118impl<T: IterableStorage> IterableStorage for &mut T {
119    type KeysIterator<'a> = T::KeysIterator<'a> where Self: 'a;
120    type ValuesIterator<'a> = T::ValuesIterator<'a> where Self: 'a;
121    type PairsIterator<'a> = T::PairsIterator<'a> where Self: 'a;
122
123    fn keys<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::KeysIterator<'a> {
124        (**self).keys(start, end)
125    }
126
127    fn values<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::ValuesIterator<'a> {
128        (**self).values(start, end)
129    }
130
131    fn pairs<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::PairsIterator<'a> {
132        (**self).pairs(start, end)
133    }
134}
135
136/// Iteration interface for binary key-value storage in reverse order.
137///
138/// The iterator walks key-value pairs in reverse lexicographical order of keys.
139pub trait RevIterableStorage {
140    /// The type of the iterator returned by [`rev_keys`](Self::rev_keys).
141    type RevKeysIterator<'a>: Iterator<Item = Vec<u8>>
142    where
143        Self: 'a;
144
145    /// The type of the iterator returned by [`rev_values`](Self::rev_values).
146    type RevValuesIterator<'a>: Iterator<Item = Vec<u8>>
147    where
148        Self: 'a;
149
150    /// The type of the iterator returned by [`rev_pairs`](Self::rev_pairs).
151    type RevPairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)>
152    where
153        Self: 'a;
154
155    /// Get a reverse iterator over keys.
156    ///
157    /// The iterator walks keys in reverse lexicographical order.
158    ///
159    /// The [`Bound`] type is used to specify either end of the range - whether it should be
160    /// bounded at all, and if so, whether it should be inclusive or exclusive. See the
161    /// [`Bound`] documentation for more details.
162    fn rev_keys<'a>(&'a self, start: Bound<&[u8]>, end: Bound<&[u8]>) -> Self::RevKeysIterator<'a>;
163
164    /// Get a reverse iterator over values.
165    ///
166    /// The iterator walks values corresponding to keys in reverse lexicographical order.
167    ///
168    /// The [`Bound`] type is used to specify either end of the range - whether it should be
169    /// bounded at all, and if so, whether it should be inclusive or exclusive. See the
170    /// [`Bound`] documentation for more details.
171    fn rev_values<'a>(
172        &'a self,
173        start: Bound<&[u8]>,
174        end: Bound<&[u8]>,
175    ) -> Self::RevValuesIterator<'a>;
176
177    /// Get a reverse iterator over key-value pairs.
178    ///
179    /// The iterator walks key-value pairs in reverse lexicographical order.
180    ///
181    /// The [`Bound`] type is used to specify either end of the range - whether it should be
182    /// bounded at all, and if so, whether it should be inclusive or exclusive. See the
183    /// [`Bound`] documentation for more details.
184    fn rev_pairs<'a>(
185        &'a self,
186        start: Bound<&[u8]>,
187        end: Bound<&[u8]>,
188    ) -> Self::RevPairsIterator<'a>;
189}