-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortedFile.cc
339 lines (293 loc) · 9.39 KB
/
SortedFile.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
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
#include "SortedFile.h"
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include "GenericDBFile.h"
using namespace std;
SortedFile::SortedFile() {
currentPage = new (std::nothrow) Page();
tempRec = new Record();
}
// create a metadata file
int SortedFile::Create(const char *f_path, void *startup) {
sortdata = (SortInfo *)startup;
string metadataFileName(f_path);
metadataFileName += ".data";
FILE *metadata = fopen(metadataFileName.c_str(), "w");
fprintf(metadata, "%s\n", "sorted");
fprintf(metadata, "%d\n", sortdata->runLength);
WriteOrderMaker(sortdata->myOrder, metadata);
fclose(metadata);
sortOrder = sortdata->myOrder;
runLength = sortdata->runLength;
file = new File();
file->Open(0, f_path);
prevGetNext = false;
readMode = true;
return 1;
}
void SortedFile::Load(Schema &f_schema, const char *loadpath) {
initQ();
readMode = false;
FILE *tableFile = fopen(loadpath, "r");
if (!tableFile) {
cout << "ERROR : Cannot open file. EXIT !!!\n";
exit(1);
}
Record temp;
while (temp.SuckNextRecord(&f_schema, tableFile) == 1) {
input->Insert(&temp);
}
// input->ShutDown();
// WritePipeToDisk(output);
// // set it to read Mode
// readMode = true;
// delete input;
// delete output;
// delete bigq;
// bigq = NULL;
prevGetNext = false;
}
int SortedFile::Open(const char *f_path) {
readMode = true;
string metadataFileName(f_path);
metadataFileName += ".data";
FILE *metadata = fopen(metadataFileName.c_str(), "r");
char arr[10];
fscanf(metadata, "%s", arr);
fscanf(metadata, "%s", arr);
runLength = atoi(arr);
sortOrder = new OrderMaker;
ReadOrderMaker(sortOrder, metadata);
file = new File();
file->Open(1, f_path);
prevGetNext = false;
return 1;
}
void SortedFile::initQ() {
if (bigq == NULL) {
input = new Pipe(100);
output = new Pipe(100);
bigq = new BigQ(*input, *output, *sortOrder, runLength);
}
}
void SortedFile::Add(Record *rec) {
// change mode to writing
readMode = false;
initQ();
input->Insert(rec);
prevGetNext = false;
}
int SortedFile::GetNext(Record &fetchme) {
WriteToRead();
prevGetNext = false;
return GetNextRecord(&fetchme);
}
// GetNext returns zero if not found
int SortedFile::GetNext(Record &fetchme, CNF &cnf, Record &literal) {
Schema myschema("catalog", "lineitem");
OrderMaker *newOrderMaker = new OrderMaker();
if (!prevGetNext) {
cnf.CreateQueryOrderMaker(sortOrder, newOrderMaker);
}
ComparisonEngine ceng;
int result = -1;
if (newOrderMaker->numAtts != 0) {
if (!prevGetNext) {
while (true) {
int numRecords = currentPage->numRecs;
Record *recs = new Record[numRecords];
for (int i = 0; i < numRecords; i++) {
currentPage->GetFirst(recs + i);
}
result = BinarySearch(recs, 0, numRecords - 1, &literal, newOrderMaker);
if (result != -1) {
break;
}
if (currPageNum + 1 < file->GetLength() - 1) {
currPageNum++;
file->GetPage(currentPage, currPageNum);
} else {
break;
}
}
if (result == -1) {
return 0;
}
prevGetNext = true;
// get the page where the record has been found
file->GetPage(currentPage, currPageNum);
currentPage->GetRecord(&fetchme, result - 1);
}
while (true) {
// scan the current loaded page
while (currentPage->GetFirst(&fetchme)) {
if (ceng.CompareLit(&fetchme, &literal, newOrderMaker) == 0 && (ceng.Compare(&fetchme, &literal, &cnf) == 1)) {
return 1;
}
}
if (currPageNum + 1 < file->GetLength() - 1) {
currPageNum++;
file->GetPage(currentPage, currPageNum);
} else {
break;
}
}
} else {
while (true) {
while (currentPage->GetFirst(&fetchme)) {
if (ceng.Compare(&fetchme, &literal, &cnf) == 1) {
return 1;
}
}
if (currPageNum + 1 < file->GetLength() - 1) {
currPageNum++;
file->GetPage(currentPage, currPageNum);
} else {
break;
}
}
}
return 0;
}
void SortedFile::WriteToRead() {
Schema mySchema("catalog", "lineitem");
if (!readMode) {
input->ShutDown();
if (file->GetLength() == 0) {
WritePipeToDisk(output);
} else {
output->Remove(tempRec);
file->GetPage(currentPage, 0);
currPageNum = 0;
ComparisonEngine ceng;
Record *fromFile = new Record;
while (GetNextRecord(fromFile) == 1) {
// left is greater than right
if (ceng.Compare(fromFile, tempRec, sortOrder) >= 0)
break;
}
int tempCurrPageNum = currPageNum;
Pipe *tempInput, *tempOutput;
tempInput = new Pipe(100);
tempOutput = new Pipe(100);
BigQ *tempQ = new BigQ(*tempInput, *tempOutput, *sortOrder, 10);
// move to the first record of the
file->GetPage(currentPage, tempCurrPageNum);
currPageNum = tempCurrPageNum;
while (GetNextRecord(fromFile) == 1) {
tempInput->Insert(fromFile);
}
// add the first removed record also into the pipe
tempInput->Insert(tempRec);
while (output->Remove(tempRec)) {
tempInput->Insert(tempRec);
}
tempInput->ShutDown();
// empty the page before adding everything back
// currentPage->EmptyItOut();
delete currentPage;
currentPage = new Page();
currPageNum = tempCurrPageNum;
WritePipeToDiskOverWrite(tempOutput);
tempOutput->ShutDown();
// now switch to read mode / switch out of write mode
delete tempInput;
delete tempOutput;
delete tempQ;
}
readMode = true;
delete input;
delete output;
delete bigq;
bigq = NULL;
tempRec->ClearRecord();
}
}
void SortedFile::MoveFirst() {
WriteToRead();
// WriteCurrentPageToDisk();
file->GetPage(currentPage, 0);
currPageNum = 0;
prevGetNext = false;
}
int SortedFile::Close() {
WriteToRead();
// WriteCurrentPageToDisk();
prevGetNext = false;
fsync(file->myFilDes);
// returns 1 on success
if (!file->Close()) {
return 1;
} else {
return 0;
}
}
void SortedFile::WritePipeToDisk(Pipe *output) {
while (output->Remove(tempRec)) {
if (!currentPage->Append(tempRec)) {
WriteCurrentPageToDisk();
currPageNum++;
// empty the page out
delete currentPage;
currentPage = new (std::nothrow) Page();
// append record to empty page
currentPage->Append(tempRec);
}
}
WriteCurrentPageToDisk();
}
void SortedFile::WritePipeToDiskOverWrite(Pipe *output) {
while (output->Remove(tempRec)) {
if (!currentPage->Append(tempRec)) {
PageToDiskOverWrite();
currPageNum++;
// empty the page out
delete currentPage;
currentPage = new (std::nothrow) Page();
// append record to empty page
currentPage->Append(tempRec);
}
}
PageToDiskOverWrite();
}
// recursive BinarySearch lookup
// int SortedFile::BinarySearch(Record *recs, int start, int end, Record *key, OrderMaker *sortOrder) {
// if (start <= end) {
// int mid = start + (end - start) / 2;
// ComparisonEngine ceng;
// if (ceng.Compare((recs + mid), key, sortOrder) > 0) {
// // if left is greater than right
// // end = mid - 1;
// return BinarySearch(recs, start, mid - 1, key, sortOrder);
// } else if (ceng.Compare((recs + mid), key, sortOrder) < 0) {
// // if left is less than right
// // start = mid + 1;
// return BinarySearch(recs, mid + 1, end, key, sortOrder);
// } else if (start != mid) {
// return BinarySearch(recs, start, mid, key, sortOrder);
// } else {
// return mid;
// }
// }
// return -1;
// }
// Iterative BinarySearch lookup - Faster
int SortedFile::BinarySearch(Record *recs, int start, int end, Record *key, OrderMaker *sortOrder) {
int index = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
ComparisonEngine ceng;
if (ceng.CompareLit((recs + mid), key, sortOrder) == 0) {
index = mid;
end = mid - 1;
} else if (ceng.CompareLit((recs + mid), key, sortOrder) > 0) {
// if left is greater than right
end = mid - 1;
} else if (ceng.CompareLit((recs + mid), key, sortOrder) < 0) {
// if left is less than right
start = mid + 1;
}
}
return index;
}