aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/src/memtable_secondary.rs
blob: a7a9a8a1785ddbc83ba36d6080dca2f62dcf5988 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use once_cell::sync::Lazy;

use super::*;
use std::collections::{BTreeMap, HashSet};

pub struct SecondaryMemtable {
    /// Map of records indexed by key. The value is the set of primary key values of records
    /// that have the secondary key value. The actual `Record` objects are stored in the
    /// primary memtable, which acts as the shared heap.
    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) {
        match self.records.get_mut(key) {
            Some(set) => {
                set.insert(value.clone());
            }
            None => {
                self.records
                    .insert(key.clone(), LogKeySet::new_with_initial(&value));
            }
        };
    }

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

    // Remove all log keys associated with the given key
    pub fn remove_all(&mut self, key: &IndexableValue) -> Option<LogKeySet> {
        self.records.remove(key)
    }

    // Remove a single log key associated with the given key. Returns `true`
    // if the log key existed and was removed, `false` otherwise.
    pub fn remove(&mut self, key: &IndexableValue, log_key: &LogKey) -> bool {
        let set = match self.records.get_mut(key) {
            Some(set) => set,
            None => return false,
        };
        if set.len() == 1 && set.contains(log_key) {
            self.records.remove(key);
            true
        } else {
            return match set.remove(log_key) {
                Ok(_) => true,
                Err(LogKeySetError::NotFoundError) => false,
                Err(e) => panic!("{:?}", e),
            };
        }
    }

    // Remove all log keys associated with the given log key
    // Note: This is a linear time operation, prefer using the `remove` method
    // if you know the secondary key associated with the log key.
    pub fn scan_remove(&mut self, log_key: &LogKey) -> u64 {
        let mut removed = 0;
        self.records.iter_mut().for_each(|(_, set)| {
            if let Ok(_) = set.remove(&log_key) {
                removed += 1;
            }
        });

        removed
    }
}