-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbwtree.hpp
226 lines (173 loc) · 7.55 KB
/
bwtree.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
#ifndef BWTREE_H
#define BWTREE_H
#include <tuple>
#include <vector>
#include <mutex>
#include <atomic>
#include <random>
#include <iostream>
#include <stack>
#include <assert.h>
#include <sys/wait.h>
#include "nodes.hpp"
#include "epoque.hpp"
namespace BwTree {
template<typename Key, typename Data>
struct FindDataPageResult {
const PID pid;
Node<Key, Data> *startNode;
const Node<Key, Data> *const dataNode;
const Data *const data = nullptr;
const PID needConsolidatePage;
const PID needSplitPage;
const PID needSplitPageParent;
FindDataPageResult(PID const pid, Node<Key, Data> *startNode, Node<Key, Data> const *dataNode, PID const needConsolidatePage, PID const needSplitPage, PID const needSplitPageParent);
FindDataPageResult(PID const pid, Node<Key, Data> *startNode, Node<Key, Data> const *dataNode, Data const *data, PID const needConsolidatePage, PID const needSplitPage, PID const needSplitPageParent);
};
struct Settings {
std::string name;
Settings(std::string name, size_t splitLeaf, std::vector<size_t> const &splitInner, size_t consolidateLeaf, std::vector<size_t> const &consolidateInner)
: name(name), splitLeaf(splitLeaf),
splitInner(splitInner),
consolidateLeaf(consolidateLeaf),
consolidateInner(consolidateInner) {
}
std::size_t splitLeaf;
const std::size_t &getSplitLimitLeaf() const {
return splitLeaf;
}
std::vector<std::size_t> splitInner;
const std::size_t &getSplitLimitInner(unsigned level) const {
return level < splitInner.size() ? splitInner[level] : splitInner[splitInner.size() - 1];
}
std::size_t consolidateLeaf;
const std::size_t &getConsolidateLimitLeaf() const {
return consolidateLeaf;
}
std::vector<std::size_t> consolidateInner;
const std::size_t &getConsolidateLimitInner(unsigned level) const {
return level < consolidateInner.size() ? consolidateInner[level] : consolidateInner[consolidateInner.size() - 1];
}
const std::string &getName() const {
return name;
}
};
template<typename Key, typename Data>
class Tree {
static constexpr bool DEBUG = false;
/**
* Special Invariant:
* - Leaf nodes always contain special infinity value at the right end for the last pointer
*/
std::atomic<PID> root;
std::vector<std::atomic<Node<Key, Data> *>> mapping{4194304};
std::atomic<PID> mappingNext{0};
std::atomic<unsigned long> atomicCollisions{0};
std::atomic<unsigned long> successfulLeafConsolidate{0};
std::atomic<unsigned long> successfulInnerConsolidate{0};
std::atomic<unsigned long> failedLeafConsolidate{0};
std::atomic<unsigned long> failedInnerConsolidate{0};
std::atomic<unsigned long> successfulLeafSplit{0};
std::atomic<unsigned long> successfulInnerSplit{0};
std::atomic<unsigned long> failedLeafSplit{0};
std::atomic<unsigned long> failedInnerSplit{0};
Epoche<Key, Data> epoque{64};
const Settings &settings;
Node<Key, Data> *PIDToNodePtr(const PID node) {
return mapping[node];
}
PID newNode(Node<Key, Data> *node) {
PID nextPID = mappingNext++;
if (nextPID >= mapping.size()) {
std::cerr << "Mapping table is full, aborting!" << std::endl;
exit(1);
}
mapping[nextPID] = node;
return nextPID;
}
/**
* page id of the leaf node, first node in the chain (corresponds to PID), actual node where the data was found
*/
FindDataPageResult<Key, Data> findDataPage(Key key);
void consolidatePage(const PID pid, ThreadInfo<Key, Data> &threadInfo) {
Node<Key, Data> *node = PIDToNodePtr(pid);
if (isLeaf(node)) {
consolidateLeafPage(pid, node, threadInfo);
} else {
consolidateInnerPage(pid, node, threadInfo);
}
}
void consolidateInnerPage(PID pid, Node<Key, Data> *startNode, ThreadInfo<Key, Data> &threadInfo);
std::tuple<PID, PID, bool> getConsolidatedInnerData(Node<Key, Data> *node, PID pid, std::vector<KeyPid<Key, Data>> &returnNodes);
void consolidateLeafPage(PID pid, Node<Key, Data> *startNode, ThreadInfo<Key, Data> &threadInfo);
std::tuple<PID, PID> getConsolidatedLeafData(Node<Key, Data> *node, std::vector<KeyValue<Key, Data>> &returnNodes);
void splitPage(const PID needSplitPage, const PID needSplitPageParent);
std::tuple<PID, Node<Key, Data> *> findInnerNodeOnLevel(PID pid, Key key);
bool isLeaf(Node<Key, Data> *node) {
switch (node->getType()) {
case PageType::inner: /* fallthrough */
case PageType::deltaSplitInner: /* fallthrough */
case PageType::deltaIndex:
return false;
case PageType::leaf:
case PageType::deltaDelete: /* fallthrough */
case PageType::deltaSplit: /* fallthrough */
case PageType::deltaInsert:
return true;
}
assert(false);
return false;
}
template<typename T>
static size_t binarySearch(T array, std::size_t length, Key key);
std::default_random_engine d;
std::uniform_int_distribution<int> rand{0, 100};
public:
Tree(Settings &settings) : settings(settings) {
Node<Key, Data> *datanode = Leaf<Key, Data>::create(0, NotExistantPID, NotExistantPID);
PID dataNodePID = newNode(datanode);
InnerNode<Key, Data> *innerNode = InnerNode<Key, Data>::create(1, NotExistantPID, NotExistantPID);
innerNode->nodes[0] = KeyPid<Key, Data>(std::numeric_limits<Key>::max(), dataNodePID);
root.store(newNode(innerNode));
}
~Tree();
void insert(Key key, const Data *const record, ThreadInfo<Key, Data> &threadInfo);
void deleteKey(Key key, ThreadInfo<Key, Data> &threadInfo);
Data *search(Key key, ThreadInfo<Key, Data> &threadInfo);
ThreadInfo<Key, Data> getThreadInfo();
/**
* has to be called when no further work is to be done the next time so that the epoques can be freed.
*/
void threadFinishedWithTree() {
//TODO EnterEpoque<Key, Data>::threadFinishedWithTree(epoque);
}
unsigned long getAtomicCollisions() const {
return atomicCollisions;
}
unsigned long getSuccessfulLeafConsolidate() const {
return successfulLeafConsolidate;
}
unsigned long getSuccessfulInnerConsolidate() const {
return successfulInnerConsolidate;
}
unsigned long getFailedLeafConsolidate() const {
return failedLeafConsolidate;
}
unsigned long getFailedInnerConsolidate() const {
return failedInnerConsolidate;
}
unsigned long getSuccessfulLeafSplit() const {
return successfulLeafSplit;
}
unsigned long getSuccessfulInnerSplit() const {
return successfulInnerSplit;
}
unsigned long getFailedLeafSplit() const {
return failedLeafSplit;
}
unsigned long getFailedInnerSplit() const {
return failedInnerSplit;
}
};
}
#endif