-
Notifications
You must be signed in to change notification settings - Fork 5
/
SortedDBFile.cc
390 lines (366 loc) · 11.1 KB
/
SortedDBFile.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
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
#include "SortedDBFile.h"
#include "TwoWayList.h"
#include "Record.h"
#include "Schema.h"
#include "File.h"
#include "Comparison.h"
#include "ComparisonEngine.h"
#include "DBFile.h"
#include "Defs.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdlib>
/* Morgan Bauer */
SortedDBFile::SortedDBFile () : currentRWMode(reading), filepath(), f(), curPage(), curPageIndex(0), runLength(100), so(), toBigQ(NULL), fromBigQ(NULL), bq(NULL)
{}
int SortedDBFile::Open (char *f_path)
{
int t;
{ // whole purpose is to set up t, really.
string metafileName;
metafileName.append(f_path);
metafileName.append(".meta");
ifstream metafile;
metafile.open(metafileName.c_str());
if(!metafile) return 1;
metafile >> t;
if(!metafile) return 1;
metafile >> runLength;
metafile >> so; // read in ordermaker
// so.Print();
fType dbfileType = (fType) t;
metafile.close();
// cout << "file type is " << dbfileType << endl;
}
filepath = f_path;
f.Open(1, f_path);
currentRWMode = reading;
MoveFirst();
cout << "file opened with " << f.GetLength() << " pages" << endl;
return 0;
}
int SortedDBFile::Create (char *f_path, fType f_type, void *startup)
{
assert(sorted == f_type);
// SortInfo si = *((SortInfo *)startup);
filepath = f_path;
f.Open(0,filepath.c_str());
return 1;
}
void SortedDBFile::Load (Schema &f_schema, char *loadpath)
{
currentRWMode = writing;
if (NULL == bq) // initialize pipes, and BigQ
{
toBigQ = new Pipe(pipeBufferSize);
fromBigQ = new Pipe(pipeBufferSize);
bq = new BigQ(*toBigQ,*fromBigQ,so, runLength );
}
FILE *tableFile = fopen (loadpath, "r");
if (0 == tableFile)
exit(-1);
Record tempRecord;
int recordCounter = 0; // counter for debug
while (1 == tempRecord.SuckNextRecord (&f_schema, tableFile))
{ // there is another record available
assert(recordCounter >= 0);
recordCounter++;
if (recordCounter % 10000 == 0) {
cerr << recordCounter << "\n";
}
// use tempRecord, and put into tempPage. Later if page is full, write to file,
toBigQ->Insert(&tempRecord);
}
}
void SortedDBFile::MoveFirst ()
{
if (writing == currentRWMode)
{
MergeDifferential();
}
// consider keeping an index value, rather than holding the page itself.
curPageIndex = (off_t) 0;
if (0 != f.GetLength())
{
f.GetPage(&curPage, curPageIndex);
}
else
{
curPage.EmptyItOut();
}
}
int SortedDBFile::Close ()
{
cout << "closing sorted DBFile" << endl
<< "f len is " << f.GetLength() << endl;
if (0 == f.GetLength()) // file was new, so don't bother merging, just write from pipe to file.
{ // special case, we have a fresh file.
if (writing == currentRWMode)
{
cout << "writing to empty file, stream outpipe to file directly" << endl;
if (NULL != bq)
{
toBigQ->ShutDown();
{// read from outPipe and write to file.
Record tempRecord;
Page tempPage;
while (SUCCESS == fromBigQ->Remove(&tempRecord)) // while we can take records out of the pipe, do so.
{
if (FAILURE == tempPage.Append(&tempRecord)) // no more space in page, write to disk
{ //
if (0 < f.GetLength())
{
f.AddPage(&tempPage,f.GetLength()-1); // new final page
tempPage.EmptyItOut();
tempPage.Append(&tempRecord);
}
else
{
f.AddPage(&tempPage,0);
tempPage.EmptyItOut();
tempPage.Append(&tempRecord);
}
}
}
// finally write last thing to file
if (0 < f.GetLength())
{
f.AddPage(&tempPage,f.GetLength()-1); // new final page
tempPage.EmptyItOut();
}
else // we might have never written a page, so this might be the first still.
{
f.AddPage(&tempPage,0);
tempPage.EmptyItOut();
}
cout << "flen = " << f.GetLength() << endl;
}
// close all things
delete toBigQ;
delete fromBigQ;
delete bq;
// null pointers
toBigQ = NULL;
fromBigQ = NULL;
bq = NULL;
}
}
int fsize = f.Close();
cout << "at close we have " << fsize << " pages" << endl;
if (fsize >= 0) // check that file size is positive. This is the only rational test I can come up with at this time.
{
return 1;
}
else
{
cout << "fsize was: " << fsize << endl;
return 0; //failure, negative file size, or some other error.
}
}
else // file needs to be merged
{
cout << "file is not 0 length" << endl;
if (writing == currentRWMode)
{
cout << "mode is writing" << endl;
MergeDifferential();
}
else if (reading == currentRWMode)
{
cout << "mode is reading" << endl;
}
else
{
cout << "unknown mode, crash" << endl;
exit(-1);
}
int fsize = f.Close();
cout << "at close we have " << fsize << " pages" << endl;
if (fsize >= 0) // check that file size is positive. This is the only rational test I can come up with at this time.
{
return 1;
}
else
{
cout << "fsize was: " << fsize << endl;
return 0; //failure, negative file size, or some other error.
}
}
}
void SortedDBFile::Add (Record &rec)
{
// cout << "add";
if (writing == currentRWMode) // we are in the correct RW mode.
{
toBigQ->Insert(&rec);
}
else if (reading == currentRWMode) // wrong RW mode, need to switch, and startup Q stuff.
{
currentRWMode = writing;
if (NULL == bq) // initialize pipes, and BigQ
{
toBigQ = new Pipe(pipeBufferSize);
fromBigQ = new Pipe(pipeBufferSize);
bq = new BigQ(*toBigQ,*fromBigQ,so, runLength );
}
// insert first record
toBigQ->Insert(&rec);
}
else
{
cout << "no known mode, inconsistent state, exiting" << endl;
exit(-1);
}
}
int SortedDBFile::GetNext (Record &fetchme)
{
if (writing == currentRWMode)
{
MergeDifferential();
}
if(SUCCESS == curPage.GetFirst(&fetchme))
{ // page is not empty, return the next record.
return 1;
}
else
{ // page is empty, get next page, if available, and return a record from it.
++curPageIndex;
if(curPageIndex + 1 <= f.GetLength() - 1) // if there are still more pages to read.
{
f.GetPage(&curPage, curPageIndex);
int ret = curPage.GetFirst(&fetchme);
if (1 != ret)
{
cout << "gn flen = " << f.GetLength() << endl;
}
assert(1 == ret); // we can't now have fewer pages than we did four lines ago.
return 1;
}
else // there are no more pages to read.
{
return 0;
}
}
return 0;
}
int SortedDBFile::GetNext (Record &fetchme, CNF &cnf, Record &literal)
{
if (writing == currentRWMode)
{
MergeDifferential ();
}
// Compare sortorder ordermaker attributes, to cnf attributes
// create new ordermaker based on this
//
// OrderMakers have this
// int numAtts;
// int whichAtts[MAX_ANDS];
// Type whichTypes[MAX_ANDS];
//
// CNFs have this
// Comparison orList[MAX_ANDS][MAX_ORS];
// int orLens[MAX_ANDS];
// int numAnds;
// check if cached order maker is usable
// create new ordermaker if not
OrderMaker query;
// three conditions check (or maybe up above).
ComparisonEngine comp;
while(1 == GetNext(fetchme)) // there are more records
{
if (comp.Compare(&fetchme,&literal,&cnf)) // check the record
{
return 1;
}
}
return 0;
}
void SortedDBFile :: MergeDifferential (void)
{
currentRWMode = reading;
// toBigQ->ShutDown(); // uncomment later
StupidMergeDifferential();
// merge differential file
// two things to read from. bigq bq and already sorted file f.
// write into new temp file, merging.
// close all things
delete toBigQ;
delete fromBigQ;
delete bq;
// null pointers
toBigQ = NULL;
fromBigQ = NULL;
bq = NULL;
// move to beginning
MoveFirst();
cout << "need to write merge diff function, crashing" << endl;
// exit(-1);
}
void SortedDBFile :: StupidMergeDifferential (void)
{
cout << endl << "Doing stupidmerge" << endl<< endl;
// two things to read from. bigq bq and already sorted file f.
// Pipe in(100);
// Pipe out(100);
// BigQ stupid(in, out, so, runLength);
// Pipe& diffPipe = *fromBigQ;
// put the differential file into the pipe
Record tempRecord;
// while (SUCCESS == diffPipe.Remove(&tempRecord))
// {
// in.Insert(&tempRecord);
// }
int origFileRecs = 0;
// put the original file into the pipe.
MoveFirst();
cout << "original file had " << f.GetLength() << " pages" << endl;
if (0 != f.GetLength()) // file was new, so don't bother merging, just write from pipe to file.
{
cout << "merge calling getnext" << endl;
while (SUCCESS == GetNext(tempRecord))
{
origFileRecs++;
toBigQ->Insert(&tempRecord);
}
}
cout << "original file had " << origFileRecs << " records"<< endl;
toBigQ->ShutDown();
// everything should be in the pipe, close it now
// in.ShutDown();
// close the old file
int fsize = f.Close();
cout << "original file had " << fsize << " pages" << endl;
// reopen file in create mode, starting from 0.
cout << "REOPEN FILE" << endl;
cout << "path is " << filepath << endl;
f.Open(0,filepath.c_str());
curPageIndex = 0;
cout << "curPageIndex is" << curPageIndex << endl;
cout << "new sorted merged file has " << f.GetLength() << " pages" << endl;
// read everything from pipe and write to file.
{
int fromBigQrecs = 0;
int fromBigQpgs = 0;
curPage.EmptyItOut();
while (SUCCESS == fromBigQ->Remove(&tempRecord))
{
fromBigQrecs++;
if (FAILURE == curPage.Append(&tempRecord))
{ // page was full
fromBigQpgs++;
f.AddPage(&curPage,curPageIndex++); // add page to file
curPage.EmptyItOut();
curPage.Append(&tempRecord);
}
}
cout << "BigQ had " << fromBigQrecs << " records"<< endl;
f.AddPage(&curPage,curPageIndex++);
fromBigQpgs++;
curPage.EmptyItOut();
cout << "took " << fromBigQpgs << " pages from bigq and put into file" << endl;
}
cout << "new sorted merged file has " << f.GetLength() << " pages" << endl;
// everything should be done.
}