-
Notifications
You must be signed in to change notification settings - Fork 5
/
fptree_wrapper.hpp
117 lines (99 loc) · 2.86 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
109
110
111
112
113
114
115
116
117
#ifndef __FPTREE_WRAPPER_HPP__
#define __FPTREE_WRAPPER_HPP__
#include "tree_api.hpp"
#include "fptree.h"
#include <cstring>
#include <mutex>
#include <shared_mutex>
#include <libpmemobj.h>
// #define DEBUG_MSG
class fptree_wrapper : public tree_api
{
public:
#ifdef PMEM
fptree_wrapper(const char* path_ptr, long long pool_size);
#else
fptree_wrapper();
#endif
virtual ~fptree_wrapper();
virtual bool find(const char* key, size_t key_sz, char* value_out) override;
virtual bool insert(const char* key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool update(const char* key, size_t key_sz, const char* value, size_t value_sz) override;
virtual bool remove(const char* key, size_t key_sz) override;
virtual int scan(const char* key, size_t key_sz, int scan_sz, char*& values_out) override;
private:
FPtree tree_;
};
#ifdef PMEM
fptree_wrapper::fptree_wrapper(const char* path_ptr, long long pool_size)
{
tree_.pmemInit(path_ptr, pool_size);
}
#else
fptree_wrapper::fptree_wrapper()
{
}
#endif
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;
}
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