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
79
|
use super::*;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::fmt::Debug;
pub struct SecondaryMemtable<Field: Eq + Clone + Debug> {
pub field: Field,
/// 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, HashSet<IndexableValue>>,
}
impl<Field: Eq + Clone + Debug> SecondaryMemtable<Field> {
pub fn new(field: &Field) -> SecondaryMemtable<Field> {
SecondaryMemtable {
field: field.clone(),
records: BTreeMap::new(),
}
}
pub fn set(&mut self, key: &IndexableValue, value: &IndexableValue) {
debug!(
"Inserting/updating record in secondary memtable with key {:?} = {:?}",
&key, &value,
);
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 mut set = HashSet::with_capacity(1);
set.insert(value.clone());
self.records.insert(key.clone(), set);
}
}
}
pub fn set_all(&mut self, key: &IndexableValue, values: &[IndexableValue]) {
debug!(
"Replacing set of records in secondary memtable with key {:?} ({} values)",
&key,
&values.len(),
);
let mut set = HashSet::with_capacity(values.len());
values.iter().for_each(|value| {
set.insert(value.clone());
});
self.records.insert(key.clone(), set);
}
pub fn find_all(
&mut self,
primary_memtable: &PrimaryMemtable,
key: &IndexableValue,
) -> Vec<Record> {
match self.records.get(key) {
None => vec![],
Some(set) => set
.iter()
.map(|key| {
primary_memtable
.get_without_update(key)
.expect("Record not found")
.clone()
})
.collect(),
}
}
}
|