-
Notifications
You must be signed in to change notification settings - Fork 117
/
btree.cc
2010 lines (1751 loc) · 57.8 KB
/
btree.cc
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
#include <unistd.h>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <sstream>
#include <atomic>
#include <memory>
#include "core.h"
#include "btree.h"
#include "btree_impl.h"
#include "thread.h"
#include "txn.h"
#include "util.h"
#include "scopedperf.hh"
#if defined(NDB_MASSTREE)
#include "masstree_btree.h"
struct testing_concurrent_btree_traits : public masstree_params {
static const bool RcuRespCaller = false;
};
typedef mbtree<testing_concurrent_btree_traits> testing_concurrent_btree;
#define HAVE_REVERSE_RANGE_SCANS
#else
struct testing_concurrent_btree_traits : public concurrent_btree_traits {
static const bool RcuRespCaller = false;
};
typedef btree<testing_concurrent_btree_traits> testing_concurrent_btree;
#endif
using namespace std;
using namespace util;
class scoped_rate_timer {
private:
util::timer t;
string region;
size_t n;
public:
scoped_rate_timer(const string ®ion, size_t n) : region(region), n(n)
{}
~scoped_rate_timer()
{
double x = t.lap() / 1000.0; // ms
double rate = double(n) / (x / 1000.0);
cerr << "timed region `" << region << "' took " << x
<< " ms (" << rate << " events/sec)" << endl;
}
};
class btree_worker : public ndb_thread {
public:
btree_worker(testing_concurrent_btree *btr) : btr(btr) {}
btree_worker(testing_concurrent_btree &btr) : btr(&btr) {}
protected:
testing_concurrent_btree *const btr;
};
static void
test1()
{
testing_concurrent_btree btr;
btr.invariant_checker();
// fill up root leaf node
for (size_t i = 0; i < testing_concurrent_btree::NKeysPerNode; i++) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == testing_concurrent_btree::NKeysPerNode);
// induce a split
btr.insert(u64_varkey(testing_concurrent_btree::NKeysPerNode), (typename testing_concurrent_btree::value_type) (testing_concurrent_btree::NKeysPerNode));
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == testing_concurrent_btree::NKeysPerNode + 1);
// now make sure we can find everything post split
for (size_t i = 0; i < testing_concurrent_btree::NKeysPerNode + 1; i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
// now fill up the new root node
const size_t n = (testing_concurrent_btree::NKeysPerNode + testing_concurrent_btree::NKeysPerNode * (testing_concurrent_btree::NMinKeysPerNode));
for (size_t i = testing_concurrent_btree::NKeysPerNode + 1; i < n; i++) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == n);
// cause the root node to split
btr.insert(u64_varkey(n), (typename testing_concurrent_btree::value_type) n);
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == n + 1);
// once again make sure we can find everything
for (size_t i = 0; i < n + 1; i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
}
static void
test2()
{
testing_concurrent_btree btr;
const size_t n = 1000;
for (size_t i = 0; i < n; i += 2) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
for (size_t i = 1; i < n; i += 2) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == n);
}
static void
test3()
{
testing_concurrent_btree btr;
for (size_t i = 0; i < testing_concurrent_btree::NKeysPerNode * 2; i++) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == testing_concurrent_btree::NKeysPerNode * 2);
for (size_t i = 0; i < testing_concurrent_btree::NKeysPerNode * 2; i++) {
btr.remove(u64_varkey(i));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
ALWAYS_ASSERT(btr.size() == 0);
for (size_t i = 0; i < testing_concurrent_btree::NKeysPerNode * 2; i++) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == testing_concurrent_btree::NKeysPerNode * 2);
for (ssize_t i = testing_concurrent_btree::NKeysPerNode * 2 - 1; i >= 0; i--) {
btr.remove(u64_varkey(i));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
ALWAYS_ASSERT(btr.size() == 0);
for (size_t i = 0; i < testing_concurrent_btree::NKeysPerNode * 2; i++) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == testing_concurrent_btree::NKeysPerNode * 2);
for (ssize_t i = testing_concurrent_btree::NKeysPerNode; i >= 0; i--) {
btr.remove(u64_varkey(i));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
for (size_t i = testing_concurrent_btree::NKeysPerNode + 1; i < testing_concurrent_btree::NKeysPerNode * 2; i++) {
btr.remove(u64_varkey(i));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
ALWAYS_ASSERT(btr.size() == 0);
}
static void
test4()
{
testing_concurrent_btree btr;
const size_t nkeys = 10000;
for (size_t i = 0; i < nkeys; i++) {
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == nkeys);
srand(12345);
for (size_t i = 0; i < nkeys; i++) {
size_t k = rand() % nkeys;
btr.remove(u64_varkey(k));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(k), v));
}
for (size_t i = 0; i < nkeys; i++) {
btr.remove(u64_varkey(i));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
ALWAYS_ASSERT(btr.size() == 0);
}
static void
test5()
{
// insert in random order, delete in random order
testing_concurrent_btree btr;
unsigned int seeds[] = {
54321, 2013883780, 3028985725, 3058602342, 256561598, 2895653051
};
for (size_t iter = 0; iter < ARRAY_NELEMS(seeds); iter++) {
srand(seeds[iter]);
const size_t nkeys = 20000;
set<size_t> s;
for (size_t i = 0; i < nkeys; i++) {
size_t k = rand() % nkeys;
s.insert(k);
btr.insert(u64_varkey(k), (typename testing_concurrent_btree::value_type) k);
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(k), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) k);
}
ALWAYS_ASSERT(btr.size() == s.size());
for (size_t i = 0; i < nkeys * 2; i++) {
size_t k = rand() % nkeys;
btr.remove(u64_varkey(k));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(k), v));
}
// clean it up
for (size_t i = 0; i < nkeys; i++) {
btr.remove(u64_varkey(i));
btr.invariant_checker();
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
ALWAYS_ASSERT(btr.size() == 0);
}
}
namespace test6_ns {
struct scan_callback {
typedef vector<
pair< std::string, // we want to make copies of keys
typename testing_concurrent_btree::value_type > > kv_vec;
scan_callback(kv_vec *data, bool reverse = false)
: data(data), reverse_(reverse) {}
inline bool
operator()(const typename testing_concurrent_btree::string_type &k,
typename testing_concurrent_btree::value_type v) const
{
if (!data->empty()) {
const bool geq =
typename testing_concurrent_btree::string_type(data->back().first) >= k;
const bool leq =
typename testing_concurrent_btree::string_type(data->back().first) <= k;
if ((!reverse_ && geq) || (reverse_ && leq)) {
cerr << "data->size(): " << data->size() << endl;
cerr << "prev: " << varkey(data->back().first) << endl;
cerr << "cur : " << varkey(k) << endl;
ALWAYS_ASSERT(false);
}
}
data->push_back(make_pair(k, v));
return true;
}
kv_vec *data;
bool reverse_;
};
}
static void
test6()
{
testing_concurrent_btree btr;
const size_t nkeys = 1000;
for (size_t i = 0; i < nkeys; i++)
btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == nkeys);
using namespace test6_ns;
scan_callback::kv_vec data;
scan_callback cb(&data);
u64_varkey max_key(600);
btr.search_range(u64_varkey(500), &max_key, cb);
ALWAYS_ASSERT(data.size() == 100);
for (size_t i = 0; i < 100; i++) {
const varkey lhs(data[i].first), rhs(u64_varkey(500 + i));
ALWAYS_ASSERT(lhs == rhs);
ALWAYS_ASSERT(data[i].second == (typename testing_concurrent_btree::value_type) (500 + i));
}
data.clear();
btr.search_range(u64_varkey(500), NULL, cb);
ALWAYS_ASSERT(data.size() == 500);
for (size_t i = 0; i < 500; i++) {
ALWAYS_ASSERT(varkey(data[i].first) == u64_varkey(500 + i));
ALWAYS_ASSERT(data[i].second == (typename testing_concurrent_btree::value_type) (500 + i));
}
#ifdef HAVE_REVERSE_RANGE_SCANS
data.clear();
scan_callback cb_rev(&data, true);
btr.rsearch_range(u64_varkey(499), NULL, cb_rev);
ALWAYS_ASSERT(data.size() == 500);
for (ssize_t i = 499; i >= 0; i--) {
ALWAYS_ASSERT(varkey(data[499 - i].first) == u64_varkey(i));
ALWAYS_ASSERT(data[499 - i].second == (typename testing_concurrent_btree::value_type) (i));
}
data.clear();
u64_varkey min_key(499);
btr.rsearch_range(u64_varkey(999), &min_key, cb_rev);
ALWAYS_ASSERT(data.size() == 500);
for (ssize_t i = 999; i >= 500; i--) {
ALWAYS_ASSERT(varkey(data[999 - i].first) == u64_varkey(i));
ALWAYS_ASSERT(data[999 - i].second == (typename testing_concurrent_btree::value_type) (i));
}
#endif
}
static void
test7()
{
testing_concurrent_btree btr;
ALWAYS_ASSERT(!btr.remove(u64_varkey(0)));
ALWAYS_ASSERT(btr.insert(u64_varkey(0), (typename testing_concurrent_btree::value_type) 0));
ALWAYS_ASSERT(!btr.insert(u64_varkey(0), (typename testing_concurrent_btree::value_type) 1));
typename testing_concurrent_btree::value_type v;
ALWAYS_ASSERT(btr.search(u64_varkey(0), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) 1);
ALWAYS_ASSERT(!btr.insert_if_absent(u64_varkey(0), (typename testing_concurrent_btree::value_type) 2));
ALWAYS_ASSERT(btr.search(u64_varkey(0), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) 1);
ALWAYS_ASSERT(btr.remove(u64_varkey(0)));
ALWAYS_ASSERT(btr.insert_if_absent(u64_varkey(0), (typename testing_concurrent_btree::value_type) 2));
ALWAYS_ASSERT(btr.search(u64_varkey(0), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) 2);
}
static void
test_varlen_single_layer()
{
testing_concurrent_btree btr;
const char *k0 = "a";
const char *k1 = "aa";
const char *k2 = "aaa";
const char *k3 = "aaaa";
const char *k4 = "aaaaa";
const char *keys[] = {k0, k1, k2, k3, k4};
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
ALWAYS_ASSERT(btr.insert(varkey(keys[i]), (typename testing_concurrent_btree::value_type) keys[i]));
btr.invariant_checker();
}
ALWAYS_ASSERT(btr.size() == ARRAY_NELEMS(keys));
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(varkey(keys[i]), v));
ALWAYS_ASSERT(strcmp((const char *) v, keys[i]) == 0);
}
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
ALWAYS_ASSERT(btr.remove(varkey(keys[i])));
btr.invariant_checker();
}
ALWAYS_ASSERT(btr.size() == 0);
}
static void
test_varlen_multi_layer()
{
testing_concurrent_btree btr;
const char *k0 = "aaaaaaa";
const char *k1 = "aaaaaaaa";
const char *k2 = "aaaaaaaaa";
const char *k3 = "aaaaaaaaaa";
const char *k4 = "aaaaaaaaaaa";
const char *k5 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char *keys[] = {k0, k1, k2, k3, k4, k5};
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
ALWAYS_ASSERT(btr.insert(varkey(keys[i]), (typename testing_concurrent_btree::value_type) keys[i]));
btr.invariant_checker();
}
ALWAYS_ASSERT(btr.size() == ARRAY_NELEMS(keys));
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(varkey(keys[i]), v));
ALWAYS_ASSERT(strcmp((const char *) v, keys[i]) == 0);
}
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
ALWAYS_ASSERT(btr.remove(varkey(keys[i])));
btr.invariant_checker();
}
ALWAYS_ASSERT(btr.size() == 0);
}
static void
test_two_layer()
{
const char *k0 = "aaaaaaaaa";
const char *k1 = "aaaaaaaaaa";
testing_concurrent_btree btr;
ALWAYS_ASSERT(btr.insert(varkey(k0), (typename testing_concurrent_btree::value_type) k0));
ALWAYS_ASSERT(btr.insert(varkey(k1), (typename testing_concurrent_btree::value_type) k1));
ALWAYS_ASSERT(btr.size() == 2);
}
static __attribute__((used)) void test_ensure_printable() {
testing_concurrent_btree btr;
btr.print();
}
class test_range_scan_helper : public testing_concurrent_btree::search_range_callback {
public:
struct expect {
expect() : tag(), expected_size() {}
expect(size_t expected_size)
: tag(0), expected_size(expected_size) {}
expect(const set<string> &expected_keys)
: tag(1), expected_keys(expected_keys) {}
uint8_t tag;
size_t expected_size;
set<string> expected_keys;
};
enum ExpectType {
EXPECT_EXACT,
EXPECT_ATLEAST,
};
test_range_scan_helper(
testing_concurrent_btree &btr,
const testing_concurrent_btree::key_type &begin,
const testing_concurrent_btree::key_type *end,
bool reverse,
const expect &expectation,
ExpectType ex_type = EXPECT_EXACT)
: btr(&btr),
begin(begin),
end(end ? new testing_concurrent_btree::key_type(*end) : NULL),
reverse_(reverse),
expectation(expectation),
ex_type(ex_type)
{
}
~test_range_scan_helper()
{
if (end)
delete end;
}
virtual bool
invoke(const typename testing_concurrent_btree::string_type &k,
typename testing_concurrent_btree::value_type v)
{
VERBOSE(cerr << "test_range_scan_helper::invoke(): received key(size="
<< k.size() << "): " << hexify(k) << endl);
if (!keys.empty()) {
if (!reverse_)
ALWAYS_ASSERT(typename testing_concurrent_btree::string_type(keys.back()) < k);
else
ALWAYS_ASSERT(typename testing_concurrent_btree::string_type(keys.back()) > k);
}
keys.push_back(k);
return true;
}
void test()
{
keys.clear();
if (!reverse_)
btr->search_range_call(begin, end, *this);
else
btr->rsearch_range_call(begin, end, *this);
if (expectation.tag == 0) {
switch (ex_type) {
case EXPECT_EXACT:
ALWAYS_ASSERT(keys.size() == expectation.expected_size);
break;
case EXPECT_ATLEAST:
ALWAYS_ASSERT(keys.size() >= expectation.expected_size);
break;
}
} else {
switch (ex_type) {
case EXPECT_EXACT: {
ALWAYS_ASSERT(keys.size() == expectation.expected_keys.size());
vector<string> cmp;
if (!reverse_)
cmp.assign(expectation.expected_keys.begin(), expectation.expected_keys.end());
else
cmp.assign(expectation.expected_keys.rbegin(), expectation.expected_keys.rend());
for (size_t i = 0; i < keys.size(); i++) {
if (keys[i] != cmp[i]) {
cerr << "A: " << hexify(keys[i]) << endl;
cerr << "B: " << hexify(cmp[i]) << endl;
ALWAYS_ASSERT(false);
}
}
break;
}
case EXPECT_ATLEAST: {
ALWAYS_ASSERT(keys.size() >= expectation.expected_keys.size());
// every key in the expected set must be present
set<string> keyset(keys.begin(), keys.end());
for (auto it = expectation.expected_keys.begin();
it != expectation.expected_keys.end(); ++it)
ALWAYS_ASSERT(keyset.count(*it) == 1);
break;
}
}
}
}
private:
testing_concurrent_btree *const btr;
testing_concurrent_btree::key_type begin;
testing_concurrent_btree::key_type *end;
bool reverse_;
expect expectation;
ExpectType ex_type;
vector<string> keys;
};
static void
test_two_layer_range_scan()
{
const char *keys[] = {
"a",
"aaaaaaaa",
"aaaaaaaaa",
"aaaaaaaaaa",
"aaaaaaaaaaa",
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s",
};
testing_concurrent_btree btr;
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
ALWAYS_ASSERT(btr.insert(varkey(keys[i]), (typename testing_concurrent_btree::value_type) keys[i]));
btr.invariant_checker();
}
test_range_scan_helper::expect ex(set<string>(keys, keys + ARRAY_NELEMS(keys)));
test_range_scan_helper tester(btr, varkey(""), NULL, false, ex);
tester.test();
#ifdef HAVE_REVERSE_RANGE_SCANS
test_range_scan_helper tester_rev(btr, varkey("zzzzzzzzzzzzzzzzzzzzzz"), NULL, true, ex);
tester_rev.test();
#endif
}
static void
test_multi_layer_scan()
{
const uint8_t lokey_cstr[] = {
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x45, 0x49, 0x4E, 0x47,
0x41, 0x54, 0x49, 0x4F, 0x4E, 0x45, 0x49, 0x4E, 0x47, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
const uint8_t hikey_cstr[] = {
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x45, 0x49, 0x4E, 0x47,
0x41, 0x54, 0x49, 0x4F, 0x4E, 0x45, 0x49, 0x4E, 0x47, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
const string lokey_s((const char *) &lokey_cstr[0], ARRAY_NELEMS(lokey_cstr));
const string hikey_s((const char *) &hikey_cstr[0], ARRAY_NELEMS(hikey_cstr));
string lokey_s_next(lokey_s);
lokey_s_next.resize(lokey_s_next.size() + 1);
const varkey hikey(hikey_s);
testing_concurrent_btree btr;
ALWAYS_ASSERT(btr.insert(varkey(lokey_s), (typename testing_concurrent_btree::value_type) 0x123));
test_range_scan_helper::expect ex(0);
test_range_scan_helper tester(btr, varkey(lokey_s_next), &hikey, false, ex);
tester.test();
#ifdef HAVE_REVERSE_RANGE_SCANS
const varkey lokey(lokey_s);
test_range_scan_helper tester_rev(btr, varkey(hikey_s), &lokey, true, ex);
tester_rev.test();
#endif
}
static void
test_null_keys()
{
const uint8_t k0[] = {};
const uint8_t k1[] = {'\0'};
const uint8_t k2[] = {'\0', '\0'};
const uint8_t k3[] = {'\0', '\0', '\0'};
const uint8_t k4[] = {'\0', '\0', '\0', '\0'};
const uint8_t k5[] = {'\0', '\0', '\0', '\0', '\0'};
const uint8_t k6[] = {'\0', '\0', '\0', '\0', '\0', '\0'};
const uint8_t k7[] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0'};
const uint8_t k8[] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
const uint8_t k9[] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
const uint8_t k10[] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
const uint8_t *keys[] = {k0, k1, k2, k3, k4, k5, k6, k7, k8, k9, k10};
testing_concurrent_btree btr;
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
ALWAYS_ASSERT(btr.insert(varkey(keys[i], i), (typename testing_concurrent_btree::value_type) i));
btr.invariant_checker();
}
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(varkey(keys[i], i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
for (size_t i = 1; i <= 20; i++) {
ALWAYS_ASSERT(btr.insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i));
btr.invariant_checker();
}
for (size_t i = 0; i < ARRAY_NELEMS(keys); i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(varkey(keys[i], i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
for (size_t i = 1; i <= 20; i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
}
static void
test_null_keys_2()
{
const size_t nprefixes = 200;
testing_concurrent_btree btr;
fast_random r(9084398309893);
set<string> prefixes;
for (size_t i = 0; i < nprefixes; i++) {
retry:
const string k(r.next_string(r.next() % 30));
if (prefixes.count(k) == 1)
goto retry;
prefixes.insert(k);
}
set<string> keys;
for (auto &prefix : prefixes) {
for (size_t i = 1; i <= 12; i++) {
std::string x(prefix);
x.resize(x.size() + i);
keys.insert(x);
}
}
size_t ctr = 1;
for (auto it = keys.begin(); it != keys.end(); ++it, ++ctr) {
ALWAYS_ASSERT(btr.insert(varkey(*it), (typename testing_concurrent_btree::value_type) it->data()));
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == ctr);
}
ALWAYS_ASSERT(btr.size() == keys.size());
for (auto it = keys.begin(); it != keys.end(); ++it) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(varkey(*it), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) it->data());
}
test_range_scan_helper::expect ex(keys);
test_range_scan_helper tester(btr, varkey(*keys.begin()), NULL, false, ex);
tester.test();
#ifdef HAVE_REVERSE_RANGE_SCANS
test_range_scan_helper tester_rev(btr, varkey(*keys.rbegin()), NULL, true, ex);
tester_rev.test();
#endif
ctr = keys.size() - 1;
for (auto it = keys.begin(); it != keys.end(); ++it, --ctr) {
ALWAYS_ASSERT(btr.remove(varkey(*it)));
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == ctr);
}
ALWAYS_ASSERT(btr.size() == 0);
}
static inline string
maxkey(unsigned size)
{
return string(size, 255);
}
static void
test_random_keys()
{
testing_concurrent_btree btr;
fast_random r(43698);
const size_t nkeys = 10000;
const unsigned int maxkeylen = 1000;
set<string> keyset;
vector<string> keys;
keys.resize(nkeys);
for (size_t i = 0; i < nkeys; i++) {
retry:
string k = r.next_readable_string(r.next() % (maxkeylen + 1));
if (keyset.count(k) == 1)
goto retry;
keyset.insert(k);
swap(keys[i], k);
btr.insert(varkey(keys[i]), (typename testing_concurrent_btree::value_type) keys[i].data());
btr.invariant_checker();
}
ALWAYS_ASSERT(btr.size() == keyset.size());
for (size_t i = 0; i < nkeys; i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(varkey(keys[i]), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) keys[i].data());
}
test_range_scan_helper::expect ex(keyset);
test_range_scan_helper tester(btr, varkey(""), NULL, false, ex);
tester.test();
#ifdef HAVE_REVERSE_RANGE_SCANS
const string mkey = maxkey(maxkeylen);
test_range_scan_helper tester_rev(btr, varkey(mkey), NULL, true, ex);
tester_rev.test();
#endif
for (size_t i = 0; i < nkeys; i++) {
btr.remove(varkey(keys[i]));
btr.invariant_checker();
}
ALWAYS_ASSERT(btr.size() == 0);
}
static void
test_insert_remove_mix()
{
testing_concurrent_btree btr;
fast_random r(38953623328597);
// bootstrap with keys, then alternate insert/remove
const size_t nkeys_start = 100000;
vector<string> start_keys_v;
set<string> start_keys;
for (size_t i = 0; i < nkeys_start; i++) {
retry:
string k = r.next_readable_string(r.next() % 200);
if (start_keys.count(k) == 1)
goto retry;
start_keys_v.push_back(k);
start_keys.insert(k);
ALWAYS_ASSERT(btr.insert(varkey(k), (typename testing_concurrent_btree::value_type) k.data()));
}
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == start_keys.size());
vector<string> insert_keys_v;
set<string> insert_keys;
for (size_t i = 0; i < nkeys_start; i++) {
retry1:
string k = r.next_readable_string(r.next() % 200);
if (start_keys.count(k) == 1 || insert_keys.count(k) == 1)
goto retry1;
insert_keys_v.push_back(k);
insert_keys.insert(k);
}
for (size_t i = 0; i < nkeys_start; i++) {
ALWAYS_ASSERT(btr.remove(varkey(start_keys_v[i])));
ALWAYS_ASSERT(btr.insert(varkey(insert_keys_v[i]), (typename testing_concurrent_btree::value_type) insert_keys_v[i].data()));
}
btr.invariant_checker();
ALWAYS_ASSERT(btr.size() == insert_keys.size());
}
namespace mp_test1_ns {
static const size_t nkeys = 20000;
class ins0_worker : public btree_worker {
public:
ins0_worker(testing_concurrent_btree &btr) : btree_worker(btr) {}
virtual void run()
{
for (size_t i = 0; i < nkeys / 2; i++)
btr->insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
}
};
class ins1_worker : public btree_worker {
public:
ins1_worker(testing_concurrent_btree &btr) : btree_worker(btr) {}
virtual void run()
{
for (size_t i = nkeys / 2; i < nkeys; i++)
btr->insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
}
};
}
static void
mp_test1()
{
using namespace mp_test1_ns;
// test a bunch of concurrent inserts
testing_concurrent_btree btr;
ins0_worker w0(btr);
ins1_worker w1(btr);
w0.start(); w1.start();
w0.join(); w1.join();
btr.invariant_checker();
for (size_t i = 0; i < nkeys; i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(btr.search(u64_varkey(i), v));
ALWAYS_ASSERT(v == (typename testing_concurrent_btree::value_type) i);
}
ALWAYS_ASSERT(btr.size() == nkeys);
}
namespace mp_test2_ns {
static const size_t nkeys = 20000;
class rm0_worker : public btree_worker {
public:
rm0_worker(testing_concurrent_btree &btr) : btree_worker(btr) {}
virtual void run()
{
for (size_t i = 0; i < nkeys / 2; i++)
btr->remove(u64_varkey(i));
}
};
class rm1_worker : public btree_worker {
public:
rm1_worker(testing_concurrent_btree &btr) : btree_worker(btr) {}
virtual void run()
{
for (size_t i = nkeys / 2; i < nkeys; i++)
btr->remove(u64_varkey(i));
}
};
}
static void
mp_test2()
{
using namespace mp_test2_ns;
// test a bunch of concurrent removes
testing_concurrent_btree btr;
for (size_t i = 0; i < nkeys; i++)
btr.insert(u64_varkey(u64_varkey(i)), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
rm0_worker w0(btr);
rm1_worker w1(btr);
w0.start(); w1.start();
w0.join(); w1.join();
btr.invariant_checker();
for (size_t i = 0; i < nkeys; i++) {
typename testing_concurrent_btree::value_type v = 0;
ALWAYS_ASSERT(!btr.search(u64_varkey(i), v));
}
ALWAYS_ASSERT(btr.size() == 0);
}
namespace mp_test3_ns {
static const size_t nkeys = 20000;
class rm0_worker : public btree_worker {
public:
rm0_worker(testing_concurrent_btree &btr) : btree_worker(btr) {}
virtual void run()
{
// remove the even keys
for (size_t i = 0; i < nkeys; i += 2)
btr->remove(u64_varkey(i));
}
};
class ins0_worker : public btree_worker {
public:
ins0_worker(testing_concurrent_btree &btr) : btree_worker(btr) {}
virtual void run()
{
// insert the odd keys
for (size_t i = 1; i < nkeys; i += 2)
btr->insert(u64_varkey(i), (typename testing_concurrent_btree::value_type) i);
}
};
}
static void
mp_test3()
{
using namespace mp_test3_ns;
// test a bunch of concurrent inserts and removes
testing_concurrent_btree btr;
// insert the even keys
for (size_t i = 0; i < nkeys; i += 2)
btr.insert(u64_varkey(u64_varkey(i)), (typename testing_concurrent_btree::value_type) i);
btr.invariant_checker();
rm0_worker w0(btr);
ins0_worker w1(btr);
w0.start(); w1.start();