Skip to content

Commit 97a58b7

Browse files
committed
add comments
1 parent f9054c0 commit 97a58b7

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/BPtree.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ class Btreenode
105105
/*Function that inserts a new record in a node which
106106
is not yet full */
107107
void insert_key(int key, int point){
108+
//get the position in vector keys to insert
109+
//new key so that keys[] remains sorted;
108110
int pos = get_next_key(key);
109111
keys.insert(keys.begin() + pos, key);
112+
//insert position of key inside pointers;
110113
if (leaf)
111114
pointers.insert(pointers.begin() + pos, point);
112115
else
@@ -144,13 +147,24 @@ class Btreenode
144147
}
145148

146149
//Overloading write operator to write to a file
150+
/*
151+
write to file in format
152+
(is_leaf_or_not,keys_size,for(0 to key_size)<keys_Values>,pointer_size,for(0to pointers_size)<pointer_values>, next_node);
153+
154+
according to sample program
155+
tree0.dat will store data in following format
156+
(1 8 1 3 5 6 7 8 9 12 8 2 4 1 7 3 0 6 5 -1);
157+
*/
147158
friend std::ofstream & operator<<(std::ofstream & os, const Btreenode & en){
159+
// is leaf or not;
148160
os << en.leaf << " ";
161+
// keys size
149162
os << (int) en.keys.size() << " ";
150163
for (unsigned int i = 0; i < en.keys.size(); i++){
151164
os << en.keys[i] << " ";
152165
}
153166
os << (int) en.pointers.size() << " ";
167+
// pointers are the indices of the stored elements in vector a;
154168
for (unsigned int i = 0; i < en.pointers.size(); i++){
155169
os << en.pointers[i] << " ";
156170
}
@@ -160,6 +174,7 @@ class Btreenode
160174

161175
//Overloading read operator to read from a fil
162176
friend std::ifstream & operator>>(std::ifstream & is, Btreenode & en){
177+
// read all the data stored in file <descrbed in << operator overloading>
163178
int ts;
164179
is >> en.leaf;
165180
is >> ts;
@@ -192,9 +207,11 @@ class Btreenode
192207
void BPtree :: write_node(int filenum, Btreenode n){
193208
char *str;
194209
str = (char *) malloc(sizeof(char) * BPTREE_MAX_FILE_PATH_SIZE);
210+
//if filenum=0; open tree0.dat;
195211
sprintf(str, "table/%s/tree/tree%d.dat", tablename,filenum);
196212
std::ofstream out_file(str, std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
197213
free(str);
214+
//write data to out_file root=n()
198215
out_file << n;
199216
out_file.close();
200217
}
@@ -240,10 +257,14 @@ BPtree :: BPtree(char table_name[]){
240257
out_file.write((char *) (&root_num), sizeof(root_num));
241258
//out_file << files_till_now << " " << root_num;
242259
out_file.close();
260+
//initialize with root node =leaf node;
243261
Btreenode root(true);
262+
//set next_node=-1; as it is root;
244263
root.set_next(-1);
264+
//write node data to outfile;
245265
write_node(0, root);
246-
}else{
266+
}//if file already created read the previously stored data;
267+
else{
247268
/* Read old Meta Data */
248269
in_file >> files_till_now >> root_num;
249270
in_file.close();
@@ -274,11 +295,16 @@ Btreenode BPtree::search_leaf(int primary_key){
274295
//Traversing the Tree from root till leaf
275296
while (!n.isleaf()){
276297
q = n.num_pointers();
298+
//check primary key if it is smaller than the key[0]
277299
if (primary_key <= n.get_key(1)){
300+
//set curr_node =pointers[0];
278301
curr_node = n.get_pointer(1);
279-
}else if (primary_key > n.get_key(q - 1)){
302+
}//check if primary key is greater than key[q-1];// q==size of vector keys;
303+
else if (primary_key > n.get_key(q - 1)){
304+
//set curr_node = pointers[q-1];
280305
curr_node = n.get_pointer(q);
281306
}else{
307+
//find the correct position of key to be stored;
282308
curr_node = n.get_pointer(n.get_next_key(primary_key) + 1);
283309
}
284310
read_node(curr_node, n);
@@ -307,15 +333,19 @@ int BPtree::get_record(int primary_key){
307333
//key is first coloumn of database either can be int or varchar;
308334
int BPtree::insert_record(int primary_key, int record_num){
309335
//printf("pri %d\n record_num %d",primary_key,record_num);
336+
//Btreenode n= leaf=true, next_node =-1;
310337
Btreenode n(true);
311338
int q, j, prop_n, prop_k, prop_new, curr_node = root_num;
312339
bool finish = false;
313340
std::stack < int >S;
341+
//read all the data of node stored in file tree%d.data (%d==curr_node=root_num=file_no);
342+
//now n contains all the previously stored data;
314343
read_node(curr_node, n);
315344

316345
//Traverse the tree till we get the leaf node;
317346
while (!n.isleaf()){
318347
S.push(curr_node); //Storing address in case of split
348+
//num_pointers==function that returns size of pointers vector from the block file;
319349
q = n.num_pointers();
320350
if (primary_key <= n.get_key(1)){
321351
curr_node = n.get_pointer(1);
@@ -324,6 +354,7 @@ int BPtree::insert_record(int primary_key, int record_num){
324354
}else{
325355
curr_node = n.get_pointer(n.get_next_key(primary_key) + 1);
326356
}
357+
//get all the data of node n from file tree%d.dat(%d==curr_node);
327358
read_node(curr_node, n);
328359
}
329360

@@ -337,7 +368,10 @@ int BPtree::insert_record(int primary_key, int record_num){
337368
if (!n.full()){
338369
//leaf node empty insert here and exit
339370
n.insert_key(primary_key, record_num);
371+
//n is updated now;
372+
//now write this node back to file;
340373
write_node(curr_node, n);
374+
//update meta-data after key is inserted successfully;
341375
update_meta_data();
342376
return BPTREE_INSERT_SUCCESS;
343377
}
@@ -346,19 +380,27 @@ int BPtree::insert_record(int primary_key, int record_num){
346380
Btreenode temp(true), new_node(true);
347381

348382
temp = n;
383+
//insert the key and pointer into n i.e. temp now;
349384
temp.insert_key(primary_key, record_num);
350385
j = ceil((BPTREE_MAX_KEYS_PER_NODE + 1.0) / 2.0);
386+
//if max_key_per_node = 30;; j=16;
387+
//copy the first half values to new node(temp) created;
351388
n.copy_first(temp, j);
389+
//now one file is increased to store the new node;
352390
files_till_now++;
391+
// set new_node to returned next_node;
353392
new_node.set_next(n.get_next());
354393
n.set_next(files_till_now);
394+
//copy remaining values to other node created(new_node);
355395
new_node.copy_last(temp, j);
356-
396+
//return keys[j-1];
357397
prop_k = temp.get_key(j);
358398
prop_new = files_till_now;
359399
prop_n = curr_node;
400+
//write back the two new nodes created to their respective files;
360401
write_node(files_till_now, new_node);
361402
write_node(curr_node, n);
403+
//empty keys[] and pointers[];
362404
temp.clear_data();
363405
new_node.clear_data();
364406

0 commit comments

Comments
 (0)