/
Tables.cpp
1116 lines (910 loc) · 23.5 KB
/
Tables.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
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <vector>
#include <errno.h>
#include <cstdio>
#include "Btree.h"
#include "Tables.h"
#include "MyItr.h"
#include "Tuple.h"
#include "Global.h"
#include "HW4-expr.h"
#include "Expression.h"
// after the table is created, all the tables are gone
// fix it
int LOAD_CNT = 100;
typedef basic_string<char> string;
/*
Table::Table(string _name, vector<string> _cols, MyItr * _myItr )
{
name = _name;
cols = _cols;
myItr = _myItr;
//rows = content;
}
*/
Table::Table(string _name, string first_row)
{
string line;
name.assign( _name );
vector<string> key_list;
//cout << first_row << endl;
char* s = toChars( first_row );
char* token;
token = strtok ( s, "\t" );
while ( token ){
if ( token[0] == '*' )
{
key_list.push_back( string( token + 1 ) );
cols.push_back( string( token + 1 ) );
}
else
cols.push_back( string( token ) );
token = strtok ( NULL, "\t" );
}
if ( key_list.size() > 0 )
build_keys( key_list );
//cout << "!" << _name << endl;
myItr = new MyItr( _name, 10 );
}
//
string Table::get_entry( string tuple, int pos )
{
char _tuple[tuple.size() + 1];
strcpy( _tuple, tuple.c_str() );
// tokenize
char * token = strtok ( _tuple, "\t" );
for ( int i = 0; i < pos; i ++ )
token = strtok( NULL, "\t" );
return string( token );
}
string Table::get_entry( string tuple, std::string col )
{
int pos = get_col_pos( col );
std::string to_return;
if ( pos == -1 )
{
return to_return;
}
char _tuple[tuple.size() + 1];
strcpy( _tuple, tuple.c_str() );
// tokenize
char * token = strtok ( _tuple, "\t" );
for ( int i = 0; i < pos; i ++ )
token = strtok( NULL, "\t" );
return string( token );
}
bool Table::has_index_file(string col)
{
// construct name
string path ( "./data/" );
path.append( name + string( "." ) + col + string( ".idx" ) );
//cout << path << endl;
ifstream fs( path.c_str() );
if ( !fs.is_open() )
{
return false;
}
fs.close();
return true;
}
void Table::build_key_from_file( string col )
{
fstream fs( get_idx_path( col ) );
if( !fs.is_open( ) )
{
cerr << "ERROR open file in Table::build_key_from_file(). " << endl;
return;
}
BTree * btree = new BTree( 5, 6 );
string line;
while ( getline( fs, line ) )
{
char s[line.size() + 1];
strcpy( s, line.c_str() );
int type = 0;
char * c_type;
int n_type;
char * token = strtok ( s, " " );
type = atoi( token );
token = strtok( NULL, " " );
if ( type == NUM )
n_type = atoi( token );
else
{
c_type = new char[strlen( token )];
strcpy( c_type, token );
}
token = strtok( NULL, " " );
vector<streampos> vp;
while( token )
{
vp.push_back( atoi( token ) );
token = strtok( NULL, " " );
}
if ( type == NUM )
btree->insert( Tuple( n_type, vp ) );
else
btree->insert( Tuple( c_type, vp ) );
}
keys.insert( pair<string, BTree*>( col, btree ) );
fs.close();
}
string Table::get_idx_path ( string col )
{
string path ( "./data/" );
path.append( name + string( "." ) + col + string( ".idx" ) );
return path;
}
void Table::dump_tree( string col, BTreeNode * node )
{
ofstream ofs ( get_idx_path( col ).c_str(), std::ofstream::out | std::ofstream::trunc );
BTreeNode * cur_node = node;
while ( cur_node )
{
Tuple * values = ((LeafNode *)cur_node)->get_values();
for ( int i = 0; i < cur_node->getCount(); i++ )
{
ofs << values[i].datatype << " ";
if (values[i].datatype == NUM )
ofs << values[i].value.num << " ";
else
ofs << values[i].value.str << " ";
for( int j = 0; j < values[i].offset.size(); j++ )
ofs << values[i].offset[j] << " ";
ofs << "\n";
}
cur_node = cur_node->getRightSibling();
}
ofs.close();
}
void Table::delete_associate()
{
// delete index files
for ( map<string, BTree*>::iterator itr = keys.begin(); itr != keys.end(); itr++ )
{
string path = get_idx_path ( itr->first );
remove( path.c_str() );
}
// remove table
string path = get_path();
remove( path.c_str() );
path = construct_sta_path( name );
remove( path.c_str() );
}
bool Table::is_number(std::string s){
//scan();
std::string::const_iterator it = s.begin();
while ( it != s.end() && isdigit( *it ) )
it++;
return !s.empty() && it == s.end();
}
void Table::build_keys( vector<string> key_list )
{
// construct file pat
string path( "./data/" );
path.append( name );
path.append( ".tbl" );
ifstream fs( path.c_str() );
if ( !fs.is_open() )
{
cout << path << endl;
cerr << "ERROR: file does not exist in Table::build_keys(). " << endl;
return;
}
for ( int i = 0; i < key_list.size(); i++ )
{
if( has_index_file( key_list[i] ) )
{
build_key_from_file( key_list[i] );
continue;
}
int pos = get_col_pos( key_list[i] );
streampos offset = 0;
BTree * btree = new BTree ( 5, 6 );
string line;
getline( fs, line ); // get rid of the first line
offset = fs.tellg();
while ( getline ( fs, line ) && line.size() > 0)
{
// to ignore the deleted lines
//cout << line << endl;
if( line[0] == '@' )
{
//cout << line << endl;
offset = fs.tellg();
continue;
}
string entry = get_entry( line, pos );
if ( is_number ( entry ) )
{
Tuple cur_tuple( atoi( entry.c_str() ), offset );
btree->insert( cur_tuple );
}
else
{
Tuple cur_tuple( entry.c_str() , offset );
btree->insert( cur_tuple );
}
offset = fs.tellg();
}
keys.insert( pair<string, BTree*>( key_list[i], btree ) );
fs.clear();
fs.seekg( fs.beg );
BTreeNode * node = btree->get_first();
dump_tree( key_list[i], node );
}
fs.close();
}
int Table::update_btree( std::string key_to_btree, std::string tuple, streampos offset )
{ // return distinct count of the btree
// find the btree
map<string, BTree*>::iterator map_itr = keys.find( key_to_btree );
BTree * btree;
if ( map_itr == keys.end() )
{
cerr << "ERROR: Key does not exist in Table::update_btree()." << endl;
return -1;
}
btree = map_itr->second;
// get the targeted tuple
// construct a tuple
int pos = get_col_pos( key_to_btree );
char * token;
char * s = toChars( tuple );
token = strtok( s, "\t" );
for ( int i = 0; i < pos; i++ )
token = strtok( NULL, "\t" );
Tuple * t;
string key ( token );
if ( is_number( key ) )
t = new Tuple( atoi( key.c_str() ), offset );
else
t = new Tuple( key.c_str(), offset );
btree->insert( *t );
delete( t );
return btree->get_distinct_cnt();
// get distinct count
}
bool Table::operator == ( Table rhs )
{
return (name.compare( rhs.name ) == 0 );
}
bool Table::operator == ( string rhs )
{
//cout << name << " " << rhs << endl;
return ( name.compare( rhs ) == 0 );
}
bool Table::has_index( string col )
{
for (map<string, BTree*>::iterator itr = keys.begin(); itr != keys.end(); itr++ )
{
if ( itr->first.compare( col ) == 0 )
return true;
}
return false;
}
bool Table::has_index( std::vector<std::string> vs )
{
unsigned int i;
for ( i = 0; i < vs.size(); i++ )
{
map<string, BTree*>::iterator itr;
for( itr = keys.begin(); itr != keys.end(); itr++ )
if ( itr->first.compare( vs[i] ) == 0 )
break;
if ( itr == keys.end() )
return false;
}
return true;
}
MyItr * Table::index_find(int op, string key_to_btree, Tuple to_find )
{
//cout << op << endl;
map<string, BTree*>::iterator map_itr = keys.find( key_to_btree );
MyItr * itr = NULL;
//tuples.push_back( myItr->first() ); // get the headers
// if there is not index for the column, should never happen
if ( map_itr == keys.end() )
{
return NULL;
}
BTree * btree = map_itr->second;
if ( op == OP_EQUAL )
{
//cout << "euql " << endl;
BTreeNode * node = btree->find( to_find );
if ( node == NULL )
return NULL;
for ( int i = 0; i < node->getCount(); i++ )
{
if ( to_find == ( (LeafNode *) node )->get_values()[i] )
{
vector<streampos> offset = ( (LeafNode *) node )->get_values()[i].offset;
//cout << offset.size() << endl;
itr = index_fetch( offset );
}
}
}
else if ( op == OP_LEQ )
{
BTreeNode * node = btree->find_leq( to_find );
if ( node == NULL )
return NULL;
BTreeNode * cur_node = node;
vector<string> tuples;
myItr->open();
tuples.push_back( myItr->first() );
myItr->close();
itr = new MyItr( name, tuples );
while( node )
{
for ( int i = node->getCount() - 1; i >= 0; i-- )
{
if ( ( (LeafNode *) node )->get_values()[i] <= to_find)
{
vector<streampos> offset = ( (LeafNode *) node )->get_values()[i].offset;
*itr += *( index_fetch( offset ) );
}
}
node = node->getLeftSibling();
}
}
else if ( op == OP_LT )
{
BTreeNode * node = btree->find_leq( to_find );
if ( node == NULL )
return NULL;
BTreeNode * cur_node = node;
vector<string> tuples;
myItr->open();
tuples.push_back( myItr->first() );
myItr->close();
itr = new MyItr( name, tuples );
//cout << " at least here" << endl;
while( node )
{
for ( int i = node->getCount() - 1; i >= 0; i-- )
{
if ( ( (LeafNode *) node )->get_values()[i] < to_find)
{
vector<streampos> offset = ( (LeafNode *) node )->get_values()[i].offset;
*itr += *( index_fetch( offset ) );
}
}
node = node->getLeftSibling();
}
}
else if ( op == OP_GEQ )
{
BTreeNode * node = btree->find_geq( to_find );
if ( node == NULL )
return NULL;
BTreeNode * cur_node = node;
vector<string> tuples;
myItr->open();
tuples.push_back( myItr->first() );
myItr->close();
itr = new MyItr( name, tuples );
//cout << " at least here" << endl;
while( node )
{
for ( int i = 0; i < node->getCount(); i++ )
{
if ( ( (LeafNode *) node )->get_values()[i] >= to_find)
{
vector<streampos> offset = ( (LeafNode *) node )->get_values()[i].offset;
*itr += *( index_fetch( offset ) );
}
}
node = node->getRightSibling();
}
}
else if ( op == OP_GT )
{
//cout << "GT" << endl;
BTreeNode * node = btree->find_geq( to_find );
if ( node == NULL )
return NULL;
BTreeNode * cur_node = node;
vector<string> tuples;
myItr->open();
tuples.push_back( myItr->first() );
myItr->close();
itr = new MyItr( name, tuples );
//cout << " at least here" << endl;
//cout << node << endl;
while( node )
{
for ( int i = 0; i < node->getCount(); i++ )
{
if ( ( (LeafNode *) node )->get_values()[i] > to_find)
{
vector<streampos> offset = ( (LeafNode *) node )->get_values()[i].offset;
*itr += *( index_fetch( offset ) );
}
}
node = node->getRightSibling();
}
}
else
{
cerr << "Error in Table::index_find(): should never be here." << endl;
return NULL;
}
return itr;
}
string Table::get_path()
{
string path ( "./data/" );
path.append( name );
path.append( ".tbl" );
return path;
}
void Table::deleterow( streampos pos, string tuple, string col )
{
//cout << col << endl;
ifstream ifs( get_path( ) );
string temp_file;
if( !ifs.is_open() )
{
cerr << "ERROR opening file in Executor::deleterow(). " << endl;
return;
}
string s;
string line;
while ( ifs.tellg() != pos )
{
getline( ifs, line );
temp_file.append( line + string( "\n") );
}
getline( ifs, line );
//temp_file.append( line + string( "\n") );
//cout << line << endl;
// update btree index
if( has_index( col ) )
{
map<string, BTree*>::iterator map_itr = keys.find( col );
BTree * btree = map_itr->second;
//construct tuple
int type;
string entry( get_entry( line, get_col_pos( col ) ) );
if( is_number( entry ) )
btree->remove( Tuple ( atoi( entry.c_str() ), pos ) );
else
btree->remove( Tuple ( entry.c_str(), pos ) );
dump_tree( col, btree->get_first() );
}
line[0] = '@';
temp_file.append( line + string( "\n") );
while ( getline( ifs, line ) )
temp_file.append( line + string( "\n") );
ifs.close();
// rewrite the whole file
ofstream ofs ( get_path( ), std::ofstream::out | std::ofstream::trunc );
if( !ofs.is_open() )
{
cerr << "ERROR opening out file in Executor::deleterow(). " << endl;
return;
}
ofs << temp_file;
ofs.close();
}
MyItr * Table::index_fetch ( vector<streampos> offsets )
{
//cout << "inddex_ fetch" << endl;
vector<string> tuples;
string line;
string path( get_path() );
ifstream fs( path );
if( !fs.is_open() )
return new MyItr( name, tuples );
// get the first line, aka headers
getline ( fs, line );
tuples.push_back( line );
// load tuples specified
for ( int i = 0; i < offsets.size(); i++ )
{
if( offsets[i] == -1 )
continue;
fs.clear();
fs.seekg( offsets[i] );
getline(fs, line );
//cout << line << endl;
tuples.push_back( line );
}
fs.close();
return new MyItr( name, tuples );
}
int Table::arithmetic( expression * ep, string row )
{
if ( !ep )
{
cout << "error in Table::arithmetic()" << endl;
return 0;
}
expression* node[2];
node[0] = ep->values[0].ep;
node[1] = ep->values[1].ep;
int v_lhs = 0;
int v_rhs = 0;
int ops[2];
// tokenize row
vector<string> row_v;
char *s = toChars( row );
char *token;
token = strtok( s, "\t" );
while ( token )
{
row_v.push_back( string( token ) );
token = strtok ( NULL, "\t" );
}
if ( row_v.size() != cols.size() )
return false;
// see if nodes are number or string,
// if it is string, we need to find the actual value in database
for ( int i = 0; i < 2; i++ )
{
if ( node[i]->func != OP_COLNAME && node[i]->func != OP_NUMBER )
return arithmetic( node[i], row );
else if ( node[i]->func == OP_NUMBER )
{
ops[i] = node[i]->values[0].num;
}
else if ( node[i]->func == OP_COLNAME )
{
int pos = get_col_pos( node[i]->values[0].data );
if ( pos < 0 )
{
cout << "column does not exist!" << endl;
return false;
}
ops[i] = atoi( row_v[pos].c_str() );
}
}
// start comparison
switch( ep->func ){
case OP_PLUS: return ops[0] + ops[1]; break;
case OP_BMINUS: return ops[0] - ops[1]; break;
case OP_TIMES: return ops[0] * ops[1]; break;
case OP_DIVIDE: return ops[0] / ops[1]; break;
case OP_UMINUS: return -ops[0]; break;
default :
cout << "OPERATOR NOT SUPPORTED! " << endl;
return false;
break;
}
}
bool Table::AND( expression * ep, string tuple )
{
bool lhs = false;
bool rhs = false;
if ( ep->values[0].ep->func == OP_AND )
lhs = AND ( ep->values[0].ep, tuple );
else if ( ep->values[0].ep->func == OP_OR )
lhs = OR ( ep->values[0].ep, tuple );
else
lhs = comparison ( ep->values[0].ep, tuple );
if ( ep->values[1].ep->func == OP_AND )
rhs = AND ( ep->values[1].ep, tuple );
else if ( ep->values[1].ep->func == OP_OR )
rhs = OR ( ep->values[1].ep, tuple );
else
rhs = comparison ( ep->values[1].ep, tuple );
return lhs && rhs;
}
bool Table::OR( expression * ep, string tuple )
{
bool lhs = false;
bool rhs = false;
if ( ep->values[0].ep->func == OP_AND )
lhs = AND ( ep->values[0].ep, tuple );
else if ( ep->values[0].ep->func == OP_OR )
lhs = OR ( ep->values[0].ep, tuple );
else
lhs = comparison ( ep->values[0].ep, tuple );
if ( ep->values[1].ep->func == OP_AND )
rhs = AND ( ep->values[1].ep, tuple );
else if ( ep->values[1].ep->func == OP_OR )
rhs = OR ( ep->values[1].ep, tuple );
else
rhs = comparison ( ep->values[1].ep, tuple );
return lhs || rhs;
}
bool Table::comparison( expression * ep, string tuple )
{
if ( !ep )
return false;
//if ( ep->func )
if( ep->values[0].ep->func == OP_STRING ||
ep->values[1].ep->func == OP_STRING )
{
//cout << "here" << endl;
return string_comparison( ep, tuple );
}
else
{
return num_comparison( ep, tuple );
}
}
bool Table::string_comparison( expression * ep, string row )
{
//cout << "here " << endl;
if ( !ep )
return false;
//cout << "what?" << endl;
expression* node[2];
node[0] = ep->values[0].ep;
node[1] = ep->values[1].ep;
string ops[2];
vector<string> row_v;
char *s = toChars( row );
char *token;
token = strtok( s, "\t" );
while ( token )
{
row_v.push_back( string( token ) );
token = strtok ( NULL, "\t" );
}
if ( row_v.size() != cols.size() )
return false;
// see if nodes are number or string,
// if it is string, we need to find the actual value in database
for ( int i = 0; i < 2; i++ )
{
if ( node[i]->func == OP_STRING )
{
ops[i] = string( node[i]->values[0].data );
//cout << "!" << ops[i] << endl;
}
else if ( node[i]->func == OP_COLNAME )
{
//cout << node[i]->values[i].name << endl;
int pos = get_col_pos( node[i]->values[i].name );
if ( pos < 0 )
{
cout << "column does not exist!" << endl;
return false;
}
ops[i] = string( row_v[pos] );
}
}
switch( ep->func ){
case OP_EQUAL: return (ops[0].compare( ops[1] ) == 0 ); break;
default :
cout << "OPERATOR ON STRING NOT SUPPORTED! " << endl;
return false;
break;
}
}
bool Table::num_comparison( expression * ep, string row )
{
if ( !ep )
return false;
expression* node[2];
node[0] = ep->values[0].ep;
node[1] = ep->values[1].ep;
int v_lhs = 0;
int v_rhs = 0;
int ops[2];
// tokenize row
vector<string> row_v;
char *s = toChars( row );
char *token;
token = strtok( s, "\t" );
while ( token )
{
row_v.push_back( string( token ) );
token = strtok ( NULL, "\t" );
}
if ( row_v.size() != cols.size() )
return false;
// see if nodes are number or string,
// if it is string, we need to find the actual value in database
for ( int i = 0; i < 2; i++ )
{
if ( node[i]->func != OP_COLNAME && node[i]->func != OP_NUMBER )
{
ops[i] = arithmetic( node[i], row );
}
else if ( node[i]->func == OP_NUMBER )
{
ops[i] = node[i]->values[0].num;
}
else if ( node[i]->func == OP_COLNAME )
{
int pos = get_col_pos( string( node[i]->values[0].name ) );
if ( pos < 0 )
{
cout << "column does not exist!" << endl;
return false;
}
ops[i] = atoi( row_v[pos].c_str() );
}
}
// start comparison
switch( ep->func ){
case OP_EQUAL: return ops[0] == ops[1]; break;
case OP_NOTEQ: return ops[0] != ops[1]; break;
case OP_LEQ: return ops[0] <= ops[1]; break;
case OP_GEQ: return ops[0] >= ops[1]; break;
case OP_LT: return ops[0] < ops[1]; break;
case OP_GT: return ops[0] > ops[1]; break;
default :
cout << "OPERATOR NOT SUPPORTED! " << endl;
return false;
break;
}
}
int Table::get_col_pos( string col ){
int i = 0;
if( col[0] == '*' || col[0] == '&' )
col = col.substr( 1, col.size() );
for ( vector<string>::iterator itr = cols.begin() ; itr != cols.end(); itr++ )
{
if ( (*itr).compare( col ) == 0){
return i;
}
i++;
}
return -1;
}
int Table::get_colCnt()
{
return cols.size();
}
bool Table::hasCol( std::string col_name ){
bool hasCol = false;
for ( vector<string>::iterator it = cols.begin(); it != cols.end(); ++it ){
if ( it->compare( col_name ) )
{
hasCol = true;
break;
}
}
return hasCol;
}
void Table::print_table(){
cout << name << ": " << endl;
for ( int i = 0 ; i < cols.size(); i++ )
cout << " " << cols[i] << " ";
cout << endl;
}
void Table::print_keys()
{
for (map<string, BTree*>::iterator itr = keys.begin(); itr != keys.end(); itr++ )
cout << "DEBUG: " << name << "." << itr->first << " has a B-tree index. " << endl;
}
MyItr * Table::get_itr()
{
return myItr;
}
Tables::Tables(){
if ( !existingDir( string( "data" ) )){
if ( !existingDir( "data" ) )
{
if ( system( "mkdir data" ) != 0 ){
cerr << "Error making database directory" << endl;
exit(1);
}
system( "cd data" );
}
}
DIR *dir;
struct dirent *ent;
if ((dir = opendir( "./data" )) != NULL ){
while( (ent = readdir( dir )) != NULL) {
string file_name( ent->d_name );
if ( isTBLfile( file_name ) ){
string first_line;
ifstream table( toChars( string ( string( "./data/") + string (ent->d_name ) ) ) );
if( table.is_open() ){
getline( table, first_line );
table.close();
}
add_table( file_name.substr(0, file_name.size() - 4 ) , first_line );
}
}
}
}
string Tables::getCurDir(){
char cwd[2048];
if ( getcwd( cwd, 2048 ) )
return string( cwd );
else