-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbplus.cpp
1153 lines (948 loc) · 35.1 KB
/
bplus.cpp
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015 Srijan R Shetty <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* Structure of file
-----------------
fileIndex
leaf
parent
nextLeaf
previousLeaf
keySize
key1
key2
...
keyn
child1
child2
...
child (n+1)
------------------
*/
/* Conventions
1. Caller ensures the Node is loaded into memory.
2. If a function modifies the Node, it saves it back to disk
*/
// Configuration parameters
#define CONFIG_FILE "./bplustree.config"
#define SESSION_FILE "./.tree.session"
// Constants
#define TREE_PREFIX "leaves/leaf_"
#define OBJECT_FILE "objects/objectFile"
#define DEFAULT_LOCATION -1
// #define DEBUG_VERBOSE
// #define DEBUG_NORMAL
// Two modes of running the program, either time it or show output
#define OUTPUT
// #define TIME
#include <chrono>
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
namespace BPlusTree {
// A generic compare function for pairs of numbers
template<typename T>
class compare {
public:
bool operator() (pair<T, double> T1, pair<T, double> T2) {
return T1.second > T2.second;
}
};
// Database objects
class DBObject {
private:
double key;
long fileIndex;
string dataString;
public:
static long objectCount;
public:
DBObject(double _key, string _dataString) : key(_key), dataString(_dataString) {
fileIndex = objectCount++;
// Open a file and write the string to it
ofstream ofile(OBJECT_FILE, ios::app);
ofile << dataString << endl;
ofile.close();
}
DBObject(double _key, long _fileIndex) : key(_key), fileIndex(_fileIndex) {
// Open a file and read the dataString
ifstream ifile(OBJECT_FILE);
for (long i = 0; i < fileIndex + 1; ++i) {
getline(ifile, dataString);
}
ifile.close();
}
// Return the key of the object
double getKey() { return key; }
// Return the string
string getDataString() { return dataString; }
// Return the fileIndex
long getFileIndex() { return fileIndex; }
};
long DBObject::objectCount = 0;
class Node {
public:
static long fileCount; // Count of all files
static long lowerBound;
static long upperBound;
static long pageSize;
private:
long fileIndex; // Name of file to store contents
bool leaf; // Type of leaf
public:
long parentIndex;
long nextLeafIndex;
long previousLeafIndex;
double keyType; // Dummy to indicate container base
vector<double> keys;
vector<long> childIndices; // FileIndices of the children
vector<long> objectPointers; // To store the object pointers
public:
// Basic initialization
Node();
// Given a fileIndex, read it
Node(long _fileIndex);
// Check if leaf
bool isLeaf() { return leaf; }
// Get the file name
string getFileName() { return TREE_PREFIX + to_string(fileIndex); }
// Get the fileIndex
long getFileIndex() { return fileIndex; }
// set to internalNode
void setToInternalNode() { leaf = false; }
// Return the size of keys
long size() { return keys.size(); }
// Initialize the for the tree
static void initialize();
// Return the position of a key in keys
long getKeyPosition(double key);
// Commit node to disk
void commitToDisk();
// Read from the disk into memory
void readFromDisk();
// Print node information
void printNode();
// Serialize the subtree
void serialize();
// Insert object into disk
void insertObject(DBObject object);
// Insert an internal node into the tree
void insertNode(double key, long leftChildIndex, long rightChildIndex);
// Split the current Leaf Node
void splitLeaf();
// Split the current internal Node
void splitInternal();
};
// Initialize static variables
long Node::lowerBound = 0;
long Node::upperBound = 0;
long Node::pageSize = 0;
long Node::fileCount = 0;
Node *bRoot = nullptr;
Node::Node() {
// Initially all the fileNames are DEFAULT_LOCATION
parentIndex = DEFAULT_LOCATION;
nextLeafIndex = DEFAULT_LOCATION;
previousLeafIndex = DEFAULT_LOCATION;
// Initially every node is a leaf
leaf = true;
// Exit if the lowerBoundKey is not defined
if (lowerBound == 0) {
cout << "LowerKeyBound not defined";
exit(1);
}
// LeafNode properties
fileIndex = ++fileCount;
}
Node::Node(long _fileIndex) {
// Exit if the lowerBoundKey is not defined
if (lowerBound == 0) {
cout << "LowerKeyBound not defined";
exit(1);
}
// Load the current node from disk
fileIndex = _fileIndex;
readFromDisk();
}
void Node::initialize() {
// Read in the pageSize from the configuration file
ifstream configFile;
configFile.open(CONFIG_FILE);
configFile >> pageSize;
// Save some place in the file for the header
long headerSize = sizeof(fileIndex)
+ sizeof(leaf)
+ sizeof(parentIndex)
+ sizeof(nextLeafIndex)
+ sizeof(previousLeafIndex);
pageSize = pageSize - headerSize;
// Compute parameters
long nodeSize = sizeof(fileIndex);
long keySize = sizeof(keyType);
lowerBound = floor((pageSize - nodeSize) / (2 * (keySize + nodeSize)));
upperBound = 2 * lowerBound;
pageSize = pageSize + headerSize;
}
long Node::getKeyPosition(double key) {
// If keys are empty, return
if (keys.size() == 0 || key <= keys.front()) {
return 0;
}
for (long i = 1; i < (long)keys.size(); ++i) {
if (keys[i -1] < key && key <= keys[i]) {
return i;
}
}
return keys.size();
}
void Node::commitToDisk() {
// Create a character buffer which will be written to disk
long location = 0;
char buffer[pageSize];
// Store the fileIndex
memcpy(buffer + location, &fileIndex, sizeof(fileIndex));
location += sizeof(fileIndex);
// Add the leaf to memory
memcpy(buffer + location, &leaf, sizeof(leaf));
location += sizeof(leaf);
// Add parent to memory
memcpy(buffer + location, &parentIndex, sizeof(parentIndex));
location += sizeof(parentIndex);
// Add the previous leaf node
memcpy(buffer + location, &previousLeafIndex, sizeof(nextLeafIndex));
location += sizeof(nextLeafIndex);
// Add the next leaf node
memcpy(buffer + location, &nextLeafIndex, sizeof(nextLeafIndex));
location += sizeof(nextLeafIndex);
// Store the number of keys
long numKeys = keys.size();
memcpy(buffer + location, &numKeys, sizeof(numKeys));
location += sizeof(numKeys);
// Add the keys to memory
for (auto key : keys) {
memcpy(buffer + location, &key, sizeof(key));
location += sizeof(key);
}
// Add the child pointers to memory
if (!leaf) {
for (auto childIndex : childIndices) {
memcpy(buffer + location, &childIndex, sizeof(childIndex));
location += sizeof(childIndex);
}
} else {
for (auto objectPointer : objectPointers) {
memcpy(buffer + location, &objectPointer, sizeof(objectPointer));
location += sizeof(objectPointer);
}
}
// Create a binary file and write to memory
ofstream nodeFile;
nodeFile.open(getFileName(), ios::binary|ios::out);
nodeFile.write(buffer, pageSize);
nodeFile.close();
}
void Node::readFromDisk() {
// Create a character buffer which will be written to disk
long location = 0;
char buffer[pageSize];
// Open the binary file ane read into memory
ifstream nodeFile;
nodeFile.open(getFileName(), ios::binary|ios::in);
nodeFile.read(buffer, pageSize);
nodeFile.close();
// Retrieve the fileIndex
memcpy((char *) &fileIndex, buffer + location, sizeof(fileIndex));
location += sizeof(fileIndex);
// Retreive the type of node
memcpy((char *) &leaf, buffer + location, sizeof(leaf));
location += sizeof(leaf);
// Retrieve the parentIndex
memcpy((char *) &parentIndex, buffer + location, sizeof(parentIndex));
location += sizeof(parentIndex);
// Retrieve the previousLeafIndex
memcpy((char *) &previousLeafIndex, buffer + location, sizeof(previousLeafIndex));
location += sizeof(previousLeafIndex);
// Retrieve the nextLeafIndex
memcpy((char *) &nextLeafIndex, buffer + location, sizeof(nextLeafIndex));
location += sizeof(nextLeafIndex);
// Retrieve the number of keys
long numKeys;
memcpy((char *) &numKeys, buffer + location, sizeof(numKeys));
location += sizeof(numKeys);
// Retrieve the keys
keys.clear();
double key;
for (long i = 0; i < numKeys; ++i) {
memcpy((char *) &key, buffer + location, sizeof(key));
location += sizeof(key);
keys.push_back(key);
}
// Retrieve childPointers
if (!leaf) {
childIndices.clear();
long childIndex;
for (long i = 0; i < numKeys + 1; ++i) {
memcpy((char *) &childIndex, buffer + location, sizeof(childIndex));
location += sizeof(childIndex);
childIndices.push_back(childIndex);
}
} else {
objectPointers.clear();
long objectPointer;
for (long i = 0; i < numKeys; ++i) {
memcpy((char *) &objectPointer, buffer + location, sizeof(objectPointer));
location += sizeof(objectPointer);
objectPointers.push_back(objectPointer);
}
}
}
void Node::printNode() {
cout << endl << endl;
cout << "File : " << fileIndex << endl;
cout << "IsLeaf : " << leaf << endl;
cout << "Parent : " << parentIndex << endl;
cout << "PreviousLeaf : " << previousLeafIndex << endl;
cout << "NextLeaf : " << nextLeafIndex << endl;
// Print keys
cout << "Keys : ";
for (auto key : keys) {
cout << key << " ";
}
cout << endl;
// Print the name of the child
cout << "Children : ";
for (auto childIndex : childIndices) {
cout << childIndex << " ";
}
cout << endl;
}
void Node::insertObject(DBObject object) {
long position = getKeyPosition(object.getKey());
// insert the new key to keys
keys.insert(keys.begin() + position, object.getKey());
// insert the object pointer to the end
objectPointers.insert(objectPointers.begin() + position, object.getFileIndex());
// Commit the new node back into memory
commitToDisk();
}
void Node::serialize() {
// Return if node is empty
if (keys.size() == 0) {
return;
}
// Prettify
cout << endl << endl;
queue< pair<long, char> > previousLevel;
previousLevel.push(make_pair(fileIndex, 'N'));
long currentIndex;
Node *iterator;
char type;
while (!previousLevel.empty()) {
queue< pair<long, char> > nextLevel;
while (!previousLevel.empty()) {
// Get the front and pop
currentIndex = previousLevel.front().first;
iterator = new Node(currentIndex);
type = previousLevel.front().second;
previousLevel.pop();
// If it a seperator, print and move ahead
if (type == '|') {
cout << "|| ";
continue;
}
// Print all the keys
for (auto key : iterator->keys) {
cout << key << " ";
}
// Enqueue all the children
for (auto childIndex : iterator->childIndices) {
nextLevel.push(make_pair(childIndex, 'N'));
// Insert a marker to indicate end of child
nextLevel.push(make_pair(DEFAULT_LOCATION, '|'));
}
// Delete allocated memory
delete iterator;
}
// Seperate different levels
cout << endl << endl;
previousLevel = nextLevel;
}
}
void Node::insertNode(double key, long leftChildIndex, long rightChildIndex) {
// insert the new key to keys
long position = getKeyPosition(key);
keys.insert(keys.begin() + position, key);
// insert the newChild
childIndices.insert(childIndices.begin() + position + 1, rightChildIndex);
// commit changes to disk
commitToDisk();
#ifdef DEBUG_VERBOSE
cout << endl;
cout << "InsertNode : " << endl;
cout << "Base Node : ";
for (auto key : keys) {
cout << key << " ";
}
cout << endl;
// Print them out
Node *leftChild = new Node(leftChildIndex);
cout << "LeftNode : ";
for (auto key : leftChild->keys) {
cout << key << " ";
}
cout << endl;
delete leftChild;
Node *rightChild = new Node(rightChildIndex);
cout << "RightNode : ";
for (auto key : rightChild->keys) {
cout << key << " ";
}
cout << endl;
delete rightChild;
#endif
// If this overflows, we move again upward
if ((long)keys.size() > upperBound) {
splitInternal();
}
// Update the root if the element was inserted in the root
if (fileIndex == bRoot->getFileIndex()) {
bRoot->readFromDisk();
}
}
void Node::splitInternal() {
#ifdef DEBUG_VERBOSE
cout << endl;
cout << "SplitInternal : " << endl;
cout << "Base Node : ";
for (auto key : keys) {
cout << key << " ";
}
cout << endl;
#endif
// Create a surrogate internal node
Node *surrogateInternalNode = new Node();
surrogateInternalNode->setToInternalNode();
// Fix the keys of the new node
double startPoint = *(keys.begin() + lowerBound);
for (auto key = keys.begin() + lowerBound + 1; key != keys.end(); ++key) {
surrogateInternalNode->keys.push_back(*key);
}
// Resize the keys of the current node
keys.resize(lowerBound);
#ifdef DEBUG_VERBOSE
// Print them out
cout << "First InternalNode : ";
for (auto key : keys) {
cout << key << " ";
}
cout << endl;
cout << "Second InternalNode : ";
for (auto key : surrogateInternalNode->keys) {
cout << key << " ";
}
cout << endl;
cout << "Split At " << startPoint << endl;
#endif
// Partition children for the surrogateInternalNode
for (auto childIndex = childIndices.begin() + lowerBound + 1; childIndex != childIndices.end(); ++childIndex) {
surrogateInternalNode->childIndices.push_back(*childIndex);
// Assign parent to the children nodes
Node *tempChildNode = new Node(*childIndex);
tempChildNode->parentIndex = surrogateInternalNode->fileIndex;
tempChildNode->commitToDisk();
delete tempChildNode;
}
// Fix children for the current node
childIndices.resize(lowerBound + 1);
// If the current node is not a root node
if (parentIndex != DEFAULT_LOCATION) {
// Assign parents
surrogateInternalNode->parentIndex = parentIndex;
surrogateInternalNode->commitToDisk();
commitToDisk();
// Now we push up the splitting one level
Node *tempParent = new Node(parentIndex);
tempParent->insertNode(startPoint, fileIndex, surrogateInternalNode->fileIndex);
delete tempParent;
} else {
// Create a new parent node
Node *newParent = new Node();
newParent->setToInternalNode();
// Assign parents
surrogateInternalNode->parentIndex = newParent->fileIndex;
parentIndex = newParent->fileIndex;
// Insert the key into the keys
newParent->keys.push_back(startPoint);
// Insert the children
newParent->childIndices.push_back(fileIndex);
newParent->childIndices.push_back(surrogateInternalNode->fileIndex);
// Commit changes to disk
newParent->commitToDisk();
commitToDisk();
surrogateInternalNode->commitToDisk();
// Clean up the previous root node
delete bRoot;
// Reset the root node
bRoot = newParent;
}
// Clean the surrogateInternalNode
delete surrogateInternalNode;
}
void Node::splitLeaf() {
#ifdef DEBUG_VERBOSE
cout << endl;
cout << "SplitLeaf : " << endl;
cout << "Base Node : ";
for (auto key : keys) {
cout << key << " ";
}
cout << endl;
#endif
// Create a surrogate leaf node with the keys and object Pointers
Node *surrogateLeafNode = new Node();
for (long i = lowerBound; i < (long) keys.size(); ++i) {
DBObject object = DBObject(keys[i], objectPointers[i]);
surrogateLeafNode->insertObject(object);
}
// Resize the current leaf node and commit the node to disk
keys.resize(lowerBound);
objectPointers.resize(lowerBound);
#ifdef DEBUG_VERBOSE
// Print them out
cout << "First Leaf : ";
for (auto key : keys) {
cout << key << " ";
}
cout << endl;
cout << "Second Leaf : ";
for (auto key : surrogateLeafNode->keys) {
cout << key << " ";
}
cout << endl;
#endif
// Link up the leaves
long tempLeafIndex = nextLeafIndex;
nextLeafIndex = surrogateLeafNode->fileIndex;
surrogateLeafNode->nextLeafIndex = tempLeafIndex;
// If the tempLeafIndex is not null we have to load it and set its
// previous index
if (tempLeafIndex != DEFAULT_LOCATION) {
Node *tempLeaf = new Node(tempLeafIndex);
tempLeaf->previousLeafIndex = surrogateLeafNode->fileIndex;
tempLeaf->commitToDisk();
delete tempLeaf;
}
surrogateLeafNode->previousLeafIndex = fileIndex;
// Consider the case when the current node is not a root
if (parentIndex != DEFAULT_LOCATION) {
// Assign parents
surrogateLeafNode->parentIndex = parentIndex;
surrogateLeafNode->commitToDisk();
commitToDisk();
// Now we push up the splitting one level
Node *tempParent = new Node(parentIndex);
tempParent->insertNode(surrogateLeafNode->keys.front(), fileIndex, surrogateLeafNode->fileIndex);
delete tempParent;
} else {
// Create a new parent node
Node *newParent = new Node();
newParent->setToInternalNode();
// Assign parents
surrogateLeafNode->parentIndex = newParent->fileIndex;
parentIndex = newParent->fileIndex;
// Insert the key into the keys
newParent->keys.push_back(surrogateLeafNode->keys.front());
// Insert the children
newParent->childIndices.push_back(this->fileIndex);
newParent->childIndices.push_back(surrogateLeafNode->fileIndex);
// Commit to disk
newParent->commitToDisk();
surrogateLeafNode->commitToDisk();
commitToDisk();
// Clean up the root node
delete bRoot;
// Reset the root node
bRoot = newParent;
}
// Clean up surrogateNode
delete surrogateLeafNode;
}
// Insert a key into the BPlusTree
void insert(Node *root, DBObject object) {
// If the root is a leaf, we can directly insert
if (root->isLeaf()) {
// Insert object
root->insertObject(object);
// Split if required
if (root->size() > root->upperBound) {
root->splitLeaf();
}
#ifdef DEBUG_VERBOSE
// Serialize
bRoot->serialize();
#endif
} else {
// We traverse the tree
long position = root->getKeyPosition(object.getKey());
// Load the node from disk
Node *nextRoot = new Node(root->childIndices[position]);
// Recurse into the node
insert(nextRoot, object);
// Clean up
delete nextRoot;
}
}
// Point search in a BPlusTree
void pointQuery(Node *root, double searchKey) {
// If the root is a leaf, we can directly search
if (root->isLeaf()) {
// Print all nodes in the current leaf
for (long i = 0; i < (long) root->keys.size(); ++i) {
if (root->keys[i] == searchKey) {
#ifdef DEBUG_NORMAL
cout << root->keys[i] << " ";
#endif
#ifdef OUTPUT
cout << DBObject(root->keys[i], root->objectPointers[i]).getDataString() << endl;
#endif
}
}
// Check nextleaf for same node
if (root->nextLeafIndex != DEFAULT_LOCATION) {
// Load up the nextLeaf from disk
Node *tempNode = new Node(root->nextLeafIndex);
// Check in the nextLeaf and delegate
if (tempNode->keys.front() == searchKey) {
pointQuery(tempNode, searchKey);
}
delete tempNode;
}
} else {
// We traverse the tree
long position = root->getKeyPosition(searchKey);
// Load the node from disk
Node *nextRoot = new Node(root->childIndices[position]);
// Recurse into the node
pointQuery(nextRoot, searchKey);
// Clean up
delete nextRoot;
}
}
// window search
void windowQuery(Node *root, double lowerLimit, double upperLimit) {
// If the root is a leaf, we can directly search
if (root->isLeaf()) {
// Print all nodes in the current leaf which satisfy the criteria
for (long i = 0; i < (long) root->keys.size(); ++i) {
if (root->keys[i] >= lowerLimit && root->keys[i] <= upperLimit) {
#ifdef DEBUG_NORMAL
cout << root->keys[i] << " ";
#endif
#ifdef OUTPUT
cout << DBObject(root->keys[i], root->objectPointers[i]).getDataString() << endl;
#endif
}
}
// If the nextLeafNode is not null
if (root->nextLeafIndex != DEFAULT_LOCATION) {
Node *tempNode= new Node(root->nextLeafIndex);
// Check for condition and recurse
if (tempNode->keys.front() >= lowerLimit && tempNode->keys.front() <=upperLimit) {
windowQuery(tempNode, lowerLimit, upperLimit);
}
// Delete the tempNode
delete tempNode;
}
} else {
// We traverse the tree
long position = root->getKeyPosition(lowerLimit);
// Load the node from disk
Node *nextRoot = new Node(root->childIndices[position]);
// Recurse into the node
windowQuery(nextRoot, lowerLimit, upperLimit);
// Clean up
delete nextRoot;
}
}
//rangesearch
void rangeQuery(Node *root, double center, double range) {
double upperBound = center + range;
double lowerBound = (center - range >= 0) ? center - range : 0;
// Call windowQuery internally
windowQuery(root, lowerBound, upperBound);
}
// kNN query
void kNNQuery(Node *root, double center, long k) {
// If the root is a leaf, we can directly search
if (root->isLeaf()) {
vector< pair<double, long> > answers;
// We traverse the tree
long position = root->getKeyPosition(center);
// Get k keys from ahead
long count = 0;
for (long i = position; i < (long)root->keys.size(); ++i, ++count) {
answers.push_back(make_pair(root->keys[i], root->objectPointers[i]));
}
// Now check for leaves in front
long nextIndex = root->nextLeafIndex;
while (count < k && nextIndex != DEFAULT_LOCATION) {
Node tempNode = Node(nextIndex);
for (long i = 0; i < (long) tempNode.keys.size(); ++i, ++ count) {
answers.push_back(make_pair(tempNode.keys[i], tempNode.objectPointers[i]));
}
// Update the nextIndex
nextIndex = tempNode.nextLeafIndex;
}
// Get k keys from behind
count = 0;
for (long i = 0; i < (long) position; ++i, ++count) {
answers.push_back(make_pair(root->keys[i], root->objectPointers[i]));
}
// Check for leaves behind
long previousIndex = root->previousLeafIndex;
while (count < k && previousIndex != DEFAULT_LOCATION) {
Node tempNode = Node(previousIndex);
for (long i = 0; i < (long) tempNode.keys.size(); ++i, ++ count) {
answers.push_back(make_pair(tempNode.keys[i], tempNode.objectPointers[i]));
}
// Update the nextIndex
previousIndex = tempNode.previousLeafIndex;
}
// Sort the obtained answers
sort(answers.begin(), answers.end(),
[&](pair<double, long> T1, pair<double, long> T2) {
return (abs(T1.first - center) < abs(T2.first - center));
});
// Print the answers
pair <double, long> answer;
for (long i = 0; i < k && i < (long) answers.size(); ++i) {
answer = answers[i];
#ifdef DEBUG_NORMAL
cout << answer.first << " ";
#endif
#ifdef OUTPUT
cout << DBObject(answer.first, answer.second).getDataString() << endl;
#endif
}
} else {
// We traverse the tree
long position = root->getKeyPosition(center);
// Load the node from disk
Node *nextRoot = new Node(root->childIndices[position]);
// Recurse into the node
kNNQuery(nextRoot, center, k);
// Clean up
delete nextRoot;
}
}
void storeSession() {
// Create a character buffer which will be written to disk
long location = 0;
char buffer[Node::pageSize];
// Store root's fileIndex
long fileIndex = bRoot->getFileIndex();
memcpy(buffer + location, &fileIndex, sizeof(fileIndex));
location += sizeof(fileIndex);
// Store the fileCount
memcpy(buffer + location, &Node::fileCount, sizeof(Node::fileCount));
location += sizeof(Node::fileCount);
// Store the objectCount for DBObject
memcpy(buffer + location, &DBObject::objectCount, sizeof(DBObject::objectCount));
location += sizeof(DBObject::objectCount);
// Create a binary file and write to memory
ofstream sessionFile;
sessionFile.open(SESSION_FILE, ios::binary|ios::out);
sessionFile.write(buffer, Node::pageSize);
sessionFile.close();
}
void loadSession() {
// Create a character buffer which will be written to disk
long location = 0;
char buffer[Node::pageSize];
// Open the binary file ane read into memory
ifstream sessionFile;
sessionFile.open(SESSION_FILE, ios::binary|ios::in);
sessionFile.read(buffer, Node::pageSize);
sessionFile.close();
// Retrieve the fileIndex
long fileIndex;
memcpy((char *) &fileIndex, buffer + location, sizeof(fileIndex));
location += sizeof(fileIndex);
// Retreive the fileCount
long fileCount;
memcpy((char *) &fileCount, buffer + location, sizeof(fileCount));
location += sizeof(fileCount);
// Retrieve the objectCount
long objectCount;
memcpy((char *) &objectCount, buffer + location, sizeof(objectCount));
location += sizeof(objectCount);
// Store the session variables
Node::fileCount = fileCount;
DBObject::objectCount = objectCount;
delete bRoot;
bRoot = new Node(fileIndex);
bRoot->readFromDisk();
}
}
using namespace BPlusTree;
void buildTree() {