-
Notifications
You must be signed in to change notification settings - Fork 16
/
all_tree.hpp
1072 lines (924 loc) · 27.8 KB
/
all_tree.hpp
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
// #define CXL
#ifdef USE_FPTREE
#include "fptree.h"
#elif USE_NBTREE
#include "nbtree.h"
#elif USE_FASTFAIR
#ifdef CXL
#include "fastfair_dram.h"
#else
#include "fastfair.h"
#endif
#elif USE_ROART
#include "Tree.h"
#include "threadinfo.h"
#elif USE_DPTREE
#include "concur_dptree.hpp"
#include "btreeolc.hpp"
#elif USE_UTREE
#include "utree.h"
#elif USE_PACTREE
#include "pactree.h"
#include <numa-config.h>
#include "pactreeImpl.h"
#include "common.h"
#elif USE_BZTREE
#include "bztree.h"
#else
#include "zbtree.h"
#endif
#include <string>
#include <iostream>
#include <fstream>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef USE_FASTFAIR
using namespace std;
#endif
#ifdef USE_ROART
using namespace PART_ns;
const char* pool_path_ = "/mnt/pmem/roart";
size_t pool_size_ = 1024*1024*1024*32UL;
#endif
#ifdef USE_DPTREE
int parallel_merge_worker_num;
const char* pool_path_ = "/mnt/pmem/dptree";
#ifdef LONG_VAL
size_t pool_size_ = 1024*1024*1024*128UL;
#else
size_t pool_size_ = 1024*1024*1024*32UL;
#endif
#endif
#ifdef USE_PACTREE
std::atomic<uint64_t> dram_allocated(0);
std::atomic<uint64_t> pmem_allocated(0);
std::atomic<uint64_t> dram_freed(0);
std::atomic<uint64_t> pmem_freed(0);
size_t pool_size_ = 1024*1024*1024*32ULL;
std::string *pool_dir_ = new std::string("/mnt/pmem");
#endif
// #define LONG_VAL
int val_len;
size_t pm_pool_size;
int run_thread;
class Tree_api{
public:
virtual ~Tree_api(){};
virtual bool find(size_t key, size_t sz) = 0;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) = 0;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) = 0;
virtual bool remove(size_t key, size_t key_sz) = 0;
/**
* @brief Scan records starting from record with given key.
*
* @param[in] key Pointer to the beginning of key of first record.
* @param[in] key_sz Size of key in bytes of first record.
* @param[in] scan_sz Amount of following records to be scanned.
* @param[out] values_out Pointer to location of scanned records.
* @return int Amount of records scanned.
*
* The implementation of scan must set 'values_out' internally to point to
* a memory region containing the resulting records. The wrapper must
* guarantee that this memory region is not deallocated and that access to
* it is protected (i.e., not modified by other threads). The expected
* contents of the memory is a contiguous sequence of <key><value>
* representing the scanned records in ascending key order.
*
* A simple implementation of the scan method could be something like:
*
* static thread_local std::vector<std::pair<K,V>> results;
* results.clear();
*
* auto it = tree.lower_bound(key);
*
* int scanned;
* for(scanned=0; (scanned < scan_sz) && (it != map_.end()); ++scanned,++it)
* results.push_back(std::make_pair(it->first, it->second));
*
* values_out = results.data();
* return scanned;
*/
virtual int scan(size_t key, size_t key_sz, int scan_sz) = 0;
};
#ifdef USE_FPTREE
class FingerTree : public Tree_api{
public:
FingerTree();
virtual ~FingerTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
FPtree fp_tree;
};
FingerTree::FingerTree(){
}
FingerTree::~FingerTree(){
}
bool FingerTree::find(size_t key, size_t sz){
uint64_t result = 0;
result = fp_tree.find(key);
#ifdef LONG_VAL
char buffer[val_len];
memcpy(buffer, (char*)result, sz);
if(buffer[0] == ' ')
return false;
else
return true;
#else
if(result != 0)
return true;
else
{
//std::cout<<"key "<<key<<"Not found! "<<std::endl;
return false;
}
#endif
}
bool FingerTree::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
#ifdef LONG_VAL
PMEMoid val_addr;
pmemobj_alloc(pop, &val_addr, value_sz, 0, nullptr, nullptr);
pmemobj_memcpy_persist(pop, pmemobj_direct(val_addr), value, value_sz);
uint64_t long_val = (uint64_t) pmemobj_direct(val_addr);
KV kv = KV(key, long_val);
#else
KV kv =KV(key, (uint64_t)value);
#endif
return fp_tree.insert(kv);
}
bool FingerTree::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
KV kv = KV(key, (uint64_t)value);
return fp_tree.update(kv);
}
bool FingerTree::remove(size_t key, size_t key_sz){
return fp_tree.deleteKey(key);
}
int FingerTree::scan(size_t key, size_t key_sz, int scan_sz){
return fp_tree.rangeScan(key, scan_sz, nullptr);
}
#elif USE_NBTREE
class NBTree : public Tree_api{
public:
NBTree();
virtual ~NBTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
btree* nbtree;
};
NBTree::NBTree(){
#ifdef LONG_VAL
size_t pool_size_ = 1024*1024*1024*200UL;
#else
size_t pool_size_ = 1024*1024*1024*64UL;
#endif
const char* pool_path_ = "/mnt/pmem/nbtree";
nbpop = pmemobj_create(pool_path_, "nbtree", pool_size_, 0666);
#ifdef USE_PMDK
#elif USE_NOALC
int fd = open(pool_path_, O_RDWR|O_CREAT, 0666);
if (fd < 0)
{
printf("[NVM MGR]\tfailed to open nvm file\n");
exit(-1);
}
if (ftruncate(fd, pool_size_) < 0)
{
printf("[NVM MGR]\tfailed to truncate file\n");
exit(-1);
}
void *pmem = mmap(NULL, pool_size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
memset(pmem, 0, pool_size_);
heap_start = (char *)pmem;
temp_start = heap_start;
#endif
nbtree = new btree();
}
NBTree::~NBTree(){
delete nbtree;
}
bool NBTree::find(size_t key, size_t sz){
#ifdef VAR_KEY
#endif
char* value = nbtree->search(key);
#ifdef LONG_VAL
char buffer[val_len];
//codec
size_t value_64 = (size_t) value;
size_t value_48 = value_64 & 0xffffffffffff;
memcpy(buffer, (char*)value_48, val_len);
if(buffer[0] == ' ')
{
return false;
}
else
return true;
#else
if(value == nullptr)
{
return false;
}
return true;
#endif
}
bool NBTree::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
#ifdef LONG_VAL
PMEMoid val_addr;
pmemobj_alloc(nbpop, &val_addr, value_sz, 0, nullptr, nullptr);
void* real_addr = pmemobj_direct(val_addr);
memcpy(real_addr, value, value_sz);
pmem_persist(real_addr, value_sz);
return nbtree->insert(key, (char*)real_addr);
#endif
return nbtree->insert(key, (char*)value);
}
bool NBTree::remove(size_t key, size_t key_sz){
return nbtree->remove(key);
}
bool NBTree::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
return nbtree->update(key, (char*)value);
}
int NBTree::scan(size_t key, size_t key_sz, int scan_sz){
return 1;
}
#elif USE_FASTFAIR
class FastTree:public Tree_api{
public:
FastTree();
virtual ~FastTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
#ifdef CXL
btree bt;
#else
TOID(btree) bt = TOID_NULL(btree);
PMEMobjpool *pop;
#endif
};
FastTree::FastTree(){
#ifdef CXL
std::cout<<"init cxl fast fair version"<<std::endl;
#else
#ifndef LONG_VAL
pop = pmemobj_create("/mnt/pmem/fast", "btree", 1024*1024*1024*64UL,
0666); // make 1GB memory pool
#else
pop = pmemobj_create("/mnt/pmem/fast", "btree", 1024*1024*1024*128UL, 0666);
#endif
bt = POBJ_ROOT(pop, btree);
D_RW(bt)->constructor(pop);
#endif
}
FastTree::~FastTree(){
#ifndef CXL
D_RW(bt)->show_space();
#endif
}
bool FastTree::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
#ifdef LONG_VAL
PMEMoid pmem_ptr;
pmemobj_alloc(pop, &pmem_ptr, value_sz, 0, nullptr, nullptr);
void* normal_ptr = pmemobj_direct(pmem_ptr);
memcpy(normal_ptr, value, value_sz);
pmem_persist(normal_ptr, value_sz);
D_RW(bt)->btree_insert(key, (char*)normal_ptr);
return true;
#endif
#ifdef CXL
bt.btree_insert(key, const_cast<char*>(value));
#else
D_RW(bt)->btree_insert(key, const_cast<char*>(value));
#endif
return true;
}
bool FastTree::find(size_t key, size_t key_sz){
#ifdef LONG_VAL
char buffer[val_len];
char* value = D_RW(bt)->btree_search(key);
if(value == nullptr)
return false;
memcpy(buffer, value, val_len);
return true;
#endif
#ifdef CXL
char* value = bt.btree_search(key);
if(value == nullptr)
return false;
return true;
#else
char* value = D_RW(bt)->btree_search(key);
if(value == nullptr)
return false;
return true;
#endif
}
bool FastTree::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
#ifndef CXL
D_RW(bt)->btree_insert(key, const_cast<char*>(value));
return true;
#endif
//TODO
}
bool FastTree::remove(size_t key, size_t key_sz){
#ifndef CXL
D_RW(bt)->btree_delete(key);
return true;
#endif
}
int FastTree::scan(size_t key, size_t key_sz, int scan_sz){
size_t scan_array[scan_sz+100];
//D_RW(bt)->btree_search_range(key, ~0ULL>>1, scan_array, scan_sz);
return 1;
}
#elif USE_ROART
struct ThreadHelper
{
ThreadHelper()
{
NVMMgr_ns::register_threadinfo();
}
~ThreadHelper()
{
// NVMMgr_ns::unregister_threadinfo();
}
};
class Roart:public Tree_api{
public:
Roart();
virtual ~Roart();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
Tree roart;
//std::unordered_map<int, ThreadHelper*> worker;
};
struct KV
{
uint64_t key;
uint64_t val;
};
Roart::Roart(){
}
Roart::~Roart(){
}
bool Roart::find(size_t key, size_t sz){
char value_out[val_len];
thread_local ThreadHelper t;
// int worker_id = gettid();
// if(worker[worker_id] == nullptr)
// {
// ThreadHelper* new_worker = new ThreadHelper();
// worker[worker_id] = new_worker;
// }
Key k;
#ifndef VAR_TEST
k.Init((char*)&key, sz, const_cast<char*>(value_out), 8);
#else
k.Init((char*)key, 8, const_cast<char*>(value_out), sz);
#endif
auto leaf = roart.lookup(&k);
if (leaf != nullptr)
{
return true;
}
#ifdef DEBUG_MSG
std::cout << "Key not found!\n";
#endif
return false;
/*#ifdef LONG_KEY
Key k;
k.Init(const_cast<char*>(key), key_sz, const_cast<char*>(value_out), 8);
#else
#ifdef KEY_INLINE
Key k = Key(*reinterpret_cast<const uint64_t*>(key), key_sz, 0);
#else
Key k;
k.Init(const_cast<char*>(key), key_sz, const_cast<char*>(value_out), 8);
#endif
#endif
auto leaf = roart.lookup(&k);
if (leaf != nullptr)
{
memcpy(value_out, leaf->GetValue(), key_sz);
return true;
}
#ifdef DEBUG_MSG
std::cout << "Key not found!\n";
#endif
return false;*/// 8 B6 E7 B8 5B EE 3E 9F
}
bool Roart::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
thread_local ThreadHelper t;
// int worker_id = gettid();
// if(worker[worker_id] == nullptr)
// {
// ThreadHelper* new_worker = new ThreadHelper();
// worker[worker_id] = new_worker;
// }
Key k;
//char* str_key = new char [9];
//memcpy(str_key, (char*)&key, 8);
#ifndef VAR_TEST
k.Init((char*)&key, key_sz, const_cast<char*>(value), value_sz);
#else
k.Init((char*)key, key_sz, const_cast<char*>(value), value_sz);
#endif
//Key k = Key(key, key_sz, *reinterpret_cast<const uint64_t*>(value));
Tree::OperationResults result = roart.insert(&k);
if (result != Tree::OperationResults::Success)
{
#ifdef DEBUG_MSG
std::cout << "Insert failed!\n";
#endif
return false;
}
return true;
/*#ifdef LONG_KEY
Key k;
k.Init(const_cast<char*>(key), key_sz, const_cast<char*>(value), value_sz);
#else
#ifdef KEY_INLINE
Key k = Key(*reinterpret_cast<const uint64_t*>(key), key_sz, *reinterpret_cast<const uint64_t*>(value));
#else
Key k;
k.Init(const_cast<char*>(key), key_sz, const_cast<char*>(value), value_sz);
#endif*/
//#endif
}
bool Roart::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
thread_local ThreadHelper t;
Key k;
k.Init((char*)&key, key_sz, const_cast<char*>(value), value_sz);
Tree::OperationResults result = roart.update(&k);
if(result != Tree::OperationResults::Success)
return false;
return true;
}
bool Roart::remove(size_t key, size_t key_sz){
thread_local ThreadHelper t;
Key k;
char value_out[8];
k.Init((char*)&key, key_sz, const_cast<char*>(value_out), 8);
Tree::OperationResults result = roart.remove(&k);
if(result != Tree::OperationResults::Success)
return false;
return true;
}
int Roart::scan(size_t key, size_t key_sz, int scan_sz){
thread_local ThreadHelper t;
constexpr size_t ONE_MB = 1ULL << 20;
static thread_local char results[ONE_MB];
size_t scanned = 0;
uint64_t max = (uint64_t)-1;
Key k, end_k;
#ifdef VAR_TEST
k.Init((char*)key, key_sz, (char*)key, key_sz);
#else
k.Init((char*)&key, key_sz, (char*)&key, key_sz);
#endif
end_k.Init((char*)&max, key_sz, (char*)&max, key_sz);
roart.lookupRange(&k, &end_k, nullptr, (PART_ns::Leaf**)&results, scan_sz, scanned);
// auto arr = (KV*)results;
// std::sort(arr, arr + scanned, [] (const KV l1, const KV l2) {
// return l1.key < l2.key;
// });
#ifdef DEBUG_MSG
if (scanned != 100)
printf("%d records scanned.\n", scanned);
#endif
return scanned;
}
#elif USE_DPTREE
class DPTree : public Tree_api{
public:
DPTree();
virtual ~DPTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
dptree::concur_dptree<uint64_t, uint64_t> my_dptree;
};
DPTree::DPTree(){
std::cout<<"Init dptree!"<<std::endl;
}
DPTree::~DPTree(){
}
bool DPTree::find(size_t key, size_t key_sz){
size_t val=0;
my_dptree.lookup(key, val);
if(val != 0)
{
return true;
}
return false;
}
bool DPTree::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
size_t k = key;
size_t val = *reinterpret_cast<size_t*>(const_cast<char*>(value));
my_dptree.insert(k, val);
return true;
}
bool DPTree::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
size_t k = key;
size_t val = *reinterpret_cast<size_t*>(const_cast<char*>(value));
my_dptree.upsert(k, val);
return true;
}
bool DPTree::remove(size_t key, size_t key_sz){
size_t k = key;
//return my_dptree.remove(k);
}
int DPTree::scan(size_t key, size_t key_sz, int scan_sz){
static thread_local std::vector<uint64_t> v(scan_sz*2);
v.clear();
my_dptree.scan(key, scan_sz, v);
scan_sz = v.size() / 2;
#ifdef DEBUG_MSG
if (scan_sz != 100)
printf("%d records scanned!\n", scan_sz);
#endif
return scan_sz;
}
#elif USE_UTREE
class UTree : public Tree_api{
public:
UTree();
virtual ~UTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
btree my_utree;
};
UTree::UTree(){
}
UTree::~UTree(){
}
bool UTree::find(size_t key, size_t sz){
char* value = my_utree.search(key, sz);
#ifdef LONG_VAL
char buffer[val_len];
memcpy(buffer, value, val_len);
if(buffer[0] == ' ')
{
return false;
}
return true;
#else
if(value == nullptr)
return false;
return true;
#endif
}
bool UTree::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
#ifdef LONG_VAL
my_utree.insert(key, const_cast<char*>(value), value_sz);
#else
my_utree.insert(key, const_cast<char*>(value));
#endif
return true;
}
bool UTree::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
my_utree.update(key, const_cast<char*>(value));
return true;
}
bool UTree::remove(size_t key, size_t key_sz){
my_utree.remove(key);
return true;
}
int UTree::scan(size_t key, size_t key_sz, int scan_sz){
size_t scan_array[scan_sz];
return my_utree.scan(key, scan_sz, scan_array);
}
#elif USE_PACTREE
class PACTree : public Tree_api{
public:
PACTree();
virtual ~PACTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
pactree *tree_ = nullptr;
thread_local static bool thread_init;
};
thread_local bool PACTree::thread_init = false;
struct ThreadHelper
{
ThreadHelper(pactree* t){
t->registerThread();
// int id = omp_get_thread_num();
// printf("Thread ID: %d\n", id);
}
~ThreadHelper(){}
};
PACTree::PACTree(){
tree_ = new pactree(1);
}
PACTree::~PACTree(){
if(tree_!=nullptr)
delete tree_;
}
bool PACTree::find(size_t key, size_t key_sz){
thread_local ThreadHelper t(tree_);
Val_t val = tree_->lookup(key);
if(val == 0)
return false;
return true;
}
bool PACTree::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
thread_local ThreadHelper t(tree_);
if(!tree_->insert(key, *reinterpret_cast<Val_t*>(const_cast<char*>(value))))
{
return false;
}
return true;
}
bool PACTree::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
return true;
}
bool PACTree::remove(size_t key, size_t key_sz){
return true;
}
int PACTree::scan(size_t key, size_t key_sz, int scan_sz){
return 0;
}
#elif USE_BZTREE
class BzTreee : public Tree_api{
public:
BzTreee();
virtual ~BzTreee();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
bztree::BzTree* create_tree();
bztree::BzTree* my_tree;
bztree::BzTree* recovery_tree();
size_t pool_size = 1024*1021*1024*64UL;
};
bztree::BzTree* BzTreee::create_tree(){
bztree::BzTree::ParameterSet param(1024, 512, 1024);
uint32_t num_threads = run_thread + 1; // account for the loading thread
uint32_t desc_pool_size = 1024 * run_thread;
std::string pool_path("/mnt/pmem/bztree");
pmwcas::InitLibrary(
pmwcas::PMDKAllocator::Create(pool_path.c_str(), "bztree_layout",
pool_size),
pmwcas::PMDKAllocator::Destroy, pmwcas::LinuxEnvironment::Create,
pmwcas::LinuxEnvironment::Destroy);
auto pmdk_allocator =
reinterpret_cast<pmwcas::PMDKAllocator *>(pmwcas::Allocator::Get());
bztree::Allocator::Init(pmdk_allocator);
auto *bztree = reinterpret_cast<bztree::BzTree *>(
pmdk_allocator->GetRoot(sizeof(bztree::BzTree)));
pmdk_allocator->Allocate((void **)&bztree->pmwcas_pool,
sizeof(pmwcas::DescriptorPool));
new (bztree->pmwcas_pool)
pmwcas::DescriptorPool(desc_pool_size, num_threads, false);
new (bztree)
bztree::BzTree(param, bztree->pmwcas_pool,
reinterpret_cast<uint64_t>(pmdk_allocator->GetPool()));
return bztree;
}
bztree::BzTree* BzTreee::recovery_tree() {
uint32_t num_threads = run_thread + 1; // account for the loading thread
uint32_t desc_pool_size = 1024 * num_threads;
std::string pool_path("/mnt/pmem/bztree");
pmwcas::InitLibrary(
pmwcas::PMDKAllocator::Create(pool_path.c_str(), "bztree_layout",
pool_size),
pmwcas::PMDKAllocator::Destroy, pmwcas::LinuxEnvironment::Create,
pmwcas::LinuxEnvironment::Destroy);
auto pmdk_allocator =
reinterpret_cast<pmwcas::PMDKAllocator *>(pmwcas::Allocator::Get());
bztree::Allocator::Init(pmdk_allocator);
auto tree = reinterpret_cast<bztree::BzTree *>(
pmdk_allocator->GetRoot(sizeof(bztree::BzTree)));
tree->Recovery(num_threads);
pmdk_allocator->Allocate((void **)&tree->pmwcas_pool,
sizeof(pmwcas::DescriptorPool));
new (tree->pmwcas_pool)
pmwcas::DescriptorPool(desc_pool_size, num_threads, false);
tree->SetPMWCASPool(tree->pmwcas_pool);
return tree;
}
static bool FileExists(const char *pool_path) {
struct stat buffer;
return (stat(pool_path, &buffer) == 0);
}
BzTreee::BzTreee(){
std::string pool_path("/mnt/pmem/bztree");
if (FileExists(pool_path.c_str())) {
std::cout << "file existed" << std::endl;
my_tree = recovery_tree();
} else {
std::cout << "creating new tree on pool." << std::endl;
my_tree = create_tree();
}
}
BzTreee::~BzTreee(){
// pmwcas::Thread::ClearRegistry();
my_tree->~BzTree();
auto pmdk_allocator =
reinterpret_cast<pmwcas::PMDKAllocator *>(pmwcas::Allocator::Get());
auto *pool = my_tree->GetPMWCASPool();
pool->~DescriptorPool();
}
bool BzTreee::find(size_t key, size_t sz){
uint64_t k = key;
uint64_t value_out;
return my_tree
->Read(reinterpret_cast<const char *>(&k), sz, &value_out)
.IsOk();
// return true;
}
bool BzTreee::insert(size_t key, size_t key_sz, const char* value, size_t value_sz){
assert(value_sz == sizeof(uint64_t));
uint64_t v = reinterpret_cast<uint64_t>(const_cast<char *>(value));
auto rv = my_tree->Insert(reinterpret_cast<const char *>(&key), key_sz, v);
LOG_IF(INFO, !rv.IsOk()) << "insert failed!" << std::endl;
return rv.IsOk();
// return true;
}
bool BzTreee::update(size_t key, size_t key_sz, const char* value, size_t value_sz){
return true;
}
bool BzTreee::remove(size_t key, size_t key_sz){
return true;
}
int BzTreee::scan(size_t key, size_t key_sz, int scan_sz){
static thread_local std::array<char, (1 << 20)> results;
int scanned = 0;
char *dst = results.data();
auto iter = my_tree->RangeScanBySize(reinterpret_cast<const char *>(&key), key_sz,
scan_sz);
for (scanned = 0; (scanned < scan_sz); ++scanned) {
auto record = iter->GetNext();
if (record == nullptr) break;
// uint64_t result_key = __builtin_bswap64(
// *reinterpret_cast<const uint64_t *>(record->GetKey()));
// memcpy(dst, &result_key, sizeof(uint64_t));
// dst += sizeof(uint64_t);
auto payload = record->GetPayload();
memcpy(dst, &payload, sizeof(uint64_t));
dst += sizeof(uint64_t);
}
// values_out = results.data();
return scanned;
}
#else
class ZBTree : public Tree_api{
public:
ZBTree();
virtual ~ZBTree();
virtual bool find(size_t key, size_t sz) override;
virtual bool insert(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(size_t key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(size_t key, size_t key_sz) override;
virtual int scan(size_t key, size_t key_sz, int scan_sz) override;
private:
Tree zb_tree;
};
/*void reply(Nyx_na* nyx_ptr, Log_Node* start, int log_end){
for(int i=0; i<log_end; i++)
{
Log_Node* log = &start[i];
if(log->version_number >= log->pptr->version_field)
{
Leaf_Node* log_leaf = log->pptr;
if(log->op_kind == LOG_INSERT)
{
nyx_ptr->insert(log->key, (void*)log->val);
}
else if(log->op_kind == LOG_UPDATE)
{
nyx_ptr->update(log->key, (void*)log->val);
}
else
{
nyx_ptr->remove(log->key);
}
}
}
}*/
ZBTree::ZBTree(){
std::cout<<"construct"<<std::endl;
}
ZBTree::~ZBTree(){
}
bool ZBTree::find(size_t key, size_t sz){
char* result = nullptr;
#ifdef VAR_TEST
result = (char*) zb_tree.find(key, sz);
#else
result = (char*) zb_tree.find(key, sz);
#endif
#ifdef LONG_VAL
char buffer[val_len];
memcpy(buffer, result, val_len);
if(buffer!= nullptr)
{
return true;
}
else
return false;
#else
if(result != nullptr)
{
return true;
}
else
{
//std::cout<<"not found!"<<std::endl;
//abort();
return false;
}
#endif