-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathmemory_state.rs
287 lines (248 loc) · 9.66 KB
/
memory_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::collections::HashMap;
use std::rc::Rc;
use rand::{self, Rng};
use crate::prelude::*;
use crate::state::single_state::SingleState;
use common::SizeOf;
#[derive(Default)]
pub struct MemoryState {
state: Vec<SingleState>,
by_tag: HashMap<Tag, usize>,
mem_size: u64,
}
impl SizeOf for MemoryState {
fn size_of(&self) -> u64 {
use std::mem::size_of;
size_of::<Self>() as u64
}
fn deep_size_of(&self) -> u64 {
self.mem_size
}
fn is_empty(&self) -> bool {
self.state[0].is_empty()
}
}
impl State for MemoryState {
fn add_key(&mut self, columns: &[usize], partial: Option<Vec<Tag>>) {
let (i, exists) = if let Some(i) = self.state_for(columns) {
// already keyed by this key; just adding tags
(i, true)
} else {
// will eventually be assigned
(self.state.len(), false)
};
if let Some(ref p) = partial {
for &tag in p {
self.by_tag.insert(tag, i);
}
}
if exists {
return;
}
self.state
.push(SingleState::new(columns, partial.is_some()));
if !self.state.is_empty() && partial.is_none() {
// we need to *construct* the index!
let (new, old) = self.state.split_last_mut().unwrap();
if !old.is_empty() {
assert!(!old[0].partial());
for rs in old[0].values() {
for r in rs {
new.insert_row(Row::from(r.0.clone()));
}
}
}
}
}
fn is_useful(&self) -> bool {
!self.state.is_empty()
}
fn is_partial(&self) -> bool {
self.state.iter().any(SingleState::partial)
}
fn process_records(&mut self, records: &mut Records, partial_tag: Option<Tag>) {
if self.is_partial() {
records.retain(|r| {
// we need to check that we're not erroneously filling any holes
// there are two cases here:
//
// - if the incoming record is a partial replay (i.e., partial.is_some()), then we
// *know* that we are the target of the replay, and therefore we *know* that the
// materialization must already have marked the given key as "not a hole".
// - if the incoming record is a normal message (i.e., partial.is_none()), then we
// need to be careful. since this materialization is partial, it may be that we
// haven't yet replayed this `r`'s key, in which case we shouldn't forward that
// record! if all of our indices have holes for this record, there's no need for us
// to forward it. it would just be wasted work.
//
// XXX: we could potentially save come computation here in joins by not forcing
// `right` to backfill the lookup key only to then throw the record away
match *r {
Record::Positive(ref r) => self.insert(r.clone(), partial_tag),
Record::Negative(ref r) => self.remove(r),
}
});
} else {
for r in records.iter() {
match *r {
Record::Positive(ref r) => {
let hit = self.insert(r.clone(), None);
debug_assert!(hit);
}
Record::Negative(ref r) => {
let hit = self.remove(r);
debug_assert!(hit);
}
}
}
}
}
fn rows(&self) -> usize {
self.state.iter().map(SingleState::rows).sum()
}
fn mark_filled(&mut self, key: Vec<DataType>, tag: Tag) {
debug_assert!(!self.state.is_empty(), "filling uninitialized index");
let index = self.by_tag[&tag];
self.state[index].mark_filled(key);
}
fn mark_hole(&mut self, key: &[DataType], tag: Tag) {
debug_assert!(!self.state.is_empty(), "filling uninitialized index");
let index = self.by_tag[&tag];
let freed_bytes = self.state[index].mark_hole(key);
self.mem_size = self.mem_size.checked_sub(freed_bytes).unwrap();
}
fn lookup<'a>(&'a self, columns: &[usize], key: &KeyType) -> LookupResult<'a> {
debug_assert!(!self.state.is_empty(), "lookup on uninitialized index");
let index = self
.state_for(columns)
.expect("lookup on non-indexed column set");
self.state[index].lookup(key)
}
fn keys(&self) -> Vec<Vec<usize>> {
self.state.iter().map(|s| s.key().to_vec()).collect()
}
fn cloned_records(&self) -> Vec<Vec<DataType>> {
#[allow(clippy::ptr_arg)]
fn fix<'a>(rs: &'a Rows) -> impl Iterator<Item = Vec<DataType>> + 'a {
rs.iter().map(|r| Vec::clone(&**r))
}
assert!(!self.state[0].partial());
self.state[0].values().flat_map(fix).collect()
}
fn evict_random_keys(&mut self, count: usize) -> (&[usize], Vec<Vec<DataType>>, u64) {
let mut rng = rand::thread_rng();
let index = rng.gen_range(0, self.state.len());
let (bytes_freed, keys) = self.state[index].evict_random_keys(count, &mut rng);
self.mem_size = self.mem_size.saturating_sub(bytes_freed);
(self.state[index].key(), keys, bytes_freed)
}
fn evict_keys(&mut self, tag: Tag, keys: &[Vec<DataType>]) -> Option<(&[usize], u64)> {
// we may be told to evict from a tag that add_key hasn't been called for yet
// this can happen if an upstream domain issues an eviction for a replay path that we have
// been told about, but that has not yet been finalized.
self.by_tag.get(&tag).cloned().map(move |index| {
let bytes = self.state[index].evict_keys(keys);
self.mem_size = self.mem_size.saturating_sub(bytes);
(self.state[index].key(), bytes)
})
}
fn clear(&mut self) {
for state in &mut self.state {
state.clear();
}
self.mem_size = 0;
}
}
impl MemoryState {
/// Returns the index in `self.state` of the index keyed on `cols`, or None if no such index
/// exists.
fn state_for(&self, cols: &[usize]) -> Option<usize> {
self.state.iter().position(|s| s.key() == cols)
}
fn insert(&mut self, r: Vec<DataType>, partial_tag: Option<Tag>) -> bool {
let r = Rc::new(r);
if let Some(tag) = partial_tag {
let i = match self.by_tag.get(&tag) {
Some(i) => *i,
None => {
// got tagged insert for unknown tag. this will happen if a node on an old
// replay path is now materialized. must return true to avoid any records
// (which are destined for a downstream materialization) from being pruned.
return true;
}
};
self.mem_size += r.deep_size_of();
self.state[i].insert_row(Row::from(r))
} else {
let mut hit_any = false;
for i in 0..self.state.len() {
hit_any |= self.state[i].insert_row(Row::from(r.clone()));
}
if hit_any {
self.mem_size += r.deep_size_of();
}
hit_any
}
}
fn remove(&mut self, r: &[DataType]) -> bool {
let mut hit = false;
for s in &mut self.state {
if let Some(row) = s.remove_row(r, &mut hit) {
if Rc::strong_count(&row.0) == 1 {
self.mem_size = self.mem_size.checked_sub(row.deep_size_of()).unwrap();
}
}
}
hit
}
}
#[cfg(test)]
mod tests {
use super::*;
fn insert<S: State>(state: &mut S, row: Vec<DataType>) {
let record: Record = row.into();
state.process_records(&mut record.into(), None);
}
#[test]
fn memory_state_process_records() {
let mut state = MemoryState::default();
let records: Records = vec![
(vec![1.into(), "A".into()], true),
(vec![2.into(), "B".into()], true),
(vec![3.into(), "C".into()], true),
(vec![1.into(), "A".into()], false),
]
.into();
state.add_key(&[0], None);
state.process_records(&mut Vec::from(&records[..3]).into(), None);
state.process_records(&mut records[3].clone().into(), None);
// Make sure the first record has been deleted:
match state.lookup(&[0], &KeyType::Single(&records[0][0])) {
LookupResult::Some(RecordResult::Borrowed(rows)) => assert_eq!(rows.len(), 0),
_ => unreachable!(),
};
// Then check that the rest exist:
for record in &records[1..3] {
match state.lookup(&[0], &KeyType::Single(&record[0])) {
LookupResult::Some(RecordResult::Borrowed(rows)) => {
assert_eq!(&**rows.iter().next().unwrap(), &**record)
}
_ => unreachable!(),
};
}
}
#[test]
fn memory_state_old_records_new_index() {
let mut state = MemoryState::default();
let row: Vec<DataType> = vec![10.into(), "Cat".into()];
state.add_key(&[0], None);
insert(&mut state, row.clone());
state.add_key(&[1], None);
match state.lookup(&[1], &KeyType::Single(&row[1])) {
LookupResult::Some(RecordResult::Borrowed(rows)) => {
assert_eq!(&**rows.iter().next().unwrap(), &row)
}
_ => unreachable!(),
};
}
}