-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTreeNode.cpp
More file actions
590 lines (515 loc) · 12 KB
/
Copy pathBTreeNode.cpp
File metadata and controls
590 lines (515 loc) · 12 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
/*
* This file is part of PRODNAME.
*
* See the COPYRIGHT file at the top-level directory of this distribution
* for details of code ownership.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "BTree.h"
#include "BTreeNode.h"
#include "DataStore.h"
BTreeNode::BTreeNode(std::shared_ptr<BTree> b)
{
_KeyCount = 0;
datastore = NULL;
position = 0;
Keys = NULL;
Base = b;
leaf = false;
Next = 0;
Prev = 0;
}
BTreeNode::~BTreeNode()
{
if (Keys != NULL)
{
for (int x = 0; x < _KeyCount; x++)
{
if (Keys[x] != NULL)
delete Keys[x];
}
delete[]Keys;
Keys = NULL;
}
}
int BTreeNode::KeyCount()
{
return _KeyCount;
}
bool BTreeNode::Load(DataStore *ds, std::streamoff pos)
{
datastore = ds;
position = pos;
int offset = 0;
char *buf = datastore->ReadBlock(position);
if (buf == NULL) return false;
memcpy(&_KeyCount, buf, sizeof(short));
if (_KeyCount < 0)
{
_KeyCount = -(_KeyCount + 1);
leaf = true;
}
else
{
leaf = false;
}
offset += sizeof(short);
memcpy( &Prev, buf + offset, sizeof(std::streamoff));
offset += sizeof(std::streamoff);
memcpy(&Next, buf + offset, sizeof(std::streamoff));
offset += sizeof(std::streamoff);
Keys = new BTreeKey * [Base->KeysPerNode(leaf)];
if (_KeyCount > Base->KeysPerNode(leaf))
{
cout << "CRITICAL FILE ERROR: Node contains too many keys!" << endl;
delete buf;
return false;
}
BTreeKeyTemplate* Tmpl = &Base->BranchTemplate;
if (leaf == true) Tmpl = &Base->LeafTemplate;
for (int x = 0; x < Base->KeysPerNode(leaf); x++)
{
Keys[x] = NULL;
}
for (int x = 0; x < _KeyCount; x++)
{
if (x < _KeyCount)
{
Keys[x] = new BTreeKey(Tmpl);
offset += Keys[x]->Read(buf + offset);
}
}
delete buf;
return true;
}
std::streamoff BTreeNode::Create(DataStore* ds, bool leaf)
{
datastore = ds;
position = ds->AllocateBlock();
this->leaf = leaf;
BTreeKeyTemplate* Tmpl = &Base->BranchTemplate;
if (leaf == true) Tmpl = &Base->LeafTemplate;
int KeyCount = Base->KeysPerNode(leaf);
Keys = new BTreeKey * [Base->KeysPerNode(leaf)];
for (int x = 0; x < KeyCount; x++)
{
Keys[x] = new BTreeKey(Tmpl);
}
return position;// Write();
}
bool BTreeNode::Write()
{
int offset = 0;
char *buf = datastore->InitBlock();
short s = _KeyCount;
if (leaf == true)
s = -_KeyCount-1;
memcpy(buf, &s, 2);
offset+=2;
memcpy(buf + offset, &Prev, sizeof(std::streamoff));
offset += sizeof(std::streamoff);
memcpy(buf + offset, &Next, sizeof(std::streamoff));
offset += sizeof(std::streamoff);
if (_KeyCount > 0)
{
int tkeylen = Keys[0]->Length();
//int offset = 1;
for (int x = 0; x < _KeyCount; x++)
{
Keys[x]->Write(buf + offset);
offset += tkeylen;
}
}
datastore->Mark(buf, "BTREN", 5);
datastore->WriteBlock(position, buf);
delete []buf;
return true;
}
BTreeSearchChain BTreeNode::Search(const void* key, int mode)
{
BTreeSearchChain Chain;
ChainElement CE;
CE.location = this->position;
CE.index = -1;
CE.changed = false;
Chain.sequence.push_back(CE);
_Search(key, mode, &Chain);
return Chain;
}
bool BTreeNode::_Search(const void* key, int mode, BTreeSearchChain* Chain)
{
BTreeKey* Closest = NULL;
int cmp = 0;
int ClosestIndex = 0;
int HighestIndex = -1;
for (int x = 0; x < _KeyCount; x++)
{
if (Keys[x] != NULL)
{
if ((Keys[x]->Code() & KEYCODE_DELETED) > 0)
continue;
cmp = Keys[x]->CompareTo(key);
if (cmp == 0)
{
if ((mode != BTREE_SEARCH_NEXTEXC) || ((leaf == false) && (mode == BTREE_SEARCH_NEXTEXC)))
{
if (leaf == true)
{
Closest = Keys[x];
Chain->Key = new BTreeKey(*Closest);
return true;
}
else
{
Closest = Keys[x];
ClosestIndex = x;
break;
}
}
}
if (cmp < 0)
{
Closest = Keys[x];
ClosestIndex = x;
}
if (cmp > 0)
{
if (mode == BTREE_SEARCH_PREVIOUS)
{
if (Closest != NULL)
{
Chain->Key = new BTreeKey(*Closest); //Closest
}
else
{
Closest = Keys[0];
Chain->Key = new BTreeKey(*Keys[0]); //First
}
ClosestIndex = x-1;
break;
}
if ((mode == BTREE_SEARCH_NEXT) || (mode == BTREE_SEARCH_NEXTEXC))
{
Closest = Keys[x];
ClosestIndex = x;
HighestIndex = x;
Chain->Key = new BTreeKey(*Keys[x]); //Closest
}
}
}
}
if ((leaf == true) && (mode == BTREE_SEARCH_NEXT))
{
if (HighestIndex == -1)
{
Closest = NULL;
ClosestIndex = -1;
}
}
if (Closest != NULL)
{
if (leaf == false)
{
if (Base->Root->position == this->position)
{
Chain->sequence[Chain->sequence.size()-1].index = ClosestIndex;
}
std::streamoff offset = *(std::streamoff *)Closest->Payload();
BTreeNode *N = new BTreeNode(Base);
N->Load(datastore, offset);
ChainElement CE;
CE.location = N->position;
CE.index = -1;
Chain->sequence.push_back(CE);
N->_Search(key, mode, Chain);
if (Chain->Key != NULL)
{
delete N;
return true;
}
Chain->sequence.pop_back();
delete N;
}
else
{
if ((mode == BTREE_SEARCH_PREVIOUS) || (mode == BTREE_SEARCH_NEXT) || (mode == BTREE_SEARCH_NEXTEXC))
{
Chain->Key = new BTreeKey(*Closest);
Chain->sequence[Chain->sequence.size() - 1].index = ClosestIndex + 1;
return Closest;
}
}
}
//Chain->sequence.pop_back();
return false;
}
std::streamoff BTreeNode::_Split(int SplitPoint)
{
//Figure out how many we are splitting off...
int RemoveTo = (int)(_KeyCount / 2);
if (SplitPoint == BTREE_SPLITMODE_END)
{
RemoveTo = _KeyCount;
}
//Create the new Node...
BTreeNode* A = new BTreeNode(Base);
A->Create(datastore,leaf);
//Transfer the excess keys...
int DestIndex = 0;
for (int x = RemoveTo + 1; x < _KeyCount; x++)
{
A->Keys[DestIndex]->Set(Keys[x]->Key(), Keys[x]->Payload());
DestIndex++;
}
A->_KeyCount = _KeyCount - RemoveTo;
_KeyCount = RemoveTo;
Write();
Next = A->position;
A->Write();
std::streamoff Pos = A->position;
delete A;
return Pos;
}
bool BTreeNode::_Add(const void* key, const void* value, BTreeSearchChain* Chain, int Depth, int SplitMode)
{
//Is there room in here?
//Re-calculate the split point...
BTreeKey* Closest = NULL;
int cmp = 0;
int ClosestIndex = 0;
int HighestIndex = -1;
for (int x = 0; x <= _KeyCount; x++)
{
if (x == _KeyCount)
{
ClosestIndex = x;
break;
}
if (Keys[x] != NULL)
{
cmp = Keys[x]->CompareTo(key);
if (cmp == 0)
{
//Existing Key!
return true;
}
if (cmp < 0)
{
Closest = Keys[x];
ClosestIndex = x;
}
if (cmp > 0)
{
ClosestIndex = x - 1;
break;
}
}
}
Chain->sequence[Depth].index = ClosestIndex;
//Now, begin adding...
Chain->sequence[Depth].changed = true;
if (_KeyCount >= this->Base->KeysPerNode(leaf))
{
//This won't fit here - begin splitting a key
if (Depth > 0)
{
//Create a new, split mode.
std::streamoff newnode = _Split(SplitMode);
BTreeNode* NewNode = new BTreeNode(Base);
NewNode->Load(datastore, newnode);
Next = newnode;
//Figure out which split the new key lives in...
if (NewNode->_KeyCount == 0)
{
Chain->sequence[Depth].index = -1;
NewNode->_Add(key, value, Chain, Depth, SplitMode);
}
else
{
if (NewNode->Keys[0]->CompareTo(key) >= 0)
{
Chain->sequence[Depth].index = -1;
NewNode->_Add(key, value, Chain, Depth, SplitMode);
}
else
{
Chain->sequence[Depth].index = -1;
_Add(key, value, Chain, Depth, SplitMode);
}
}
//Update the parent node
std::streamoff ParentPos = Chain->sequence[Depth - 1].location;
BTreeNode* Parent = new BTreeNode(Base);
Parent->Load(datastore,ParentPos);
//Find insertion point...
Parent->_Add(NewNode->Keys[0]->Key(), &NewNode->position, Chain, Depth - 1, SplitMode);
NewNode->Write();
Write();
}
else
{
//Splitting the root node is more complicated.
//Create a new, split node
std::streamoff newnode = _Split(SplitMode);
BTreeNode* NewNode = new BTreeNode(Base);
NewNode->Load(datastore, newnode);
//Create a new node for THIS node.
BTreeNode* Branch = new BTreeNode(Base);
Branch->Create(datastore, false);
//Change the file storage positions around
std::streamoff currentpos = position;
this->position = Branch->position;
Branch->position = currentpos;
this->Next = NewNode->position;
Write();
//Create new parent records...
currentpos = position;
void* splitkey = new char[Base->BranchTemplate.KeySize];
memcpy(splitkey, Keys[0]->Key(), Base->BranchTemplate.KeySize);
Chain->sequence[Depth].index = -1;
Branch->_Add(splitkey, ¤tpos,Chain,0,0);
delete splitkey;
if (NewNode->KeyCount() > 0)
{
currentpos = NewNode->position;
Branch->_Add(NewNode->Keys[0]->Key(), ¤tpos, Chain, 0, 0);
}
else
{
//Chain->index = -1;
Chain->sequence[Depth].index = -1;
currentpos = NewNode->position;
Branch->_Add(key, ¤tpos, Chain, Depth, 0);
if ((NewNode->KeyCount() == 0) || ((NewNode->Keys[0] != NULL) && (NewNode->Keys[0]->CompareTo(key) >= 0)))
{
Chain->sequence[Depth].index = -1;
NewNode->_Add(key, value, Chain, Depth, SplitMode);
//NewNode->_Add(key, value, Chain, Depth, SplitMode);
}
else
{
_Add(key, value, Chain, Depth, SplitMode);
}
}
Branch->Write();
Base->Root->Load(datastore,Base->Root->position);
int q = 0;
}
}
else
{
//This should fit
int index = Chain->sequence[Depth].index;
//if (index != -1) index++;
if ((index == -1) || (index >= _KeyCount))
{
if (index == -1) index = _KeyCount;
if (Keys[index] == NULL)
{
if (leaf == false)
Keys[index] = new BTreeKey(&Base->BranchTemplate);
else
Keys[index] = new BTreeKey(&Base->LeafTemplate);
}
Keys[index]->Set(key, value);
_KeyCount++;
}
else
{
//Nudge Existing Keys Forward...
int last = Base->KeysPerNode(leaf) - 1;
index++;
if (Keys[last] != NULL)
{
delete Keys[last];
}
for (int x = last-1; x >= index;x--)
{
Keys[x + 1] = Keys[x];
}
if (leaf == false)
Keys[index] = new BTreeKey(&Base->BranchTemplate);
else
Keys[index] = new BTreeKey(&Base->LeafTemplate);
Keys[index]->Set(key, value);
_KeyCount++;
}
return Write();
}
return true;
}
void BTreeNode::Copy(BTreeNode* N)
{
leaf = N->leaf;
if (Keys != NULL)
{
for (int x = 0; x < _KeyCount; x++)
{
if (Keys[x] != NULL)
delete Keys[x];
}
delete[]Keys;
Keys = NULL;
}
Keys = new BTreeKey * [Base->KeysPerNode(leaf)];
BTreeKeyTemplate* Tmpl = &Base->BranchTemplate;
if (leaf == true) Tmpl = &Base->LeafTemplate;
for (int x = 0; x < Base->KeysPerNode(leaf); x++)
{
Keys[x] = NULL;
}
_KeyCount = N->_KeyCount;
for (int x = 0; x < _KeyCount; x++)
{
Keys[x] = NULL;
if (x < _KeyCount)
{
Keys[x] = new BTreeKey(Tmpl);
Keys[x]->Set(N->Keys[x]->Key(), N->Keys[x]->Payload());
}
}
}
BTreeNode* BTreeNode::FirstLeaf()
{
if (this->leaf == true)
return this;
BTreeNode* Active = this;
while (Active != NULL)
{
if (Active->leaf == true) return Active;
BTreeNode* NextNode = new BTreeNode(Base);
NextNode->Load(Base->GetDataStore().get(), *((std::streamoff*)Active->Keys[0]->Payload()));
if (Active != this)
{
delete Active;
}
Active = NextNode;
}
return NULL;
}
BTreeSearchChain::BTreeSearchChain()
{
found = false;
Key = NULL;
//index = -1;
}
BTreeSearchChain::~BTreeSearchChain()
{
/*if (Key != NULL)
{
delete Key;
}*/
}