-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathART.h
More file actions
492 lines (446 loc) · 18.5 KB
/
Copy pathART.h
File metadata and controls
492 lines (446 loc) · 18.5 KB
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
/*
Adaptive Radix Tree
Viktor Leis, 2012
leis@in.tum.de
*/
#pragma once
#include <assert.h>
#include <emmintrin.h> // x86 SSE intrinsics
#include <immintrin.h> // AVX512
#include <stdint.h> // integer types
#include <stdio.h>
#include <stdlib.h> // malloc, free
#include <string.h> // memset, memcpy
#include <sys/time.h> // gettime
#include <algorithm> // std::random_shuffle
#include <array>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <locale>
#include <memory>
#include <stdexcept>
#include "ArtNode.h" // ArtNode definitions
#include "Chain.h" // Chain definitions
#include "Helper.h" // Helper functions
namespace ART {
class ART {
public:
ArtNode* root; // pointer to root node of tree
ArtNode* fp; // pointer to fast path node
std::array<ArtNode*, maxPrefixLength> fp_path; // path that leads to fp
std::array<ArtNode**, maxPrefixLength>
fp_path_ref; // references to nodes on fp_path
size_t fp_path_length; // stores length of fp path
ArtNode* fp_leaf; // pointer to leaf node in fast path
size_t fp_depth; // depth that will be used during fp insertion
ArtNode** fp_ref; // reference to fp node, used for insertion
// constructor
ART()
: root(nullptr),
fp(nullptr),
fp_path{nullptr},
fp_path_length(0),
fp_leaf(nullptr),
fp_depth(0),
fp_ref(nullptr) {}
void insert(uint8_t key[], uintptr_t value) {
insert(this, root, &root, key, 0, value, maxPrefixLength);
}
ArtNode* lookup(uint8_t key[]) {
return lookup(root, key, maxPrefixLength, 0, maxPrefixLength);
}
Chain* rangelookup(uint8_t l_key[], unsigned l_keyLength, uint8_t h_key[],
uint8_t h_keyLength, unsigned maxKeyLength) {
return rangelookup(root, l_key, l_keyLength, h_key, h_keyLength,
maxKeyLength);
}
void printTree() { printTree(this->root, 0); }
// Method to verify the tail path after each insertion
// Returns true if the fast path (fp_path) leads to the correct fp and
// fp_leaf
bool verifyTailPath() {
if (this->fp_path_length == 0) {
// No fast path to verify
return true;
}
ArtNode* current = this->root;
// Traverse the tree following the fp_path
for (size_t i = 0; i < this->fp_path_length; i++) {
// If we're at the last node in the fp_path, check if it's the fp
// node
if (i == this->fp_path_length - 1) {
if (current == this->fp) {
// Check if the leaf value matches the expected fp_leaf
if (getLeafValue(maximum(current)) ==
getLeafValue(this->fp_leaf)) {
return true;
} else {
std::cerr << "Error: fp_leaf mismatch. Expected "
<< getLeafValue(maximum(current)) << ", got "
<< getLeafValue(this->fp_leaf) << "."
<< std::endl;
return false;
}
} else {
std::cerr << "Error: last node in fp_path is not the fp. "
"Expected "
<< static_cast<void*>(current) << ", got "
<< static_cast<void*>(this->fp) << "."
<< std::endl;
return false;
}
}
// Move to the rightmost child for each node type
switch (current->type) {
case NodeType4: {
Node4* node = static_cast<Node4*>(current);
if (node->count > 0) {
// Move to the last child (rightmost)
current = node->child[node->count - 1];
} else {
std::cerr << "Error: NodeType4 has no children."
<< std::endl;
return false;
}
break;
}
case NodeType16: {
Node16* node = static_cast<Node16*>(current);
if (node->count > 0) {
// Move to the last child (rightmost)
current = node->child[node->count - 1];
} else {
std::cerr << "Error: NodeType16 has no children."
<< std::endl;
return false;
}
break;
}
case NodeType48: {
Node48* node = static_cast<Node48*>(current);
unsigned pos = 255;
// Find the rightmost valid child
while (pos > 0 && node->childIndex[pos] == emptyMarker)
pos--;
if (node->childIndex[pos] != emptyMarker) {
current = node->child[node->childIndex[pos]];
} else {
std::cerr << "Error: NodeType48 has no valid children."
<< std::endl;
return false;
}
break;
}
case NodeType256: {
Node256* node = static_cast<Node256*>(current);
unsigned pos = 255;
// Find the rightmost valid child
while (pos > 0 && !node->child[pos]) pos--;
if (node->child[pos]) {
current = node->child[pos];
} else {
std::cerr << "Error: NodeType256 has no valid children."
<< std::endl;
return false;
}
break;
}
default:
std::cerr << "Error: Unknown node type." << std::endl;
return false;
}
}
// If we exit the loop without returning, the path is incorrect
std::cerr << "Error: fp_path does not lead to the fp." << std::endl;
return false;
}
private:
// Void insert function
void insert(ART* tree, ArtNode* node, ArtNode** nodeRef, uint8_t key[],
unsigned depth, uintptr_t value, unsigned maxKeyLength) {
// Insert the leaf value into the tree
if (node == NULL) {
*nodeRef = makeLeaf(value);
return;
}
if (isLeaf(node)) {
// Replace leaf with Node4 and store both leaves in it
uint8_t existingKey[maxKeyLength];
loadKey(getLeafValue(node), existingKey);
unsigned newPrefixLength = 0;
while (existingKey[depth + newPrefixLength] ==
key[depth + newPrefixLength])
newPrefixLength++;
Node4* newNode = new Node4();
newNode->prefixLength = newPrefixLength;
memcpy(newNode->prefix, key + depth,
min(newPrefixLength, maxPrefixLength));
*nodeRef = newNode;
newNode->insertNode4(this, nodeRef,
existingKey[depth + newPrefixLength], node);
newNode->insertNode4(this, nodeRef, key[depth + newPrefixLength],
makeLeaf(value));
return;
}
// Handle prefix of inner node
if (node->prefixLength) {
unsigned mismatchPos =
prefixMismatch(node, key, depth, maxKeyLength);
if (mismatchPos != node->prefixLength) {
// Prefix differs, create new node
Node4* newNode = new Node4();
*nodeRef = newNode;
newNode->prefixLength = mismatchPos;
memcpy(newNode->prefix, node->prefix,
min(mismatchPos, maxPrefixLength));
// Break up prefix
if (node->prefixLength < maxPrefixLength) {
newNode->insertNode4(this, nodeRef,
node->prefix[mismatchPos], node);
node->prefixLength -= (mismatchPos + 1);
memmove(node->prefix, node->prefix + mismatchPos + 1,
min(node->prefixLength, maxPrefixLength));
} else {
node->prefixLength -= (mismatchPos + 1);
uint8_t minKey[maxKeyLength];
loadKey(getLeafValue(minimum(node)), minKey);
newNode->insertNode4(this, nodeRef,
minKey[depth + mismatchPos], node);
memmove(node->prefix, minKey + depth + mismatchPos + 1,
min(node->prefixLength, maxPrefixLength));
}
newNode->insertNode4(this, nodeRef, key[depth + mismatchPos],
makeLeaf(value));
return;
}
depth += node->prefixLength;
}
// Recurse
ArtNode** child = findChild(node, key[depth]);
if (*child) {
insert(tree, *child, child, key, depth + 1, value, maxKeyLength);
return;
}
// Insert leaf into inner node
ArtNode* newNode = makeLeaf(value);
switch (node->type) {
case NodeType4:
static_cast<Node4*>(node)->insertNode4(this, nodeRef,
key[depth], newNode);
break;
case NodeType16:
static_cast<Node16*>(node)->insertNode16(this, nodeRef,
key[depth], newNode);
break;
case NodeType48:
static_cast<Node48*>(node)->insertNode48(this, nodeRef,
key[depth], newNode);
break;
case NodeType256:
static_cast<Node256*>(node)->insertNode256(this, nodeRef,
key[depth], newNode);
break;
}
}
// Lookup function, returns ArtNode
ArtNode* lookup(ArtNode* node, uint8_t key[], unsigned keyLength,
unsigned depth, unsigned maxKeyLength) {
// Find the node with a matching key, optimistic version
bool skippedPrefix = false; // Did we optimistically skip some prefix
// without checking it?
while (node != NULL) {
if (isLeaf(node)) {
if (!skippedPrefix && depth == keyLength) // No check required
return node;
if (depth != keyLength) {
// Check leaf
uint8_t leafKey[maxKeyLength];
loadKey(getLeafValue(node), leafKey);
for (unsigned i = (skippedPrefix ? 0 : depth);
i < keyLength; i++)
if (leafKey[i] != key[i]) return NULL;
}
return node;
}
if (node->prefixLength) {
if (node->prefixLength < maxPrefixLength) {
for (unsigned pos = 0; pos < node->prefixLength; pos++)
if (key[depth + pos] != node->prefix[pos]) return NULL;
} else
skippedPrefix = true;
depth += node->prefixLength;
}
node = *findChild(node, key[depth]);
depth++;
}
return NULL;
}
// Erase function, deletes a leaf from the tree
void erase(ArtNode* node, ArtNode** nodeRef, uint8_t key[],
unsigned keyLength, unsigned depth, unsigned maxKeyLength) {
// Delete a leaf from a tree
if (!node) return;
if (isLeaf(node)) {
// Make sure we have the right leaf
if (leafMatches(node, key, keyLength, depth, maxKeyLength))
*nodeRef = NULL;
return;
}
// Handle prefix
if (node->prefixLength) {
if (prefixMismatch(node, key, depth, maxKeyLength) !=
node->prefixLength)
return;
depth += node->prefixLength;
}
ArtNode** child = findChild(node, key[depth]);
if (isLeaf(*child) &&
leafMatches(*child, key, keyLength, depth, maxKeyLength)) {
// Leaf found, delete it in inner node
switch (node->type) {
case NodeType4:
static_cast<Node4*>(node)->eraseNode4(this, nodeRef, child);
break;
case NodeType16:
static_cast<Node16*>(node)->eraseNode16(this, nodeRef,
child);
break;
case NodeType48:
static_cast<Node48*>(node)->eraseNode48(this, nodeRef,
key[depth]);
break;
case NodeType256:
static_cast<Node256*>(node)->eraseNode256(this, nodeRef,
key[depth]);
break;
}
} else {
// Recurse
erase(*child, child, key, keyLength, depth + 1, maxKeyLength);
}
}
// Range lookup function, returns a Chain of ArtNode
Chain* rangelookup(ArtNode* node, uint8_t l_key[], unsigned l_keyLength,
uint8_t h_key[], uint8_t h_keyLength,
unsigned maxKeyLength) {
// Find the node with a matching key, optimistic version
Chain* queue =
new Chain((ChainItem*)new ChainItemWithDepth(node, 0, true, true));
Chain* result = new Chain();
while (!queue->isEmpty()) {
ChainItemWithDepth* item = (ChainItemWithDepth*)queue->pop_front();
node = item->nodeptr();
int depth = item->depth_;
bool lequ = item->lequ_, hequ = item->hequ_;
bool continue_flag =
0; // true means the range vialates the key range
unsigned pos;
auto compare_and_set = [&](unsigned pos,
uint8_t compared_byte) -> void {
uint8_t lkey = pos >= l_keyLength ? 0 : l_key[pos];
uint8_t hkey = pos >= h_keyLength ? 0 : l_key[pos];
if (lkey < compared_byte)
lequ = 0;
else if (lkey > compared_byte)
continue_flag = 1;
if (hkey < compared_byte)
continue_flag = 1;
else if (hkey > compared_byte)
hequ = 0;
};
if (isLeaf(node)) {
uint8_t leafKey[maxKeyLength];
loadKey(getLeafValue(node), leafKey);
for (unsigned i = depth;
i < maxKeyLength && !continue_flag && (lequ || hequ); i++)
compare_and_set(i, leafKey[i]);
if (!continue_flag) {
result->extend_item(new ChainItem(node));
}
continue;
}
if (node->prefixLength > maxPrefixLength) {
for (pos = 0;
pos < maxPrefixLength && !continue_flag && (lequ || hequ);
pos++) {
compare_and_set(depth + pos, node->prefix[pos]);
}
uint8_t minKey[maxKeyLength];
loadKey(getLeafValue(minimum(node)), minKey);
for (; pos < node->prefixLength && !continue_flag &&
(lequ || hequ);
pos++) {
compare_and_set(depth + pos, minKey[depth + pos]);
}
} else {
for (pos = 0; pos < node->prefixLength && !continue_flag &&
(lequ || hequ);
pos++) {
compare_and_set(depth + pos, node->prefix[pos]);
}
}
if (continue_flag) continue;
depth += node->prefixLength;
std::unique_ptr<Chain> newly_added =
std::move(std::unique_ptr<Chain>(newly_added->findChildbyRange(
item->nodeptr(), lequ ? l_key[depth] : 0,
hequ ? h_key[depth] : 255, depth, lequ, hequ)));
queue->extend(std::move(newly_added));
}
delete queue;
return result;
}
void printTree(ArtNode* node, int depth) {
if (!node) return;
// Indent based on depth
for (int i = 0; i < depth; i++) {
printf(" ");
}
if (isLeaf(node)) {
printf("Leaf(%lu)\n", getLeafValue(node));
return;
}
switch (node->type) {
case NodeType4: {
Node4* n = static_cast<Node4*>(node);
printf("Node4 [%p]\n", static_cast<void*>(n));
for (unsigned i = 0; i < n->count; i++) {
printTree(n->child[i], depth + 1);
}
break;
}
case NodeType16: {
Node16* n = static_cast<Node16*>(node);
printf("Node16 [%p]\n", static_cast<void*>(n));
for (unsigned i = 0; i < n->count; i++) {
printTree(n->child[i], depth + 1);
}
break;
}
case NodeType48: {
Node48* n = static_cast<Node48*>(node);
printf("Node48 [%p]\n", static_cast<void*>(n));
for (unsigned i = 0; i < 256; i++) {
if (n->childIndex[i] != emptyMarker) {
printTree(n->child[n->childIndex[i]], depth + 1);
}
}
break;
}
case NodeType256: {
Node256* n = static_cast<Node256*>(node);
printf("Node256 [%p]\n", static_cast<void*>(n));
for (unsigned i = 0; i < 256; i++) {
if (n->child[i]) {
printTree(n->child[i], depth + 1);
}
}
break;
}
}
}
};
} // namespace ART