-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericDBFile.cc
108 lines (98 loc) · 2.99 KB
/
GenericDBFile.cc
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
// #include "TwoWayList.h"
// #include "Record.h"
// #include "Schema.h"
// #include "File.h"
// #include "Comparison.h"
// #include "ComparisonEngine.h"
#include "GenericDBFile.h"
#include "Defs.h"
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
// using namespace string;
// GenericDBFile::GenericDBFile() {
// // currentPage = new(std::nothrow) Page();
// }
void GenericDBFile::WriteCurrentPageToDisk() {
if (!currentPage->pageToDisk) {
if (!file->GetLength()) {
file->AddPage(currentPage, file->GetLength());
} else {
file->AddPage(currentPage, file->GetLength() - 1);
}
currentPage->pageToDisk = true;
}
}
void GenericDBFile::PageToDiskOverWrite() {
if (!currentPage->pageToDisk) {
if (!file->GetLength()) {
file->AddPage(currentPage, file->GetLength());
} else {
file->AddPage(currentPage, currPageNum);
}
currentPage->pageToDisk = true;
}
}
void GenericDBFile::WriteOrderMaker(OrderMaker *sortOrder, FILE *file) {
int numAtts = sortOrder->numAtts;
fprintf(file, "%d\n", numAtts);
int *whichAtts = sortOrder->whichAtts;
Type *whichTypes = sortOrder->whichTypes;
string temp;
for (int i = 0; i < numAtts; i++) {
temp = std::to_string(whichAtts[i]);
switch (whichTypes[i]) {
case Int:
temp = temp + " " + "Int";
break;
case String:
temp = temp + " " + "String";
break;
case Double:
temp = temp + " " + "Double";
break;
}
const char *data = temp.c_str();
fprintf(file, "%s\n", data);
}
}
// first sentence of "sorted" already read
// here the next one, i.e, numAtts onwards will be read
void GenericDBFile::ReadOrderMaker(OrderMaker *sortOrder, FILE *file) {
char space[100];
// num Atts read
fscanf(file, "%s", space);
sortOrder->numAtts = std::atoi(space);
for (int i = 0; i < sortOrder->numAtts; i++) {
fscanf(file, "%s", space);
sortOrder->whichAtts[i] = atoi(space);
fscanf(file, "%s", space);
if (!strcmp(space, "Int")) {
sortOrder->whichTypes[i] = Int;
} else if (!strcmp(space, "Double")) {
sortOrder->whichTypes[i] = Double;
} else if (!strcmp(space, "String")) {
sortOrder->whichTypes[i] = String;
} else {
cout << "Bad attribute type for "
<< "\n";
exit(1);
}
}
}
int GenericDBFile::GetNextRecord(Record *tempRec) {
if (!currentPage->GetFirst(tempRec)) {
// assuming the page is empty here so we move to the next page
if (file->GetLength() > currPageNum + 2) {
file->GetPage(currentPage, ++currPageNum);
currentPage->GetFirst(tempRec);
return 1;
} else {
return 0;
}
}
return 1;
}