aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/src/memtable_secondary.rs
blob: 8300ada97e7c72295173338c65dd437bfce02232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use super::*;
use once_cell::sync::Lazy;
use std::collections::BTreeMap;
use std::collections::HashSet;

pub struct SecondaryMemtable {
    /// Map of records indexed by key. Values are non-empty sets of `LogKey` values.
    records: BTreeMap<IndexableValue, LogKeySet>,
}

static EMPTY_SET: Lazy<HashSet<LogKey>> = Lazy::new(|| HashSet::new());

impl SecondaryMemtable {
    pub fn new() -> SecondaryMemtable {
        SecondaryMemtable {
            records: BTreeMap::new(),
        }
    }

    pub fn set(&mut self, key: &IndexableValue, value: &LogKey) {
        debug!(
            "Inserting/updating record in secondary memtable with key {:?} = segment {} index {}",
            &key,
            &value.segment_num(),
            &value.index()
        );

        match self.records.get_mut(key) {
            Some(existing) => {
                debug!(
                    "Existing entry found with {} records in the set",
                    &existing.len()
                );
                existing.insert(value.clone());
            }
            None => {
                debug!("No existing entry found, creating one.");
                let set = LogKeySet::new_with_initial(&value);
                self.records.insert(key.clone(), set);
            }
        }
    }

    pub fn set_all(&mut self, key: &IndexableValue, values: &[LogKey]) {
        debug!(
            "Replacing set of records in secondary memtable with key {:?} ({} values)",
            &key,
            &values.len(),
        );

        let set = LogKeySet::from_slice(values);
        self.records.insert(key.clone(), set);
    }

    pub fn find_all(&mut self, key: &IndexableValue) -> &HashSet<LogKey> {
        match self.records.get(key) {
            Some(set) => set.log_keys(),
            None => &EMPTY_SET,
        }
    }
}