lsm_tree/
iter_guard.rs

1use crate::{
2    blob_tree::Guard as BlobGuard, tree::Guard as StandardGuard, KvPair, UserKey, UserValue,
3};
4use enum_dispatch::enum_dispatch;
5
6/// Guard to access key-value pairs
7#[enum_dispatch]
8pub trait IterGuard {
9    /// Accesses the key-value pair if the predicate returns `true`.
10    ///
11    /// The predicate receives the key - if returning false, the value
12    /// may not be loaded if the tree is key-value separated.
13    ///
14    /// # Errors
15    ///
16    /// Will return `Err` if an IO error occurs.
17    fn into_inner_if(self, pred: impl Fn(&UserKey) -> bool) -> crate::Result<Option<KvPair>>;
18
19    /// Accesses the key-value pair.
20    ///
21    /// # Errors
22    ///
23    /// Will return `Err` if an IO error occurs.
24    fn into_inner(self) -> crate::Result<KvPair>;
25
26    /// Accesses the key.
27    ///
28    /// # Errors
29    ///
30    /// Will return `Err` if an IO error occurs.
31    fn key(self) -> crate::Result<UserKey>;
32
33    /// Returns the value size.
34    ///
35    /// # Errors
36    ///
37    /// Will return `Err` if an IO error occurs.
38    fn size(self) -> crate::Result<u32>;
39
40    /// Accesses the value.
41    ///
42    /// # Errors
43    ///
44    /// Will return `Err` if an IO error occurs.
45    fn value(self) -> crate::Result<UserValue>
46    where
47        Self: Sized,
48    {
49        self.into_inner().map(|(_, v)| v)
50    }
51}
52
53#[enum_dispatch(IterGuard)]
54pub enum IterGuardImpl {
55    Standard(StandardGuard),
56    Blob(BlobGuard),
57}