-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexternal_fs_commands.cpp
588 lines (497 loc) · 16.7 KB
/
external_fs_commands.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
#include <iostream>
#include <cstdio>
#include <cstring>
#include "external_fs_commands.h"
#include "disk_structures.h"
#include "block_access.h"
#include "OpenRelTable.h"
#include "algebra.h"
#include "schema.h"
using namespace std;
void string_to_char_array(string x, char *a, int size);
void writeHeaderFieldToFile(FILE *fp, int32_t headerField);
void writeHeaderToFile(FILE *fp_export, HeadInfo h);
void writeAttributeToFile(FILE *fp, Attribute attribute, int type, int lastLineFlag);
void dump_relcat() {
string relation_catalog = "relation_catalog";
string filePath = OUTPUT_FILES_PATH + relation_catalog;
char fileName[filePath.length() + 1];
string_to_char_array(filePath, fileName, filePath.length() + 1);
FILE *fp_export = fopen(fileName, "w");
Attribute relCatRecord[ATTR_SIZE];
HeadInfo headInfo;
// write header of block 4 to disk
headInfo = getHeader(RELCAT_BLOCK);
writeHeaderToFile(fp_export, headInfo);
unsigned char slotmap[headInfo.numSlots];
getSlotmap(slotmap, RELCAT_BLOCK);
for (int slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
unsigned char ch = slotmap[slotNum];
fputc(ch, fp_export);
}
fputs("\n", fp_export);
for (int slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
getRecord(relCatRecord, RELCAT_BLOCK, slotNum);
if ((char) slotmap[slotNum] == SLOT_UNOCCUPIED)
strcpy(relCatRecord[0].sval, "NULL");
// RelationName
writeAttributeToFile(fp_export, relCatRecord[0], STRING, 0);
// #Attributes
writeAttributeToFile(fp_export, relCatRecord[1], NUMBER, 0);
// #Records
writeAttributeToFile(fp_export, relCatRecord[2], NUMBER, 0);
// FirstBlock
writeAttributeToFile(fp_export, relCatRecord[3], NUMBER, 0);
// LastBlock
writeAttributeToFile(fp_export, relCatRecord[4], NUMBER, 0);
// #SlotsPerBlock
writeAttributeToFile(fp_export, relCatRecord[5], NUMBER, 1);
}
fclose(fp_export);
}
void dump_attrcat() {
string attribute_catalog = "attribute_catalog";
string filePath = OUTPUT_FILES_PATH + attribute_catalog;
char fileName[filePath.length() + 1];
string_to_char_array(filePath, fileName, filePath.length() + 1);
FILE *fp_export = fopen(fileName, "w");
Attribute attrCatRecord[ATTR_SIZE];
int attrCatBlock = ATTRCAT_BLOCK;
HeadInfo headInfo;
while (attrCatBlock != -1) {
headInfo = getHeader(attrCatBlock);
writeHeaderToFile(fp_export, headInfo);
unsigned char slotmap[headInfo.numSlots];
getSlotmap(slotmap, attrCatBlock);
for (int slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
unsigned char ch = slotmap[slotNum];
fputc(ch, fp_export);
}
fputs("\n", fp_export);
for (int slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
getRecord(attrCatRecord, attrCatBlock, slotNum);
if ((char) slotmap[slotNum] == SLOT_UNOCCUPIED) {
strcpy(attrCatRecord[0].sval, "NULL");
strcpy(attrCatRecord[1].sval, "NULL");
}
// RelationName
writeAttributeToFile(fp_export, attrCatRecord[0], STRING, 0);
// AttributeName
writeAttributeToFile(fp_export, attrCatRecord[1], STRING, 0);
// AttributeType
writeAttributeToFile(fp_export, attrCatRecord[2], NUMBER, 0);
// PrimaryFlag
writeAttributeToFile(fp_export, attrCatRecord[3], NUMBER, 0);
// RootBlock
writeAttributeToFile(fp_export, attrCatRecord[4], NUMBER, 0);
// Offset
writeAttributeToFile(fp_export, attrCatRecord[5], NUMBER, 1);
}
attrCatBlock = headInfo.rblock;
}
fclose(fp_export);
}
void dumpBlockAllocationMap() {
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);
int blockNum;
char s[ATTR_SIZE];
string block_allocation_map = "block_allocation_map";
string filePath = OUTPUT_FILES_PATH + block_allocation_map;
char fileName[filePath.length() + 1];
string_to_char_array(filePath, fileName, filePath.length() + 1);
FILE *fp_export = fopen(fileName, "w");
for (blockNum = 0; blockNum < 4; blockNum++) {
fputs("Block ", fp_export);
sprintf(s, "%d", blockNum);
fputs(s, fp_export);
fputs(": Block Allocation Map\n", fp_export);
}
for (blockNum = 4; blockNum < 8192; blockNum++) {
fputs("Block ", fp_export);
sprintf(s, "%d", blockNum);
fputs(s, fp_export);
if ((int32_t) (blockAllocationMap[blockNum]) == UNUSED_BLK) {
fputs(": Unused Block\n", fp_export);
}
if ((int32_t) (blockAllocationMap[blockNum]) == REC) {
fputs(": Record Block\n", fp_export);
}
if ((int32_t) (blockAllocationMap[blockNum]) == IND_INTERNAL) {
fputs(": Internal Index Block\n", fp_export);
}
if ((int32_t) (blockAllocationMap[blockNum]) == IND_LEAF) {
fputs(": Leaf Index Block\n", fp_export);
}
}
fclose(fp_export);
}
void ls() {
Attribute relCatRecord[6];
int attr_blk = 4;
HeadInfo headInfo;
headInfo = getHeader(attr_blk);
unsigned char slotmap[headInfo.numSlots];
getSlotmap(slotmap, attr_blk);
for (int i = 0; i < 20; i++) {
getRecord(relCatRecord, attr_blk, i);
if ((char) slotmap[i] == SLOT_OCCUPIED)
std::cout << relCatRecord[0].sval << "\n";
}
std::cout << "\n";
}
int importRelation(char *fileName) {
FILE *file = fopen(fileName, "r");
/*
* GET ATTRIBUTE NAMES FROM FIRST LINE OF FILE
*/
char *firstLine = (char *) malloc(sizeof(char));
int numOfCharactersInLine = 1;
int currentCharacter, previousCharacter;
int numOfAttributes = 1;
previousCharacter = ',';
while ((currentCharacter = fgetc(file)) != '\n') {
if (currentCharacter == EOF)
break;
while (currentCharacter == ' ' || currentCharacter == '\t' || currentCharacter == '\n') {
currentCharacter = fgetc(file);
}
if (currentCharacter == EOF)
break;
if (currentCharacter == ',') {
numOfAttributes++;
if (previousCharacter == currentCharacter) {
cout << "Null values are not allowed in attribute names\n";
return FAILURE;
}
}
firstLine[numOfCharactersInLine - 1] = currentCharacter;
numOfCharactersInLine++;
firstLine = (char *) realloc(firstLine, (numOfCharactersInLine) * sizeof(char));
previousCharacter = currentCharacter;
}
if (previousCharacter == ',') {
cout << "Null values are not allowed in attribute names\n";
return FAILURE;
}
if (numOfAttributes > 125) {
return E_MAXATTRS;
}
firstLine[numOfCharactersInLine - 1] = '\0';
int currentCharIndexInLine = 0, attrOffsetIterator, attributeIndexIterator;
char attributeNames[numOfAttributes][ATTR_SIZE];
attrOffsetIterator = 0;
while (attrOffsetIterator < numOfAttributes) {
attributeIndexIterator = 0;
while (((firstLine[currentCharIndexInLine] != ',') && (firstLine[currentCharIndexInLine] != '\0')) &&
(attributeIndexIterator < ATTR_SIZE - 1)) {
if (checkIfInvalidCharacter(firstLine[currentCharIndexInLine])) {
cout << "Invalid character : '" << firstLine[currentCharIndexInLine] << "' in attribute name\n";
return FAILURE;
}
attributeNames[attrOffsetIterator][attributeIndexIterator++] = firstLine[currentCharIndexInLine++];
}
if (attributeIndexIterator == ATTR_SIZE - 1) {
while (firstLine[currentCharIndexInLine] != ',')
currentCharIndexInLine++;
}
attributeNames[attrOffsetIterator][attributeIndexIterator] = '\0';
attrOffsetIterator++;
currentCharIndexInLine++;
}
currentCharIndexInLine = 0;
/*
* INFER ATTRIBUTE TYPES FROM SECOND LINE OF FILE
*/
char *secondLine = (char *) malloc(sizeof(char));
numOfCharactersInLine = 1;
while ((currentCharacter = fgetc(file)) != '\n') {
if (currentCharacter == EOF)
break;
secondLine[numOfCharactersInLine - 1] = currentCharacter;
numOfCharactersInLine++;
secondLine = (char *) realloc(secondLine, (numOfCharactersInLine) * sizeof(char));
}
secondLine[numOfCharactersInLine - 1] = '\0';
currentCharIndexInLine = 0;
char secondLineFields[numOfAttributes][ATTR_SIZE];
int attrTypes[numOfAttributes];
attrOffsetIterator = 0;
while (attrOffsetIterator < numOfAttributes) {
attributeIndexIterator = 0;
while (((secondLine[currentCharIndexInLine] != ',') && (secondLine[currentCharIndexInLine] != '\0')) &&
(attributeIndexIterator < ATTR_SIZE - 1)) {
secondLineFields[attrOffsetIterator][attributeIndexIterator++] = secondLine[currentCharIndexInLine++];
}
secondLineFields[attrOffsetIterator][attributeIndexIterator] = '\0';
attrTypes[attrOffsetIterator] = checkAttrTypeOfValue(secondLineFields[attrOffsetIterator]);
attrOffsetIterator++;
currentCharIndexInLine++;
}
// EXTRACT RELATION NAME FROM FILE PATH
currentCharIndexInLine = 0;
char relationName[ATTR_SIZE];
int fileNameIterator = strlen(fileName) - 1;
while (fileName[fileNameIterator] != '.') {
fileNameIterator--;
}
fileNameIterator--;
int end = fileNameIterator;
while (fileName[fileNameIterator] != '/') {
fileNameIterator--;
}
int start;
int relname_iter;
for (start = fileNameIterator + 1, relname_iter = 0;
start <= end && relname_iter < ATTR_SIZE - 1; start++, relname_iter++) {
relationName[relname_iter] = fileName[start];
}
if (relname_iter == ATTR_SIZE - 1) {
cout << "File name is more than 15 characters, trimming to get relation name\n";
}
relationName[relname_iter] = '\0';
if (std::strcmp(relationName, TEMP) == 0) {
return E_CREATETEMP;
}
// CREATE RELATION
int ret;
ret = createRel(relationName, numOfAttributes, attributeNames, attrTypes);
if (ret != SUCCESS) {
cout << "Import not possible as createRel failed\n";
return ret;
}
// OPEN RELATION
int relId = OpenRelTable::openRelation(relationName);
if (relId == E_CACHEFULL) {
cout << "Import not possible as openRel failed\n";
return FAILURE;
}
// Skip first line containing attribute names
file = fopen(fileName, "r");
while ((currentCharacter = fgetc(file)) != '\n')
continue;
char *currentLineAsCharArray = (char *) malloc(sizeof(char));
numOfCharactersInLine = 1;
int lineNumber = 2;
while (true) {
currentCharacter = fgetc(file);
if (currentCharacter == EOF)
break;
while (currentCharacter == ' ' || currentCharacter == '\t' || currentCharacter == '\n') {
currentCharacter = fgetc(file);
}
if (currentCharacter == EOF)
break;
numOfCharactersInLine = 1;
int numOfFieldsInLine = 0;
previousCharacter = ',';
while ((currentCharacter != '\n') && (currentCharacter != EOF)) {
if (currentCharacter == ',')
numOfFieldsInLine++;
if (currentCharacter == previousCharacter && currentCharacter == ',') {
OpenRelTable::closeRelation(relId);
ba_delete(relationName);
cout << "Null values are not allowed in attribute fields\n";
return FAILURE;
}
currentLineAsCharArray[numOfCharactersInLine - 1] = currentCharacter;
numOfCharactersInLine++;
currentLineAsCharArray = (char *) realloc(currentLineAsCharArray, (numOfCharactersInLine) * sizeof(char));
previousCharacter = currentCharacter;
currentCharacter = fgetc(file);
}
if (previousCharacter == ',') {
OpenRelTable::closeRelation(relId);
ba_delete(relationName);
cout << "Null values are not allowed in attribute fields\n";
return FAILURE;
}
if (numOfAttributes != numOfFieldsInLine + 1) {
OpenRelTable::closeRelation(relId);
ba_delete(relationName);
cout << "Mismatch in number of attributes\n";
return FAILURE;
}
currentLineAsCharArray[numOfCharactersInLine - 1] = '\0';
currentCharIndexInLine = 0;
char attributesCharArray[numOfAttributes][ATTR_SIZE];
attrOffsetIterator = 0;
while (attrOffsetIterator < numOfAttributes) {
attributeIndexIterator = 0;
while (((currentLineAsCharArray[currentCharIndexInLine] != ',') &&
(currentLineAsCharArray[currentCharIndexInLine] != '\0')) &&
(attributeIndexIterator < ATTR_SIZE - 1)) {
attributesCharArray[attrOffsetIterator][attributeIndexIterator++] = currentLineAsCharArray[currentCharIndexInLine++];
}
if (attributeIndexIterator == ATTR_SIZE - 1) {
while (currentLineAsCharArray[currentCharIndexInLine] != ',')
currentCharIndexInLine++;
}
currentCharIndexInLine++;
attributesCharArray[attrOffsetIterator][attributeIndexIterator] = '\0';
attrOffsetIterator++;
}
Attribute record[numOfAttributes];
int retValue = constructRecordFromAttrsArray(numOfAttributes, record, attributesCharArray, attrTypes);
if (retValue == E_ATTRTYPEMISMATCH) {
OpenRelTable::closeRelation(relId);
ba_delete(relationName);
return E_ATTRTYPEMISMATCH;
} else if (retValue == E_INVALID) {
OpenRelTable::closeRelation(relId);
ba_delete(relationName);
cout << "Invalid character at line " << lineNumber << " in file \n";
return FAILURE;
}
int retVal = ba_insert(relId, record);
if (retVal != SUCCESS) {
OpenRelTable::closeRelation(relId);
ba_delete(relationName);
cout << "Insert failed at line"<< lineNumber << " in file" << endl;
return retVal;
}
if (currentCharacter == EOF)
break;
lineNumber++;
}
OpenRelTable::closeRelation(relId);
fclose(file);
return SUCCESS;
}
int exportRelation(char *relname, char *filename) {
FILE *fp_export = fopen(filename, "w");
if (!fp_export) {
cout << " Invalid file path" << endl;
return FAILURE;
}
HeadInfo headInfo;
Attribute relcat_rec[6];
int firstBlock, numOfAttrs;
int slotNum;
for (slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
int retval = getRecord(relcat_rec, RELCAT_BLOCK, slotNum);
if (retval == SUCCESS && strcmp(relcat_rec[0].sval, relname) == 0) {
firstBlock = (int) relcat_rec[3].nval;
numOfAttrs = (int) relcat_rec[1].nval;
break;
}
}
if (slotNum == SLOTMAP_SIZE_RELCAT_ATTRCAT) {
cout << "The relation does not exist\n";
return FAILURE;
}
if (firstBlock == -1) {
cout << "No records exist for the relation\n";
return FAILURE;
}
Attribute rec[6];
int recBlock_Attrcat = ATTRCAT_BLOCK;
int nextRecBlock_Attrcat;
// Array for attribute names and types
int attrNo = 0;
char attrName[numOfAttrs][ATTR_SIZE];
int attrType[numOfAttrs];
/*
* Searching the Attribute Catalog Disk Blocks
* for finding and storing all the attributes of the given relation
*/
while (recBlock_Attrcat != -1) {
headInfo = getHeader(recBlock_Attrcat);
nextRecBlock_Attrcat = headInfo.rblock;
for (slotNum = 0; slotNum < SLOTMAP_SIZE_RELCAT_ATTRCAT; slotNum++) {
int retval = getRecord(rec, recBlock_Attrcat, slotNum);
if (retval == SUCCESS && strcmp(rec[0].sval, relname) == 0) {
// Attribute belongs to this Relation - add info to array
strcpy(attrName[attrNo], rec[1].sval);
attrType[attrNo] = (int) rec[2].nval;
attrNo++;
}
}
recBlock_Attrcat = nextRecBlock_Attrcat;
}
// Write the Attribute names to o/p file
for (attrNo = 0; attrNo < numOfAttrs; attrNo++) {
fputs(attrName[attrNo], fp_export);
if (attrNo != numOfAttrs - 1)
fputs(",", fp_export);
}
fputs("\n", fp_export);
int block_num = firstBlock;
int num_slots;
int num_attrs;
/*
* Iterate over the record blocks of this relation
* Linked list traversal
*/
while (block_num != -1) {
headInfo = getHeader(block_num);
num_slots = headInfo.numSlots;
num_attrs = headInfo.numAttrs;
nextRecBlock_Attrcat = headInfo.rblock;
unsigned char slotmap[num_slots];
getSlotmap(slotmap, block_num);
Attribute A[num_attrs];
slotNum = 0;
// Go through all slots and write the record entry to file
for (slotNum = 0; slotNum < num_slots; slotNum++) {
if (slotmap[slotNum] == SLOT_OCCUPIED) {
getRecord(A, block_num, slotNum);
char s[ATTR_SIZE];
for (int l = 0; l < numOfAttrs; l++) {
if (attrType[l] == NUMBER) {
sprintf(s, "%f", A[l].nval);
fputs(s, fp_export);
}
if (attrType[l] == STRING) {
fputs(A[l].sval, fp_export);
}
if (l != numOfAttrs - 1)
fputs(",", fp_export);
}
fputs("\n", fp_export);
}
}
block_num = nextRecBlock_Attrcat;
}
fclose(fp_export);
return SUCCESS;
}
void writeHeaderToFile(FILE *fp_export, HeadInfo h) {
writeHeaderFieldToFile(fp_export, h.blockType);
writeHeaderFieldToFile(fp_export, h.pblock);
writeHeaderFieldToFile(fp_export, h.lblock);
writeHeaderFieldToFile(fp_export, h.rblock);
writeHeaderFieldToFile(fp_export, h.numEntries);
writeHeaderFieldToFile(fp_export, h.numAttrs);
writeHeaderFieldToFile(fp_export, h.numSlots);
}
void writeHeaderFieldToFile(FILE *fp, int32_t headerField) {
char tmp[ATTR_SIZE];
sprintf(tmp, "%d", headerField);
fputs(tmp, fp);
fputs(",", fp);
}
void writeAttributeToFile(FILE *fp, Attribute attribute, int type, int lastLineFlag) {
char tmp[ATTR_SIZE];
if (type == NUMBER) {
sprintf(tmp, "%d", (int) attribute.nval);
fputs(tmp, fp);
} else if (type == STRING) {
fputs(attribute.sval, fp);
}
if (lastLineFlag == 0) {
fputs(",", fp);
} else {
fputs("\n", fp);
}
}
bool checkIfInvalidCharacter(char character) {
if (character >= 48 && character <= 57 || character >= 65 && character <= 90 ||
character >= 97 && character <= 122 || character == '-' || character == '_') {
return false;
}
return true;
}