forked from ix-project/silo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
btree.h
1794 lines (1583 loc) · 45.6 KB
/
btree.h
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <assert.h>
#include <malloc.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <atomic>
#include <thread>
#include "log2.hh"
#include "ndb_type_traits.h"
#include "varkey.h"
#include "counter.h"
#include "macros.h"
#include "prefetch.h"
#include "amd64.h"
#include "rcu.h"
#include "util.h"
#include "small_vector.h"
#include "ownership_checker.h"
namespace private_ {
template <typename T, typename P> struct u64manip;
template <typename P>
struct u64manip<uint64_t, P> {
static inline uint64_t Load(uint64_t t) { return t; }
static inline void Store(uint64_t &t, uint64_t v) { t = v; }
static inline uint64_t
Lock(uint64_t &t)
{
#ifdef CHECK_INVARIANTS
INVARIANT(!P::IsLocked(t));
t |= P::HDR_LOCKED_MASK;
return t;
#endif
return 0;
}
static inline uint64_t
LockWithSpinCount(uint64_t &t, unsigned &spins)
{
const uint64_t ret = Lock(t);
spins = 0;
return ret;
}
static inline void
Unlock(uint64_t &t)
{
#ifdef CHECK_INVARIANTS
INVARIANT(P::IsLocked(t));
t &= ~(P::HDR_LOCKED_MASK | P::HDR_MODIFYING_MASK);
#endif
}
static inline uint64_t
StableVersion(uint64_t t)
{
INVARIANT(!P::IsModifying(t));
return t;
}
static inline bool
CheckVersion(uint64_t t, uint64_t stablev)
{
INVARIANT(!P::IsModifying(stablev));
INVARIANT((t & ~P::HDR_LOCKED_MASK) == (stablev & ~P::HDR_LOCKED_MASK));
return true;
}
};
template <typename P>
struct u64manip<std::atomic<uint64_t>, P> {
static inline uint64_t
Load(const std::atomic<uint64_t> &t)
{
return t.load(std::memory_order_acquire);
}
static inline void
Store(std::atomic<uint64_t> &t, uint64_t v)
{
t.store(v, std::memory_order_release);
}
static inline uint64_t
Lock(std::atomic<uint64_t> &t)
{
#ifdef SPINLOCK_BACKOFF
uint64_t backoff_shift = 0;
#endif
uint64_t v = Load(t);
while ((v & P::HDR_LOCKED_MASK) ||
!t.compare_exchange_strong(v, v | P::HDR_LOCKED_MASK)) {
#ifdef SPINLOCK_BACKOFF
if (backoff_shift < 63)
backoff_shift++;
uint64_t spins = (1UL << backoff_shift) * BACKOFF_SPINS_FACTOR;
while (spins) {
nop_pause();
spins--;
}
#else
nop_pause();
#endif
v = Load(t);
}
COMPILER_MEMORY_FENCE;
return v;
}
static inline uint64_t
LockWithSpinCount(std::atomic<uint64_t> &t, unsigned &spins)
{
#ifdef SPINLOCK_BACKOFF
uint64_t backoff_shift = 0;
#endif
spins = 0;
uint64_t v = Load(t);
while ((v & P::HDR_LOCKED_MASK) ||
!t.compare_exchange_strong(v, v | P::HDR_LOCKED_MASK)) {
#ifdef SPINLOCK_BACKOFF
if (backoff_shift < 63)
backoff_shift++;
uint64_t backoff_spins = (1UL << backoff_shift) * BACKOFF_SPINS_FACTOR;
while (backoff_spins) {
nop_pause();
backoff_spins--;
}
#else
nop_pause();
#endif
v = Load(t);
spins++;
}
COMPILER_MEMORY_FENCE;
return v;
}
static inline void
Unlock(std::atomic<uint64_t> &v)
{
INVARIANT(P::IsLocked(v));
const uint64_t oldh = Load(v);
uint64_t h = oldh;
bool newv = false;
if ((h & P::HDR_MODIFYING_MASK) ||
(h & P::HDR_DELETING_MASK)) {
newv = true;
const uint64_t n = (h & P::HDR_VERSION_MASK) >> P::HDR_VERSION_SHIFT;
h &= ~P::HDR_VERSION_MASK;
h |= (((n + 1) << P::HDR_VERSION_SHIFT) & P::HDR_VERSION_MASK);
}
// clear locked + modifying bits
h &= ~(P::HDR_LOCKED_MASK | P::HDR_MODIFYING_MASK);
if (newv)
INVARIANT(!CheckVersion(oldh, h));
INVARIANT(!(h & P::HDR_LOCKED_MASK));
INVARIANT(!(h & P::HDR_MODIFYING_MASK));
COMPILER_MEMORY_FENCE;
Store(v, h);
}
static inline uint64_t
StableVersion(const std::atomic<uint64_t> &t)
{
uint64_t v = Load(t);
while ((v & P::HDR_MODIFYING_MASK)) {
nop_pause();
v = Load(t);
}
COMPILER_MEMORY_FENCE;
return v;
}
static inline bool
CheckVersion(uint64_t t, uint64_t stablev)
{
INVARIANT(!(stablev & P::HDR_MODIFYING_MASK));
COMPILER_MEMORY_FENCE;
return (t & ~P::HDR_LOCKED_MASK) ==
(stablev & ~P::HDR_LOCKED_MASK);
}
};
}
/**
* manipulates a btree version
*
* hdr bits: layout is (actual bytes depend on the NKeysPerNode parameter)
*
* <-- low bits
* [type | key_slots_used | locked | is_root | modifying | deleting | version ]
* [0:1 | 1:5 | 5:6 | 6:7 | 7:8 | 8:9 | 9:64 ]
*
* bit invariants:
* 1) modifying => locked
* 2) deleting => locked
*
* WARNING: the correctness of our concurrency scheme relies on being able
* to do a memory reads/writes from/to hdr atomically. x86 architectures
* guarantee that aligned writes are atomic (see intel spec)
*/
template <typename VersionType, unsigned NKeysPerNode>
class btree_version_manip {
typedef
typename private_::typeutil<VersionType>::func_param_type
LoadVersionType;
typedef
private_::u64manip<
VersionType,
btree_version_manip<VersionType, NKeysPerNode>>
U64Manip;
static inline constexpr uint64_t
LowMask(uint64_t nbits)
{
return (1UL << nbits) - 1UL;
}
public:
static const uint64_t HDR_TYPE_BITS = 1;
static const uint64_t HDR_TYPE_MASK = 0x1;
static const uint64_t HDR_KEY_SLOTS_SHIFT = HDR_TYPE_BITS;
static const uint64_t HDR_KEY_SLOTS_BITS = ceil_log2_const(NKeysPerNode);
static const uint64_t HDR_KEY_SLOTS_MASK = LowMask(HDR_KEY_SLOTS_BITS) << HDR_KEY_SLOTS_SHIFT;
static const uint64_t HDR_LOCKED_SHIFT = HDR_KEY_SLOTS_SHIFT + HDR_KEY_SLOTS_BITS;
static const uint64_t HDR_LOCKED_BITS = 1;
static const uint64_t HDR_LOCKED_MASK = LowMask(HDR_LOCKED_BITS) << HDR_LOCKED_SHIFT;
static const uint64_t HDR_IS_ROOT_SHIFT = HDR_LOCKED_SHIFT + HDR_LOCKED_BITS;
static const uint64_t HDR_IS_ROOT_BITS = 1;
static const uint64_t HDR_IS_ROOT_MASK = LowMask(HDR_IS_ROOT_BITS) << HDR_IS_ROOT_SHIFT;
static const uint64_t HDR_MODIFYING_SHIFT = HDR_IS_ROOT_SHIFT + HDR_IS_ROOT_BITS;
static const uint64_t HDR_MODIFYING_BITS = 1;
static const uint64_t HDR_MODIFYING_MASK = LowMask(HDR_MODIFYING_BITS) << HDR_MODIFYING_SHIFT;
static const uint64_t HDR_DELETING_SHIFT = HDR_MODIFYING_SHIFT + HDR_MODIFYING_BITS;
static const uint64_t HDR_DELETING_BITS = 1;
static const uint64_t HDR_DELETING_MASK = LowMask(HDR_DELETING_BITS) << HDR_DELETING_SHIFT;
static const uint64_t HDR_VERSION_SHIFT = HDR_DELETING_SHIFT + HDR_DELETING_BITS;
static const uint64_t HDR_VERSION_MASK = ((uint64_t)-1) << HDR_VERSION_SHIFT;
// sanity checks
static_assert(NKeysPerNode >= 1, "XX");
static_assert(std::numeric_limits<uint64_t>::max() == (
HDR_TYPE_MASK |
HDR_KEY_SLOTS_MASK |
HDR_LOCKED_MASK |
HDR_IS_ROOT_MASK |
HDR_MODIFYING_MASK |
HDR_DELETING_MASK |
HDR_VERSION_MASK
), "XX");
static_assert( !(HDR_TYPE_MASK & HDR_KEY_SLOTS_MASK) , "XX");
static_assert( !(HDR_KEY_SLOTS_MASK & HDR_LOCKED_MASK) , "XX");
static_assert( !(HDR_LOCKED_MASK & HDR_IS_ROOT_MASK) , "XX");
static_assert( !(HDR_IS_ROOT_MASK & HDR_MODIFYING_MASK) , "XX");
static_assert( !(HDR_MODIFYING_MASK & HDR_DELETING_MASK) , "XX");
static_assert( !(HDR_DELETING_MASK & HDR_VERSION_MASK) , "XX");
// low level ops
static inline uint64_t
Load(LoadVersionType v)
{
return U64Manip::Load(v);
}
static inline void
Store(VersionType &t, uint64_t v)
{
U64Manip::Store(t, v);
}
// accessors
static inline bool
IsLeafNode(LoadVersionType v)
{
return (Load(v) & HDR_TYPE_MASK) == 0;
}
static inline bool
IsInternalNode(LoadVersionType v)
{
return !IsLeafNode(v);
}
static inline size_t
KeySlotsUsed(LoadVersionType v)
{
return (Load(v) & HDR_KEY_SLOTS_MASK) >> HDR_KEY_SLOTS_SHIFT;
}
static inline bool
IsLocked(LoadVersionType v)
{
return (Load(v) & HDR_LOCKED_MASK);
}
static inline bool
IsRoot(LoadVersionType v)
{
return (Load(v) & HDR_IS_ROOT_MASK);
}
static inline bool
IsModifying(LoadVersionType v)
{
return (Load(v) & HDR_MODIFYING_MASK);
}
static inline bool
IsDeleting(LoadVersionType v)
{
return (Load(v) & HDR_DELETING_MASK);
}
static inline uint64_t
Version(LoadVersionType v)
{
return (Load(v) & HDR_VERSION_MASK) >> HDR_VERSION_SHIFT;
}
static std::string
VersionInfoStr(LoadVersionType v)
{
std::ostringstream buf;
buf << "[";
if (IsLeafNode(v))
buf << "LEAF";
else
buf << "INT";
buf << " | ";
buf << KeySlotsUsed(v) << " | ";
if (IsLocked(v))
buf << "LOCKED";
else
buf << "-";
buf << " | ";
if (IsRoot(v))
buf << "ROOT";
else
buf << "-";
buf << " | ";
if (IsModifying(v))
buf << "MOD";
else
buf << "-";
buf << " | ";
if (IsDeleting(v))
buf << "DEL";
else
buf << "-";
buf << " | ";
buf << Version(v);
buf << "]";
return buf.str();
}
// mutators
static inline void
SetKeySlotsUsed(VersionType &v, size_t n)
{
INVARIANT(n <= NKeysPerNode);
INVARIANT(IsModifying(v));
uint64_t h = Load(v);
h &= ~HDR_KEY_SLOTS_MASK;
h |= (n << HDR_KEY_SLOTS_SHIFT);
Store(v, h);
}
static inline void
IncKeySlotsUsed(VersionType &v)
{
SetKeySlotsUsed(v, KeySlotsUsed(v) + 1);
}
static inline void
DecKeySlotsUsed(VersionType &v)
{
INVARIANT(KeySlotsUsed(v) > 0);
SetKeySlotsUsed(v, KeySlotsUsed(v) - 1);
}
static inline void
SetRoot(VersionType &v)
{
INVARIANT(IsLocked(v));
INVARIANT(!IsRoot(v));
uint64_t h = Load(v);
h |= HDR_IS_ROOT_MASK;
Store(v, h);
}
static inline void
ClearRoot(VersionType &v)
{
INVARIANT(IsLocked(v));
INVARIANT(IsRoot(v));
uint64_t h = Load(v);
h &= ~HDR_IS_ROOT_MASK;
Store(v, h);
}
static inline void
MarkModifying(VersionType &v)
{
INVARIANT(IsLocked(v));
INVARIANT(!IsModifying(v));
uint64_t h = Load(v);
h |= HDR_MODIFYING_MASK;
Store(v, h);
}
static inline void
MarkDeleting(VersionType &v)
{
INVARIANT(IsLocked(v));
INVARIANT(!IsDeleting(v));
uint64_t h = Load(v);
h |= HDR_DELETING_MASK;
Store(v, h);
}
// concurrency control
static inline uint64_t
StableVersion(LoadVersionType v)
{
return U64Manip::StableVersion(v);
}
static inline uint64_t
UnstableVersion(LoadVersionType v)
{
return Load(v);
}
static inline bool
CheckVersion(LoadVersionType v, uint64_t stablev)
{
return U64Manip::CheckVersion(Load(v), stablev);
}
static inline uint64_t
Lock(VersionType &v)
{
return U64Manip::Lock(v);
}
static inline uint64_t
LockWithSpinCount(VersionType &v, unsigned &spins)
{
return U64Manip::LockWithSpinCount(v, spins);
}
static inline void
Unlock(VersionType &v)
{
U64Manip::Unlock(v);
}
};
struct base_btree_config {
static const unsigned int NKeysPerNode = 15;
static const bool RcuRespCaller = true;
};
struct concurrent_btree_traits : public base_btree_config {
typedef std::atomic<uint64_t> VersionType;
};
struct single_threaded_btree_traits : public base_btree_config {
typedef uint64_t VersionType;
};
/**
* A concurrent, variable key length b+-tree, optimized for read heavy
* workloads.
*
* This b+-tree maps uninterpreted binary strings (key_type) of arbitrary
* length to a single pointer (value_type). Binary string values are copied
* into the b+-tree, so the caller does not have to worry about preserving
* memory for key values.
*
* This b+-tree does not manage the memory pointed to by value_type. The
* pointer is treated completely opaquely.
*
* So far, this b+-tree has only been tested on 64-bit intel x86 processors.
* It's correctness, as it is implemented (not conceptually), requires the
* semantics of total store order (TSO) for correctness. To fix this, we would
* change compiler fences into actual memory fences, at the very least.
*/
template <typename P>
class btree {
template <template <typename> class, typename>
friend class base_txn_btree;
public:
typedef varkey key_type;
typedef std::string string_type;
typedef uint64_t key_slice;
typedef uint8_t* value_type;
typedef typename std::conditional<!P::RcuRespCaller,
scoped_rcu_region,
disabled_rcu_region>::type rcu_region;
// public to assist in testing
static const unsigned int NKeysPerNode = P::NKeysPerNode;
static const unsigned int NMinKeysPerNode = P::NKeysPerNode / 2;
private:
typedef std::pair<ssize_t, size_t> key_search_ret;
typedef
btree_version_manip<typename P::VersionType, NKeysPerNode>
VersionManip;
typedef
btree_version_manip<uint64_t, NKeysPerNode>
RawVersionManip;
struct node {
typename P::VersionType hdr_;
#ifdef BTREE_LOCK_OWNERSHIP_CHECKING
std::thread::id lock_owner_;
#endif /* BTREE_LOCK_OWNERSHIP_CHECKING */
/**
* Keys are assumed to be stored in contiguous sorted order, so that all
* the used slots are grouped together. That is, elems in positions
* [0, key_slots_used) are valid, and elems in positions
* [key_slots_used, NKeysPerNode) are empty
*/
key_slice keys_[NKeysPerNode];
node() :
hdr_()
#ifdef BTREE_LOCK_OWNERSHIP_CHECKING
, lock_owner_()
#endif
{}
~node()
{
INVARIANT(!is_locked());
INVARIANT(is_deleting());
}
inline bool
is_leaf_node() const
{
return VersionManip::IsLeafNode(hdr_);
}
inline bool
is_internal_node() const
{
return !is_leaf_node();
}
inline size_t
key_slots_used() const
{
return VersionManip::KeySlotsUsed(hdr_);
}
inline void
set_key_slots_used(size_t n)
{
VersionManip::SetKeySlotsUsed(hdr_, n);
}
inline void
inc_key_slots_used()
{
VersionManip::IncKeySlotsUsed(hdr_);
}
inline void
dec_key_slots_used()
{
VersionManip::DecKeySlotsUsed(hdr_);
}
inline bool
is_locked() const
{
return VersionManip::IsLocked(hdr_);
}
#ifdef BTREE_LOCK_OWNERSHIP_CHECKING
inline bool
is_lock_owner() const
{
return std::this_thread::get_id() == lock_owner_;
}
#else
inline bool
is_lock_owner() const
{
return true;
}
#endif /* BTREE_LOCK_OWNERSHIP_CHECKING */
inline uint64_t
lock()
{
#ifdef ENABLE_EVENT_COUNTERS
static event_avg_counter
evt_avg_btree_leaf_node_lock_acquire_spins(
util::cxx_typename<btree<P>>::value() +
std::string("_avg_btree_leaf_node_lock_acquire_spins"));
static event_avg_counter
evt_avg_btree_internal_node_lock_acquire_spins(
util::cxx_typename<btree<P>>::value() +
std::string("_avg_btree_internal_node_lock_acquire_spins"));
unsigned spins;
const uint64_t ret = VersionManip::LockWithSpinCount(hdr_, spins);
if (is_leaf_node())
evt_avg_btree_leaf_node_lock_acquire_spins.offer(spins);
else
evt_avg_btree_internal_node_lock_acquire_spins.offer(spins);
#else
const uint64_t ret = VersionManip::Lock(hdr_);
#endif
#ifdef BTREE_LOCK_OWNERSHIP_CHECKING
lock_owner_ = std::this_thread::get_id();
AddNodeToLockRegion(this);
INVARIANT(is_lock_owner());
#endif
return ret;
}
inline void
unlock()
{
#ifdef BTREE_LOCK_OWNERSHIP_CHECKING
lock_owner_ = std::thread::id();
INVARIANT(!is_lock_owner());
#endif
VersionManip::Unlock(hdr_);
}
inline bool
is_root() const
{
return VersionManip::IsRoot(hdr_);
}
inline void
set_root()
{
VersionManip::SetRoot(hdr_);
}
inline void
clear_root()
{
VersionManip::ClearRoot(hdr_);
}
inline bool
is_modifying() const
{
return VersionManip::IsModifying(hdr_);
}
inline void
mark_modifying()
{
VersionManip::MarkModifying(hdr_);
}
inline bool
is_deleting() const
{
return VersionManip::IsDeleting(hdr_);
}
inline void
mark_deleting()
{
VersionManip::MarkDeleting(hdr_);
}
inline uint64_t
unstable_version() const
{
return VersionManip::UnstableVersion(hdr_);
}
/**
* spin until we get a version which is not modifying (but can be locked)
*/
inline uint64_t
stable_version() const
{
return VersionManip::StableVersion(hdr_);
}
inline bool
check_version(uint64_t version) const
{
return VersionManip::CheckVersion(hdr_, version);
}
inline std::string
version_info_str() const
{
return VersionManip::VersionInfoStr(hdr_);
}
void base_invariant_unique_keys_check() const;
// [min_key, max_key)
void
base_invariant_checker(const key_slice *min_key,
const key_slice *max_key,
bool is_root) const;
/** manually simulated virtual function (so we don't make node virtual) */
void
invariant_checker(const key_slice *min_key,
const key_slice *max_key,
const node *left_sibling,
const node *right_sibling,
bool is_root) const;
/** another manually simulated virtual function */
inline void prefetch() const;
};
struct leaf_node : public node {
union value_or_node_ptr {
value_type v_;
node *n_;
};
key_slice min_key_; // really is min_key's key slice
value_or_node_ptr values_[NKeysPerNode];
// format is:
// [ slice_length | type | unused ]
// [ 0:4 | 4:5 | 5:8 ]
uint8_t lengths_[NKeysPerNode];
leaf_node *prev_;
leaf_node *next_;
// starts out empty- once set, doesn't get freed until dtor (even if all
// keys w/ suffixes get removed)
imstring *suffixes_;
inline ALWAYS_INLINE varkey
suffix(size_t i) const
{
return suffixes_ ? varkey(suffixes_[i]) : varkey();
}
//static event_counter g_evt_suffixes_array_created;
inline void
alloc_suffixes()
{
INVARIANT(this->is_modifying());
INVARIANT(!suffixes_);
suffixes_ = new imstring[NKeysPerNode];
//++g_evt_suffixes_array_created;
}
inline void
ensure_suffixes()
{
INVARIANT(this->is_modifying());
if (!suffixes_)
alloc_suffixes();
}
leaf_node();
~leaf_node();
static const uint64_t LEN_LEN_MASK = 0xf;
static const uint64_t LEN_TYPE_SHIFT = 4;
static const uint64_t LEN_TYPE_MASK = 0x1 << LEN_TYPE_SHIFT;
inline void
prefetch() const
{
#ifdef BTREE_NODE_PREFETCH
prefetch_object(this);
#endif
}
inline size_t
keyslice_length(size_t n) const
{
INVARIANT(n < NKeysPerNode);
return lengths_[n] & LEN_LEN_MASK;
}
inline void
keyslice_set_length(size_t n, size_t len, bool layer)
{
INVARIANT(n < NKeysPerNode);
INVARIANT(this->is_modifying());
INVARIANT(len <= 9);
INVARIANT(!layer || len == 9);
lengths_[n] = (len | (layer ? LEN_TYPE_MASK : 0));
}
inline bool
value_is_layer(size_t n) const
{
INVARIANT(n < NKeysPerNode);
return lengths_[n] & LEN_TYPE_MASK;
}
inline void
value_set_layer(size_t n)
{
INVARIANT(n < NKeysPerNode);
INVARIANT(this->is_modifying());
INVARIANT(keyslice_length(n) == 9);
INVARIANT(!value_is_layer(n));
lengths_[n] |= LEN_TYPE_MASK;
}
/**
* keys[key_search(k).first] == k if key_search(k).first != -1
* key does not exist otherwise. considers key length also
*/
inline key_search_ret
key_search(key_slice k, size_t len) const
{
size_t n = this->key_slots_used();
ssize_t lower = 0;
ssize_t upper = n;
while (lower < upper) {
ssize_t i = (lower + upper) / 2;
key_slice k0 = this->keys_[i];
size_t len0 = this->keyslice_length(i);
if (k0 < k || (k0 == k && len0 < len))
lower = i + 1;
else if (k0 == k && len0 == len)
return key_search_ret(i, n);
else
upper = i;
}
return key_search_ret(-1, n);
}
/**
* tightest lower bound key, -1 if no such key exists. operates only
* on key slices (internal nodes have unique key slices)
*/
inline key_search_ret
key_lower_bound_search(key_slice k, size_t len) const
{
ssize_t ret = -1;
size_t n = this->key_slots_used();
ssize_t lower = 0;
ssize_t upper = n;
while (lower < upper) {
ssize_t i = (lower + upper) / 2;
key_slice k0 = this->keys_[i];
size_t len0 = this->keyslice_length(i);
if (k0 < k || (k0 == k && len0 < len)) {
ret = i;
lower = i + 1;
} else if (k0 == k && len0 == len) {
return key_search_ret(i, n);
} else {
upper = i;
}
}
return key_search_ret(ret, n);
}
void
invariant_checker_impl(const key_slice *min_key,
const key_slice *max_key,
const node *left_sibling,
const node *right_sibling,
bool is_root) const;
static inline leaf_node*
alloc()
{
void * const p = rcu::s_instance.alloc(LeafNodeAllocSize);
INVARIANT(p);
return new (p) leaf_node;
}
static void
deleter(void *p)
{
leaf_node *n = (leaf_node *) p;
INVARIANT(n->is_deleting());
INVARIANT(!n->is_locked());
n->~leaf_node();
rcu::s_instance.dealloc(p, LeafNodeAllocSize);
}
static inline void
release(leaf_node *n)
{
if (unlikely(!n))
return;
n->mark_deleting();
rcu::s_instance.free_with_fn(n, deleter);
}
};
struct internal_node : public node {
/**
* child at position child_idx is responsible for keys
* [keys[child_idx - 1], keys[child_idx])
*
* in the case where child_idx == 0 or child_idx == this->key_slots_used(), then
* the responsiblity value of the min/max key, respectively, is determined
* by the parent
*/
node *children_[NKeysPerNode + 1];
internal_node();
~internal_node();
inline void
prefetch() const
{
#ifdef BTREE_NODE_PREFETCH
prefetch_object(this);
#endif
}
/**
* keys[key_search(k).first] == k if key_search(k).first != -1
* key does not exist otherwise. operates ony on key slices
* (internal nodes have unique key slices)
*/
inline key_search_ret
key_search(key_slice k) const
{
size_t n = this->key_slots_used();
ssize_t lower = 0;
ssize_t upper = n;
while (lower < upper) {
ssize_t i = (lower + upper) / 2;
key_slice k0 = this->keys_[i];
if (k0 == k)
return key_search_ret(i, n);
else if (k0 > k)
upper = i;
else
lower = i + 1;
}
return key_search_ret(-1, n);
}
/**
* tightest lower bound key, -1 if no such key exists. operates only
* on key slices (internal nodes have unique key slices)
*/
inline key_search_ret
key_lower_bound_search(key_slice k) const
{
ssize_t ret = -1;
size_t n = this->key_slots_used();
ssize_t lower = 0;
ssize_t upper = n;
while (lower < upper) {
ssize_t i = (lower + upper) / 2;
key_slice k0 = this->keys_[i];
if (k0 == k)
return key_search_ret(i, n);
else if (k0 > k)
upper = i;
else {
ret = i;
lower = i + 1;
}