-
Notifications
You must be signed in to change notification settings - Fork 115
/
masstree_struct.hh
802 lines (714 loc) · 26.6 KB
/
masstree_struct.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2016 President and Fellows of Harvard College
* Copyright (c) 2012-2016 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_STRUCT_HH
#define MASSTREE_STRUCT_HH
#include "masstree.hh"
#include "nodeversion.hh"
#include "stringbag.hh"
#include "mtcounters.hh"
#include "timestamp.hh"
namespace Masstree {
template <typename P>
struct make_nodeversion {
typedef nodeversion_parameters<typename P::nodeversion_value_type> parameters_type;
typedef typename mass::conditional<P::concurrent,
nodeversion<parameters_type>,
singlethreaded_nodeversion<parameters_type> >::type type;
};
template <typename P>
struct make_prefetcher {
typedef typename mass::conditional<P::prefetch,
value_prefetcher<typename P::value_type>,
do_nothing>::type type;
};
template <typename P>
class node_base : public make_nodeversion<P>::type {
public:
static constexpr bool concurrent = P::concurrent;
static constexpr int nikey = 1;
typedef leaf<P> leaf_type;
typedef internode<P> internode_type;
typedef node_base<P> base_type;
typedef typename P::value_type value_type;
typedef leafvalue<P> leafvalue_type;
typedef typename P::ikey_type ikey_type;
typedef key<ikey_type> key_type;
typedef typename make_nodeversion<P>::type nodeversion_type;
typedef typename P::threadinfo_type threadinfo;
node_base(bool isleaf)
: nodeversion_type(isleaf) {
}
inline base_type* parent() const {
// almost always an internode
if (this->isleaf())
return static_cast<const leaf_type*>(this)->parent_;
else
return static_cast<const internode_type*>(this)->parent_;
}
inline bool parent_exists(base_type* p) const {
return p != nullptr;
}
inline bool has_parent() const {
return parent_exists(parent());
}
inline internode_type* locked_parent(threadinfo& ti) const;
inline void set_parent(base_type* p) {
if (this->isleaf())
static_cast<leaf_type*>(this)->parent_ = p;
else
static_cast<internode_type*>(this)->parent_ = p;
}
inline void make_layer_root() {
set_parent(nullptr);
this->mark_root();
}
inline base_type* maybe_parent() const {
base_type* x = parent();
return parent_exists(x) ? x : const_cast<base_type*>(this);
}
inline leaf_type* reach_leaf(const key_type& k, nodeversion_type& version,
threadinfo& ti) const;
void prefetch_full() const {
for (int i = 0; i < std::min(16 * std::min(P::leaf_width, P::internode_width) + 1, 4 * 64); i += 64)
::prefetch((const char *) this + i);
}
void print(FILE* f, const char* prefix, int depth, int kdepth) const;
};
template <typename P>
class internode : public node_base<P> {
public:
static constexpr int width = P::internode_width;
typedef typename node_base<P>::nodeversion_type nodeversion_type;
typedef key<typename P::ikey_type> key_type;
typedef typename P::ikey_type ikey_type;
typedef typename key_bound<width, P::bound_method>::type bound_type;
typedef typename P::threadinfo_type threadinfo;
uint8_t nkeys_;
uint32_t height_;
ikey_type ikey0_[width];
node_base<P>* child_[width + 1];
node_base<P>* parent_;
kvtimestamp_t created_at_[P::debug_level > 0];
internode(uint32_t height)
: node_base<P>(false), nkeys_(0), height_(height), parent_() {
}
static internode<P>* make(uint32_t height, threadinfo& ti) {
void* ptr = ti.pool_allocate(sizeof(internode<P>),
memtag_masstree_internode);
internode<P>* n = new(ptr) internode<P>(height);
assert(n);
if (P::debug_level > 0)
n->created_at_[0] = ti.operation_timestamp();
return n;
}
int size() const {
return nkeys_;
}
key_type get_key(int p) const {
return key_type(ikey0_[p]);
}
ikey_type ikey(int p) const {
return ikey0_[p];
}
int compare_key(ikey_type a, int bp) const {
return ::compare(a, ikey(bp));
}
int compare_key(const key_type& a, int bp) const {
return ::compare(a.ikey(), ikey(bp));
}
inline int stable_last_key_compare(const key_type& k, nodeversion_type v,
threadinfo& ti) const;
void prefetch() const {
for (int i = 64; i < std::min(16 * width + 1, 4 * 64); i += 64)
::prefetch((const char *) this + i);
}
void print(FILE* f, const char* prefix, int depth, int kdepth) const;
void deallocate(threadinfo& ti) {
ti.pool_deallocate(this, sizeof(*this), memtag_masstree_internode);
}
void deallocate_rcu(threadinfo& ti) {
ti.pool_deallocate_rcu(this, sizeof(*this), memtag_masstree_internode);
}
private:
void assign(int p, ikey_type ikey, node_base<P>* child) {
child->set_parent(this);
child_[p + 1] = child;
ikey0_[p] = ikey;
}
void shift_from(int p, const internode<P>* x, int xp, int n) {
masstree_precondition(x != this);
if (n) {
memcpy(ikey0_ + p, x->ikey0_ + xp, sizeof(ikey0_[0]) * n);
memcpy(child_ + p + 1, x->child_ + xp + 1, sizeof(child_[0]) * n);
}
}
void shift_up(int p, int xp, int n) {
memmove(ikey0_ + p, ikey0_ + xp, sizeof(ikey0_[0]) * n);
for (node_base<P> **a = child_ + p + n, **b = child_ + xp + n; n; --a, --b, --n)
*a = *b;
}
void shift_down(int p, int xp, int n) {
memmove(ikey0_ + p, ikey0_ + xp, sizeof(ikey0_[0]) * n);
for (node_base<P> **a = child_ + p + 1, **b = child_ + xp + 1; n; ++a, ++b, --n)
*a = *b;
}
int split_into(internode<P>* nr, int p, ikey_type ka, node_base<P>* value,
ikey_type& split_ikey, int split_type);
template <typename PP> friend class tcursor;
};
template <typename P>
class leafvalue {
public:
typedef typename P::value_type value_type;
typedef typename make_prefetcher<P>::type prefetcher_type;
leafvalue() {
}
leafvalue(value_type v) {
u_.v = v;
}
leafvalue(node_base<P>* n) {
u_.x = reinterpret_cast<uintptr_t>(n);
}
static leafvalue<P> make_empty() {
return leafvalue<P>(value_type());
}
typedef bool (leafvalue<P>::*unspecified_bool_type)() const;
operator unspecified_bool_type() const {
return u_.x ? &leafvalue<P>::empty : 0;
}
bool empty() const {
return !u_.x;
}
value_type value() const {
return u_.v;
}
value_type& value() {
return u_.v;
}
node_base<P>* layer() const {
return reinterpret_cast<node_base<P>*>(u_.x);
}
void prefetch(int keylenx) const {
if (!leaf<P>::keylenx_is_layer(keylenx))
prefetcher_type()(u_.v);
else
u_.n->prefetch_full();
}
private:
union {
node_base<P>* n;
value_type v;
uintptr_t x;
} u_;
};
template <typename P>
class leaf : public node_base<P> {
public:
static constexpr int width = P::leaf_width;
typedef typename node_base<P>::nodeversion_type nodeversion_type;
typedef key<typename P::ikey_type> key_type;
typedef typename node_base<P>::leafvalue_type leafvalue_type;
typedef kpermuter<P::leaf_width> permuter_type;
typedef typename P::ikey_type ikey_type;
typedef typename key_bound<width, P::bound_method>::type bound_type;
typedef typename P::threadinfo_type threadinfo;
typedef stringbag<uint8_t> internal_ksuf_type;
typedef stringbag<uint16_t> external_ksuf_type;
typedef typename P::phantom_epoch_type phantom_epoch_type;
static constexpr int ksuf_keylenx = 64;
static constexpr int layer_keylenx = 128;
enum {
modstate_insert = 0, modstate_remove = 1, modstate_deleted_layer = 2
};
int8_t extrasize64_;
uint8_t modstate_;
uint8_t keylenx_[width];
typename permuter_type::storage_type permutation_;
ikey_type ikey0_[width];
leafvalue_type lv_[width];
external_ksuf_type* ksuf_;
union {
leaf<P>* ptr;
uintptr_t x;
} next_;
leaf<P>* prev_;
node_base<P>* parent_;
phantom_epoch_type phantom_epoch_[P::need_phantom_epoch];
kvtimestamp_t created_at_[P::debug_level > 0];
internal_ksuf_type iksuf_[0];
leaf(size_t sz, phantom_epoch_type phantom_epoch)
: node_base<P>(true), modstate_(modstate_insert),
permutation_(permuter_type::make_empty()),
ksuf_(), parent_(), iksuf_{} {
masstree_precondition(sz % 64 == 0 && sz / 64 < 128);
extrasize64_ = (int(sz) >> 6) - ((int(sizeof(*this)) + 63) >> 6);
if (extrasize64_ > 0) {
new((void*) &iksuf_[0]) internal_ksuf_type(width, sz - sizeof(*this));
}
if (P::need_phantom_epoch) {
phantom_epoch_[0] = phantom_epoch;
}
}
static leaf<P>* make(int ksufsize, phantom_epoch_type phantom_epoch, threadinfo& ti) {
size_t sz = iceil(sizeof(leaf<P>) + std::min(ksufsize, 128), 64);
void* ptr = ti.pool_allocate(sz, memtag_masstree_leaf);
leaf<P>* n = new(ptr) leaf<P>(sz, phantom_epoch);
assert(n);
if (P::debug_level > 0) {
n->created_at_[0] = ti.operation_timestamp();
}
return n;
}
static leaf<P>* make_root(int ksufsize, leaf<P>* parent, threadinfo& ti) {
leaf<P>* n = make(ksufsize, parent ? parent->phantom_epoch() : phantom_epoch_type(), ti);
n->next_.ptr = n->prev_ = 0;
n->ikey0_[0] = 0; // to avoid undefined behavior
n->make_layer_root();
return n;
}
static size_t min_allocated_size() {
return (sizeof(leaf<P>) + 63) & ~size_t(63);
}
size_t allocated_size() const {
int es = (extrasize64_ >= 0 ? extrasize64_ : -extrasize64_ - 1);
return (sizeof(*this) + es * 64 + 63) & ~size_t(63);
}
phantom_epoch_type phantom_epoch() const {
return P::need_phantom_epoch ? phantom_epoch_[0] : phantom_epoch_type();
}
int size() const {
return permuter_type::size(permutation_);
}
permuter_type permutation() const {
return permuter_type(permutation_);
}
typename nodeversion_type::value_type full_version_value() const {
static_assert(int(nodeversion_type::traits_type::top_stable_bits) >= int(permuter_type::size_bits), "not enough bits to add size to version");
return (this->version_value() << permuter_type::size_bits) + size();
}
typename nodeversion_type::value_type full_unlocked_version_value() const {
static_assert(int(nodeversion_type::traits_type::top_stable_bits) >= int(permuter_type::size_bits), "not enough bits to add size to version");
typename node_base<P>::nodeversion_type v(*this);
if (v.locked()) {
// subtly, unlocked_version_value() is different than v.unlock();
// v.version_value() because the latter will add a split bit if
// we're doing a split. So we do the latter to get the fully
// correct version.
v.unlock();
}
return (v.version_value() << permuter_type::size_bits) + size();
}
using node_base<P>::has_changed;
bool has_changed(nodeversion_type oldv,
typename permuter_type::storage_type oldperm) const {
return this->has_changed(oldv) || oldperm != permutation_;
}
key_type get_key(int p) const {
int keylenx = keylenx_[p];
if (!keylenx_has_ksuf(keylenx))
return key_type(ikey0_[p], keylenx);
else
return key_type(ikey0_[p], ksuf(p));
}
ikey_type ikey(int p) const {
return ikey0_[p];
}
ikey_type ikey_bound() const {
return ikey0_[0];
}
int compare_key(const key_type& a, int bp) const {
return a.compare(ikey(bp), keylenx_[bp]);
}
inline int stable_last_key_compare(const key_type& k, nodeversion_type v,
threadinfo& ti) const;
inline leaf<P>* advance_to_key(const key_type& k, nodeversion_type& version,
threadinfo& ti) const;
static bool keylenx_is_layer(int keylenx) {
return keylenx > 127;
}
static bool keylenx_has_ksuf(int keylenx) {
return keylenx == ksuf_keylenx;
}
bool is_layer(int p) const {
return keylenx_is_layer(keylenx_[p]);
}
bool has_ksuf(int p) const {
return keylenx_has_ksuf(keylenx_[p]);
}
Str ksuf(int p, int keylenx) const {
(void) keylenx;
masstree_precondition(keylenx_has_ksuf(keylenx));
return ksuf_ ? ksuf_->get(p) : iksuf_[0].get(p);
}
Str ksuf(int p) const {
return ksuf(p, keylenx_[p]);
}
bool ksuf_equals(int p, const key_type& ka) const {
return ksuf_equals(p, ka, keylenx_[p]);
}
bool ksuf_equals(int p, const key_type& ka, int keylenx) const {
if (!keylenx_has_ksuf(keylenx))
return true;
Str s = ksuf(p, keylenx);
return s.len == ka.suffix().len
&& string_slice<uintptr_t>::equals_sloppy(s.s, ka.suffix().s, s.len);
}
// Returns 1 if match & not layer, 0 if no match, <0 if match and layer
int ksuf_matches(int p, const key_type& ka) const {
int keylenx = keylenx_[p];
if (keylenx < ksuf_keylenx)
return 1;
if (keylenx == layer_keylenx)
return -(int) sizeof(ikey_type);
Str s = ksuf(p, keylenx);
return s.len == ka.suffix().len
&& string_slice<uintptr_t>::equals_sloppy(s.s, ka.suffix().s, s.len);
}
int ksuf_compare(int p, const key_type& ka) const {
int keylenx = keylenx_[p];
if (!keylenx_has_ksuf(keylenx))
return 0;
return ksuf(p, keylenx).compare(ka.suffix());
}
size_t ksuf_used_capacity() const {
if (ksuf_)
return ksuf_->used_capacity();
else if (extrasize64_ > 0)
return iksuf_[0].used_capacity();
else
return 0;
}
size_t ksuf_capacity() const {
if (ksuf_)
return ksuf_->capacity();
else if (extrasize64_ > 0)
return iksuf_[0].capacity();
else
return 0;
}
bool ksuf_external() const {
return ksuf_;
}
Str ksuf_storage(int p) const {
if (ksuf_)
return ksuf_->get(p);
else if (extrasize64_ > 0)
return iksuf_[0].get(p);
else
return Str();
}
bool deleted_layer() const {
return modstate_ == modstate_deleted_layer;
}
void prefetch() const {
for (int i = 64; i < std::min(16 * width + 1, 4 * 64); i += 64)
::prefetch((const char *) this + i);
if (extrasize64_ > 0)
::prefetch((const char *) &iksuf_[0]);
else if (extrasize64_ < 0) {
::prefetch((const char *) ksuf_);
::prefetch((const char *) ksuf_ + CACHE_LINE_SIZE);
}
}
void print(FILE* f, const char* prefix, int depth, int kdepth) const;
leaf<P>* safe_next() const {
return reinterpret_cast<leaf<P>*>(next_.x & ~(uintptr_t) 1);
}
void deallocate(threadinfo& ti) {
if (ksuf_)
ti.deallocate(ksuf_, ksuf_->capacity(),
memtag_masstree_ksuffixes);
if (extrasize64_ != 0)
iksuf_[0].~stringbag();
ti.pool_deallocate(this, allocated_size(), memtag_masstree_leaf);
}
void deallocate_rcu(threadinfo& ti) {
if (ksuf_)
ti.deallocate_rcu(ksuf_, ksuf_->capacity(),
memtag_masstree_ksuffixes);
ti.pool_deallocate_rcu(this, allocated_size(), memtag_masstree_leaf);
}
private:
inline void mark_deleted_layer() {
modstate_ = modstate_deleted_layer;
}
inline void assign(int p, const key_type& ka, threadinfo& ti) {
lv_[p] = leafvalue_type::make_empty();
ikey0_[p] = ka.ikey();
if (!ka.has_suffix()) {
keylenx_[p] = ka.length();
} else {
keylenx_[p] = ksuf_keylenx;
assign_ksuf(p, ka.suffix(), false, ti);
}
}
inline void assign_initialize(int p, const key_type& ka, threadinfo& ti) {
lv_[p] = leafvalue_type::make_empty();
ikey0_[p] = ka.ikey();
if (!ka.has_suffix()) {
keylenx_[p] = ka.length();
} else {
keylenx_[p] = ksuf_keylenx;
assign_ksuf(p, ka.suffix(), true, ti);
}
}
inline void assign_initialize(int p, leaf<P>* x, int xp, threadinfo& ti) {
lv_[p] = x->lv_[xp];
ikey0_[p] = x->ikey0_[xp];
keylenx_[p] = x->keylenx_[xp];
if (x->has_ksuf(xp)) {
assign_ksuf(p, x->ksuf(xp), true, ti);
}
}
inline void assign_initialize_for_layer(int p, const key_type& ka) {
assert(ka.has_suffix());
ikey0_[p] = ka.ikey();
keylenx_[p] = layer_keylenx;
}
void assign_ksuf(int p, Str s, bool initializing, threadinfo& ti);
inline ikey_type ikey_after_insert(const permuter_type& perm, int i,
const tcursor<P>* cursor) const;
int split_into(leaf<P>* nr, tcursor<P>* tcursor, ikey_type& split_ikey,
threadinfo& ti);
template <typename PP> friend class tcursor;
};
template <typename P>
void basic_table<P>::initialize(threadinfo& ti) {
masstree_precondition(!root_);
root_ = node_type::leaf_type::make_root(0, 0, ti);
}
/** @brief Return this node's parent in locked state.
@pre this->locked()
@post this->parent() == result && (!result || result->locked()) */
template <typename P>
internode<P>* node_base<P>::locked_parent(threadinfo& ti) const
{
node_base<P>* p;
masstree_precondition(!this->concurrent || this->locked());
while (true) {
p = this->parent();
if (!this->parent_exists(p)) {
break;
}
nodeversion_type pv = p->lock(*p, ti.lock_fence(tc_internode_lock));
if (p == this->parent()) {
masstree_invariant(!p->isleaf());
break;
}
p->unlock(pv);
relax_fence();
}
return static_cast<internode<P>*>(p);
}
template <typename P>
void node_base<P>::print(FILE* f, const char* prefix, int depth, int kdepth) const
{
if (this->isleaf())
static_cast<const leaf<P>*>(this)->print(f, prefix, depth, kdepth);
else
static_cast<const internode<P>*>(this)->print(f, prefix, depth, kdepth);
}
/** @brief Return the result of compare_key(k, LAST KEY IN NODE).
Reruns the comparison until a stable comparison is obtained. */
template <typename P>
inline int
internode<P>::stable_last_key_compare(const key_type& k, nodeversion_type v,
threadinfo& ti) const
{
while (true) {
int n = this->size();
int cmp = n ? compare_key(k, n - 1) : 1;
if (likely(!this->has_changed(v))) {
return cmp;
}
v = this->stable_annotated(ti.stable_fence());
}
}
template <typename P>
inline int
leaf<P>::stable_last_key_compare(const key_type& k, nodeversion_type v,
threadinfo& ti) const
{
while (true) {
typename leaf<P>::permuter_type perm(permutation_);
int n = perm.size();
// If `n == 0`, then this node is empty: it was deleted without ever
// splitting, or it split and then was emptied.
// - It is always safe to return 1, because then the caller will
// check more precisely whether `k` belongs in `this`.
// - It is safe to return anything if `this->deleted()`, because
// viewing the deleted node will always cause a retry.
// - Thus it is safe to return a comparison with the key stored in slot
// `perm[0]`. If the node ever had keys in it, then kpermuter ensures
// that slot holds the most recently deleted key, which would belong
// in this leaf. Otherwise, `perm[0]` is 0.
int p = perm[n ? n - 1 : 0];
int cmp = compare_key(k, p);
if (likely(!this->has_changed(v))) {
return cmp;
}
v = this->stable_annotated(ti.stable_fence());
}
}
/** @brief Return the leaf in this tree layer responsible for @a ka.
Returns a stable leaf. Sets @a version to the stable version. */
template <typename P>
inline leaf<P>* node_base<P>::reach_leaf(const key_type& ka,
nodeversion_type& version,
threadinfo& ti) const
{
const node_base<P> *n[2];
typename node_base<P>::nodeversion_type v[2];
unsigned sense;
// Get a non-stale root.
// Detect staleness by checking whether n has ever split.
// The true root has never split.
retry:
sense = 0;
n[sense] = this;
while (true) {
v[sense] = n[sense]->stable_annotated(ti.stable_fence());
if (v[sense].is_root()) {
break;
}
ti.mark(tc_root_retry);
n[sense] = n[sense]->maybe_parent();
}
// Loop over internal nodes.
while (!v[sense].isleaf()) {
const internode<P> *in = static_cast<const internode<P>*>(n[sense]);
in->prefetch();
int kp = internode<P>::bound_type::upper(ka, *in);
n[sense ^ 1] = in->child_[kp];
if (!n[sense ^ 1]) {
goto retry;
}
v[sense ^ 1] = n[sense ^ 1]->stable_annotated(ti.stable_fence());
if (likely(!in->has_changed(v[sense]))) {
sense ^= 1;
continue;
}
typename node_base<P>::nodeversion_type oldv = v[sense];
v[sense] = in->stable_annotated(ti.stable_fence());
if (unlikely(oldv.has_split(v[sense]))
&& in->stable_last_key_compare(ka, v[sense], ti) > 0) {
ti.mark(tc_root_retry);
goto retry;
} else {
ti.mark(tc_internode_retry);
}
}
version = v[sense];
return const_cast<leaf<P> *>(static_cast<const leaf<P> *>(n[sense]));
}
/** @brief Return the leaf at or after *this responsible for @a ka.
@pre *this was responsible for @a ka at version @a v
Checks whether *this has split since version @a v. If it has split, then
advances through the leaves using the B^link-tree pointers and returns
the relevant leaf, setting @a v to the stable version for that leaf. */
template <typename P>
leaf<P>* leaf<P>::advance_to_key(const key_type& ka, nodeversion_type& v,
threadinfo& ti) const
{
const leaf<P>* n = this;
nodeversion_type oldv = v;
v = n->stable_annotated(ti.stable_fence());
if (unlikely(v.has_split(oldv))
&& n->stable_last_key_compare(ka, v, ti) > 0) {
leaf<P> *next;
ti.mark(tc_leaf_walk);
while (likely(!v.deleted())
&& (next = n->safe_next())
&& compare(ka.ikey(), next->ikey_bound()) >= 0) {
n = next;
v = n->stable_annotated(ti.stable_fence());
}
}
return const_cast<leaf<P>*>(n);
}
/** @brief Assign position @a p's keysuffix to @a s.
This may allocate a new suffix container, copying suffixes over.
The @a initializing parameter determines which suffixes are copied. If @a
initializing is false, then this is an insertion into a live node. The
live node's permutation indicates which keysuffixes are active, and only
active suffixes are copied. If @a initializing is true, then this
assignment is part of the initialization process for a new node. The
permutation might not be set up yet. In this case, it is assumed that key
positions [0,p) are ready: keysuffixes in that range are copied. In either
case, the key at position p is NOT copied; it is assigned to @a s. */
template <typename P>
void leaf<P>::assign_ksuf(int p, Str s, bool initializing, threadinfo& ti) {
if ((ksuf_ && ksuf_->assign(p, s))
|| (extrasize64_ > 0 && iksuf_[0].assign(p, s)))
return;
external_ksuf_type* oksuf = ksuf_;
permuter_type perm(permutation_);
int n = initializing ? p : perm.size();
size_t csz = 0;
for (int i = 0; i < n; ++i) {
int mp = initializing ? i : perm[i];
if (mp != p && has_ksuf(mp))
csz += ksuf(mp).len;
}
size_t sz = iceil_log2(external_ksuf_type::safe_size(width, csz + s.len));
if (oksuf)
sz = std::max(sz, oksuf->capacity());
void* ptr = ti.allocate(sz, memtag_masstree_ksuffixes);
external_ksuf_type* nksuf = new(ptr) external_ksuf_type(width, sz);
for (int i = 0; i < n; ++i) {
int mp = initializing ? i : perm[i];
if (mp != p && has_ksuf(mp)) {
bool ok = nksuf->assign(mp, ksuf(mp));
assert(ok); (void) ok;
}
}
bool ok = nksuf->assign(p, s);
assert(ok); (void) ok;
fence();
// removed ksufs aren't copied to the new ksuf, but observers
// might need them. We ensure that observers must retry by
// ensuring that we are not currently in the remove state.
// State transitions are accompanied by mark_insert() so observers
// will retry.
masstree_invariant(modstate_ != modstate_remove);
ksuf_ = nksuf;
fence();
if (extrasize64_ >= 0) // now the new ksuf_ installed, mark old dead
extrasize64_ = -extrasize64_ - 1;
if (oksuf)
ti.deallocate_rcu(oksuf, oksuf->capacity(),
memtag_masstree_ksuffixes);
}
template <typename P>
inline basic_table<P>::basic_table()
: root_(0) {
}
template <typename P>
inline node_base<P>* basic_table<P>::root() const {
return root_;
}
template <typename P>
inline node_base<P>* basic_table<P>::fix_root() {
node_base<P>* root = root_;
if (unlikely(!root->is_root())) {
node_base<P>* old_root = root;
root = root->maybe_parent();
(void) cmpxchg(&root_, old_root, root);
}
return root;
}
} // namespace Masstree
#endif