forked from mit-pdos/noria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_state.rs
245 lines (230 loc) · 9 KB
/
single_state.rs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use super::mk_key::MakeKey;
use common::SizeOf;
use prelude::*;
use rand::prelude::*;
use state::keyed_state::KeyedState;
use std::rc::Rc;
pub(super) struct SingleState {
key: Vec<usize>,
state: KeyedState,
partial: bool,
rows: usize,
}
macro_rules! insert_row_match_impl {
($self:ident, $r:ident, $map:ident) => {{
let key = MakeKey::from_row(&$self.key, &*$r);
match $map.entry(key) {
Entry::Occupied(mut rs) => rs.get_mut().push($r),
Entry::Vacant(..) if $self.partial => return false,
rs @ Entry::Vacant(..) => rs.or_default().push($r),
}
}};
}
macro_rules! remove_row_match_impl {
($self:ident, $r:ident, $do_remove:ident, $map:ident) => {{
// TODO: can we avoid the Clone here?
let key = MakeKey::from_row(&$self.key, $r);
if let Some(ref mut rs) = $map.get_mut(&key) {
return $do_remove(&mut $self.rows, rs);
}
}};
}
impl SingleState {
pub(super) fn new(columns: &[usize], partial: bool) -> Self {
Self {
key: Vec::from(columns),
state: columns.into(),
partial,
rows: 0,
}
}
/// Inserts the given record, or returns false if a hole was encountered (and the record hence
/// not inserted).
pub(super) fn insert_row(&mut self, r: Row) -> bool {
use rahashmap::Entry;
match self.state {
KeyedState::Single(ref mut map) => {
// treat this specially to avoid the extra Vec
debug_assert_eq!(self.key.len(), 1);
// i *wish* we could use the entry API here, but it would mean an extra clone
// in the common case of an entry already existing for the given key...
if let Some(ref mut rs) = map.get_mut(&r[self.key[0]]) {
self.rows += 1;
rs.push(r);
return true;
} else if self.partial {
// trying to insert a record into partial materialization hole!
return false;
}
map.insert(r[self.key[0]].clone(), vec![r]);
}
KeyedState::Double(ref mut map) => insert_row_match_impl!(self, r, map),
KeyedState::Tri(ref mut map) => insert_row_match_impl!(self, r, map),
KeyedState::Quad(ref mut map) => insert_row_match_impl!(self, r, map),
KeyedState::Quin(ref mut map) => insert_row_match_impl!(self, r, map),
KeyedState::Sex(ref mut map) => insert_row_match_impl!(self, r, map),
}
self.rows += 1;
true
}
/// Attempt to remove row `r`.
pub(super) fn remove_row(&mut self, r: &[DataType], hit: &mut bool) -> Option<Row> {
let mut do_remove = |self_rows: &mut usize, rs: &mut Vec<Row>| -> Option<Row> {
*hit = true;
let rm = if rs.len() == 1 {
// it *should* be impossible to get a negative for a record that we don't have
debug_assert_eq!(r, &rs[0][..]);
Some(rs.swap_remove(0))
} else if let Some(i) = rs.iter().position(|rsr| &rsr[..] == r) {
Some(rs.swap_remove(i))
} else {
None
};
if rm.is_some() {
*self_rows = self_rows.checked_sub(1).unwrap();
}
rm
};
match self.state {
KeyedState::Single(ref mut map) => {
if let Some(ref mut rs) = map.get_mut(&r[self.key[0]]) {
return do_remove(&mut self.rows, rs);
}
}
KeyedState::Double(ref mut map) => remove_row_match_impl!(self, r, do_remove, map),
KeyedState::Tri(ref mut map) => remove_row_match_impl!(self, r, do_remove, map),
KeyedState::Quad(ref mut map) => remove_row_match_impl!(self, r, do_remove, map),
KeyedState::Quin(ref mut map) => remove_row_match_impl!(self, r, do_remove, map),
KeyedState::Sex(ref mut map) => remove_row_match_impl!(self, r, do_remove, map),
}
None
}
pub(super) fn mark_filled(&mut self, key: Vec<DataType>) {
let mut key = key.into_iter();
let replaced = match self.state {
KeyedState::Single(ref mut map) => map.insert(key.next().unwrap(), Vec::new()),
KeyedState::Double(ref mut map) => {
map.insert((key.next().unwrap(), key.next().unwrap()), Vec::new())
}
KeyedState::Tri(ref mut map) => map.insert(
(
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
),
Vec::new(),
),
KeyedState::Quad(ref mut map) => map.insert(
(
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
),
Vec::new(),
),
KeyedState::Quin(ref mut map) => map.insert(
(
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
),
Vec::new(),
),
KeyedState::Sex(ref mut map) => map.insert(
(
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
key.next().unwrap(),
),
Vec::new(),
),
};
assert!(replaced.is_none());
}
pub(super) fn mark_hole(&mut self, key: &[DataType]) -> u64 {
let removed = match self.state {
KeyedState::Single(ref mut map) => map.remove(&key[0]),
KeyedState::Double(ref mut map) => map.remove(&MakeKey::from_key(key)),
KeyedState::Tri(ref mut map) => map.remove(&MakeKey::from_key(key)),
KeyedState::Quad(ref mut map) => map.remove(&MakeKey::from_key(key)),
KeyedState::Quin(ref mut map) => map.remove(&MakeKey::from_key(key)),
KeyedState::Sex(ref mut map) => map.remove(&MakeKey::from_key(key)),
};
// mark_hole should only be called on keys we called mark_filled on
removed
.unwrap()
.iter()
.filter(|r| Rc::strong_count(&r.0) == 1)
.map(SizeOf::deep_size_of)
.sum()
}
pub(super) fn clear(&mut self) {
self.rows = 0;
match self.state {
KeyedState::Single(ref mut map) => map.clear(),
KeyedState::Double(ref mut map) => map.clear(),
KeyedState::Tri(ref mut map) => map.clear(),
KeyedState::Quad(ref mut map) => map.clear(),
KeyedState::Quin(ref mut map) => map.clear(),
KeyedState::Sex(ref mut map) => map.clear(),
};
}
/// Evict `count` randomly selected keys from state and return them along with the number of
/// bytes freed.
pub(super) fn evict_random_keys(
&mut self,
count: usize,
rng: &mut ThreadRng,
) -> (u64, Vec<Vec<DataType>>) {
let mut bytes_freed = 0;
let mut keys = Vec::with_capacity(count);
for _ in 0..count {
if let Some((n, key)) = self.state.evict_at_index(rng.gen()) {
bytes_freed += n;
keys.push(key);
} else {
break;
}
}
(bytes_freed, keys)
}
/// Evicts a specified key from this state, returning the number of bytes freed.
pub(super) fn evict_keys(&mut self, keys: &[Vec<DataType>]) -> u64 {
keys.iter().map(|k| self.state.evict(k)).sum()
}
pub(super) fn values<'a>(&'a self) -> Box<dyn Iterator<Item = &'a Vec<Row>> + 'a> {
match self.state {
KeyedState::Single(ref map) => Box::new(map.values()),
KeyedState::Double(ref map) => Box::new(map.values()),
KeyedState::Tri(ref map) => Box::new(map.values()),
KeyedState::Quad(ref map) => Box::new(map.values()),
KeyedState::Quin(ref map) => Box::new(map.values()),
KeyedState::Sex(ref map) => Box::new(map.values()),
}
}
pub(super) fn key(&self) -> &[usize] {
&self.key
}
pub(super) fn partial(&self) -> bool {
self.partial
}
pub(super) fn rows(&self) -> usize {
self.rows
}
pub(super) fn lookup<'a>(&'a self, key: &KeyType) -> LookupResult<'a> {
if let Some(rs) = self.state.lookup(key) {
LookupResult::Some(RecordResult::Borrowed(&rs[..]))
} else if self.partial() {
// partially materialized, so this is a hole (empty results would be vec![])
LookupResult::Missing
} else {
LookupResult::Some(RecordResult::Owned(vec![]))
}
}
}