Skip to content

Commit db13aab

Browse files
committed
cosmetic changes
1 parent 3fcf608 commit db13aab

File tree

6 files changed

+132
-125
lines changed

6 files changed

+132
-125
lines changed

build/BPtree.o

-8 Bytes
Binary file not shown.

build/bin/deepdb

-16 Bytes
Binary file not shown.

build/create.o

0 Bytes
Binary file not shown.

src/BPtree.cpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
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("\nTime elapsed for search is %f ms",elapsed);
334+
printf("\nTime 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;
343345
int 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

src/create.cpp

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,46 @@
2828
#include "file_handler.h"
2929

3030
int record_size(table *temp){
31-
int size=0;
32-
temp->prefix[0]=0;
33-
for(int i=0;i<temp->count;i++){
31+
int size = 0;
32+
temp->prefix[0] = 0;
33+
for(int i = 0; i < temp->count; i++){
3434
switch(temp->col[i].type){
3535
case INT:
36-
temp->prefix[i+1]=sizeof(int)+temp->prefix[i];
37-
size+=sizeof(int);
36+
temp->prefix[i+1] = sizeof(int) + temp->prefix[i];
37+
size += sizeof(int);
3838
break;
3939
case VARCHAR:
40-
temp->prefix[i+1]=sizeof(char)*(MAX_VARCHAR +1) +temp->prefix[i];
41-
size+=sizeof(char)*(MAX_VARCHAR +1);
40+
temp->prefix[i+1] = sizeof(char)*(MAX_VARCHAR +1) + temp->prefix[i];
41+
size += sizeof(char)*(MAX_VARCHAR + 1);
4242
break;
4343
}
4444
}
4545
return size;
4646
}
4747

48-
table * create_table(char name[],int count){
48+
table * create_table(char name[], int count){
4949
//set to check for duplicate enteries of columns;
5050
std::unordered_set <string> create_col_set;
5151
table *toreturn = new table();
52-
table *temp=new table();
53-
temp->fp=NULL;
54-
temp->blockbuf=NULL;
55-
strcpy(temp->name,name);
56-
temp->name[strlen(name)]='\0';
57-
temp->count=count;
58-
temp->rec_count=0;
59-
temp->data_size=0;
52+
table *temp = new table();
53+
temp->fp = NULL;
54+
temp->blockbuf = NULL;
55+
strcpy(temp->name, name);
56+
temp->name[strlen(name)] = '\0';
57+
temp->count = count;
58+
temp->rec_count = 0;
59+
temp->data_size = 0;
6060

6161
//enter the data for columns of table
62-
int i=0;
63-
cout<<"Enter column name,Data type(1.int 2.varchar) and max size(max allowed int=6,char=99) of column\n";
64-
for(i=0;i<count;i++){
65-
string type;
66-
string size;
67-
string temp_col_name;
68-
cin>>temp_col_name>>type>>size;
69-
//check if the column name is already entered, if not make a entry in set and allow input
62+
int i = 0;
63+
cout << "\nEnter column name,Data type(1.int 2.varchar) and max size(max allowed int=6,char=99) of column\n";
64+
for(i = 0; i < count; i++){
65+
std::string type;
66+
std::string size;
67+
std::string temp_col_name;
68+
std::cin >> temp_col_name >> type >> size;
69+
70+
// check if the column name is already entered, if not make a entry in set and allow input
7071
if(create_col_set.count(temp_col_name) == 0){
7172
strcpy(temp->col[i].col_name,temp_col_name.c_str());
7273
create_col_set.insert(temp_col_name);
@@ -75,8 +76,8 @@ table * create_table(char name[],int count){
7576
return NULL;
7677
}
7778

78-
//check if entered datatype no. is correct
79-
if(type.length()>1){
79+
// check if entered datatype no. is correct
80+
if(type.length() > 1){
8081
printf("\nwrong input(datatype should be (1=int or 2=varchar))\nexiting...\n\n");
8182
printf("-------------------------------------------------------------------------\n");
8283
return NULL;
@@ -89,7 +90,7 @@ table * create_table(char name[],int count){
8990
return NULL;
9091
}
9192
}
92-
//check size input;
93+
// check size input;
9394
if(size.length() > 2){
9495
printf("\n123wrong input\nmax size(max allowed int=6,char=99), exiting...\n");
9596
printf("---------------------------------------------------------------------\n");
@@ -124,7 +125,7 @@ table * create_table(char name[],int count){
124125
}
125126
}
126127
}
127-
if(i<count){
128+
if(i < count){
128129
printf("\n\tYou have not entered all details of columns\n\n");
129130
return NULL;
130131
}
@@ -137,19 +138,20 @@ table * create_table(char name[],int count){
137138
void create(){
138139
char *name;
139140
int count;
140-
name=(char*)malloc(sizeof(char)*MAX_NAME);
141-
cout<<"Enter table name: ";
142-
cin>>name;
141+
name = (char*)malloc(sizeof(char)*MAX_NAME);
142+
std::cout << "\nEnter table name: \n";
143+
std::cin >> name;
143144

144145
//check if new table already exists in table list or not
145146
int check = search_table(name);
146147
if(check == 1){
147-
cout << "\nERROR!\n" << name << " already exists\n";
148-
cout << "\n------------------------------------------------\n";
148+
std::cout << "\nERROR!\n" << name << " already exists\n";
149+
std::cout << "\n------------------------------------------------\n";
149150
return;
150151
}
151-
FILE *fp=fopen("./table/table_list","r+");
152-
if(fp==NULL) cout<<"file pointer is null\n";
152+
FILE *fp = fopen("./table/table_list","r+");
153+
if(fp == NULL)
154+
std::cout<<"file pointer is null\n";
153155

154156
// char tab_name[20];
155157
// while(fscanf(fp,"%s",tab_name)!=EOF){
@@ -159,9 +161,9 @@ void create(){
159161
// }
160162
// }
161163

162-
//enter table details if not exist
163-
cout<<"enter no. of columns: ";
164-
cin>>count;
164+
// enter table details if not exist
165+
std::cout << "\nenter no. of columns: \n";
166+
std::cin >> count;
165167
table *temp;
166168
temp=create_table(name,count);
167169
if(temp != NULL){
@@ -170,14 +172,14 @@ void create(){
170172
if(temp!=NULL){
171173
temp->blockbuf = malloc(temp->BLOCKSIZE);
172174
FILE *fpr;
173-
fpr = open_file(temp->name, const_cast<char*>("w"));
174-
fwrite(temp->blockbuf, 1, temp->BLOCKSIZE, fp);
175-
fclose(fpr);
176-
store_meta_data(temp);
177-
free(temp->blockbuf);
175+
fpr = open_file(temp->name, const_cast<char*>("w"));
176+
fwrite(temp->blockbuf, 1, temp->BLOCKSIZE, fp);
177+
fclose(fpr);
178+
store_meta_data(temp);
179+
free(temp->blockbuf);
178180
free(temp);
179181

180-
//if its a new table, make a entry inside table_list
182+
// if its a new table, make a entry inside table_list
181183
fseek(fp,0,SEEK_END);
182184
fprintf(fp,"%s\n",name);
183185
fclose(fp);
@@ -190,5 +192,4 @@ void create(){
190192
//printf("\nwrong input\nexiting...\n\n");
191193
return;
192194
}
193-
194195
}

0 commit comments

Comments
 (0)