-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisk.cpp
252 lines (219 loc) · 6.41 KB
/
Disk.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
#include <cstdio>
#include <cstring>
#include <string>
#include "define/constants.h"
#include "Disk.h"
#include "disk_structures.h"
#include "block_access.h"
int Disk::createDisk() {
FILE *disk = fopen(&DISK_PATH[0], "wb+");
if(disk == nullptr)
return FAILURE;
fseek(disk, 0, SEEK_SET);
// 16 MB
for(int i=0; i < DISK_SIZE; i++){
fputc(0,disk);
}
fclose(disk);
return SUCCESS;
}
Disk::~Disk() {
}
int Disk::readBlock(unsigned char *block, int blockNum) {
FILE *disk = fopen(&DISK_PATH[0], "rb");
const int offset = blockNum * BLOCK_SIZE;
fseek(disk, offset, SEEK_SET);
fread(block, BLOCK_SIZE, 1, disk);
fclose(disk);
}
int Disk::writeBlock(unsigned char *block, int blockNum) {
FILE *disk = fopen(&DISK_PATH[0], "rb+");
int offset = blockNum * BLOCK_SIZE;
fseek(disk, offset, SEEK_SET);
fwrite(block, BLOCK_SIZE, 1, disk);
fclose(disk);
}
/*
* Formats the disk
* Set the reserved_blocks entries in block allocation map
* Set Relcat and Attrcat
*/
void Disk::formatDisk() {
FILE *disk = fopen(&DISK_PATH[0], "wb+");
const int reserved_blocks = 6;
const int offset = DISK_SIZE;
fseek(disk, 0, SEEK_SET);
unsigned char blockAllocationMap[BLOCK_SIZE * BLOCK_ALLOCATION_MAP_SIZE];
// reserved_blocks Entries in Block Allocation Map (Used)
for (int i = 0; i < reserved_blocks; i++) {
if (i >= 0 && i <= 3)
blockAllocationMap[i] = (unsigned char) BMAP;
else
blockAllocationMap[i] = (unsigned char) REC;
}
// Remaining Entries in Block Allocation Map are marked Unused
for (int i = reserved_blocks; i < BLOCK_SIZE * BLOCK_ALLOCATION_MAP_SIZE; i++)
blockAllocationMap[i] = (unsigned char) UNUSED_BLK;
fwrite(blockAllocationMap, BLOCK_SIZE * BLOCK_ALLOCATION_MAP_SIZE, 1, disk);
// Remaining Locations of Disk initialised to 0
for (int i = BLOCK_SIZE * BLOCK_ALLOCATION_MAP_SIZE; i < offset; i++) {
fputc(0, disk);
}
fclose(disk);
Disk::add_disk_metainfo();
}
// TODO : review in which file this function should be
void Disk::add_disk_metainfo() {
Attribute rec[6];
HeadInfo *H = (struct HeadInfo *) malloc(sizeof(struct HeadInfo));
// TODO: use the set_headerInfo, make_relcatrec and make_attrcatrec function in schema.cpp
/*
* Set the header for Block 4 - First Block of Relation Catalog
*/
H->blockType = REC;
H->pblock = -1;
H->lblock = -1;
H->rblock = -1;
H->numEntries = 2;
H->numAttrs = NO_OF_ATTRS_RELCAT_ATTRCAT;
H->numSlots = SLOTMAP_SIZE_RELCAT_ATTRCAT;
setHeader(H, RELCAT_BLOCK);
/*
* Set the slot allocation map for Block 4
*/
unsigned char slot_map[SLOTMAP_SIZE_RELCAT_ATTRCAT];
for (int slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
if (slotNum == 0 || slotNum == 1)
slot_map[slotNum] = SLOT_OCCUPIED;
else
slot_map[slotNum] = SLOT_UNOCCUPIED;
}
setSlotmap(slot_map, SLOTMAP_SIZE_RELCAT_ATTRCAT, BLOCK_ALLOCATION_MAP_SIZE);
/*
* Create and Add 2 Records into Block 4 (Relation Catalog)
* - First for Relation Catalog Relation (Block 4 itself is used for this relation)
* - Second for Attribute Catalog Relation (Block 5 is used for this relation)
*/
strcpy(rec[0].sval, "RELATIONCAT");
rec[1].nval = 6;
rec[2].nval = 2;
rec[3].nval = 4;
rec[4].nval = 4;
rec[5].nval = 20;
setRecord(rec, 4, 0);
strcpy(rec[0].sval, "ATTRIBUTECAT");
rec[1].nval = 6;
rec[2].nval = 12;
rec[3].nval = 5;
rec[4].nval = 5;
rec[5].nval = 20;
setRecord(rec, 4, 1);
/*
* Set the header for Block 5 - First Block of Attribute Catalog
*/
H->blockType = REC;
H->pblock = -1;
H->lblock = -1;
H->rblock = -1;
H->numEntries = 12;
H->numAttrs = 6;
H->numSlots = 20;
setHeader(H, 5);
/*
* Set the slot allocation map for Block 5
*/
for (int i = 0; i < 20; i++) {
if (i >= 0 && i <= 11)
slot_map[i] = SLOT_OCCUPIED;
else
slot_map[i] = SLOT_UNOCCUPIED;
}
setSlotmap(slot_map, 20, 5);
/*
* Create Entries for every attribute for Relation Catalog and Attribute Catalog
*/
strcpy(rec[0].sval, "RELATIONCAT");
strcpy(rec[1].sval, "RelName");
rec[2].nval = STRING;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 0;
setRecord(rec, 5, 0);
strcpy(rec[0].sval, "RELATIONCAT");
strcpy(rec[1].sval, "#Attributes");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 1;
setRecord(rec, 5, 1);
strcpy(rec[0].sval, "RELATIONCAT");
strcpy(rec[1].sval, "#Records");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 2;
setRecord(rec, 5, 2);
strcpy(rec[0].sval, "RELATIONCAT");
strcpy(rec[1].sval, "FirstBlock");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 3;
setRecord(rec, 5, 3);
strcpy(rec[0].sval, "RELATIONCAT");
strcpy(rec[1].sval, "LastBlock");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 4;
setRecord(rec, 5, 4);
strcpy(rec[0].sval, "RELATIONCAT");
strcpy(rec[1].sval, "#Slots");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 5;
setRecord(rec, 5, 5);
strcpy(rec[0].sval, "ATTRIBUTECAT");
strcpy(rec[1].sval, "RelName");
rec[2].nval = STRING;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 0;
setRecord(rec, 5, 6);
strcpy(rec[0].sval, "ATTRIBUTECAT");
strcpy(rec[1].sval, "AttributeName");
rec[2].nval = STRING;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 1;
setRecord(rec, 5, 7);
strcpy(rec[0].sval, "ATTRIBUTECAT");
strcpy(rec[1].sval, "AttributeType");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 2;
setRecord(rec, 5, 8);
strcpy(rec[0].sval, "ATTRIBUTECAT");
strcpy(rec[1].sval, "PrimaryFlag");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 3;
setRecord(rec, 5, 9);
strcpy(rec[0].sval, "ATTRIBUTECAT");
strcpy(rec[1].sval, "RootBlock");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 4;
setRecord(rec, 5, 10);
strcpy(rec[0].sval, "ATTRIBUTECAT");
strcpy(rec[1].sval, "Offset");
rec[2].nval = NUMBER;
rec[3].nval = -1;
rec[4].nval = -1;
rec[5].nval = 5;
setRecord(rec, 5, 11);
}