-
Notifications
You must be signed in to change notification settings - Fork 1
/
StorageManager.cpp
executable file
·1070 lines (955 loc) · 31.3 KB
/
StorageManager.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 <ctime>
#include <climits>
#include "Block.h"
#include "Config.h"
#include "Disk.h"
#include "Field.h"
#include "MainMemory.h"
#include "Relation.h"
#include "Schema.h"
#include "SchemaManager.h"
#include "Tuple.h"
using namespace std;
Disk::Disk() { resetDiskIOs(); resetDiskTimer(); }
/*
bool Disk::extendTrack(int schema_index, int block_index, const Tuple& t) {
if (block_index<0) {
cerr << "extendTrack ERROR: block index " << block_index << " out of disk bound" << endl;
return false;
}
vector<Block>& track=tracks[schema_index];
int j=track.size();
if (block_index>=j) {
if (j>0) {
while (!track[j-1].isFull()) { // first fill the last block with invalid tuples
track[j-1].appendTuple(t);
}
}
// fill the gap with invalid tuples
for (int i=j;i<block_index;i++) {
track.push_back(Block::getDummyBlock());
while (!track[i].isFull()) {
track[i].appendTuple(t);
}
}
}
return true;
}
*/
bool Disk::extendTrack(int schema_index, int block_index, const Tuple& t) {
if (block_index<0) {
cerr << "extendTrack ERROR: block index " << block_index << " out of disk bound" << endl;
return false;
}
vector<Block>& track=tracks[schema_index];
int j=track.size();
if (block_index>j) {
if (j>0) {
while (!track[j-1].isFull()) { // first fill the last block with invalid tuples
track[j-1].appendTuple(t);
}
}
// fill the gap with invalid tuples
for (int i=j;i<block_index-1;i++) {
track.push_back(Block::getDummyBlock());
while (!track[i].isFull()) {
track[i].appendTuple(t);
}
}
// fill the last block with only one invalid tuple
track.push_back(Block::getDummyBlock());
track[block_index-1].appendTuple(t);
}
return true;
}
bool Disk::shrinkTrack(int schema_index, int block_index) {
if (block_index<0 || block_index >= tracks[schema_index].size()) {
cerr << "shrinkTrack ERROR: block index " << block_index << " out of disk bound" << endl;
return false;
}
tracks[schema_index].erase(tracks[schema_index].begin()+block_index,tracks[schema_index].end());
return true;
}
Block Disk::getBlock(int schema_index, int block_index) {
if (block_index<0 || block_index>=tracks[schema_index].size()) {
cerr << "getBlock ERROR: block index " << block_index << " out of disk bound" << endl;
return Block::getDummyBlock();
}
incrementDiskIOs(1);
incrementDiskTimer(1);
return tracks[schema_index][block_index];
}
vector<Block> Disk::getBlocks(int schema_index, int block_index, int num_blocks) {
if (block_index<0 || block_index>=tracks[schema_index].size()) {
cerr << "getBlocks ERROR: block index " << block_index << " out of disk bound" << endl;
return vector<Block>();
}
int i;
if ((i=block_index+num_blocks-1)>=tracks[schema_index].size()) {
cerr << "getBlocks ERROR: num of blocks out of disk bound: " << i << endl;
return vector<Block>();
}
incrementDiskIOs(num_blocks);
incrementDiskTimer(num_blocks);
vector<Block> v(tracks[schema_index].begin()+block_index,
tracks[schema_index].begin()+block_index+num_blocks);
return v;
}
bool Disk::setBlock(int schema_index, int block_index, const Block& b) {
if (block_index<0) {
cerr << "setBlock ERROR: block index " << block_index << " out of disk bound" << endl;
return false;
}
incrementDiskIOs(1);
incrementDiskTimer(1);
tracks[schema_index][block_index]=b;
return true;
}
bool Disk::setBlocks(int schema_index, int block_index, const vector<Block>& vb) {
if (block_index<0) {
cerr << "setBlocks ERROR: block index " << block_index << " out of disk bound" << endl;
return false;
}
incrementDiskIOs(vb.size());
incrementDiskTimer(vb.size());
copy< vector<Block>::const_iterator, vector<Block>::iterator >
(vb.begin(),vb.end(),tracks[schema_index].begin()+block_index);
return true;
}
void Disk::incrementDiskIOs(int count) {
if (DISK_I_O_DEBUG)
cerr << "DEBUG: Disk I/O is incremented by " << count << endl;
diskIOs+=count;
}
void Disk::incrementDiskTimer(int num_blocks) {
if (SIMULATED_DISK_LATENCY_ON==1) {
clock_t start_time;
start_time=clock();
clock_t delay=(clock_t)(avg_seek_time+avg_rotation_latency+avg_transfer_time_per_block*num_blocks)
*CLOCKS_PER_SEC/1000;
while (clock()-start_time < delay){
;
}
}
timer+=avg_seek_time+avg_rotation_latency+avg_transfer_time_per_block*num_blocks;
}
void Disk::resetDiskIOs() {
diskIOs=0;
}
unsigned long int Disk::getDiskIOs() const {
return diskIOs;
}
void Disk::resetDiskTimer() {
timer=0;
}
double Disk::getDiskTimer() const {
return timer;
}
Schema::Schema() {}
Schema::Schema(const vector<string>& field_names, const vector<enum FIELD_TYPE>& field_types){
if(field_names.size()!=field_types.size()){
cerr<<"Schema ERROR: size of field_names and size of field_types do not match"<<endl;
return;
}
if (field_names.size()==0) {
cerr<<"Schema ERROR: empty fields"<<endl;
return;
} else if (field_names.size()>MAX_NUM_OF_FIELDS_IN_RELATION){
cerr<<"Schema ERROR: at most "<<MAX_NUM_OF_FIELDS_IN_RELATION<<" fields are allowed"<<endl;
return;
}
for (int i=0;i<field_names.size()-1;i++) {
if (field_names[i]=="") {
cerr<<"Schema ERROR: empty field name at offset " << i << endl;
return;
}
for (int j=i+1;j<field_names.size();j++) {
if (field_names[i]==field_names[j]) {
cerr<<"Schema ERROR: repeated field names " << field_names[i]
<< " at offset " << i << " and " << j << endl;
return;
}
}
}
if (field_names[field_names.size()-1]=="") {
cerr<<"Schema ERROR: empty field name at offset " << (field_names.size()-1) << endl;
return;
}
this->field_names = field_names;
this->field_types = field_types;
for(int i=0;i<field_names.size();i++){
field_offsets[field_names[i]] = i;
if (field_types[i]!=INT && field_types[i]!=STR20) {
cerr<<"Schema ERROR: "<<field_types[i]<<" is not supported"<<endl;
clear();
return;
}
}
}
bool Schema::operator==(const Schema& s) const {
return (field_names==s.field_names && field_types==s.field_types && field_offsets==s.field_offsets);
}
bool Schema::operator!=(const Schema& s) const {
return (field_names!=s.field_names || field_types!=s.field_types || field_offsets!=s.field_offsets);
}
bool Schema::isEmpty() const {
if (field_names.empty() || field_types.empty() || field_offsets.empty()) return true;
return false;
}
bool Schema::fieldNameExists(string field_name) const {
map<string,int >::const_iterator mit;
if ((mit=field_offsets.find(field_name))==field_offsets.end()) {
return false;
}
return true;
}
void Schema::clear() {
field_offsets.clear();
this->field_names.clear();
this->field_types.clear();
}
//returns the field names in defined order
vector<string> Schema::getFieldNames() const {
return field_names;
}
//returns field types in defined order
vector<enum FIELD_TYPE> Schema::getFieldTypes() const {
return field_types;
}
//returns the field name at the offset
string Schema::getFieldName(int offset) const {
if (offset<0 || offset>=getNumOfFields()) {
cerr<<"getFieldName ERROR: offset " << offset << " out of bound"<<endl;
return "";
}
return field_names[offset];
}
//returns the field type at the offset
enum FIELD_TYPE Schema::getFieldType(int offset) const {
if (offset<0 || offset>=getNumOfFields()) {
cerr<<"getFieldType ERROR: offset " << offset << " out of bound"<<endl;
return FIELD_TYPE();
}
return field_types[offset];
}
//returns the field type corresponding to the field name
enum FIELD_TYPE Schema::getFieldType(string field_name) const {
map<string,int >::const_iterator mit;
if ((mit=field_offsets.find(field_name))==field_offsets.end()) {
cerr<<"getFieldOffset ERROR: field name "<<field_name<<" is not found"<<endl;
return FIELD_TYPE();
}
return field_types[mit->second];
}
int Schema::getFieldOffset(string field_name) const {
map<string,int >::const_iterator mit;
if ((mit=field_offsets.find(field_name))==field_offsets.end()) {
cerr<<"getFieldOffset ERROR: field name "<<field_name<<" is not found"<<endl;
return -1;
}
return mit->second;
}
int Schema::getNumOfFields() const {
return field_names.size();
}
int Schema::getTuplesPerBlock() const {
return FIELDS_PER_BLOCK/field_names.size();
}
void Schema::printSchema() const {
printSchema(cout);
cout << endl;
}
void Schema::printSchema(ostream &out) const {
if (field_names.size()>0) {
out << field_names[0] << " " << (field_types[0]==0?"INT":"STR20") << ";";
for (int i=1;i<field_names.size();i++) {
out << endl << field_names[i] << " " << (field_types[i]==0?"INT":"STR20") << ";";
}
}
}
void Schema::printFieldNames() const {
printFieldNames(cout);
cout << endl;
}
void Schema::printFieldNames(ostream &out) const {
for (int i=0;i<field_names.size();i++) {
out << field_names[i] << "\t";
}
}
ostream &operator<<(ostream &out, const Schema &s) {
s.printSchema(out);
return out;
}
Tuple::Tuple(SchemaManager* schema_manager, int schema_index){
this->schema_manager=schema_manager;
this->schema_index=schema_index;
if (this->schema_manager!=NULL) {
Schema& schema=schema_manager->schemas[schema_index];
int numberOfFields=schema.getNumOfFields();
for (int i=0;i<numberOfFields;i++) {
fields.push_back(Field());
}
}
}
Tuple Tuple::getDummyTuple() {
return Tuple(NULL,-1);
}
bool Tuple::isNull() const {
return fields.size()==0;
}
Schema Tuple::getSchema() const {
return schema_manager->schemas[schema_index];
}
int Tuple::getNumOfFields() const {
Schema& schema=schema_manager->schemas[schema_index];
return schema.getNumOfFields();
}
int Tuple::getTuplesPerBlock() const {
Schema& schema=schema_manager->schemas[schema_index];
return schema.getTuplesPerBlock();
}
void Tuple::null() {
fields.clear();
}
bool Tuple::setField(int offset,string s){
Schema& schema=schema_manager->schemas[schema_index];
if (offset>=schema.getNumOfFields() || offset<0){
cerr<<"setField ERROR: offset "<<offset<<" is out of bound!"<<endl;
return false;
} else if (schema.getFieldType(offset)!=STR20) {
cerr<<"setField ERROR: field type not STR20!"<<endl;
return false;
} else {
fields[offset].str=new string(s);
}
return true;
}
bool Tuple::setField(int offset,int i){
Schema& schema=schema_manager->schemas[schema_index];
if (offset>=schema.getNumOfFields() || offset<0){
cerr<<"setField ERROR: offset "<<offset<<" is out of bound!"<<endl;
return false;
} else if (schema.getFieldType(offset)!=INT) {
cerr<<"setField ERROR: field type not INT!"<<endl;
return false;
} else {
fields[offset].integer=i;
}
return true;
}
bool Tuple::setField(string field_name,string s){
Schema& schema=schema_manager->schemas[schema_index];
if (!schema.fieldNameExists(field_name)) {
cerr<<"setField ERROR: field name " << field_name << " not found"<<endl;
return false;
}
int offset=schema.getFieldOffset(field_name);
if (schema.getFieldType(offset)!=STR20) {
cerr<<"setField ERROR: field type not STR20!"<<endl;
return false;
} else {
fields[offset].str=new string(s);
}
return true;
}
bool Tuple::setField(string field_name,int i){
Schema& schema=schema_manager->schemas[schema_index];
if (!schema.fieldNameExists(field_name)) {
cerr<<"setField ERROR: field name " << field_name << " not found"<<endl;
return false;
}
int offset=schema.getFieldOffset(field_name);
if (schema.getFieldType(offset)!=INT) {
cerr<<"setField ERROR: field type not INT!"<<endl;
return false;
} else {
fields[offset].integer=i;
}
return true;
}
union Field Tuple::getField(int offset) const{
if(offset<fields.size() && offset>=0){
return fields[offset];
} else {
cerr<<"getField ERROR: offset "<<offset<<" is out of bound!"<<endl;
return Field();
}
}
union Field Tuple::getField(string field_name) const{
Schema& schema=schema_manager->schemas[schema_index];
int offset=schema.getFieldOffset(field_name);
if(offset<fields.size() && offset>=0){
return fields[offset];
} else {
cerr<<"getField ERROR: offset "<<offset<<" is out of bound!"<<endl;
return Field();
}
}
void Tuple::printTuple() const{
printTuple(false);
}
void Tuple::printTuple(ostream &out) const{
printTuple(false, out);
}
void Tuple::printTuple(bool print_field_names) const {
printTuple(print_field_names,cout);
cout << endl;
}
void Tuple::printTuple(bool print_field_names, ostream &out) const {
Schema& schema=schema_manager->schemas[schema_index];
if (print_field_names) {
schema.printFieldNames(out);
out << endl;
}
for (int i=0;i<fields.size();i++) {
if (schema.getFieldType(i)==INT)
out << fields[i].integer << "\t";
else
out << *(fields[i].str) << "\t";
}
}
ostream &operator<<(ostream &out, const Tuple &t) {
t.printTuple(out);
return out;
}
Block::Block() {}
Block Block::getDummyBlock() {
return Block();
}
bool Block::isFull() const {
if (tuples.empty()) return false;
if (tuples.size()==tuples.front().getTuplesPerBlock()) return true;
return false;
}
bool Block::isEmpty() const {
return tuples.empty();
}
void Block::clear() {
tuples.clear();
}
int Block::getNumTuples() const {
int count=0;
for (vector<Tuple>::const_iterator it=tuples.begin();
it!=tuples.end();it++) {
if (!it->isNull()) count++;
}
return count;
}
Tuple Block::getTuple(int tuple_offset) const { // gets the tuple value at tuple_index; returns empty Tuple if tuple_index out of bound
if (!tuples.empty() && tuple_offset>=tuples.front().getTuplesPerBlock()) {
cerr << "getTuple ERROR: tuple offet " << tuple_offset << " out of bound of the block" << endl;
return Tuple::getDummyTuple();
}
if (tuple_offset<0 || tuple_offset>=tuples.size()) {
cerr << "getTuple ERROR: tuple offet " << tuple_offset << " out of bound" << endl;
return Tuple::getDummyTuple();
}
return tuples[tuple_offset];
}
vector<Tuple> Block::getTuples() const {
return tuples;
}
bool Block::setTuple(int tuple_offset, const Tuple& tuple) { // sets new tuple value at tuple_index; returns false if tuple_index out of bound
Schema s = tuple.getSchema();
if (!tuples.empty()) {
if (tuple_offset>=tuples.front().getTuplesPerBlock()) {
cerr << "setTuple ERROR: tuple offet " << tuple_offset << " out of bound of the block" << endl;
return false;
}
for (int i=0;i<tuples.size();i++) {
if (s!=tuples[i].getSchema()) {
cerr << "setTuple ERROR: tuples' schemas do not match" << endl;
return false;
}
}
}
if (tuple_offset<0 || tuple_offset>=s.getTuplesPerBlock()) {
cerr << "setTuple ERROR: tuple offet " << tuple_offset << " out of bound" << endl;
return false;
}
if (tuple_offset >= tuples.size()) {
//If there is a gap before the offset, filled it with invalid tuples
Tuple t(tuple.schema_manager,tuple.schema_index);
t.null();
for (int i=tuples.size();i<tuple_offset;i++) {
tuples.push_back(t);
}
tuples.push_back(tuple);
} else
tuples[tuple_offset]=tuple;
return true;
}
bool Block::setTuples(const vector<Tuple>& tuples) {
if (tuples.size()>tuples.front().getTuplesPerBlock()) {
cerr << "setTuples ERROR: number of tuples exceed space limit of the block" << endl;
return false;
}
this->tuples.assign(tuples.begin(),tuples.end());
return true;
}
bool Block::setTuples(const vector<Tuple>::const_iterator first, const vector<Tuple>::const_iterator last) {
if (last-first > first->getTuplesPerBlock()) {
cerr << "setTuples ERROR: number of tuples exceed space limit of the block" << endl;
return false;
}
this->tuples.assign(first,last);
return true;
}
bool Block::appendTuple(const Tuple& tuple) {
if (isFull()) {
cerr << "appendTuple ERROR: the block is full" << endl;
return false;
}
this->tuples.push_back(tuple);
return true;
}
bool Block::nullTuple(int tuple_offset) { // empty the tuple at the offset
if (tuple_offset<0 || tuple_offset>=tuples.size()) {
cerr << "nullTuple ERROR: tuple offet " << tuple_offset << " out of bound" << endl;
return false;
}
tuples[tuple_offset].null();
return true;
}
bool Block::nullTuples() { // empty all the tuples in the block
for (int i=0;i<tuples.size();i++) {
tuples[i].null();
}
return true;
}
void Block::printBlock() const {
printBlock(cout);
cout << endl;
}
void Block::printBlock(ostream &out) const {
if (tuples.empty()) return;
vector<Tuple>::const_iterator lit=tuples.begin();
if (lit->isNull())
out << "(hole)";
else {
lit->printTuple(out);
}
lit++;
for (;lit!=tuples.end();lit++) {
out << endl;
if (lit->isNull())
out << "(hole)";
else
lit->printTuple(out);
}
}
ostream &operator<<( ostream &out, const Block &b ) {
b.printBlock(out);
return out;
}
Relation::Relation() {
this->schema_manager=NULL;
this->schema_index=-1;
this->relation_name="";
this->mem=NULL;
this->disk=NULL;
}
Relation::Relation(SchemaManager* schema_manager, int schema_index, string relation_name,
MainMemory* mem, Disk* disk) {
this->schema_manager=schema_manager;
this->schema_index=schema_index;
this->relation_name=relation_name;
this->mem=mem;
this->disk=disk;
}
void Relation::null() {
//data.clear();
this->schema_manager=NULL;
this->schema_index=-1;
this->relation_name="";
this->mem=NULL;
}
string Relation::getRelationName() const {
return relation_name;
}
Schema Relation::getSchema() const {
return schema_manager->schemas[schema_index];
}
//NOTE: Because the operation should not have disk latency,
// it is implemented in Relation instead of in Disk
int Relation::getNumOfBlocks() const {
vector<Block>& data=disk->tracks[schema_index];
return data.size();
}
// returns actual number of tuples in the relation
//NOTE: Because the operation should not have disk latency,
// it is implemented in Relation instead of in Disk
int Relation::getNumOfTuples() const {
vector<Block>& data=disk->tracks[schema_index];
int total_tuples=0;
for (vector<Block>::const_iterator vit=data.begin();vit!=data.end();vit++) {
total_tuples+=vit->getNumTuples();
}
return total_tuples;
}
bool Relation::isNull() const {
return (schema_manager==NULL || schema_index==-1 || mem==NULL);
}
Tuple Relation::createTuple() const {
return Tuple(schema_manager,schema_index);
}
bool Relation::getBlock(int relation_block_index, int memory_block_index) const {
//delay();
//DIOs++;
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "getBlock ERROR: block index " << memory_block_index << " out of bound in memory" << endl;
return false;
}
/*
if (relation_block_index<0 || relation_block_index>=data.size()) {
cerr << "getBlock ERROR: block index " << relation_block_index << " out of bound in relation" << endl;
return false;
}
*/
//mem->setBlock(memory_block_index,data[relation_block_index]);
Block b = disk->getBlock(schema_index,relation_block_index);
if (!b.isEmpty()) {
mem->setBlock(memory_block_index,b);
return true;
}
return false;
}
bool Relation::getBlocks(int relation_block_index, int memory_block_index, int num_blocks) const {
//delay();
//DIOs+=num_blocks;
if (num_blocks<=0) {
cerr << "getBlocks ERROR: num of blocks " << num_blocks << " too few" << endl;
return false;
}
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "getBlocks ERROR: block index " << memory_block_index << " out of bound in memory" << endl;
return false;
}
int i;
if ((i=memory_block_index+num_blocks-1)>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "getBlocks ERROR: access to block out of memory bound" << i << endl;
return false;
}
/*
if (relation_block_index<0 || relation_block_index>=data.size()) {
cerr << "getBlocks ERROR: block index " << relation_block_index << " out of bound in relation" << endl;
return false;
}
if ((i=relation_block_index+num_blocks-1)>=data.size()) {
cerr << "getBlocks ERROR: num of blocks out of relation bound: " << i << endl;
return false;
}
mem->setBlock(memory_block_index,data.begin()+relation_block_index,
data.begin()+relation_block_index+num_blocks);
*/
vector<Block> v=disk->getBlocks(schema_index,relation_block_index,num_blocks);
mem->setBlock(memory_block_index,v.begin(),v.end());
return true;
}
bool Relation::setBlock(int relation_block_index, int memory_block_index) {
//delay();
//DIOs++;
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setBlock ERROR: block index" << memory_block_index << " out of bound in memory" << endl;
return false;
}
if (relation_block_index<0) {
cerr << "setBlock ERROR: block index " << relation_block_index << " out of bound in relation" << endl;
return false;
}
// check if the schema is correct
vector<Tuple> v = mem->getBlock(memory_block_index)->getTuples();
Schema s = getSchema();
for (int i=0;i<v.size();i++) {
if (v[i].getSchema() != s) {
cerr << "setBlock ERROR: The tuple at offest " << i << " of memory block "
<< memory_block_index << " has a different schema." << endl;
return false;
}
}
//data[relation_block_index]=*(mem->getBlock(memory_block_index));
Tuple t(schema_manager,schema_index);
t.null(); //invalidates the tuple
if (disk->extendTrack(schema_index,relation_block_index+1,t)) {
//Actual writing on disk
return disk->setBlock(schema_index,relation_block_index,*(mem->getBlock(memory_block_index)));
}
return false;
}
bool Relation::setBlocks(int relation_block_index, int memory_block_index, int num_blocks) {
//delay();
//DIOs+=num_blocks;
if (num_blocks<=0) {
cerr << "setBlocks ERROR: num of blocks " << num_blocks << " too few" << endl;
return false;
}
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setBlocks ERROR: block index " << memory_block_index << " out of bound in memory" << endl;
return false;
}
int i;
if ((i=memory_block_index+num_blocks-1)>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setBlocks ERROR: access to block out of memory bound: " << i << endl;
return false;
}
if (relation_block_index<0) {
cerr << "setBlocks ERROR: block index " << relation_block_index << " out of bound in relation" << endl;
return false;
}
vector<Block> vb;
Schema s = getSchema();
int j,k;
//for (i=relation_block_index,j=memory_block_index;i<relation_block_index+num_blocks;i++,j++) {
for (j=memory_block_index;j<memory_block_index+num_blocks;j++) {
// check if the schema is correct
vector<Tuple> v = mem->getBlock(j)->getTuples();
for (k=0;k<v.size();k++) {
if (v[k].getSchema() != s) {
cerr << "setBlocks ERROR: The tuple at offest " << k << " of memory block "
<< j << " has a different schema." << endl;
return false;
}
}
//data[i]=*(mem->getBlock(j));
vb.push_back(*(mem->getBlock(j)));
}
Tuple t(schema_manager,schema_index);
t.null(); //invalidates the tuple
if (disk->extendTrack(schema_index,relation_block_index+num_blocks,t)) {
//Actual writing on disk
return disk->setBlocks(schema_index,relation_block_index,vb);
}
return false;
}
//delete the block from [starting_block_index] to the last block
bool Relation::deleteBlocks(int starting_block_index) {
return disk->shrinkTrack(schema_index,starting_block_index);
}
void Relation::printRelation() const {
printRelation(cout);
cout << endl;
}
//NOTE: Because the operation should not have disk latency,
// it is implemented in Relation instead of in Disk
void Relation::printRelation(ostream &out) const {
vector<Block>& data=disk->tracks[schema_index];
int i=0;
out << "******RELATION DUMP BEGIN******" << endl;
schema_manager->schemas[schema_index].printFieldNames(out);
out << endl;
for (vector<Block>::const_iterator vit=data.begin();vit!=data.end();vit++) {
out << i << ": ";
vit->printBlock(out);
out << endl;
i++;
}
out << "******RELATION DUMP END******";
}
ostream &operator<<(ostream &out, const Relation &r) {
r.printRelation(out);
return out;
}
MainMemory::MainMemory() { }
bool MainMemory::setBlock(int memory_block_index, const vector<Block>::const_iterator first,
const vector<Block>::const_iterator last) {
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setBlock ERROR: block index " << memory_block_index << " out of memory bound" << endl;
return false;
}
int i=memory_block_index;
for (vector<Block>::const_iterator it=first;it!=last;it++) {
if (i>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setBlock ERROR: number of blocks reaches memory boundary" << endl;
return false;
}
blocks[i] = *it;
i++;
}
return true;
}
int MainMemory::getMemorySize() const { //returns max number of blocks
return NUM_OF_BLOCKS_IN_MEMORY;
}
Block* MainMemory::getBlock(int memory_block_index) {
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "getBlock ERROR: block index " << memory_block_index << " out of memory bound" << endl;
return NULL;
}
return blocks+memory_block_index;
}
bool MainMemory::setBlock(int memory_block_index, const Block& b) {
if (memory_block_index<0 || memory_block_index>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setBlock ERROR: block index " << memory_block_index << " out of memory bound" << endl;
return false;
}
blocks[memory_block_index] = b;
return true;
}
vector<Tuple> MainMemory::getTuples(int memory_block_begin,int num_blocks) const { //gets tuples from a range of blocks
if (memory_block_begin<0 || memory_block_begin>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "getTuples ERROR: block index " << memory_block_begin << " out of memory bound" << endl;
return vector<Tuple>();
}
if (num_blocks<=0) {
cerr << "getTuples ERROR: num of blocks " << num_blocks << " too few" << endl;
return vector<Tuple>();
}
int i;
if ((i=memory_block_begin+num_blocks-1)>=NUM_OF_BLOCKS_IN_MEMORY ) {
cerr << "getTuples ERROR: access to block out of memory bound: " << i << endl;
return vector<Tuple>();
}
vector<Tuple> tuples;
Schema s = blocks[memory_block_begin].getTuples()[0].getSchema();
for (int i=memory_block_begin;i<memory_block_begin+num_blocks;i++) {
vector<Tuple> tuples2=blocks[i].getTuples();
if (tuples2[0].getSchema() != s) {
cerr << "getTuples ERROR: schema at memory block " << i << " has a different schema" << endl;
return vector<Tuple>();
}
// Only valid tuples are returned
for (vector<Tuple>::const_iterator it=tuples2.begin();it!=tuples2.end();it++) {
if (!it->isNull()) tuples.push_back(*it);
}
}
return tuples;
}
//writes tuples consecutively starting from a particular memory block;
//returns false if out of bound in memory
bool MainMemory::setTuples(int memory_block_begin,const vector<Tuple>& tuples) {
if (memory_block_begin<0 || memory_block_begin>=NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setTuples ERROR: block index " << memory_block_begin << " out of memory bound" << endl;
return false;
}
int tuples_per_block=tuples.front().getTuplesPerBlock();
int num_blocks=tuples.size()/tuples_per_block;
int num_additional_blocks=(tuples.size()%tuples_per_block>0?1:0);
if (memory_block_begin + num_blocks + num_additional_blocks >
NUM_OF_BLOCKS_IN_MEMORY) {
cerr << "setTuples ERROR: number of tuples exceed the memory space" << endl;
return false;
}
vector<Tuple>::const_iterator lit=tuples.begin(),lit2=tuples.begin();
int i,j;
for (i=memory_block_begin;i<memory_block_begin + num_blocks;i++) {
for (j=0;j<tuples_per_block;j++,lit2++);
//blocks[i].tuples.assign(lit,lit2);
blocks[i].setTuples(lit,lit2);
lit=lit2;
}
if (num_additional_blocks==1) {
//blocks[i].tuples.assign(lit,tuples.end());
blocks[i].setTuples(lit,tuples.end());
}
return true;
}
void MainMemory::dumpMemory() const {
dumpMemory(cout);
cout << endl;
}
void MainMemory::dumpMemory(ostream &out) const {
out << "******MEMORY DUMP BEGIN******" << endl;
for (int i=0;i<NUM_OF_BLOCKS_IN_MEMORY;i++) {
out << i << ": ";
blocks[i].printBlock(out);
out << endl;
}
out << "******MEMORY DUMP END******";
}
ostream &operator<<(ostream &out, const MainMemory &m) {
m.dumpMemory(out);
return out;
}
SchemaManager::SchemaManager(MainMemory* mem, Disk* disk) {
this->mem=mem;
this->disk=disk;
offset=0;
}
Schema SchemaManager::getSchema(string relation_name) const {
map<string,int>::const_iterator it=relation_name_to_index.find(relation_name);
if (it==relation_name_to_index.end()) {
cerr << "getSchema ERROR: relation " << relation_name << " does not exist" << endl;
return Schema();
} else {
return schemas[it->second];
}
}
bool SchemaManager::relationExists(string relation_name) const {
map<string,int>::const_iterator it=relation_name_to_index.find(relation_name);
if (it==relation_name_to_index.end())
return false;
return true;
}
Relation* SchemaManager::createRelation(string relation_name,const Schema& schema){
if (relation_name=="") {
cerr << "createRelation ERROR: empty relation name" << endl;
return NULL;
}
map<string,int>::iterator it=relation_name_to_index.find(relation_name);
if (it!=relation_name_to_index.end()) {
cerr << "createRelation ERROR: " << relation_name << " already exists" << endl;