-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBSystem.cpp
More file actions
2275 lines (1941 loc) · 61.4 KB
/
DBSystem.cpp
File metadata and controls
2275 lines (1941 loc) · 61.4 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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ASSUMPTIONS
1. run code using c++11 compiler "g++ DBsystem.cpp -std=c++11"
2. LRU gets updated only when getRecord() is called
3. orderby must specify asec or desc
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <map>
#include <deque>
#include <queue>
#include <list>
#include <utility>
#include <cstring>
#include <climits>
using namespace std;
int global_table_number=0;
string configFilePath ="config.txt";
//const int n;
// structure for page
struct page_struct
{
int page_no;
int data_size;
int table_no;
string page_data;
int start_id;
int end_id;
bool modified;
};
//end
// strcture for table
struct table_struct
{
string attribute;
string att_type;
};
//end
//struct for select_query
struct select_att
{
vector<string> column_list;
bool distinct;
bool join;
vector<string> table_name;
string where_clause;
string groupby_clause;
string having_clause;
string orderby_clause;
string join_table;
string join_att;
};
//end
struct unique_val
{
int table_no;
int att_no;
map <string, int> value_instances;
};
//split fucntion BEGIN
vector<string> split(const string &s, char delim)
{
vector<string> elems;
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
//END
//double space to space
string check_spaces(string query)
{
for(string::iterator i=query.begin() ; i!=query.end() ;++i)
{
if(*i == '\t')
{
*i = ' ';
}
}
for(string::iterator i=query.begin() ; i!=query.end() ;++i)
{
if(*i == ' ')
{
if(*(i+1) == ' ')
{
query.erase(i);
i--;
}
}
}
return query;
}
//END
//converts string to lower case
string myToLower(string query)
{
for(string::iterator p=query.begin() ; p!=query.end() ;++p)
{
switch(*p)
{
case 'A':*p='a'; break;
case 'B':*p='b'; break;
case 'C':*p='c'; break;
case 'D':*p='d'; break;
case 'E':*p='e'; break;
case 'F':*p='f'; break;
case 'G':*p='g'; break;
case 'H':*p='h'; break;
case 'I':*p='i'; break;
case 'J':*p='j'; break;
case 'K':*p='k'; break;
case 'L':*p='l'; break;
case 'M':*p='m'; break;
case 'N':*p='n'; break;
case 'O':*p='o'; break;
case 'P':*p='p'; break;
case 'Q':*p='q'; break;
case 'R':*p='r'; break;
case 'S':*p='s'; break;
case 'T':*p='t'; break;
case 'U':*p='u'; break;
case 'V':*p='v'; break;
case 'W':*p='w'; break;
case 'X':*p='x'; break;
case 'Y':*p='y'; break;
case 'Z':*p='z'; break;
}
}
//cout <<endl << "to lower funct :: "<< query;
return query;
}
//END
//removes all spaces from string
string remove_space(string str)
{
string delimiter;
delimiter = " ";
while(str.find(delimiter)!= string::npos)
{
str = str.erase(str.find(delimiter));
}
return str;
}
typedef vector<int> pageNum_list; //vector of page number so that one table can have multiple pages
class sort_records_structre
{
public:
string record;
string id;
};
bool sort_record_asc(const sort_records_structre& d1,const sort_records_structre& d2)
{
return d1.id < d2.id;
}
bool sort_record_desc(const sort_records_structre& d1,const sort_records_structre& d2)
{
return d1.id > d2.id;
}
class DBSystem
{
public:
int PAGESIZE;
int NUM_PAGES;
string PATH_FOR_DATA;
map <string, int> table_name_num; // table_name --> table_number
table_struct att;
vector<pair<int , table_struct> > table_num_att; // table_no. --> table_attributes, att_type
map <int, pageNum_list> tableNum_pageNum; // table_number --> page_no.
map<int ,page_struct> page; //page implementation
queue<int> LRUque; //page_noo.
map<int , page_struct> LRUdata; //page_no. --> page_data
int LRUsize = 5 ; //size of max elements in LRU //needs to be changed afterwards
//deque<string> LRU;
unique_val att_values;
void readConfig(string configFilePath,int numpages) //changed
{
string line;
ifstream file;
file.open(configFilePath);
if(file.is_open())
{
for (int i = 0; i < 3; ++i) // for reading the structure of our DB
{
getline(file,line);
//istringstream iss(line);
vector<string> tokens=split(line, ' ');
switch(i){
case 0:
PAGESIZE = stoi(tokens[1]);
//cout << line<<"\n"<<PAGESIZE<<"\n";
break;
case 1:
LRUsize = stoi(tokens[1]);
NUM_PAGES = numpages; // to be changed //changed
//LRUsize=10;
//NUM_PAGES =100;
//cout << line<<"\n"<<NUM_PAGES<<"\n";
break;
case 2:
PATH_FOR_DATA = tokens[1];
//cout << line<<"\n"<<PATH_FOR_DATA<<"\n";
break;
}
}
while(getline(file,line))
{ // for reading each table structure (att)
if (line =="BEGIN")
{
getline(file,line);
pageNum_list empty_page_list;
table_name_num.insert(make_pair(line, global_table_number) ) ;
tableNum_pageNum.insert(make_pair(global_table_number , empty_page_list)); // this is the problem for first page
getline(file,line);
while (line != "END"){
//istringstream iss(line);
vector<string> tokens=split(line, ',');
//cout << line<<"\n";
//cout << tokens[0] << "\t" <<tokens[1]<<"\n";
att.attribute = tokens[0];
att.att_type = tokens[1];
//cout << att.attribute << "\t" <<att.att_type << "\n";
table_num_att.push_back(make_pair(global_table_number , att));
getline(file,line);
}
global_table_number ++;
}
}
file.close();
page_struct temp_page;
temp_page.data_size = 0;
temp_page.table_no=-1;
temp_page.start_id = -1;
temp_page.end_id =-1;
temp_page.page_data.resize(PAGESIZE);
temp_page.page_data = '\0';
temp_page.modified =0;
for(int i =0;i<numpages;i++)
{ //changed
temp_page.page_no =i;
page.insert(make_pair(i,temp_page));
}
//LRUsize = NUM_PAGES/8 ;
/*
//to be deleted
cout << "in readconfig \n";
for( map<string, int>::iterator ii=table_name_num.begin(); ii!=table_name_num.end(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second << endl;
}
for(int i = 0; i < table_num_att.size(); i++)
{
cout << table_num_att[i].first<< endl;
cout<< table_num_att[i].second.attribute <<" "<<table_num_att[i].second.att_type <<endl;
}
*/
}
else
cout<<"unable to open config file";
}
void populatePageInfo()
{
int page_curr = 0, assigned = 0;
string line, path_name;
ifstream file ;
int current_page =0;
for( map<string, int>::iterator ii=table_name_num.begin(); ii!=table_name_num.end(); ++ii)
{
int i=0;
int temp_record_id =-1;
path_name = PATH_FOR_DATA + (*ii).first+ ".csv";
file.open(path_name);
if ( file.is_open() )
{
while(getline(file,line))
{
temp_record_id ++; // id should be record id not the line number
int str_size = line.size(); //modify the sie to plus 3
for(i=current_page;i<NUM_PAGES;i++)
{
if (str_size <= (PAGESIZE - page[i].data_size) )
{
if(page[i].data_size == 0){
page[i].start_id = temp_record_id;
page[i].end_id = temp_record_id;
page[i].data_size = str_size +2 + to_string(temp_record_id).size();
page[i].table_no = (*ii).second;
page[i].page_data = to_string(temp_record_id) + "#" + line + "&";
page[i].modified =1;
//the tablenum_pagenum map is int->string. the existing string is read into p1. we modify the string to include the new page number and add the new string to the map
pageNum_list p1;
for( vector<int>::iterator iii=(tableNum_pageNum[page[i].table_no]).begin(); iii!=(tableNum_pageNum[page[i].table_no]).end(); ++iii)
p1.push_back(*(iii));
p1.push_back(i);
tableNum_pageNum[page[i].table_no] = p1;
}
else
{
page[i].end_id = temp_record_id;
page[i].data_size += str_size +2 + to_string(temp_record_id).size();
page[i].page_data += to_string(temp_record_id) + "#" + line + "&";
page[i].modified =1;
}
break;
}
else
current_page++;
}
}
file.close();
}
else
cout<<"unable to open csv file: "<<path_name;
current_page =i+1;
}
/*
for( map<int, page_struct>::reverse_iterator ii=page.rbegin(); ii!=page.rend(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second.end_id << endl;
}
*/
/*
for(map<int,vector<int> >::iterator i=tableNum_pageNum.begin(); i!=tableNum_pageNum.end(); ++i){
for( vector<int>::iterator itr = tableNum_pageNum[(*i).first].begin() ; itr != tableNum_pageNum[(*i).first].end() ; ++itr)
printf("%d\n",(*itr) );
}
*/
}
void updateV(string table, string row)
{
int tablenum = table_name_num[table];
string value;
int attribute_no = att_values.att_no;
if(att_values.table_no = tablenum)
{
vector<string> row_tokens=split(row, ',');
value = row_tokens[attribute_no -1];
if(att_values.value_instances.count(value) == 0)
{
att_values.value_instances.insert (std::pair<string,int>(value, 1));
}
else
{
att_values.value_instances[value] += 1;
}
}
else
return;
}
int insertRecord(string tablename, string record)
{
int hit_miss=1;
int temp_table_no =-1;
if(table_name_num.count(tablename) >0 )
{
temp_table_no= table_name_num[tablename];
vector<int> temp_page_list = tableNum_pageNum[temp_table_no];
int temp_page_size = temp_page_list.size();
int last_page;
if(temp_page_size > 0)
{
vector<int>::iterator it = max_element(temp_page_list.begin() , temp_page_list.end()) ;
last_page = *it;
}
else
{
last_page =-1;
}
//printf("last page: %d\n",last_page );
int str_size = record.size();
int temp_record_id=-1;
//vector<string> tokens=split(record, ',');
//int temp_record_id = stoi(tokens[0]);
if (str_size <= (PAGESIZE - page[last_page].data_size) && (last_page >=0) )
{
//temp_page.start_id = page[i].start_id;
temp_record_id = page[last_page].end_id +1;
page[last_page].end_id = temp_record_id;
page[last_page].data_size += str_size +2 + to_string(temp_record_id).size();
page[last_page].page_data += to_string(temp_record_id) + "#" + record + "&";
page[last_page].modified= 1;
}
else
{
if(last_page == -1)
temp_record_id = 0;
else
{
temp_record_id = ++page[last_page].end_id;
}
int new_page = ++last_page;
while(page[new_page].data_size != 0)
new_page++;
if(page[new_page].data_size == 0 && new_page<NUM_PAGES)
{
page[new_page].start_id = temp_record_id;
page[new_page].end_id = temp_record_id;
page[new_page].data_size = str_size +2 + to_string(temp_record_id).size();
page[new_page].table_no = temp_table_no;
page[new_page].page_data = to_string(temp_record_id) + "#" + record + "&";
page[new_page].modified= 1;
pageNum_list p1;
for( vector<int>::iterator iii=(tableNum_pageNum[temp_table_no]).begin(); iii!=(tableNum_pageNum[temp_table_no]).end(); ++iii)
p1.push_back(*(iii));
//p1.push_back(tableNum_pageNum[temp_table_no]);
p1.push_back(new_page);
//tableNum_pageNum.insert (make_pair (temp_table_no , p1));
tableNum_pageNum[temp_table_no] = p1;
}
else
{
printf("no more size available\n");
hit_miss =0;
}
}
updateV(tablename, record);
}
else
{
printf("No such table exists!\n");
}
return hit_miss;
/*
for( map<int, page_struct>::reverse_iterator ii=page.rbegin(); ii!=page.rend(); ++ii)
{
cout << (*ii).first << ": " << (*ii).second.end_id << endl;
}
*/
/* Get the last page for the corresponding Table in main memory, if
not already present.
If the page has enough free space, then append the record to the
page else, get new free page and add record to the new page.
Do not flush modified page immediately.*/
}
void flushPages()
{
for( map<int, page_struct>::iterator itr=page.begin(); itr!=page.end(); ++itr)
{
if((*itr).second.modified ==1)
{
(*itr).second.modified =0;
ofstream file;
string file_name = "db_" + to_string((*itr).second.page_no) +".txt";
file.open(file_name);
if(file.is_open())
{
file << (*itr).second.page_data;
file.close();
}
else
cout<<"unable to open db file"<<endl;
}
}
/* Since primary and secondary memory are independent, no need to
flush modified pages immediately, instead this function will be called
to write modified pages to disk.
Write modified pages in memory to disk.*/
}
int getRecord(string tablename, int recordId)
{
int hit_miss=1;
int found =0;
int temp_table_no = table_name_num[tablename];
vector<int> temp_page_list = tableNum_pageNum[temp_table_no];
list<int> temp_pages_of_table_not_in_lru; //all pages associated to the table not in LRU
int temp_start_id, temp_end_id;
for( vector<int>::iterator itr = temp_page_list.begin(); itr != temp_page_list.end(); ++itr)
{
int temp_page_no = *itr;
if(LRUdata.count(temp_page_no) > 0)
{
temp_start_id = LRUdata[(temp_page_no)].start_id ;
temp_end_id = LRUdata[(temp_page_no)].end_id ;
if((recordId >= temp_start_id) && (recordId<= temp_end_id))
{
hit_miss=1;
printf("HIT\n");
//return get_record_from_record_id(temp_page_no , recordId);
}
}
else
{
temp_pages_of_table_not_in_lru.push_back(temp_page_no);
}
}
if ( LRUque.size() == LRUsize )
{
int temp = LRUque.front();
LRUque.pop();
LRUdata.erase(temp);
}
for( list<int>::iterator itr = temp_pages_of_table_not_in_lru.begin(); itr != temp_pages_of_table_not_in_lru.end(); ++itr)
{
int temp_page_no = *itr;
temp_start_id = page[(temp_page_no)].start_id ;
temp_end_id = page[(temp_page_no)].end_id ;
if((recordId >= temp_start_id) && (recordId<= temp_end_id))
{
found =1;
LRUque.push(temp_page_no);
LRUdata.insert(make_pair(temp_page_no , page[temp_page_no]));
hit_miss=0;
printf("MISS<%d>\n", temp_page_no);
//return get_record_from_record_id(temp_page_no , recordId);
}
}
if(found ==0)
{
hit_miss=0;
printf("MISS<-1>\n");
return hit_miss;
}
/*
Get the corresponding record of the specified table.
DO NOT perform I/O every time. Each time a request is received, if
the page containing the record is already in memory, return that
record else bring corresponding page in memory. You are supposed
to implement LRU page replacement algorithm for the same. Print
HIT if the page is in memory, else print
MISS <pageNumber> where <pageNumber> is the page number of
memory page which is to be replaced. (You can assume page
numbers starting from 0. So, you have total 0 to <NUM_PAGES 1>
pages.)
*/
}
string get_record_from_record_id(int page_no , int recordId)
{
string line = page[page_no].page_data;
vector<string> records=split(line, '&');
int record_no = recordId - page[page_no].start_id;
return records[record_no];
}
void createCommand(string query)
{
string temp_table_name;
string temp_query;
string delimiter;
string token1;
string token2;
temp_query = myToLower(query);
cout << "passed query " << temp_query << "\n";
//checking table
delimiter = "table";
if(temp_query.find(delimiter)!=string::npos)
{
token1 = temp_query.substr(temp_query.find(delimiter) + delimiter.length()+1, temp_query.length() -1);
token2 = temp_query.substr(0, temp_query.find(delimiter) -1);
//cout <<endl<<"token2::::"<<token2<<endl;
//checking create
if(token2 != "create") //check for spaces
{
throw "1";
}
temp_query = token1;
}
else
throw "2";
//table name
delimiter = "(";
if(temp_query.find(delimiter)!=string::npos)
{
token1 = temp_query.substr(temp_query.find(delimiter) + delimiter.length(), temp_query.length() -1);
token2 = temp_query.substr(0, temp_query.find(delimiter));
temp_table_name = token2;
temp_query = token1;
}
else
throw "3";
//cout << temp_table_name<<endl;
//remove the last 2 char of temp_query
unsigned found = temp_query.find_last_of(")");
temp_query.erase(found , temp_query.size()-1);
//cout <<endl << "temp_query ==" <<endl<<temp_query;
//now open the config file and start appending also update the table_no->att data structure
fstream file;
file.open(configFilePath, fstream::in | fstream::out | fstream::app);
temp_table_name = "data/"+temp_table_name;
fstream data_file;
data_file.open(temp_table_name+".data", fstream::in | fstream::out | fstream::app);
fstream csv_file;
csv_file.open(temp_table_name+".csv", fstream::in | fstream::out | fstream::app);
if(file.is_open() && data_file.is_open() && csv_file.is_open() )
{
file << "\nBEGIN\n" << temp_table_name ;
pageNum_list empty_page_list;
table_name_num.insert(make_pair(temp_table_name, global_table_number) ) ;
tableNum_pageNum.insert(make_pair(global_table_number , empty_page_list));
vector<string> tokens=split(temp_query, ',');
int temp_count =0;
for( vector<string>::iterator ii=tokens.begin(); ii!=tokens.end(); ++ii)
{
cout<<endl<<*ii;
temp_count++;
vector<string> temp_tokens=split(*ii, ' ');
for(vector<string>::iterator i=temp_tokens.begin(); i!=temp_tokens.end(); ++i)
{
if((*i).empty())
temp_tokens.erase(i,i+1);
}
att.attribute = temp_tokens[0];
att.att_type = temp_tokens[1];
//cout << endl << att.attribute << ", " << att.att_type;
file << endl << att.attribute << ", " << att.att_type;
data_file << att.attribute << ":" << att.att_type <<",";
table_num_att.push_back(make_pair(global_table_number , att));
}
global_table_number++;
file <<endl<<"END";
file.close();
data_file.close();
csv_file.close();
}
else
cout <<"\n error opening config while while creating\n";
}
// returns the sub_clause details in the format: (query_type,comparison_string,att_number)
string find_att_no(string where_sub_clause, int table_no)
{
// =,>=,<=, !=,>,<, LIKE
string attribute;
string delimiter;
int query_type;
string comparison_string;
int count=1, found_att=0;
delimiter = "=";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =1;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+1, where_sub_clause.length());
if (std::string::npos == comparison_string.find_first_of("0123456789"))
{
query_type =7;
}
}
delimiter = ">=";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =2;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+2, where_sub_clause.length());
}
delimiter = "<=";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =3;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+2, where_sub_clause.length());
}
delimiter = "!=";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =4;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+2, where_sub_clause.length());
}
delimiter = ">";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =5;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+1, where_sub_clause.length());
}
delimiter = "<";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =6;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+1, where_sub_clause.length());
}
//add the case sensitive thing here for 'like' and 'LIKE'----DONE !!
delimiter = "like";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =7;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+4, where_sub_clause.length());
}
delimiter = "LIKE";
if(where_sub_clause.find(delimiter)!= string::npos)
{
attribute = where_sub_clause.substr(0, where_sub_clause.find(delimiter));
query_type =7;
comparison_string = where_sub_clause.substr(where_sub_clause.find(delimiter)+4, where_sub_clause.length());
}
attribute = remove_space(attribute);
comparison_string = remove_space(comparison_string);
// finding the attribute number
for(vector<pair<int , table_struct> >::iterator found = table_num_att.begin(); found != table_num_att.end(); ++found)
{
if( found->first == table_no)
{
string original_att = remove_space((found->second).attribute);
if(original_att != attribute)
{
count++;
//cout<<"\n:att search:"<<(found->second).attribute<<":count:"<<count;
}
else
{
found_att =1;
break;
}
}
}
if(found_att == 0)
{
cout<<"no such attribute like "<< attribute;
return "error";
}
else
{
return (to_string(query_type) + "," + comparison_string + "," + to_string(count));
}
}
// returns the att_no of orderby clause
int find_orderby_att_no(string orderby_clause, int table_no)
{
int count=1, found_att = 0;;
// finding the attribute number
for(vector<pair<int , table_struct> >::iterator found = table_num_att.begin(); found != table_num_att.end(); ++found)
{
if( found->first == table_no)
{
string original_att = remove_space((found->second).attribute);
if(original_att != orderby_clause)
{
count++;
//cout<<"\n:att search:"<<(found->second).attribute<<":count:"<<count;
}
else
{
found_att =1;
break;
}
}
}
return count;
}
vector<string> order_results(string result, string orderby_clause, int table_no)
{
//cout << "in order_results\torderby condition \t"<<orderby_clause<<endl;
//cout << "result \t"<<result<<endl;
string query_type, delimiter;
vector<int> att_nos;
vector<sort_records_structre> record_vector;
vector<string> after_sorting_result;
delimiter = " ";
if(orderby_clause.find(delimiter)!=string::npos)
{
query_type = orderby_clause.substr(orderby_clause.find(delimiter) + 1, orderby_clause.length() ); //tells whether asc or desc
orderby_clause = orderby_clause.substr(0, orderby_clause.find(delimiter)); //tells which clause to order on
//cout << "order_by_clause_name\t"<<orderby_clause;
vector<string> tokens=split(orderby_clause, ',');
for( vector<string>::iterator it = tokens.begin() ; it != tokens.end() ; ++it)
{
att_nos.push_back(find_orderby_att_no((*it),table_no));
}
query_type = remove_space(query_type);
query_type = myToLower(query_type);
vector<string> records_split_amp =split(result, '&');
sort_records_structre data;
//cout<<"beginnig to print what is needed for the eyes"<<endl;
for(vector<string>::iterator record_itr = records_split_amp.begin() ; record_itr != records_split_amp.end() ; record_itr++)
{
if((*record_itr).size() == 0)
break;
//cout <<(*record_itr)<<endl;
data.record = (*record_itr);
vector<string> records_split_hash =split((*record_itr), '#');
vector<string> records_split =split(records_split_hash[1], ',');
int order_attribute = (*att_nos.begin()) -1;
data.id = records_split[order_attribute];
//cout<< data.record<<endl<<data.id<<endl;
record_vector.push_back(data);
}
//cout<<"beginnig to print what is needed for the round of the clock"<<endl;
/*
for(vector<sort_records_structre>::iterator temp_itr = record_vector.begin() ; temp_itr != record_vector.end() ; temp_itr++)
{
//cout<< (*temp_itr).record<<endl<<(*temp_itr).id<<endl;
}
*/
query_type = remove_space(query_type);
query_type = myToLower(query_type);
if(query_type == "asc")
{
//cout <<"in asc"<<endl;
std::sort(record_vector.begin() , record_vector.end() , sort_record_asc);
//call merge_sort_inc()
//result = merge_sort_inc(tokens[0]);
}
else if(query_type == "desc")
{
//cout <<"in desc"<<endl;
std::sort(record_vector.begin() , record_vector.end() , sort_record_desc);
//call merge_sort_desc()
//result = merge_sort_desc(tokens[0]);
}
else
{
cout<<"error";
//return ;
}
//cout<<"beginnig to print what is needed for the eyes"<<endl;
for(vector<sort_records_structre>::iterator temp_itr = record_vector.begin() ; temp_itr != record_vector.end() ; temp_itr++)
{
//cout<< (*temp_itr).record<<endl<<(*temp_itr).id<<endl;
after_sorting_result.push_back((*temp_itr).record);
//after_sorting_result.append("&");
}
}
return after_sorting_result;
}
string get_record_from_table(string table_name)
{
string result;
int table_no = table_name_num[table_name];
vector<int> page_list = tableNum_pageNum[table_no];
page_struct p;
for(vector<int>::iterator temp_itr = page_list.begin() ; temp_itr != page_list.end() ; temp_itr++)
{
p = page[(*temp_itr)];
result.append(p.page_data);
}
return result;
}
string get_record_from_table(int table_no)
{
string result;
vector<int> page_list = tableNum_pageNum[table_no];
page_struct p;
for(vector<int>::iterator temp_itr = page_list.begin() ; temp_itr != page_list.end() ; temp_itr++)
{
p = page[(*temp_itr)];
result.append(p.page_data);
}
return result;
}
//print result