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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
use super::common::*;
use priority_queue::PriorityQueue;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
struct UniqueRecord {
/// The value of the record's primary key field
primary_value: IndexableValue,
/// The record itself.
record: Record,
}
impl PartialEq for UniqueRecord {
fn eq(&self, other: &UniqueRecord) -> bool {
self.primary_value == other.primary_value
}
}
impl Eq for UniqueRecord {}
impl Hash for UniqueRecord {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.primary_value.hash(state)
}
}
pub struct SecondaryMemtable<Field: Eq + Clone + Debug> {
pub field: Field,
pub primary_field_index: usize,
capacity: usize,
/// Running counter of memtable operations, used as priority
/// in evict_queue.
n_operations: u64,
records: BTreeMap<IndexableValue, HashSet<UniqueRecord>>,
/// A max heap priority queue of keys. Note: n_operations must
/// be negated upon append to evict oldest values first.
evict_queue: PriorityQueue<IndexableValue, i64>,
evict_policy: MemtableEvictPolicy,
}
impl<Field: Eq + Clone + Debug> SecondaryMemtable<Field> {
pub fn new(
field: &Field,
primary_field_index: usize,
capacity: usize,
evict_policy: MemtableEvictPolicy,
) -> SecondaryMemtable<Field> {
SecondaryMemtable {
field: field.clone(),
primary_field_index,
capacity,
n_operations: 0,
records: BTreeMap::new(),
evict_queue: PriorityQueue::new(),
evict_policy,
}
}
pub fn set(&mut self, key: &IndexableValue, value: &Record) {
if self.capacity == 0 {
return;
}
debug!(
"Inserting/updating record in secondary memtable with key {:?} = {:?}",
&key, &value,
);
if self.records.len() >= self.capacity {
let (evict_key, _prio) = self.evict_queue.pop().expect("Evict queue was empty");
self.records.remove(&evict_key);
}
let unique_record = UniqueRecord {
primary_value: value.values[self.primary_field_index]
.as_indexable()
.expect("Value at primary field index was not indexable"),
record: value.clone(),
};
match self.records.get_mut(key) {
Some(existing) => {
debug!(
"Existing entry found with {} records in the set",
&existing.len()
);
existing.insert(unique_record);
}
None => {
debug!("No existing entry found, creating one.");
let mut set = HashSet::with_capacity(1);
set.insert(unique_record);
self.records.insert(key.clone(), set);
}
}
if self.evict_policy == MemtableEvictPolicy::LeastWritten
|| self.evict_policy == MemtableEvictPolicy::LeastReadOrWritten
{
self.set_priority(&key);
}
}
pub fn set_all(&mut self, key: &IndexableValue, values: &[Record]) {
if self.capacity == 0 {
return;
}
debug!(
"Replacing set of records in secondary memtable with key {:?} ({} values)",
&key,
&values.len(),
);
if self.records.len() >= self.capacity {
let (evict_key, _prio) = self.evict_queue.pop().expect("Evict queue was empty");
self.records.remove(&evict_key);
}
let mut set = HashSet::with_capacity(values.len());
values.iter().for_each(|value| {
let unique_record = UniqueRecord {
primary_value: value.values[self.primary_field_index]
.as_indexable()
.expect("Value at primary field index was not indexable"),
record: value.clone(),
};
set.insert(unique_record);
});
self.records.insert(key.clone(), set);
if self.evict_policy == MemtableEvictPolicy::LeastWritten
|| self.evict_policy == MemtableEvictPolicy::LeastReadOrWritten
{
self.set_priority(&key);
}
}
pub fn find_all(&mut self, key: &IndexableValue) -> Vec<&Record> {
if self.evict_policy == MemtableEvictPolicy::LeastRead
|| self.evict_policy == MemtableEvictPolicy::LeastReadOrWritten
{
self.set_priority(&key);
}
match self.records.get(key) {
None => vec![],
Some(set) => set
.iter()
.map(|unique_record| &unique_record.record)
.collect(),
}
}
fn set_priority(&mut self, key: &IndexableValue) {
let priority = self.get_and_increment_current_priority();
match self.evict_queue.get(key) {
Some(_) => {
self.evict_queue.change_priority(key, priority);
}
None => {
self.evict_queue.push(key.clone(), priority);
}
}
}
fn get_and_increment_current_priority(&mut self) -> i64 {
let ret = -(self.n_operations as i64);
self.n_operations += 1;
ret
}
}
|