-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.cpp
executable file
·353 lines (294 loc) · 9.73 KB
/
btree.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
#include <iostream> // std::cerr
#include <stdexcept>
#include "btree.h"
using namespace std;
BTreeIndex::BTreeIndex(DbRelation& relation, Identifier name, ColumnNames key_columns, bool unique)
: DbIndex(relation, name, key_columns, unique),
closed(true),
stat(nullptr),
root(nullptr),
file(relation.get_table_name() + "-" + name),
key_profile() {
if (!unique)
throw DbRelationError("BTree index must have unique key");
build_key_profile();
}
//Build Profile
//Figure out the data types of each key component and encode them in self.key_profile,
// a list of int/str classes.
void BTreeIndex::build_key_profile(){
ColumnAttributes* column_attributes = this->relation.get_column_attributes(this->key_columns);
for (ColumnAttribute col: *column_attributes) {
this->key_profile.push_back(col.get_data_type());
}
}
//destructor
BTreeIndex::~BTreeIndex() {
delete(this->stat);
delete(this->root);
}
// Create the index.
void BTreeIndex::create() {
this->file.create();
this->stat = new BTreeStat(this->file, this->STAT, this->STAT + 1, this->key_profile);
this->root = new BTreeLeaf(this->file, this->stat->get_root_id(), this->key_profile, true);
this->closed= false;
//build index , add every row from relation to index
Handles* all_rows_handle= this->relation.select();
for (auto const& handle : *all_rows_handle) {
this->insert(handle);
}
delete all_rows_handle;
}
// Drop the index.
void BTreeIndex::drop() {
this->file.drop();
}
// Open existing index. Enables: lookup, range, insert, delete, update.
void BTreeIndex::open() {
if(this->closed){
this->file.open();
this->stat = new BTreeStat(this->file,this->STAT, this->key_profile);
if (this->stat->get_height() == 1)
this->root = new BTreeLeaf(this->file, this->stat->get_root_id(), this->key_profile, false);
else
this->root = new BTreeInterior(this->file, this->stat->get_root_id(), this->key_profile, false);
this->closed= false;
}
}
// Closes the index. Disables: lookup, range, insert, delete, update.
void BTreeIndex::close() {
this->file.close();
this->stat = nullptr;
this->root = nullptr;
this->closed = true;
}
/*
* LOOKUP
* split into two sections, recursively search down the tree
* */
// Find all the rows whose columns are equal to key. Assumes key is a dictionary whose keys are the column
// names in the index. Returns a list of row handles.
Handles* BTreeIndex::lookup(ValueDict* key_dict) const {
KeyValue* tkey_val= this->tkey(key_dict);
return this->_lookup(this->root, this->stat->get_height(), tkey_val);
}
Handles* BTreeIndex::_lookup(BTreeNode *node, uint height, const KeyValue *key) const {
//handles for lookup
Handles* handles = new Handles();
//get a handle
Handle handle;
if(height==1){
try{
BTreeLeaf* leaf = (BTreeLeaf*)node;
handle = leaf->find_eq(key);
handles->push_back(handle);
}
//displays out of range if looking up empty leaf
catch (const std::out_of_range& oor) {
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
return handles;
}
else{
BTreeInterior* inter= (BTreeInterior*)node;
return _lookup((inter->find(key, this->stat->get_height())), this->stat->get_height()-1, key);
}
}
/*
* INSERTION
* need split root to add new root index
* */
// Insert a row with the given handle. Row must exist in relation already.
void BTreeIndex::insert(Handle handle) {
//this->open();
ValueDict* dict= this->relation.project(handle, &key_columns);
KeyValue* t_Key = this->tkey(dict);
Insertion split_root = this->_insert(this->root,this->stat->get_height(),t_Key, handle);
if(!BTreeNode::insertion_is_none(split_root) ){
//split_root(split_root_in, this->root, this->stat->get_height());
BTreeInterior* root = new BTreeInterior(this->file, 0, this->key_profile, true);
root->set_first(this->root->get_id());
root->insert(&split_root.second, split_root.first); //height/id
root->save();
this->stat->set_root_id(root->get_id());
uint temp_height= this->stat->get_height() + 1;
this->stat->set_height(temp_height);
this->stat->save();
this->root = root;
}
}
//recursively insert
Insertion BTreeIndex::_insert(BTreeNode *node, uint height, const KeyValue* key, Handle handle) {
// Recursive insert. If a split happens at this level, return the (new node, boundary) of the split.
Insertion insertion;
if (height == 1){
BTreeLeaf* leaf = (BTreeLeaf*)node;
insertion = (leaf)->insert(key, handle);
leaf->save();
}
else {
BTreeInterior* inter = (BTreeInterior*)node;
Insertion new_insertion = _insert((inter)->find(key, height), height - 1, key, handle);
if (!BTreeNode::insertion_is_none(new_insertion)){
insertion = ((BTreeInterior*)node)->insert(&new_insertion.second, new_insertion.first);
inter->save();
}
}
return insertion;
}
KeyValue *BTreeIndex::tkey(const ValueDict *key) const {
KeyValue* keyValue = new KeyValue();
//get Value from key
for(auto const& col: this->key_columns){
keyValue->push_back(key->at(col));
}
return keyValue;
}
void BTreeIndex::del(Handle handle) {
throw DbRelationError("Don't know how to delete from a BTree index yet");
}
//RANGE
Handles* BTreeIndex::range(ValueDict* min_key, ValueDict* max_key) const {
throw DbRelationError("Don't know how to do a range query on Btree index yet");
}
//BTREE TESTING
bool test_btree(){
bool result = false;
Identifier testID= "test_btree";
ColumnNames colNames;
colNames.push_back("a");
colNames.push_back("b");
ColumnAttributes colAttributes;
ColumnAttribute ca;
ColumnAttribute ca2;
ca= ColumnAttribute::INT;
ca2= ColumnAttribute::INT;
colAttributes.push_back(ca);
colAttributes.push_back(ca2);
HeapTable table(testID, colNames, colAttributes);
table.create();
//for inserts
ValueDict *row1= new ValueDict();
ValueDict *row2= new ValueDict();
//append values
(*row1)["a"] = Value(12);
(*row1)["b"] = Value(99);
(*row2)["a"] = Value(88);
(*row2)["b"] = Value(101);
//For results
ValueDict *result_1 = new ValueDict();
ValueDict *result_2 = new ValueDict();
ValueDict *result_3 = new ValueDict();
ValueDict *result_4 = new ValueDict();
//test values
(*result_1)["a"]= Value(12);
(*result_2)["a"]= Value(88);
(*result_3)["a"]= Value(6);
table.insert(row1);
table.insert(row2);
ColumnNames columnNames2;
columnNames2.push_back(colNames.at(0));
for (uint i = 0; i < 1000; i++) {
ValueDict *row = new ValueDict();
(*row)["a"] = Value(i + 100);
(*row)["b"] = Value(-i);
table.insert(row);
delete row;
}
DbIndex* index = new BTreeIndex(table, "test_btreeIndex", columnNames2, true);
index->create();
//t1 SHOULD BE SAME
Handles* handles_t1 = new Handles();
handles_t1 = index->lookup(result_1);
for(auto const& handle: *handles_t1){
ValueDict* row_proj = table.project(handle);
if((*row_proj)["a"] == (*result_1)["a"] ){
cout<<"pass t1"<<endl;
result= true;
delete row_proj;
break;
}
else{
result = false;
cout<<"failed t1" << endl;
delete row_proj;
break;
}
}
delete result_1;
delete handles_t1;
//t2 SHOULD BE SAME
result=false;
Handles* handles_t2 = new Handles();
handles_t2 = index->lookup(result_2);
for(auto const& handle: *handles_t2){
ValueDict* row_proj = table.project(handle);
if((*row_proj)["a"] == (*result_2)["a"] ){
cout<<"pass t2"<<endl;
result= true;
delete row_proj;
break;
}
else{
result = false;
cout<<"failed t2" << endl;
delete row_proj;
break;
}
}
delete result_2;
delete handles_t2;
//t3 SHOULD BE EMPTY
result=false;
Handles* handles_t3 = new Handles();
handles_t3 = index->lookup(result_3);
if(handles_t3->empty()){
cout<<"pass t3"<<endl;
result= true;
}else{
for(auto const& handle: *handles_t3){
ValueDict* row_proj = table.project(handle);
if((*row_proj)["a"] == (*result_3)["a"] ){
cout<<"failed t3" << endl;
result= true;
delete row_proj;
break;
}
}
}
delete result_3;
delete handles_t3;
//t4
Handles* handles_t4 = new Handles();
for (uint j = 0; j <10 ; j++) {
for (uint i = 0; j < 1000; j++) {
(*result_4)["a"]= Value(i + 100);
(*result_4)["b"]= Value(-i);
handles_t4 = index->lookup(result_4);
for (auto const& handle : *handles_t4) {
ValueDict* row_proj = table.project(handle);
if ((*row_proj)["a"] == (*result_4)["a"] && (*row_proj)["b"] == (*result_4)["b"]) {
result = true;
delete row_proj;
break;
}
else{
result = false;
cout<<"failed t4" << endl;
delete row_proj;
break;
}
}
}
}
if(result){
cout<< "passed t4"<<endl;
}
delete handles_t4;
delete row1;
delete row2;
delete index;
table.drop();
return result;
}