Skip to main content

photondb_engine/tree/
map.rs

1use std::sync::Arc;
2
3use crossbeam_epoch::pin;
4
5use super::{page::*, stats::Stats, tree::*, Options, Result};
6use crate::util::Sequencer;
7
8/// A lock-free ordered map.
9#[derive(Clone)]
10pub struct Map {
11    raw: RawMap,
12    lsn: Arc<Sequencer>,
13}
14
15impl Map {
16    /// Opens a map with the given options.
17    pub fn open(opts: Options) -> Result<Self> {
18        let raw = RawMap::open(opts)?;
19        raw.set_min_lsn(u64::MAX);
20        let lsn = Arc::new(Sequencer::new(0));
21        Ok(Self { raw, lsn })
22    }
23
24    /// Gets the value corresponding to `key` and calls `func` with it.
25    pub fn get<F>(&self, key: &[u8], func: F) -> Result<()>
26    where
27        F: FnMut(Option<&[u8]>),
28    {
29        self.raw.get(key, u64::MAX, func)
30    }
31
32    pub fn iter(&self) -> Iter {
33        self.raw.iter()
34    }
35
36    /// Puts the key-value pair into this map.
37    pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
38        let lsn = self.lsn.inc();
39        self.raw.put(key, lsn, value)
40    }
41
42    /// Deletes the key-value pair from this map.
43    pub fn delete(&self, key: &[u8]) -> Result<()> {
44        let lsn = self.lsn.inc();
45        self.raw.delete(key, lsn)
46    }
47
48    /// Returns statistics of this map.
49    pub fn stats(&self) -> Stats {
50        self.raw.stats()
51    }
52}
53
54/// A lock-free ordered map with raw interfaces for advanced use cases.
55#[derive(Clone)]
56pub struct RawMap {
57    tree: Arc<Tree>,
58}
59
60impl RawMap {
61    /// Opens a map with the given options.
62    pub fn open(opts: Options) -> Result<Self> {
63        let tree = Tree::open(opts)?;
64        Ok(Self {
65            tree: Arc::new(tree),
66        })
67    }
68
69    /// Finds the value corresponding to `key` and calls `func` with it.
70    pub fn get<F>(&self, key: &[u8], lsn: u64, f: F) -> Result<()>
71    where
72        F: FnMut(Option<&[u8]>),
73    {
74        let guard = &pin();
75        let key = Key::new(key, lsn);
76        self.tree.get(key, guard).map(f)
77    }
78
79    pub fn iter(&self) -> Iter {
80        Iter::new(self.tree.clone())
81    }
82
83    /// Puts the key-value pair into this map.
84    pub fn put(&self, key: &[u8], lsn: u64, value: &[u8]) -> Result<()> {
85        let guard = &pin();
86        let key = Key::new(key, lsn);
87        let value = Value::Put(value);
88        self.tree.insert(key, value, guard)
89    }
90
91    /// Deletes the key-value pair into this map.
92    pub fn delete(&self, key: &[u8], lsn: u64) -> Result<()> {
93        let guard = &pin();
94        let key = Key::new(key, lsn);
95        let value = Value::Delete;
96        self.tree.insert(key, value, guard)
97    }
98
99    /// Returns statistics of this map.
100    pub fn stats(&self) -> Stats {
101        self.tree.stats()
102    }
103
104    /// Returns the minimal valid LSN for reads.
105    pub fn min_lsn(&self) -> u64 {
106        self.tree.min_lsn()
107    }
108
109    /// Updates the minimal valid LSN for reads.
110    ///
111    /// Entries with smaller LSNs will be dropped later.
112    pub fn set_min_lsn(&self, lsn: u64) {
113        self.tree.set_min_lsn(lsn);
114    }
115}