forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masstree_scan.hh
406 lines (368 loc) · 12.7 KB
/
masstree_scan.hh
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2014 President and Fellows of Harvard College
* Copyright (c) 2012-2014 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef MASSTREE_SCAN_HH
#define MASSTREE_SCAN_HH
#include "masstree_tcursor.hh"
#include "masstree_struct.hh"
namespace Masstree {
template <typename P>
class scanstackelt {
public:
typedef leaf<P> leaf_type;
typedef typename leaf_type::leafvalue_type leafvalue_type;
typedef typename leaf_type::bound_type bound_type;
typedef typename P::ikey_type ikey_type;
typedef key<ikey_type> key_type;
typedef typename leaf_type::permuter_type permuter_type;
typedef typename P::threadinfo_type threadinfo;
typedef typename node_base<P>::nodeversion_type nodeversion_type;
leaf<P>* node() const {
return n_;
}
typename nodeversion_type::value_type full_version_value() const {
return (v_.version_value() << permuter_type::size_bits) + perm_.size();
}
int size() const {
return perm_.size();
}
permuter_type permutation() const {
return perm_;
}
int operator()(const key_type &k, const scanstackelt<P> &n, int p) {
return n.n_->compare_key(k, p);
}
private:
node_base<P>* root_;
leaf<P>* n_;
nodeversion_type v_;
permuter_type perm_;
int ki_;
small_vector<node_base<P>*, 2> node_stack_;
enum { scan_emit, scan_find_next, scan_down, scan_up, scan_retry };
scanstackelt() {
}
template <typename H>
int find_initial(H& helper, key_type& ka, bool emit_equal,
leafvalue_type& entry, threadinfo& ti);
template <typename H>
int find_retry(H& helper, key_type& ka, threadinfo& ti);
template <typename H>
int find_next(H& helper, key_type& ka, leafvalue_type& entry);
int kp() const {
if (unsigned(ki_) < unsigned(perm_.size()))
return perm_[ki_];
else
return -1;
}
template <typename PX> friend class basic_table;
};
struct forward_scan_helper {
bool initial_ksuf_match(int ksuf_compare, bool emit_equal) const {
return ksuf_compare > 0 || (ksuf_compare == 0 && emit_equal);
}
template <typename K> bool is_duplicate(const K &k,
typename K::ikey_type ikey,
int keylenx) const {
return k.compare(ikey, keylenx) >= 0;
}
template <typename K, typename N> int lower(const K &k, const N *n) const {
return N::bound_type::lower_by(k, *n, *n).i;
}
template <typename K, typename N>
key_indexed_position lower_with_position(const K &k, const N *n) const {
return N::bound_type::lower_by(k, *n, *n);
}
void mark_key_complete() const {
}
int next(int ki) const {
return ki + 1;
}
template <typename N, typename K>
N *advance(const N *n, const K &) const {
return n->safe_next();
}
template <typename N, typename K>
typename N::nodeversion_type stable(const N *n, const K &) const {
return n->stable();
}
template <typename K> void shift_clear(K &ka) const {
ka.shift_clear();
}
};
struct reverse_scan_helper {
// We run ki backwards, referring to perm.size() each time through,
// because inserting elements into a node need not bump its version.
// Therefore, if we decremented ki, starting from a node's original
// size(), we might miss some concurrently inserted keys!
// Also, a node's size might change DURING a lower_bound operation.
// The "backwards" ki must be calculated using the size taken by the
// lower_bound, NOT some later size() (which might be bigger or smaller).
reverse_scan_helper()
: upper_bound_(false) {
}
bool initial_ksuf_match(int ksuf_compare, bool emit_equal) const {
return ksuf_compare < 0 || (ksuf_compare == 0 && emit_equal);
}
template <typename K> bool is_duplicate(const K &k,
typename K::ikey_type ikey,
int keylenx) const {
return k.compare(ikey, keylenx) <= 0 && !upper_bound_;
}
template <typename K, typename N> int lower(const K &k, const N *n) const {
if (upper_bound_)
return n->size() - 1;
key_indexed_position kx = N::bound_type::lower_by(k, *n, *n);
return kx.i - (kx.p < 0);
}
template <typename K, typename N>
key_indexed_position lower_with_position(const K &k, const N *n) const {
key_indexed_position kx = N::bound_type::lower_by(k, *n, *n);
kx.i -= kx.p < 0;
return kx;
}
int next(int ki) const {
return ki - 1;
}
void mark_key_complete() const {
upper_bound_ = false;
}
template <typename N, typename K>
N *advance(const N *n, K &k) const {
k.assign_store_ikey(n->ikey_bound());
k.assign_store_length(0);
return n->prev_;
}
template <typename N, typename K>
typename N::nodeversion_type stable(N *&n, const K &k) const {
while (1) {
typename N::nodeversion_type v = n->stable();
N *next = n->safe_next();
int cmp;
if (!next
|| (cmp = ::compare(k.ikey(), next->ikey_bound())) < 0
|| (cmp == 0 && k.length() == 0))
return v;
n = next;
}
}
template <typename K> void shift_clear(K &ka) const {
ka.shift_clear_reverse();
upper_bound_ = true;
}
private:
mutable bool upper_bound_;
};
template <typename P> template <typename H>
int scanstackelt<P>::find_initial(H& helper, key_type& ka, bool emit_equal,
leafvalue_type& entry, threadinfo& ti)
{
key_indexed_position kx;
int keylenx = 0;
char suffixbuf[MASSTREE_MAXKEYLEN];
Str suffix;
retry_root:
n_ = root_->reach_leaf(ka, v_, ti);
retry_node:
if (v_.deleted())
goto retry_root;
n_->prefetch();
perm_ = n_->permutation();
kx = helper.lower_with_position(ka, this);
if (kx.p >= 0) {
keylenx = n_->keylenx_[kx.p];
fence();
entry = n_->lv_[kx.p];
entry.prefetch(keylenx);
if (n_->keylenx_has_ksuf(keylenx)) {
suffix = n_->ksuf(kx.p);
memcpy(suffixbuf, suffix.s, suffix.len);
suffix.s = suffixbuf;
}
}
if (n_->has_changed(v_)) {
ti.mark(tc_leaf_retry);
n_ = n_->advance_to_key(ka, v_, ti);
goto retry_node;
}
ki_ = kx.i;
if (kx.p >= 0) {
if (n_->keylenx_is_layer(keylenx)) {
node_stack_.push_back(root_);
node_stack_.push_back(n_);
root_ = entry.layer();
return scan_down;
} else if (n_->keylenx_has_ksuf(keylenx)) {
int ksuf_compare = suffix.compare(ka.suffix());
if (helper.initial_ksuf_match(ksuf_compare, emit_equal)) {
int keylen = ka.assign_store_suffix(suffix);
ka.assign_store_length(keylen);
return scan_emit;
}
} else if (emit_equal)
return scan_emit;
// otherwise, this entry must be skipped
ki_ = helper.next(ki_);
}
return scan_find_next;
}
template <typename P> template <typename H>
int scanstackelt<P>::find_retry(H& helper, key_type& ka, threadinfo& ti)
{
retry:
n_ = root_->reach_leaf(ka, v_, ti);
if (v_.deleted())
goto retry;
n_->prefetch();
perm_ = n_->permutation();
ki_ = helper.lower(ka, this);
return scan_find_next;
}
template <typename P> template <typename H>
int scanstackelt<P>::find_next(H &helper, key_type &ka, leafvalue_type &entry)
{
int kp;
if (v_.deleted())
return scan_retry;
retry_entry:
kp = this->kp();
if (kp >= 0) {
ikey_type ikey = n_->ikey0_[kp];
int keylenx = n_->keylenx_[kp];
int keylen = keylenx;
fence();
entry = n_->lv_[kp];
entry.prefetch(keylenx);
if (n_->keylenx_has_ksuf(keylenx))
keylen = ka.assign_store_suffix(n_->ksuf(kp));
if (n_->has_changed(v_))
goto changed;
else if (helper.is_duplicate(ka, ikey, keylenx)) {
ki_ = helper.next(ki_);
goto retry_entry;
}
// We know we can emit the data collected above.
ka.assign_store_ikey(ikey);
helper.mark_key_complete();
if (n_->keylenx_is_layer(keylenx)) {
node_stack_.push_back(root_);
node_stack_.push_back(n_);
root_ = entry.layer();
return scan_down;
} else {
ka.assign_store_length(keylen);
return scan_emit;
}
}
if (!n_->has_changed(v_)) {
n_ = helper.advance(n_, ka);
if (!n_) {
helper.mark_key_complete();
return scan_up;
}
n_->prefetch();
}
changed:
v_ = helper.stable(n_, ka);
perm_ = n_->permutation();
ki_ = helper.lower(ka, this);
return scan_find_next;
}
template <typename P> template <typename H, typename F>
int basic_table<P>::scan(H helper,
Str firstkey, bool emit_firstkey,
F& scanner,
threadinfo& ti) const
{
typedef typename P::ikey_type ikey_type;
typedef typename node_type::key_type key_type;
typedef typename node_type::leaf_type::leafvalue_type leafvalue_type;
union {
ikey_type x[(MASSTREE_MAXKEYLEN + sizeof(ikey_type) - 1)/sizeof(ikey_type)];
char s[MASSTREE_MAXKEYLEN];
} keybuf;
masstree_precondition(firstkey.len <= (int) sizeof(keybuf));
memcpy(keybuf.s, firstkey.s, firstkey.len);
key_type ka(keybuf.s, firstkey.len);
typedef scanstackelt<P> mystack_type;
mystack_type stack;
stack.root_ = root_;
leafvalue_type entry = leafvalue_type::make_empty();
int scancount = 0;
int state;
while (1) {
state = stack.find_initial(helper, ka, emit_firstkey, entry, ti);
scanner.visit_leaf(stack, ka, ti);
if (state != mystack_type::scan_down)
break;
ka.shift();
}
while (1) {
switch (state) {
case mystack_type::scan_emit:
++scancount;
if (!scanner.visit_value(ka, entry.value(), ti))
goto done;
stack.ki_ = helper.next(stack.ki_);
state = stack.find_next(helper, ka, entry);
break;
case mystack_type::scan_find_next:
find_next:
state = stack.find_next(helper, ka, entry);
if (state != mystack_type::scan_up)
scanner.visit_leaf(stack, ka, ti);
break;
case mystack_type::scan_up:
do {
if (stack.node_stack_.empty())
goto done;
stack.n_ = static_cast<leaf<P>*>(stack.node_stack_.back());
stack.node_stack_.pop_back();
stack.root_ = stack.node_stack_.back();
stack.node_stack_.pop_back();
ka.unshift();
} while (unlikely(ka.empty()));
stack.v_ = helper.stable(stack.n_, ka);
stack.perm_ = stack.n_->permutation();
stack.ki_ = helper.lower(ka, &stack);
goto find_next;
case mystack_type::scan_down:
helper.shift_clear(ka);
goto retry;
case mystack_type::scan_retry:
retry:
state = stack.find_retry(helper, ka, ti);
break;
}
}
done:
return scancount;
}
template <typename P> template <typename F>
int basic_table<P>::scan(Str firstkey, bool emit_firstkey,
F& scanner,
threadinfo& ti) const
{
return scan(forward_scan_helper(), firstkey, emit_firstkey, scanner, ti);
}
template <typename P> template <typename F>
int basic_table<P>::rscan(Str firstkey, bool emit_firstkey,
F& scanner,
threadinfo& ti) const
{
return scan(reverse_scan_helper(), firstkey, emit_firstkey, scanner, ti);
}
} // namespace Masstree
#endif