-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathmultiw.rs
174 lines (166 loc) · 6.36 KB
/
multiw.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
use super::{key_to_double, key_to_single, Key};
use evmap;
use fnv::FnvBuildHasher;
use prelude::*;
pub(super) enum Handle {
Single(evmap::WriteHandle<DataType, Vec<DataType>, i64, FnvBuildHasher>),
Double(evmap::WriteHandle<(DataType, DataType), Vec<DataType>, i64, FnvBuildHasher>),
Many(evmap::WriteHandle<Vec<DataType>, Vec<DataType>, i64, FnvBuildHasher>),
}
impl Handle {
pub fn is_empty(&self) -> bool {
match *self {
Handle::Single(ref h) => h.is_empty(),
Handle::Double(ref h) => h.is_empty(),
Handle::Many(ref h) => h.is_empty(),
}
}
pub fn clear(&mut self, k: Key) {
match *self {
Handle::Single(ref mut h) => {
h.clear(key_to_single(k).into_owned());
}
Handle::Double(ref mut h) => {
h.clear(key_to_double(k).into_owned());
}
Handle::Many(ref mut h) => {
h.clear(k.into_owned());
}
}
}
pub fn empty(&mut self, k: Key) {
match *self {
Handle::Single(ref mut h) => {
h.empty(key_to_single(k).into_owned());
}
Handle::Double(ref mut h) => {
h.empty(key_to_double(k).into_owned());
}
Handle::Many(ref mut h) => {
h.empty(k.into_owned());
}
}
}
/// Evict `count` randomly selected keys from state and return them along with the number of
/// bytes freed.
pub fn empty_at_index(&mut self, index: usize) -> Option<&[Vec<DataType>]> {
match *self {
Handle::Single(ref mut h) => h.empty_at_index(index).map(|r| r.1),
Handle::Double(ref mut h) => h.empty_at_index(index).map(|r| r.1),
Handle::Many(ref mut h) => h.empty_at_index(index).map(|r| r.1),
}
}
pub fn refresh(&mut self) {
match *self {
Handle::Single(ref mut h) => {
h.refresh();
}
Handle::Double(ref mut h) => {
h.refresh();
}
Handle::Many(ref mut h) => {
h.refresh();
}
}
}
pub fn meta_get_and<F, T>(&self, key: Key, then: F) -> Option<(Option<T>, i64)>
where
F: FnOnce(&[Vec<DataType>]) -> T,
{
match *self {
Handle::Single(ref h) => {
assert_eq!(key.len(), 1);
h.meta_get_and(&key[0], then)
}
Handle::Double(ref h) => {
assert_eq!(key.len(), 2);
// we want to transmute &[T; 2] to &(T, T), but that's not actually safe
// we're not guaranteed that they have the same memory layout
// we *could* just clone DataType, but that would mean dealing with string refcounts
// so instead, we play a trick where we memcopy onto the stack and then forget!
//
// h/t https://gist.github.com/mitsuhiko/f6478a0dd1ef174b33c63d905babc89a
use std::mem;
use std::ptr;
unsafe {
let mut stack_key: (DataType, DataType) = mem::uninitialized();
ptr::copy_nonoverlapping(
&key[0] as *const DataType,
&mut stack_key.0 as *mut DataType,
1,
);
ptr::copy_nonoverlapping(
&key[1] as *const DataType,
&mut stack_key.1 as *mut DataType,
1,
);
let v = h.meta_get_and(&stack_key, then);
mem::forget(stack_key);
v
}
}
Handle::Many(ref h) => h.meta_get_and(&key[..], then),
}
}
pub fn add<I>(&mut self, key: &[usize], cols: usize, rs: I) -> isize
where
I: IntoIterator<Item = Record>,
{
let mut memory_delta = 0isize;
match *self {
Handle::Single(ref mut h) => {
assert_eq!(key.len(), 1);
for r in rs {
debug_assert!(r.len() >= cols);
match r {
Record::Positive(r) => {
memory_delta += r.deep_size_of() as isize;
h.insert(r[key[0]].clone(), r);
}
Record::Negative(r) => {
// TODO: evmap will remove the empty vec for a key if we remove the
// last record. this means that future lookups will fail, and cause a
// replay, which will produce an empty result. this will work, but is
// somewhat inefficient.
memory_delta -= r.deep_size_of() as isize;
h.remove(r[key[0]].clone(), r);
}
}
}
}
Handle::Double(ref mut h) => {
assert_eq!(key.len(), 2);
for r in rs {
debug_assert!(r.len() >= cols);
match r {
Record::Positive(r) => {
memory_delta += r.deep_size_of() as isize;
h.insert((r[key[0]].clone(), r[key[1]].clone()), r);
}
Record::Negative(r) => {
memory_delta -= r.deep_size_of() as isize;
h.remove((r[key[0]].clone(), r[key[1]].clone()), r);
}
}
}
}
Handle::Many(ref mut h) => {
for r in rs {
debug_assert!(r.len() >= cols);
let key = key.iter().map(|&k| &r[k]).cloned().collect();
match r {
Record::Positive(r) => {
memory_delta += r.deep_size_of() as isize;
h.insert(key, r);
}
Record::Negative(r) => {
memory_delta -= r.deep_size_of() as isize;
h.remove(key, r);
}
}
}
}
}
memory_delta
}
}