-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock_access.cpp
1097 lines (957 loc) · 32.4 KB
/
block_access.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 <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include "define/constants.h"
#include "define/errors.h"
#include "disk_structures.h"
#include "schema.h"
#include "OpenRelTable.h"
#include "BPlusTree.h"
int getFreeRecBlock();
recId getFreeSlot(int block_num);
int deleteRelCatEntry(recId relcat_recid, Attribute relcat_rec[6]);
int deleteAttrCatEntry(recId attrcat_recid);
/*
* Inserts the Record into the given Relation
*/
int ba_insert(int relId, Attribute *rec) {
Attribute relCatEntry[6];
getRelCatEntry(relId, relCatEntry);
int num_attrs = (int)relCatEntry[RELCAT_NO_ATTRIBUTES_INDEX].nval;
int first_block = (int)relCatEntry[RELCAT_FIRST_BLOCK_INDEX].nval;
int num_slots = (int)relCatEntry[RELCAT_NO_SLOTS_PER_BLOCK_INDEX].nval;
HeadInfo header;
unsigned char slotmap[num_slots];
int blockNum = first_block;
if (first_block == -1) {
blockNum = getFreeRecBlock();
relCatEntry[RELCAT_FIRST_BLOCK_INDEX].nval = blockNum;
// TODO: Take make_headerInfo() function out
HeadInfo *headInfo = (HeadInfo *) malloc(sizeof(HeadInfo));
headInfo->blockType = REC;
headInfo->pblock = -1;
headInfo->lblock = -1;
headInfo->rblock = -1;
headInfo->numEntries = 0;
headInfo->numAttrs = num_attrs;
headInfo->numSlots = num_slots;
setHeader(headInfo, blockNum);
getSlotmap(slotmap, blockNum);
// set all slots as free
memset(slotmap, SLOT_UNOCCUPIED, sizeof(slotmap));
setSlotmap(slotmap, num_slots, blockNum);
}
recId rec_id = getFreeSlot(blockNum);
// no free slot found
if (rec_id.block == -1 && rec_id.slot == -1) {
return E_DISKFULL;
} else if (rec_id.block == E_MAXRELATIONS && rec_id.slot == E_MAXRELATIONS) {
// only one block allowed for RELCAT
return E_MAXRELATIONS;
}
setRecord(rec, rec_id.block, rec_id.slot);
// increment #entries in header (as record is inserted)
header = getHeader(rec_id.block);
header.numEntries = header.numEntries + 1;
setHeader(&header, rec_id.block);
// increment #entries in relation catalog entry
relCatEntry[RELCAT_NO_RECORDS_INDEX].nval = relCatEntry[RELCAT_NO_RECORDS_INDEX].nval + 1;
// update last block in relation catalogue entry
relCatEntry[RELCAT_LAST_BLOCK_INDEX].nval = rec_id.block;
setRelCatEntry(relId, relCatEntry);
char attrName[ATTR_SIZE];
/*
* B+ TREE MODIFICATIONS
*/
// iterate through all attributes
for (int i = 0; i < num_attrs; i++) {
Attribute attrCatEntry[6];
getAttrCatEntry(relId, i, attrCatEntry);
strcpy(attrName, attrCatEntry[ATTRCAT_ATTR_NAME_INDEX].sval);
// if index exists for the attribute, insert record into b plus tree
int rootBlock = (int) attrCatEntry[ATTRCAT_ROOT_BLOCK_INDEX].nval;
if (rootBlock != -1) {
BPlusTree bPlusTree = BPlusTree(relId, attrName);
bPlusTree.bPlusInsert(rec[i], rec_id);
}
}
return SUCCESS;
}
/*
* Searches the relation specified to find the 'next' record starting from the given 'prev' record
* that satisfies the op condition on given attrval
* Uses the b+ tree if target attribute is indexed, otherwise, linear search
*/
int ba_search(relId relid, Attribute *record, char attrName[ATTR_SIZE], Attribute attrval, int op, recId *prev_recid) {
// TODO: Cleanup Code
recId recid;
if (op == PRJCT) {
recid = linear_search(relid, attrName, attrval, op, prev_recid);
} else {
Attribute attrcat_entry[6];
getAttrCatEntry(relid, attrName, attrcat_entry);
// Get the root_block from attribute catalog entry for the given attribute
int root_block = attrcat_entry[4].nval;
if (root_block == -1) {
// No indexing for the attribute
recid = linear_search(relid, attrName, attrval, op, prev_recid);
} else {
// Indexing exists for the attribute
// TODO: recid = bplus_search(relid, attrName, attrval, op,&prev_recid);
BPlusTree bPlusTree(relid, attrName);
recid = bPlusTree.BPlusSearch(attrval, op, prev_recid);
}
}
if ((recid.block == -1) && (recid.slot == -1)) {
// Fails to find a record satisfying the given condition
return FAILURE;
}
// Record Found, recid.block and recid.slot are the block and slot that contains matching record respectively
getRecord(record, recid.block, recid.slot);
return SUCCESS;
}
recId linear_search(relId relid, char attrName[ATTR_SIZE], union Attribute attrval, int op, recId *prev_recid) {
// Get the Relation Catalog Record corresponding to the given relation name
union Attribute relcat_entry[6];
getRelCatEntry(relid, relcat_entry);
int offset, attr_type;
//get the record itself in relcat_entry array of attributes
int curr_block, curr_slot, next_block = -1;
int no_of_attributes = relcat_entry[1].nval;
int no_of_slots = relcat_entry[5].nval;
if (op != PRJCT) {
union Attribute attrcat_entry[6];
getAttrCatEntry(relid, attrName, attrcat_entry);
offset = attrcat_entry[5].nval;
attr_type = attrcat_entry[2].nval;
}
recId ret_recid;
if ((prev_recid->block == -1) && prev_recid->slot == -1) {
// if linear search is done for first time on this attribute
curr_block = relcat_entry[3].nval;
curr_slot = 0;
} else {
// if the linear search knows the hit from previous search
curr_block = prev_recid->block;
curr_slot = prev_recid->slot + 1;
}
unsigned char slotmap[no_of_slots];
/*
* Iterate through all blocks starting from curr_block
*/
while (curr_block != -1) {
struct HeadInfo header;
header = getHeader(curr_block);
next_block = header.rblock;
getSlotmap(slotmap, curr_block);
/*
* Iterate through all the Slots(Records) in the curr_block
*/
for (int slotNum = curr_slot; slotNum < no_of_slots; slotNum++) {
union Attribute record[no_of_attributes];
if (slotmap[slotNum] == SLOT_UNOCCUPIED) {
continue;
}
// Get the record corresponding to {curr_block, slotNum=slotNum}
getRecord(record, curr_block, slotNum);
bool cond = false;
if (op != PRJCT) {
int flag = compareAttributes(record[offset], attrval, attr_type);
switch (op) {
case NE:
if (flag != 0)
cond = true;
break;
case LT:
if (flag < 0)
cond = true;
break;
case LE:
if (flag <= 0)
cond = true;
break;
case EQ:
if (flag == 0)
cond = true;
break;
case GT:
if (flag > 0)
cond = true;
break;
case GE:
if (flag >= 0)
cond = true;
}
}
if (cond == true || op == PRJCT) {
ret_recid = {curr_block, slotNum};
/*
* prev_recid set to denote the previous hit record for the linear search
*/
*prev_recid = ret_recid;
return ret_recid;
}
}
curr_block = next_block;
curr_slot = 0;
}
// No records match the search condition over the relation
return {-1, -1};
}
/*
* Deletes the relation of the given name
* - Clears the Data stored in ALL record blocks corresponding to the relation
* - Clears the Attribute Catalog entries for the attributes of the relation
* - Clears the Relation Catalog entry for this relation
*/
// I have changed the order in which Relation Catalog and Open Relation table is searched respectively for the given relation
//int ba_delete(int relId) {
// /* Get the Relation Catalog Entry */
// Attribute relCatEntry[6];
// getRelCatEntry(relId, relCatEntry);
//
// /* Get first record block corresponding to the given relation */
// int curr_block = (int)relCatEntry[RELCAT_FIRST_BLOCK_INDEX].nval;
// int no_of_attrs = (int)relCatEntry[RELCAT_NO_ATTRIBUTES_INDEX].nval;
// int next_block;
//
// /*
// * Delete the Relation Block-by-Block starting from the first block
// * Get the Next Block by using headerInfo.rblock of the Current Block
// * deleteBlock() will erase the block and mark UNUSED_BLK in the Block Allocation Map
// */
// while (curr_block != -1) {
// struct HeadInfo header = getHeader(curr_block);
// next_block = header.rblock;
// deleteBlock(curr_block);
// curr_block = next_block;
// }
//
// recId attrcat_recid, prev_recid;
// prev_recid.block = -1;
// prev_recid.slot = -1;
// char attrName[ATTR_SIZE];
// char relName[ATTR_SIZE];
// OpenRelTable::getRelationName(relId, relName);
// Attribute relNameAsAttribute;
// strcpy(relNameAsAttribute.sval, relName);
// for (int i = 0; i < no_of_attrs; i++) {
//
// Attribute attrCatEntry[6];
// getAttrCatEntry(relId, i, attrCatEntry);
// strcpy(attrName, attrCatEntry[ATTRCAT_ATTR_NAME_INDEX].sval);
//
// // Delete B+ tree blocks, if it exists for any of the attributes
// int rootBlock = (int) attrCatEntry[ATTRCAT_ROOT_BLOCK_INDEX].nval;
// if (rootBlock != -1) {
// BPlusTree bPlusTree = BPlusTree(relId, attrName);
// bPlusTree.bPlusDestroy(rootBlock);
// attrCatEntry[ATTRCAT_ROOT_BLOCK_INDEX].nval = -1;
// setAttrCatEntry(relId, attrName, attrCatEntry);
// }
//
// attrcat_recid = linear_search(ATTRCAT_RELID, "RelName", relNameAsAttribute, EQ, &prev_recid);
// prev_recid.block = -1;
// prev_recid.slot = -1;
// // Delete Attribute Catalog Entry
// deleteAttrCatEntry(attrcat_recid);
// }
//
// /*
// * Delete Relation Catalog Entry
// */
// recId relcat_recid;
// prev_recid.block = -1;
// prev_recid.slot = -1;
//
// relcat_recid = linear_search(RELCAT_RELID, "RelName", relNameAsAttribute, EQ, &prev_recid);
// deleteRelCatEntry(relcat_recid, relCatEntry);
//
// closeRel(relId);
//
// return SUCCESS;
//}
int ba_delete(char relName[ATTR_SIZE]) {
/* Get the Relation Catalog Entry */
Attribute relName_Attr;
strcpy(relName_Attr.sval, relName);
/* Check if a relation with the given name exists in Relation Catalog and retrieve the relcat_recid */
recId prev_recid, relcat_recid;
prev_recid.block = -1;
prev_recid.slot = -1;
relcat_recid = linear_search(RELCAT_RELID, "RelName", relName_Attr, EQ, &prev_recid);
if ((relcat_recid.block == -1) && (relcat_recid.slot == -1)) {
return E_RELNOTEXIST;
}
Attribute relCatRecord[6];
getRecord(relCatRecord, relcat_recid.block, relcat_recid.slot);
/* Get first record block corresponding to the given relation */
int curr_block = (int)relCatRecord[RELCAT_FIRST_BLOCK_INDEX].nval;
int no_of_attrs = (int)relCatRecord[RELCAT_NO_ATTRIBUTES_INDEX].nval;
int next_block;
/*
* Delete the Relation Block-by-Block starting from the first block
* Get the Next Block by using headerInfo.rblock of the Current Block
* deleteBlock() will erase the block and mark UNUSED_BLK in the Block Allocation Map
*/
while (curr_block != -1) {
struct HeadInfo header = getHeader(curr_block);
next_block = header.rblock;
deleteBlock(curr_block);
curr_block = next_block;
}
recId attrcat_recid;
prev_recid.block = -1;
prev_recid.slot = -1;
char attrName[ATTR_SIZE];
Attribute attrCatRecord[6];
Attribute relNameAsAttribute;
strcpy(relNameAsAttribute.sval, relName);
for (int i = 0; i < no_of_attrs; i++) {
attrcat_recid = linear_search(ATTRCAT_RELID, "RelName", relNameAsAttribute, EQ, &prev_recid);
if(attrcat_recid.block == -1 && attrcat_recid.slot == -1 ) {
continue;
}
getRecord(attrCatRecord, attrcat_recid.block, attrcat_recid.slot);
// Delete B+ tree blocks, if it exists for any of the attributes
int rootBlock = (int) attrCatRecord[ATTRCAT_ROOT_BLOCK_INDEX].nval;
if (rootBlock != -1) {
BPlusTree::bPlusDestroy(rootBlock);
}
// Delete Attribute Catalog Entry
deleteAttrCatEntry(attrcat_recid);
prev_recid.block = -1;
prev_recid.slot = -1;
}
/*
* Delete Relation Catalog Entry
*/
deleteRelCatEntry(relcat_recid, relCatRecord);
return SUCCESS;
}
int ba_renamerel(char oldName[ATTR_SIZE], char newName[ATTR_SIZE]) {
// CHECK IF RELATION WITH NEW NAME ALREADY EXISTS
recId prev_recid;
prev_recid.block = -1;
prev_recid.slot = -1;
recId relcat_recid, attrcat_recid;
Attribute temp;
strcpy(temp.sval, newName);
relcat_recid = linear_search(RELCAT_RELID, "RelName", temp, EQ, &prev_recid);
if (!((relcat_recid.block == -1) && (relcat_recid.slot == -1)))
return E_RELEXIST;
// CHECK IF RELATION WITH OLD NAME EXISTS
prev_recid.block = -1;
prev_recid.slot = -1;
strcpy(temp.sval, oldName);
relcat_recid = linear_search(RELCAT_RELID, "RelName", temp, EQ, &prev_recid);
if ((relcat_recid.block == -1) && (relcat_recid.slot == -1))
return E_RELNOTEXIST;
// UPDATE RELATION CATALOG WITH NEW NAME
Attribute relCatRecord[6];
getRecord(relCatRecord, relcat_recid.block, relcat_recid.slot);
strcpy(relCatRecord[0].sval, newName);
setRecord(relCatRecord, relcat_recid.block, relcat_recid.slot);
// UPDATE ALL ATTRIBUTE CATALOG ENTRIES WITH NEW NAME
prev_recid.block = -1;
prev_recid.slot = -1;
while (1) {
attrcat_recid = linear_search(ATTRCAT_RELID, "RelName", temp, EQ, &prev_recid);
if (!((attrcat_recid.block == -1) && (attrcat_recid.slot == -1))) {
Attribute attrCatRecord[6];
getRecord(attrCatRecord, attrcat_recid.block, attrcat_recid.slot);
if (std::strcmp(attrCatRecord[0].sval, oldName) == 0) {
strcpy(attrCatRecord[0].sval, newName);
setRecord(attrCatRecord, attrcat_recid.block, attrcat_recid.slot);
}
} else
break;
}
return SUCCESS;
}
int ba_renameattr(char relName[ATTR_SIZE], char oldName[ATTR_SIZE], char newName[ATTR_SIZE]) {
// CHECK IF RELATION WITH THE GIVEN NAME EXISTS
recId prev_recid;
prev_recid.block = -1;
prev_recid.slot = -1;
recId relcat_recid, attrcat_recid;
Attribute attrcat_record[6];
Attribute temp;
std::strcpy(temp.sval, relName);
relcat_recid = linear_search(RELCAT_RELID, "RelName", temp, EQ, &prev_recid);
if ((relcat_recid.block == -1) && (relcat_recid.slot == -1)) {
return E_RELNOTEXIST;
}
// CHECK IF ATTRIBUTE WITH THE NEW NAME ALREADY EXISTS
prev_recid.block = -1;
prev_recid.slot = -1;
while (true) {
attrcat_recid = linear_search(ATTRCAT_RELID, "RelName", temp, EQ, &prev_recid);
if (!((attrcat_recid.block == -1) && (attrcat_recid.slot == -1))) {
getRecord(attrcat_record, attrcat_recid.block, attrcat_recid.slot);
if (std::strcmp(attrcat_record[1].sval, newName) == 0)
return E_ATTREXIST;
} else
break;
}
/* RENAME ATTRIBUTE
* If attribute with old name does not exist, E_ATTRNOTEXIST
*/
prev_recid.block = -1;
prev_recid.slot = -1;
while (true) {
attrcat_recid = linear_search(ATTRCAT_RELID, "RelName", temp, EQ, &prev_recid);
if (!((attrcat_recid.block == -1) && (attrcat_recid.slot == -1))) {
getRecord(attrcat_record, attrcat_recid.block, attrcat_recid.slot);
if (std::strcmp(attrcat_record[1].sval, oldName) == 0) {
strcpy(attrcat_record[1].sval, newName);
setRecord(attrcat_record, attrcat_recid.block, attrcat_recid.slot);
return SUCCESS;
}
} else
return E_ATTRNOTEXIST;
}
}
/*
* Retrieves whether the block is occupied or not
* If occupied returns the type of occupied block (REC: 0, IND_INTERNAL: 1, IND_LEAF: 2)
* If Not returns UNUSED_BLK: 3
*/
int getBlockType(int blocknum) {
FILE *disk = fopen(&DISK_PATH[0], "rb");
fseek(disk, 0, SEEK_SET);
unsigned char blockAllocationMap[4 * BLOCK_SIZE];
fread(blockAllocationMap, 4 * BLOCK_SIZE, 1, disk);
fclose(disk);
return (int32_t) (blockAllocationMap[blocknum]);
}
/*
* Reads header for 'blockNum'th block from disk
*/
HeadInfo getHeader(int blockNum) {
HeadInfo header;
FILE *disk = fopen(&DISK_PATH[0], "rb");
fseek(disk, blockNum * BLOCK_SIZE, SEEK_SET);
fread(&header, 32, 1, disk);
fclose(disk);
return header;
}
/*
* Writes header for 'blockNum'th block into disk given the header information
*/
void setHeader(struct HeadInfo *header, int blockNum) {
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, blockNum * BLOCK_SIZE, SEEK_SET);
fwrite(header, 32, 1, disk);
fclose(disk);
}
/*
* Reads slotmap for 'blockNum'th block from disk
*/
void getSlotmap(unsigned char *SlotMap, int blockNum) {
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, blockNum * BLOCK_SIZE, SEEK_SET);
RecBlock R;
fread(&R, BLOCK_SIZE, 1, disk);
int numSlots = R.numSlots;
memcpy(SlotMap, R.slotMap_Records, numSlots);
fclose(disk);
}
/*
* Writes slotmap for 'blockNum'th block into disk given the number of blocks occupied
*/
void setSlotmap(unsigned char *SlotMap, int no_of_slots, int blockNum) {
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, blockNum * BLOCK_SIZE + 32, SEEK_SET);
fwrite(SlotMap, no_of_slots, 1, disk);
fclose(disk);
}
int getFreeBlock(int block_type) {
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, 0, SEEK_SET);
unsigned char blockAllocationMap[4 * BLOCK_SIZE];
fread(blockAllocationMap, 4 * BLOCK_SIZE, 1, disk);
for (int iter = 0; iter < 4 * BLOCK_SIZE; iter++) {
if ((int32_t) (blockAllocationMap[iter]) == UNUSED_BLK) {
blockAllocationMap[iter] = (unsigned char) block_type;
fseek(disk, 0, SEEK_SET);
fwrite(blockAllocationMap, BLOCK_SIZE * 4, 1, disk);
fclose(disk);
return iter;
}
}
return FAILURE;
}
/*
*
*/
int getFreeRecBlock() {
// TODO: Title for this
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, 0, SEEK_SET);
unsigned char blockAllocationMap[4 * BLOCK_SIZE];
fread(blockAllocationMap, 4 * BLOCK_SIZE, 1, disk);
for (int iter = 0; iter < 4 * BLOCK_SIZE; iter++) {
if ((int32_t) (blockAllocationMap[iter]) == UNUSED_BLK) {
blockAllocationMap[iter] = (unsigned char) REC;
fseek(disk, 0, SEEK_SET);
fwrite(blockAllocationMap, BLOCK_SIZE * 4, 1, disk);
fclose(disk);
return iter;
}
}
return FAILURE;
}
/* Finds a free slot either from :
* - the block numbered 'block_num' or
* - next blocks in the linked list of blocks for the relation or
* - a newly allotted block for the relation
*/
// TODO: is the relation checked for being relcat or attrcat and if so, is it being allocated a second block
recId getFreeSlot(int block_num) {
recId recid = {-1, -1};
int prev_block_num, next_block_num;
int num_slots;
int num_attrs;
struct HeadInfo header;
// finding free slot
while (block_num != -1) {
header = getHeader(block_num);
num_slots = header.numSlots;
next_block_num = header.rblock;
num_attrs = header.numAttrs;
// getting slotmap for the current block
unsigned char slotmap[num_slots];
getSlotmap(slotmap, block_num);
// searching for free slot in block (block_num)
int iter;
for (iter = 0; iter < num_slots; iter++) {
if (slotmap[iter] == SLOT_UNOCCUPIED) {
break;
}
}
// if free slot found, return it
if (iter < num_slots) {
slotmap[iter] = SLOT_OCCUPIED;
setSlotmap(slotmap, num_slots, block_num);
recid = {block_num, iter};
return recid;
}
// free slot not found in the current block, check the next block
prev_block_num = block_num;
block_num = next_block_num;
}
/*
* no free slots in current record blocks
* get new record block
* in case of RELCAT, do not go for next block (only one block allowed)
*/
if (prev_block_num == RELCAT_BLOCK) {
return {E_MAXRELATIONS, E_MAXRELATIONS};
} else {
block_num = getFreeRecBlock();
}
// no free blocks available in disk
if (block_num == -1) {
// no free slot can be found, return {-1, -1}
return recid;
}
// TODO: make_headerInfo() function extract
// TODO: make_slotMap() which returns an empty slotMap extract
// TODO: getFreeRecBlock() function may be expanded to also set the header information
// OR make a function that gets a free block and sets both its header and slotMap
//setting header for new record block
header = getHeader(block_num);
header.numSlots = num_slots;
header.lblock = prev_block_num;
header.rblock = -1;
header.numAttrs = num_attrs;
setHeader(&header, block_num);
//setting slotmap
unsigned char slotmap[num_slots];
getSlotmap(slotmap, block_num);
memset(slotmap, SLOT_UNOCCUPIED, sizeof(slotmap)); //all slots are free
slotmap[0] = SLOT_OCCUPIED;
setSlotmap(slotmap, num_slots, block_num);
// recid of free slot
recid = {block_num, 0};
//setting prev_block_num rblock to new block
header = getHeader(prev_block_num);
header.rblock = block_num;
setHeader(&header, prev_block_num);
return recid;
}
/*
* Reads record from disk given blockNum and slotNum
*/
int getRecord(Attribute *rec, int blockNum, int slotNum) {
HeadInfo Header;
Header = getHeader(blockNum);
int numOfSlots = Header.numSlots;
if (slotNum < 0 || slotNum > (numOfSlots - 1))
return E_OUTOFBOUND;
FILE *disk = fopen(&DISK_PATH[0], "rb+");
int BlockType = getBlockType(blockNum);
if (BlockType == REC) {
RecBlock R;
fseek(disk, blockNum * BLOCK_SIZE, SEEK_SET);
fread(&R, BLOCK_SIZE, 1, disk);
int numSlots = R.numSlots;
if (R.slotMap_Records[slotNum] == SLOT_UNOCCUPIED)
return E_FREESLOT;
int numAttrs = R.numAttrs;
/* offset :
* slotmap size ( = numSlots ) +
* size of records coming before current record ( = slotNum * numAttrs * ATTR_SIZE )
*/
memcpy(rec, (R.slotMap_Records + numSlots + (slotNum * numAttrs * ATTR_SIZE)), numAttrs * ATTR_SIZE);
fclose(disk);
return SUCCESS;
} else if (BlockType == IND_INTERNAL) {
//TODO
} else if (BlockType == IND_LEAF) {
//TODO
} else {
fclose(disk);
return FAILURE;
}
}
/*
* Writes record into disk
*/
int setRecord(Attribute *rec, int blockNum, int slotNum) {
struct HeadInfo header = getHeader(blockNum);
int numOfSlots = header.numSlots;
int numAttrs = header.numAttrs;
if (slotNum < 0 || slotNum > numOfSlots - 1)
return E_OUTOFBOUND;
int BlockType = getBlockType(blockNum);
FILE *disk = fopen(&DISK_PATH[0], "rb+");
if (BlockType == REC) {
/* offset :
* size of blocks coming before current block ( = blockNum * BLOCK_SIZE ) +
* header size ( = 32 ) +
* slot_map size ( = numSlots ) +
* size of records coming before current record ( = slotNum * numAttrs * ATTR_SIZE )
*/
fseek(disk, blockNum * BLOCK_SIZE + 32 + numOfSlots + slotNum * numAttrs * ATTR_SIZE, SEEK_SET);
fwrite(rec, numAttrs * ATTR_SIZE, 1, disk);
fclose(disk);
return SUCCESS;
} else if (BlockType == IND_INTERNAL) {
//TODO
} else if (BlockType == IND_LEAF) {
//TODO
} else {
fclose(disk);
return FAILURE;
}
}
/*
* Reads relation catalogue entry from disk
*/
int getRelCatEntry(int relationId, Attribute *relcat_entry) {
if (relationId < 0 || relationId >= MAX_OPEN)
return E_OUTOFBOUND;
if (OpenRelTable::checkIfRelationOpen(relationId) == FAILURE)
return E_RELNOTOPEN;
char relName[16];
OpenRelTable::getRelationName(relationId, relName);
for (int i = 0; i < 20; i++) {
int retval = getRecord(relcat_entry, 4, i);
if (retval == SUCCESS && strcmp(relcat_entry[0].sval, relName) == 0)
return SUCCESS;
}
return FAILURE;
}
/*
* Writes relation catalogue entry into disk
*/
int setRelCatEntry(int relationId, Attribute *relcat_entry) {
if (relationId < 0 || relationId >= MAX_OPEN)
return E_OUTOFBOUND;
if (OpenRelTable::checkIfRelationOpen(relationId) == FAILURE)
return E_RELNOTOPEN;
char relName[16];
OpenRelTable::getRelationName(relationId, relName);
Attribute relcat_entry1[6];
for (int i = 0; i < 20; i++) {
int retval = getRecord(relcat_entry1, 4, i);
if (retval == SUCCESS && strcmp(relcat_entry1[0].sval, relName) == 0) {
setRecord(relcat_entry, 4, i);
return SUCCESS;
}
}
}
/*
* Reads attribute catalogue entry from disk for the given attribute name of a given relation
*/
int getAttrCatEntry(int relationId, char attrname[ATTR_SIZE], Attribute *attrcat_entry) {
if (relationId < 0 || relationId >= MAX_OPEN)
return E_OUTOFBOUND;
if (OpenRelTable::checkIfRelationOpen(relationId) == FAILURE)
return E_RELNOTOPEN;
char relName[ATTR_SIZE];
OpenRelTable::getRelationName(relationId, relName);
int curr_block = 5;
int next_block = -1;
while (curr_block != -1) {
HeadInfo header;
header = getHeader(curr_block);
next_block = header.rblock;
for (int i = 0; i < 20; i++) {
int retval = getRecord(attrcat_entry, curr_block, i);
if (retval == SUCCESS &&
strcmp(attrcat_entry[ATTRCAT_REL_NAME_INDEX].sval, relName) == 0) {
if (strcmp(attrcat_entry[ATTRCAT_ATTR_NAME_INDEX].sval, attrname) == 0)
return SUCCESS;
}
}
curr_block = next_block;
}
return E_ATTRNOTEXIST;
}
/*
* Reads attribute catalogue entry from disk for the given attribute name of a given relation
*/
int getAttrCatEntry(int relationId, int offset, Attribute *attrCatEntry) {
if (relationId < 0 || relationId >= MAX_OPEN)
return E_OUTOFBOUND;
if (OpenRelTable::checkIfRelationOpen(relationId) == FAILURE)
return E_RELNOTOPEN;
char relName[ATTR_SIZE];
OpenRelTable::getRelationName(relationId, relName);
int curr_block = ATTRCAT_BLOCK;
int next_block = -1;
while (curr_block != -1) {
HeadInfo header;
header = getHeader(curr_block);
next_block = header.rblock;
for (int i = 0; i < 20; i++) {
int retval = getRecord(attrCatEntry, curr_block, i);
if (retval == SUCCESS &&
strcmp(attrCatEntry[0].sval, relName) == 0) {
if (static_cast<int>(attrCatEntry[5].nval) == offset)
return SUCCESS;
}
}
curr_block = next_block;
}
return E_ATTRNOTEXIST;
}
/*
* Writes attribute catalogue entry into disk
*/
int setAttrCatEntry(int relationId, char attrName[ATTR_SIZE], Attribute *attrCatEntry) {
if (relationId < 0 || relationId >= MAX_OPEN)
return E_OUTOFBOUND;
if (OpenRelTable::checkIfRelationOpen(relationId) == FAILURE)
return E_RELNOTOPEN;
char relName[ATTR_SIZE];
OpenRelTable::getRelationName(relationId, relName);
int curr_block = ATTRCAT_BLOCK;
int next_block;
while (curr_block != -1) {
HeadInfo header;
header = getHeader(curr_block);
next_block = header.rblock;
Attribute currentAttrCatEntry[NO_OF_ATTRS_RELCAT_ATTRCAT];
for (int slotIter = 0; slotIter < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotIter++) {
int retval = getRecord(currentAttrCatEntry, curr_block, slotIter);
if (retval == SUCCESS &&
strcmp(currentAttrCatEntry[ATTRCAT_REL_NAME_INDEX].sval,
relName) == 0) {
if (strcmp(currentAttrCatEntry[ATTRCAT_ATTR_NAME_INDEX].sval,
attrName) == 0) {
setRecord(attrCatEntry, curr_block, slotIter);
return SUCCESS;
}
}
}
curr_block = next_block;
}
return E_ATTRNOTEXIST;
}
///*
// * 20 = leftChildPointerSize + ATTR_SIZE
// */
//struct InternalEntry getEntry(int block, int entry_number) {
// InternalEntry rec;
// FILE *disk = fopen(&DISK_PATH[0], "rb");
// fseek(disk, block * BLOCK_SIZE + HEADER_SIZE + entry_number * 20, SEEK_SET);
// fread(&rec, sizeof(rec), 1, disk);
// fclose(disk);
// return rec;
//}
/*
* Deletes the given block from the disk
* - Clears the Block Data
* - Marks the Block UNUSED_BLK in Block Allocation Map
*/
int deleteBlock(int blockNum) {
FILE *disk;
disk = fopen(&DISK_PATH[0], "rb+");
/* Clear the data present in the block */
fseek(disk, BLOCK_SIZE * blockNum, SEEK_SET);
for (int i = 0; i < BLOCK_SIZE; i++)
fputc(0, disk);
/* Mark this block as UNUSED in the Block Allocation Map */
fseek(disk, blockNum, SEEK_SET);
fputc((unsigned char) UNUSED_BLK, disk);
fclose(disk);
return SUCCESS;
}
/*
* Delete a Relation from the Relation Catalog
* - update the header & slot map of Relation Catalog
* - update the relation catalog record present in the Relation Catalog Block
* - decrease the number of records of RELCAT by one
* - update the attribute catalog record also present in the Relation Catalog Block
* - decrease the number of records of ATTRCAT by number of attributes in the deleted relation
* - delete entry of relation being deleted in the relation catalog from the disk
*/
int deleteRelCatEntry(recId relcat_recid, Attribute relcat_rec[6]) {
struct HeadInfo relcat_header = getHeader(4);
relcat_header.numEntries = relcat_header.numEntries - 1;
setHeader(&relcat_header, 4);
unsigned char relcat_slotmap[20];
getRecord(relcat_rec, 4, 0);
relcat_rec[2].nval = relcat_rec[2].nval - 1;
setRecord(relcat_rec, 4, 0);
getRecord(relcat_rec, 4, relcat_recid.slot);
int no_of_attrs = relcat_rec[1].nval;
getRecord(relcat_rec, 4, 1);
relcat_rec[2].nval = relcat_rec[2].nval - no_of_attrs;
setRecord(relcat_rec, 4, 1);
getSlotmap(relcat_slotmap, 4);
relcat_slotmap[relcat_recid.slot] = SLOT_UNOCCUPIED;
setSlotmap(relcat_slotmap, 20, relcat_recid.block);
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, (relcat_recid.block) * BLOCK_SIZE + HEADER_SIZE + SLOTMAP_SIZE_RELCAT_ATTRCAT +
relcat_recid.slot * NO_OF_ATTRS_RELCAT_ATTRCAT * ATTR_SIZE, SEEK_SET);
for (int i = 0; i < 16 * 6; i++)
fputc(0, disk);
fclose(disk);
return SUCCESS;
}
/*
* Deletes a Single Attribute Catalog Entry for a given Attribute of a Relation
* - Clears the Disk Entry
* - Updates Header and SlotMap of Attribute Catalog
* - Removes Indexing on the Attribute (TODO: TBD)
* - Deletes Attrbute Catalog's Record Block if multiple blocks had been allocated and current block becomes empty
*/
int deleteAttrCatEntry(recId attrcat_recid) {
/* Clear the Attribute Catalog Record present in the given (Slot & Block) of the Disk */
FILE *disk = fopen(&DISK_PATH[0], "rb+");
fseek(disk, (attrcat_recid.block) * BLOCK_SIZE + HEADER_SIZE + SLOTMAP_SIZE_RELCAT_ATTRCAT +
(attrcat_recid.slot) * NO_OF_ATTRS_RELCAT_ATTRCAT * ATTR_SIZE, SEEK_SET);
for (int i = 0; i < ATTR_SIZE * NO_OF_ATTRS_RELCAT_ATTRCAT; i++)
fputc(0, disk);
fclose(disk);
/* Update the Header and SlotMap for Attribute Catalog */
struct HeadInfo header = getHeader(attrcat_recid.block);
header.numEntries = header.numEntries - 1;
setHeader(&header, attrcat_recid.block);
unsigned char slotmap[20];
getSlotmap(slotmap, attrcat_recid.block);
slotmap[attrcat_recid.slot] = SLOT_UNOCCUPIED;
setSlotmap(slotmap, 20, attrcat_recid.block);
/* Removing Indexing on the Attribute */
// TODO: When indexing use this root_block enty to determine if its present and if yes, remove the indexing on the Attribute
Attribute attrcat_rec[6];
getRecord(attrcat_rec, attrcat_recid.block, attrcat_recid.slot);
int root_block = attrcat_rec[4].nval;
/*
* NOTE: Multiple blocks have been allocated to Attribute Catalog Relation
* Delete a Block allocated to an attribute in case it becomes empty
*/
if (header.numEntries == 0) {