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(
18        self,
19        pred: impl Fn(&UserKey) -> bool,
20    ) -> crate::Result<(UserKey, Option<UserValue>)>;
21
22    /// Accesses the key-value pair.
23    ///
24    /// # Errors
25    ///
26    /// Will return `Err` if an IO error occurs.
27    fn into_inner(self) -> crate::Result<KvPair>;
28
29    /// Accesses the key.
30    ///
31    /// # Errors
32    ///
33    /// Will return `Err` if an IO error occurs.
34    fn key(self) -> crate::Result<UserKey>;
35
36    /// Returns the value size.
37    ///
38    /// # Errors
39    ///
40    /// Will return `Err` if an IO error occurs.
41    fn size(self) -> crate::Result<u32>;
42
43    /// Accesses the value.
44    ///
45    /// # Errors
46    ///
47    /// Will return `Err` if an IO error occurs.
48    fn value(self) -> crate::Result<UserValue>
49    where
50        Self: Sized,
51    {
52        self.into_inner().map(|(_, v)| v)
53    }
54}
55
56/// Generic iterator value
57#[enum_dispatch(IterGuard)]
58pub enum IterGuardImpl {
59    /// Iterator value of a standard LSM-tree
60    Standard(StandardGuard),
61
62    /// Iterator value of a key-value separated tree
63    Blob(BlobGuard),
64}