-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmasstree_index_impl.cc
194 lines (163 loc) · 5.01 KB
/
masstree_index_impl.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
#include "masstree_index_impl.h"
#include "masstree/build/config.h"
#include "masstree/masstree_insert.hh"
#include "masstree/masstree_remove.hh"
#include "masstree/masstree_tcursor.hh"
#include "masstree/masstree_print.hh"
#include "masstree/masstree_scan.hh"
#include "masstree/kvthread.hh"
#include "masstree/timestamp.hh"
#include "masstree/masstree.hh"
volatile mrcu_epoch_type active_epoch;
volatile mrcu_epoch_type globalepoch = 1;
kvtimestamp_t initial_timestamp;
kvepoch_t global_log_epoch;
namespace felis {
struct MasstreeDollyParam : public Masstree::nodeparams<15, 15> {
typedef VHandle* value_type;
// typedef VHandlePrinter value_print_type;
typedef threadinfo threadinfo_type;
};
class MasstreeMap : public Masstree::basic_table<MasstreeDollyParam> {
public:
template <class MasstreeIteratorImpl>
struct Iterator : public Table::Iterator,
public MasstreeIteratorImpl {
threadinfo *ti;
void Adapt();
using MasstreeIteratorImpl::MasstreeIteratorImpl;
void Next() override final;
bool IsValid() const override final;
static void *operator new(size_t sz) {
return mem::AllocFromRoutine(sz);
}
static void operator delete(void *p) {}
};
using ForwardIterator = Iterator<forward_scan_iterator_impl>;
using ReverseIterator = Iterator<reverse_scan_iterator_impl>;
};
template <class MasstreeIteratorImpl>
void MasstreeMap::Iterator<MasstreeIteratorImpl>::Adapt()
{
if (!this->terminated) {
// wrap the iterator
auto s = ((MasstreeIteratorImpl *) this)->key.full_string();
cur_key = VarStrView(s.length(), (uint8_t *) s.data());
vhandle = this->entry.value();
}
}
template <class MasstreeIteratorImpl>
void MasstreeMap::Iterator<MasstreeIteratorImpl>::Next()
{
this->next(*ti);
Adapt();
}
template <>
bool MasstreeMap::Iterator<MasstreeMap::forward_scan_iterator_impl>::IsValid() const
{
return !this->terminated && !(end_key < cur_key);
}
template <>
bool MasstreeMap::Iterator<MasstreeMap::reverse_scan_iterator_impl>::IsValid() const
{
return !this->terminated && !(cur_key < end_key);
}
MasstreeIndex::MasstreeIndex(std::tuple<bool> conf) noexcept
: Table()
{
enable_inline = std::get<0>(conf);
auto tree = new (get_map()) MasstreeMap();
auto ti = GetThreadInfo();
tree->initialize(*ti);
}
template <typename Func>
VHandle *MasstreeIndex::SearchOrCreateImpl(const VarStrView &k, Func f)
{
VHandle *result;
// result = this->Search(k);
// if (result) return result;
auto ti = GetThreadInfo();
typename MasstreeMap::cursor_type cursor(*get_map(), k.data(), k.length());
bool found = cursor.find_insert(*ti);
if (!found) {
cursor.value() = f();
// nr_keys[go::Scheduler::CurrentThreadPoolId() - 1].add_cnt++;
}
result = cursor.value();
cursor.finish(1, *ti);
assert(result != nullptr);
return result;
}
VHandle *MasstreeIndex::SearchOrCreate(const VarStrView &k)
{
return SearchOrCreateImpl(k, [=]() { return NewRow(); });
}
VHandle *MasstreeIndex::SearchOrCreate(const VarStrView &k, bool *created)
{
*created = false;
return SearchOrCreateImpl(k, [=]() { *created = true; return NewRow(); });
}
VHandle *MasstreeIndex::Search(const VarStrView &k)
{
auto ti = GetThreadInfo();
VHandle *result = nullptr;
get_map()->get(lcdf::Str(k.data(), k.length()), result, *ti);
return result;
}
static thread_local threadinfo *TLSThreadInfo;
threadinfo *MasstreeIndex::GetThreadInfo()
{
if (TLSThreadInfo == nullptr)
TLSThreadInfo = threadinfo::make(threadinfo::TI_PROCESS, go::Scheduler::CurrentThreadPoolId());
return TLSThreadInfo;
}
void MasstreeIndex::ResetThreadInfo()
{
TLSThreadInfo = nullptr;
}
Table::Iterator *MasstreeIndex::IndexSearchIterator(const VarStrView &start, const VarStrView &end)
{
auto it = get_map()->find_iterator<MasstreeMap::ForwardIterator>(
lcdf::Str(start.data(), start.length()), *GetThreadInfo());
set_iterator_end_key(it, end);
it->Adapt();
return it;
}
Table::Iterator *MasstreeIndex::IndexSearchIterator(const VarStrView &start)
{
return IndexSearchIterator(start, VarStrView(std::numeric_limits<uint16_t>::max(), nullptr));
}
Table::Iterator *MasstreeIndex::IndexReverseIterator(const VarStrView &start, const VarStrView &end)
{
auto it = get_map()->find_iterator<MasstreeMap::ReverseIterator>(
lcdf::Str(start.data(), start.length()), *GetThreadInfo());
set_iterator_end_key(it, end);
it->Adapt();
return it;
}
Table::Iterator *MasstreeIndex::IndexReverseIterator(const VarStrView &start)
{
return IndexReverseIterator(start, VarStrView());
}
void MasstreeIndex::ImmediateDelete(const VarStrView &k)
{
auto ti = GetThreadInfo();
typename MasstreeMap::cursor_type cursor(*get_map(), k.data(), k.length());
bool found = cursor.find_locked(*ti);
VHandle *phandle = nullptr;
if (found) {
phandle = cursor.value();
cursor.value() = nullptr;
}
cursor.finish(-1, *ti);
delete phandle;
}
void *MasstreeIndex::operator new(size_t sz)
{
return malloc(sz + sizeof(MasstreeMap));
}
void MasstreeIndex::operator delete(void *p)
{
return free(p);
}
}