forked from sfu-dis/FPTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfptree_wrapper.hpp
108 lines (92 loc) · 2.66 KB
/
fptree_wrapper.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
#ifndef __FPTREE_WRAPPER_HPP__
#define __FPTREE_WRAPPER_HPP__
#include "tree_api.h"
#include "fptree.h"
#include <cstring>
#include <mutex>
#include <shared_mutex>
#include <libpmemobj.h>
// #define DEBUG_MSG
class fptree_wrapper : public tree_api
{
public:
fptree_wrapper();
~fptree_wrapper() override;
bool find(const char* key, size_t key_sz, char* value_out) override;
bool insert(const char* key, size_t key_sz, const char* value, size_t value_sz) override;
bool update(const char* key, size_t key_sz, const char* value, size_t value_sz) override;
bool remove(const char* key, size_t key_sz) override;
int scan(const char* key, size_t key_sz, int scan_sz, char*& values_out) override;
long get_size() override;
private:
FPtree tree_;
};
fptree_wrapper::fptree_wrapper()
{
}
fptree_wrapper::~fptree_wrapper()
{
}
bool fptree_wrapper::find(const char* key, size_t key_sz, char* value_out)
{
// For now only support 8 bytes key and value (uint64_t)
uint64_t value = tree_.find(*reinterpret_cast<uint64_t*>(const_cast<char*>(key)));
if (value == 0)
{
#ifdef DEBUG_MSG
printf("Search key not found!\n");
#endif
return false;
}
memcpy(value_out, &value, sizeof(value));
return true;
}
bool fptree_wrapper::insert(const char* key, size_t key_sz, const char* value, size_t value_sz)
{
KV kv = KV(*reinterpret_cast<uint64_t*>(const_cast<char*>(key)), *reinterpret_cast<uint64_t*>(const_cast<char*>(value)));
if (!tree_.insert(kv))
{
#ifdef DEBUG_MSG
printf("Insert failed\n");
#endif
return false;
}
return true;
}
bool fptree_wrapper::update(const char* key, size_t key_sz, const char* value, size_t value_sz)
{
KV kv = KV(*reinterpret_cast<uint64_t*>(const_cast<char*>(key)), *reinterpret_cast<uint64_t*>(const_cast<char*>(value)));
if (!tree_.update(kv))
{
#ifdef DEBUG_MSG
printf("Update failed\n");
#endif
return false;
}
return true;
}
bool fptree_wrapper::remove(const char* key, size_t key_sz)
{
if (!tree_.deleteKey(*reinterpret_cast<uint64_t*>(const_cast<char*>(key))))
{
#ifdef DEBUG_MSG
printf("Remove failed\n");
#endif
return false;
}
return true;
}
long fptree_wrapper::get_size() {
return tree_.total_size;
}
int fptree_wrapper::scan(const char* key, size_t key_sz, int scan_sz, char*& values_out)
{
constexpr size_t ONE_MB = 1ULL << 20;
static thread_local char results[ONE_MB];
int scanned = tree_.rangeScan(*reinterpret_cast<uint64_t*>(const_cast<char*>(key)), (uint64_t)scan_sz, results);
#ifdef DEBUG_MSG
printf("%d records scanned\n", scanned);
#endif
return scanned;
}
#endif