-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBtree.cpp
More file actions
606 lines (532 loc) · 19.4 KB
/
Btree.cpp
File metadata and controls
606 lines (532 loc) · 19.4 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
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
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
// Constants and parameters for the B-Tree
static const string MAGIC_NUMBER = "4337PRJ3";
static const int HEADER_SIZE = 512;
static const size_t BLOCK_SIZE = 512;
static const int MIN_DEGREE = 10;
static const int MAX_KEYS = (2 * MIN_DEGREE - 1); // Maximum number of keys in a node: 19
static const int MAX_CHILDREN = (2 * MIN_DEGREE); // Maximum number of children in a node: 20
// Forward declarations of big-endian functions (implemented elsewhere)
uint64_t hostToBig(uint64_t x);
uint64_t bigToHost(uint64_t x);
// B-Tree node structure stored on disk
struct BTreeNode {
uint64_t blockId; // The block ID where this node is stored
uint64_t parentId; // The block ID of this node's parent, 0 if root
uint64_t numKeys; // Number of keys currently in this node
uint64_t keys[MAX_KEYS]; // Array of keys
uint64_t values[MAX_KEYS]; // Array of values corresponding to the keys
uint64_t children[MAX_CHILDREN]; // Array of child block IDs
bool isLeaf; // Flag indicating if the node is a leaf (no children)
BTreeNode() {
memset(this, 0, sizeof(BTreeNode));
}
};
class BTree {
private:
fstream file; // File stream for reading/writing the index file
string fileName; // Name of the currently opened file
uint64_t rootBlockId; // Block ID of the root node
uint64_t nextBlockId; // Next available block ID for new nodes
bool fileOpen; // Flag indicating if a file is currently open
// Write the B-Tree header into the file (contains magic number, root ID, next block ID)
void writeHeader() {
file.seekp(0, ios::beg);
char header[HEADER_SIZE];
memset(header, 0, HEADER_SIZE);
// Copy magic number to header
memcpy(header, MAGIC_NUMBER.c_str(), MAGIC_NUMBER.size());
// Convert and store rootBlockId and nextBlockId in big-endian
uint64_t beRoot = hostToBig(rootBlockId);
uint64_t beNext = hostToBig(nextBlockId);
memcpy(header+8, &beRoot, sizeof(beRoot));
memcpy(header+16, &beNext, sizeof(beNext));
// Write header to file
file.write(header, HEADER_SIZE);
file.flush();
}
// Read and validate the B-Tree header from the file
void readHeader() {
file.seekg(0, ios::beg);
char header[HEADER_SIZE] = {0};
file.read(header, HEADER_SIZE);
// Check if the header is correctly sized
if ((size_t)file.gcount() < HEADER_SIZE) {
throw runtime_error("Invalid file header.");
}
// Validate the magic number
if (strncmp(header, MAGIC_NUMBER.c_str(), MAGIC_NUMBER.size()) != 0) {
throw runtime_error("Magic number mismatch.");
}
// Extract and convert rootBlockId and nextBlockId from header
uint64_t beRoot = 0;
uint64_t beNext = 0;
memcpy(&beRoot, header+8, sizeof(beRoot));
memcpy(&beNext, header+16, sizeof(beNext));
rootBlockId = bigToHost(beRoot);
nextBlockId = bigToHost(beNext);
}
// Load a node from the file at a given block ID
BTreeNode loadNode(uint64_t blockId) {
file.seekg(blockId * BLOCK_SIZE, ios::beg);
uint8_t buffer[BLOCK_SIZE];
memset(buffer, 0, BLOCK_SIZE);
file.read((char*)buffer, BLOCK_SIZE);
BTreeNode node;
// Extract blockId, parentId, and numKeys from the node block
uint64_t beBlockId, beParentId, beNumKeys;
memcpy(&beBlockId, buffer, 8);
memcpy(&beParentId, buffer+8, 8);
memcpy(&beNumKeys, buffer+16, 8);
node.blockId = bigToHost(beBlockId);
node.parentId = bigToHost(beParentId);
node.numKeys = bigToHost(beNumKeys);
// Extract keys
for (int i=0; i<MAX_KEYS; i++) {
uint64_t beKey;
memcpy(&beKey, buffer+24+(i*8), 8);
node.keys[i] = bigToHost(beKey);
}
// Extract values
for (int i=0; i<MAX_KEYS; i++) {
uint64_t beVal;
memcpy(&beVal, buffer+24+(MAX_KEYS*8)+(i*8), 8);
node.values[i] = bigToHost(beVal);
}
// Extract children block IDs
for (int i=0; i<MAX_CHILDREN; i++) {
uint64_t beChild;
memcpy(&beChild, buffer+24+(MAX_KEYS*8)+(MAX_KEYS*8)+(i*8), 8);
node.children[i] = bigToHost(beChild);
}
// Determine if the node is a leaf (no children)
bool leaf = true;
for (int i=0; i<MAX_CHILDREN; i++) {
if (node.children[i] != 0) {
leaf = false;
break;
}
}
node.isLeaf = leaf;
return node;
}
// Save a node's data to the file at its block ID
void saveNode(const BTreeNode &node) {
file.seekp(node.blockId * BLOCK_SIZE, ios::beg);
uint8_t buffer[BLOCK_SIZE];
memset(buffer, 0, BLOCK_SIZE);
// Convert fields to big-endian before writing
uint64_t beBlockId = hostToBig(node.blockId);
uint64_t beParentId = hostToBig(node.parentId);
uint64_t beNumKeys = hostToBig(node.numKeys);
memcpy(buffer, &beBlockId, 8);
memcpy(buffer+8, &beParentId, 8);
memcpy(buffer+16, &beNumKeys, 8);
// Store keys
for (int i=0; i<MAX_KEYS; i++) {
uint64_t beKey = hostToBig(node.keys[i]);
memcpy(buffer+24+(i*8), &beKey, 8);
}
// Store values
for (int i=0; i<MAX_KEYS; i++) {
uint64_t beVal = hostToBig(node.values[i]);
memcpy(buffer+24+(MAX_KEYS*8)+(i*8), &beVal, 8);
}
// Store children
for (int i=0; i<MAX_CHILDREN; i++) {
uint64_t beChild = hostToBig(node.children[i]);
memcpy(buffer+24+(MAX_KEYS*8)+(MAX_KEYS*8)+(i*8), &beChild, 8);
}
// Write node block to file
file.write((char*)buffer, BLOCK_SIZE);
file.flush();
}
// Allocate a new node on disk, updating nextBlockId and header
BTreeNode allocateNode(bool leaf) {
BTreeNode node;
node.blockId = nextBlockId++;
node.parentId = 0;
node.numKeys = 0;
saveNode(node);
writeHeader();
return node;
}
// Search for a key in the B-Tree, return true if found and set valueOut
bool searchKey(uint64_t blockId, uint64_t key, uint64_t &valueOut) {
if (blockId == 0) return false; // No such node
BTreeNode node = loadNode(blockId);
int i = 0;
// Find the position of the key or where it would be inserted
while (i < (int)node.numKeys && key > node.keys[i]) i++;
// If key is found in this node, return its value
if (i < (int)node.numKeys && key == node.keys[i]) {
valueOut = node.values[i];
return true;
}
// If leaf, key not found
if (node.isLeaf) {
return false;
}
// Otherwise, search in the appropriate child
return searchKey(node.children[i], key, valueOut);
}
// Check if a key already exists in the B-Tree
bool keyExists(uint64_t key) {
if (rootBlockId == 0) return false; // Empty tree
uint64_t dummy;
return searchKey(rootBlockId, key, dummy);
}
// Insert a key/value pair into the B-Tree
void insertKey(uint64_t key, uint64_t value) {
if (rootBlockId == 0) {
// Tree is empty, create a new root node
BTreeNode root = allocateNode(true);
root.numKeys = 1;
root.keys[0] = key;
root.values[0] = value;
saveNode(root);
rootBlockId = root.blockId;
writeHeader();
return;
}
// If root is full, split it before inserting
BTreeNode root = loadNode(rootBlockId);
if (root.numKeys == MAX_KEYS) {
BTreeNode newRoot = allocateNode(false);
newRoot.children[0] = root.blockId;
root.parentId = newRoot.blockId;
saveNode(root);
// Split the old root and create a new root
splitChild(newRoot, 0, root.blockId);
saveNode(newRoot);
rootBlockId = newRoot.blockId;
writeHeader();
insertNonFull(newRoot, key, value);
} else {
// Root not full, insert into it
insertNonFull(root, key, value);
}
}
// Insert into a node that is guaranteed not to be full
void insertNonFull(BTreeNode node, uint64_t key, uint64_t value) {
while (true) {
int i = (int)node.numKeys - 1;
if (node.isLeaf) {
// Insert key/value into leaf node
while (i>=0 && key < node.keys[i]) {
node.keys[i+1] = node.keys[i];
node.values[i+1] = node.values[i];
i--;
}
// Check for duplicate key
if (i>=0 && node.keys[i] == key) {
// Key already exists, abort
saveNode(node);
throw runtime_error("Key already exists.");
}
node.keys[i+1] = key;
node.values[i+1] = value;
node.numKeys++;
saveNode(node);
return;
} else {
// Insert into internal node: find child to descend into
while (i>=0 && key < node.keys[i]) i--;
i++;
uint64_t childId = node.children[i];
BTreeNode child = loadNode(childId);
// If child is full, split it before descending
if ((int)child.numKeys == MAX_KEYS) {
splitChild(node, i, childId);
node = loadNode(node.blockId);
if (key > node.keys[i]) {
i++;
}
}
// Descend into the appropriate child
uint64_t newChildId = node.children[i];
BTreeNode newChild = loadNode(newChildId);
node = newChild; // Continue insertion in the chosen child
}
}
}
// Split a full child node into two and adjust the parent node accordingly
void splitChild(BTreeNode &parent, int index, uint64_t childId) {
BTreeNode child = loadNode(childId);
BTreeNode newChild = allocateNode(child.isLeaf);
newChild.parentId = parent.blockId;
// Move the upper half of child's keys/values to newChild
newChild.numKeys = MIN_DEGREE - 1;
for (int j=0; j<MIN_DEGREE-1; j++) {
newChild.keys[j] = child.keys[j+MIN_DEGREE];
newChild.values[j] = child.values[j+MIN_DEGREE];
}
// Move the upper half of child's children if not a leaf
if (!child.isLeaf) {
for (int j=0; j<MIN_DEGREE; j++) {
newChild.children[j] = child.children[j+MIN_DEGREE];
if (newChild.children[j] != 0) {
BTreeNode temp = loadNode(newChild.children[j]);
temp.parentId = newChild.blockId;
saveNode(temp);
}
}
}
// Adjust the old child node's number of keys
child.numKeys = MIN_DEGREE - 1;
saveNode(child);
saveNode(newChild);
// Insert newChild into parent
for (int j=(int)parent.numKeys; j>=index+1; j--) {
parent.children[j+1] = parent.children[j];
}
parent.children[index+1] = newChild.blockId;
// Move the middle key/value from child to parent
for (int j=(int)parent.numKeys-1; j>=index; j--) {
parent.keys[j+1] = parent.keys[j];
parent.values[j+1] = parent.values[j];
}
parent.keys[index] = child.keys[MIN_DEGREE-1];
parent.values[index] = child.values[MIN_DEGREE-1];
parent.numKeys++;
saveNode(parent);
}
// Print all keys/values in ascending order (in-order traversal)
void printInOrder(uint64_t blockId) {
if (blockId == 0) return; // No node
BTreeNode node = loadNode(blockId);
// Traverse the children and keys in order
for (int i=0; i<(int)node.numKeys; i++) {
printInOrder(node.children[i]);
cout << node.keys[i] << " " << node.values[i] << "\n";
}
// Print from the last child
printInOrder(node.children[node.numKeys]);
}
// Extract all keys/values in ascending order to a file
void extractInOrder(uint64_t blockId, ofstream &out) {
if (blockId == 0) return;
BTreeNode node = loadNode(blockId);
for (int i=0; i<(int)node.numKeys; i++) {
extractInOrder(node.children[i], out);
out << node.keys[i] << "," << node.values[i] << "\n";
}
extractInOrder(node.children[node.numKeys], out);
}
// Load key/value pairs from a CSV file and insert them
void loadFromFile(const string &inputFile) {
ifstream in(inputFile);
if (!in.is_open()) {
throw runtime_error("Unable to open input file for load.");
}
string line;
while (getline(in, line)) {
if (line.empty()) continue;
size_t pos = line.find(',');
if (pos == string::npos) {
cerr << "Invalid line format in load file: " << line << "\n";
continue;
}
string k = line.substr(0, pos);
string v = line.substr(pos+1);
uint64_t key, value;
try {
key = stoull(k);
value = stoull(v);
} catch (...) {
cerr << "Invalid integer in line: " << line << "\n";
continue;
}
// Skip if key already exists
if (keyExists(key)) {
cerr << "Error: key " << key << " already exists. Skipping.\n";
continue;
}
try {
insertKey(key, value);
} catch (runtime_error &e) {
cerr << "Error inserting key " << key << ": " << e.what() << "\n";
}
}
}
public:
BTree() {
fileOpen = false;
rootBlockId = 0;
nextBlockId = 1;
}
// Create a new B-Tree index file
void createFile() {
cout << "Enter the file name to create: ";
string fname; cin >> fname;
{
// Check if file already exists
ifstream test(fname, ios::binary);
if (test.is_open()) {
cout << "File already exists. Overwrite? (y/n): ";
char c; cin >> c;
if (c!='y' && c!='Y') {
cout << "File creation aborted.\n";
return;
}
}
}
fileName = fname;
// Create/truncate the file
file.open(fileName, ios::binary | ios::trunc | ios::in | ios::out);
if (!file.is_open()) {
cerr << "Error: Unable to create file.\n";
return;
}
// Initialize empty tree header
rootBlockId = 0;
nextBlockId = 1;
writeHeader();
fileOpen = true;
cout << "File created successfully.\n";
}
// Open an existing B-Tree index file
void openFile() {
cout << "Enter the file name to open: ";
string fname; cin >> fname;
fileName = fname;
file.open(fileName, ios::binary | ios::in | ios::out);
if (!file.is_open()) {
cerr << "Error: File does not exist.\n";
return;
}
try {
readHeader();
} catch (runtime_error &e) {
cerr << "Error: " << e.what() << "\n";
file.close();
return;
}
fileOpen = true;
cout << "File opened successfully.\n";
}
// Insert command: prompt user for key/value and insert
void insertCommand() {
if (!fileOpen) {
cerr << "Error: No index file is open.\n";
return;
}
cout << "Enter key and value: ";
uint64_t key, value;
if (!(cin >> key >> value)) {
cerr << "Error: Invalid input.\n";
cin.clear(); cin.ignore(10000,'\n');
return;
}
// Check for duplicate
if (keyExists(key)) {
cerr << "Error: Key already exists.\n";
return;
}
try {
insertKey(key, value);
} catch (runtime_error &e) {
cerr << "Error: " << e.what() << "\n";
}
}
// Search command: prompt user for key and search the B-Tree
void searchCommand() {
if (!fileOpen) {
cerr << "Error: No index file is open.\n";
return;
}
cout << "Enter key: ";
uint64_t key;
if (!(cin >> key)) {
cerr << "Error: Invalid input.\n";
cin.clear(); cin.ignore(10000,'\n');
return;
}
if (rootBlockId == 0) {
cerr << "Error: Key not found.\n";
return;
}
uint64_t value;
if (searchKey(rootBlockId, key, value)) {
cout << key << " " << value << "\n";
} else {
cerr << "Error: Key not found.\n";
}
}
// Load command: load key/value pairs from a given CSV file
void loadCommand() {
if (!fileOpen) {
cerr << "Error: No index file is open.\n";
return;
}
cout << "Enter the file name to load from: ";
string fname; cin >> fname;
try {
loadFromFile(fname);
} catch (runtime_error &e) {
cerr << "Error: " << e.what() << "\n";
}
}
// Print command: print all keys/values in ascending order
void printCommand() {
if (!fileOpen) {
cerr << "Error: No index file is open.\n";
return;
}
if (rootBlockId == 0) {
// Empty tree, nothing to print
return;
}
printInOrder(rootBlockId);
}
// Extract command: write all keys/values to a specified file in ascending order
void extractCommand() {
if (!fileOpen) {
cerr << "Error: No index file is open.\n";
return;
}
cout << "Enter the file name to extract to: ";
string fname; cin >> fname;
// Check if file exists
{
ifstream test(fname);
if (test.is_open()) {
cout << "File already exists. Overwrite? (y/n): ";
char c; cin >> c;
if (c!='y' && c!='Y') {
cout << "Extract aborted.\n";
return;
}
}
}
ofstream out(fname);
if (!out.is_open()) {
cerr << "Error: Unable to open output file.\n";
return;
}
if (rootBlockId != 0) {
extractInOrder(rootBlockId, out);
}
out.close();
cout << "Extract completed.\n";
}
// Close the currently open file and reset state
void closeFile() {
if (file.is_open()) {
file.close();
}
fileOpen = false;
rootBlockId = 0;
nextBlockId = 1;
}
// Destructor: ensure file is closed
~BTree() {
closeFile();
}
};