11/*
2- // code references : DFC query builder by BUG_ASSASINS team
2+ // code references : DFC query builder by BUG_ASSASINS team
3+ // File : BPtree.cpp
4+ // Author : extra modifications and optimizations done by Mandeep Singh
5+ // Purpose : store indexed data to disk
36*/
47
58#include " BPtree.h"
@@ -220,7 +223,7 @@ void BPtree :: write_node(int filenum, Btreenode n){
220223 sprintf (str, " table/%s/tree/tree%d.dat" , tablename,filenum);
221224 std::ofstream out_file (str, std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
222225 free (str);
223- // write data to out_file root= n()
226+ // write data to out_file root = n()
224227 out_file << n;
225228 out_file.close ();
226229}
@@ -266,7 +269,7 @@ BPtree :: BPtree(char table_name[]){
266269 out_file.write ((char *) (&root_num), sizeof (root_num));
267270 out_file.close ();
268271
269- // initialize with root node =leaf node;
272+ // initialize with root node = leaf node;
270273 Btreenode root (true );
271274 // set next_node=-1; as it is root;
272275 root.set_next (-1 );
@@ -304,11 +307,10 @@ Btreenode BPtree::search_leaf(int primary_key){
304307 // Traversing the Tree from root till leaf
305308 while (!n.isleaf ()){
306309 q = n.num_pointers ();
307- // check primary key if it is smaller than the key[0]
308310 if (primary_key <= n.get_key (1 )){
309311 // set curr_node =pointers[0];
310312 curr_node = n.get_pointer (1 );
311- }// check if primary key is greater than key[q-1];// q==size of vector keys;
313+ }
312314 else if (primary_key > n.get_key (q - 1 )){
313315 // set curr_node = pointers[q-1];
314316 curr_node = n.get_pointer (q);
@@ -329,7 +331,7 @@ int BPtree::get_record(int primary_key){
329331 int pos = n.get_next_key (primary_key) + 1 ;
330332 clock_t stop=clock ();
331333 double elapsed=(double )(stop-start)*1000.0 /CLOCKS_PER_SEC;
332- printf (" \n Time elapsed for search is %f ms" ,elapsed);
334+ printf (" \n Time elapsed for search is %f ms\n " ,elapsed);
333335 if (pos <= n.num_keys () && n.get_key (pos) == primary_key){
334336 return n.get_pointer (pos);
335337 }else {
@@ -339,22 +341,22 @@ int BPtree::get_record(int primary_key){
339341
340342/* A function the inserts a (key, record_num) pair in the
341343 B+ Tree */
342- // key is first coloumn of database either can be int or varchar;
344+ // key is first coloumn of database either can be int or varchar;
343345int BPtree::insert_record (int primary_key, int record_num){
344- // printf("pri %d\n record_num %d",primary_key,record_num);
345- // Btreenode n= leaf=true, next_node =-1;
346+ // printf("pri %d\n record_num %d",primary_key,record_num);
347+ // Btreenode n= leaf=true, next_node =-1;
346348 Btreenode n (true );
347349 int q, j, prop_n, prop_k, prop_new, curr_node = root_num;
348350 bool finish = false ;
349351 std::stack < std::pair<int , Btreenode> > S;
350- // read all the data of node stored in file tree%d.data (%d==curr_node=root_num=file_no);
351- // now n contains all the previously stored data;
352+ // read all the data of node stored in file tree%d.data (%d==curr_node=root_num=file_no);
353+ // now n contains all the previously stored data;
352354 read_node (curr_node, n);
353355
354- // Traverse the tree till we get the leaf node;
356+ // Traverse the tree till we get the leaf node;
355357 while (!n.isleaf ()){
356- S.push (make_pair (curr_node,n)); // Storing address in case of split
357- // num_pointers==function that returns size of pointers vector from the block file;
358+ S.push (make_pair (curr_node,n)); // Storing address in case of split
359+ // num_pointers==function that returns size of pointers vector from the block file;
358360 q = n.num_pointers ();
359361 if (primary_key <= n.get_key (1 )){
360362 curr_node = n.get_pointer (1 );
@@ -363,24 +365,23 @@ int BPtree::insert_record(int primary_key, int record_num){
363365 }else {
364366 curr_node = n.get_pointer (n.get_next_key (primary_key) + 1 );
365367 }
366- // get all the data of node n from file tree%d.dat(%d==curr_node);
368+ // get all the data of node n from file tree%d.dat(%d==curr_node);
367369 read_node (curr_node, n);
368370 }
369371
370- // Here n is Leaf Node
371- // if key exist exist then return;
372- // key exist
372+ // Here n is Leaf Node
373+ // if key already exists then return ERROR
373374 if (n.search_key (primary_key)) {
374375 return BPTREE_INSERT_ERROR_EXIST;
375376 }
376377
378+ /*
379+ if n is not full, insert key and pointer to node
380+ write back node to file, update meta_data and return
381+ */
377382 if (!n.full ()){
378- // leaf node empty insert here and exit
379383 n.insert_key (primary_key, record_num);
380- // n is updated now;
381- // now write this node back to file;
382384 write_node (curr_node, n);
383- // update meta-data after key is inserted successfully;
384385 update_meta_data ();
385386 return BPTREE_INSERT_SUCCESS;
386387 }
@@ -389,27 +390,32 @@ int BPtree::insert_record(int primary_key, int record_num){
389390 Btreenode temp (true ), new_node (true );
390391
391392 temp = n;
392- // insert the key and pointer into n i.e. temp now;
393393 temp.insert_key (primary_key, record_num);
394394 j = ceil ((BPTREE_MAX_KEYS_PER_NODE + 1.0 ) / 2.0 );
395- // if max_key_per_node = 30;; j=16;
396- // copy the first half values to new node(temp) created;
395+ // if max_key_per_node = 4, j = 3
396+ // copy the first j values of temp to node n
397+ // and remaining to new_node
397398 n.copy_first (temp, j);
398399 // now one file is increased to store the new node;
399400 files_till_now++;
400- // set new_node to returned next_node;
401+ // next pointer of new_node will be next pointer of n
402+ // and next pointer of n will be newly created node new_node
401403 new_node.set_next (n.get_next ());
402404 n.set_next (files_till_now);
403- // copy remaining values to other node created(new_node);
404405 new_node.copy_last (temp, j);
405- // return keys[j-1];
406+
407+ /*
408+ prop_k is key to be inserted into root
409+ and prop_new and prop_n are pointers to
410+ be attached to this root value
411+ */
406412 prop_k = temp.get_key (j); // key to be moved to new root
407413 prop_new = files_till_now;
408- prop_n = curr_node; // node that is splitted at first place
409- // write back the two new nodes created to their respective files;
414+ prop_n = curr_node;
415+
416+ // write back the two new nodes created
410417 write_node (files_till_now, new_node);
411418 write_node (curr_node, n);
412- // empty keys[] and pointers[];
413419 temp.clear_data ();
414420 new_node.clear_data ();
415421
0 commit comments