-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortedDBFile.cc
More file actions
174 lines (139 loc) · 5.49 KB
/
SortedDBFile.cc
File metadata and controls
174 lines (139 loc) · 5.49 KB
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
#include "SortedDBFile.h"
#include "GenericDBFile.h"
SortedDBFile::SortedDBFile(SortInfo *sortInfoParameter) {
sortInfo = sortInfoParameter;
inputPipe = new Pipe(PIPE_BUFFER_SIZE);
outputPipe = new Pipe(PIPE_BUFFER_SIZE);
queryOrderMaker = new OrderMaker();
useSameQueryOrderMaker = false;
}
SortedDBFile::~SortedDBFile() {
delete inputPipe;
delete outputPipe;
delete queryOrderMaker;
}
/* ****************************************** ALL OVERRIDDEN METHODS *********************************************** */
void SortedDBFile::SwitchToWriteMode() {
if (isInWriteMode) return;
BigQ(*inputPipe, *outputPipe, *sortInfo->myOrder, sortInfo->runLength);
useSameQueryOrderMaker = false;
isInWriteMode = true;
}
void SortedDBFile::SwitchToReadMode() {
if (!isInWriteMode) return;
isInWriteMode = false;
MergeCurrentFileAndBigQOutput();
}
void SortedDBFile::AddToDBFile(Record &addme) {
inputPipe->Insert(&addme);
}
int SortedDBFile::GetNextFromDBFile(Record &fetchme) {
return GetRecordFromReadBufferPage(fetchme);
}
int SortedDBFile::GetNextFromDBFile(Record &fetchme, CNF &cnf, Record &literal) {
if (!useSameQueryOrderMaker) {
cnf.GetCommonSortOrder(*sortInfo->myOrder, *queryOrderMaker);
}
return GetNextForSortedFile(fetchme, cnf, literal);
}
/* ****************************************** ALL PRIVATE METHODS *********************************************** */
void SortedDBFile::MergeCurrentFileAndBigQOutput() {
// Shut down input pipe of BigQ so that BigQ gives sorted records in output pipe.
inputPipe->ShutDown();
// Create Tempt file as Heap and keep on adding records in sorted order.
char tempFileName[100];
strcpy(tempFileName, dbFileName);
strcat(tempFileName, ".temp");
HeapDBFile tempFile;
tempFile.Create(tempFileName);
// Move first current file for reading.
MoveFirst();
Record currentFileRecord;
bool currentFileNotFullyConsumed = GetRecordFromReadBufferPage(currentFileRecord);
// Output pipe variables.
Record outputPipeRecord;
bool outputPipeNotFullyConsumed = outputPipe->Remove(&outputPipeRecord);
// If either of the stream is fully consumed - Break;
while (outputPipeNotFullyConsumed && currentFileNotFullyConsumed) {
// If currentFileRecord is less
if (comparisonEngine.Compare(¤tFileRecord, &outputPipeRecord, sortInfo->myOrder) < 1) {
tempFile.Add(currentFileRecord);
currentFileNotFullyConsumed = GetRecordFromReadBufferPage(currentFileRecord);
}
// If BigQ output pipe's record is less.
else {
tempFile.Add(outputPipeRecord);
outputPipeNotFullyConsumed = outputPipe->Remove(&outputPipeRecord);
}
}
// If output pipe is not fully consumed.
while (outputPipeNotFullyConsumed) {
tempFile.Add(outputPipeRecord);
outputPipeNotFullyConsumed = outputPipe->Remove(&outputPipeRecord);
}
// If current file records are not fully consumed.
while (currentFileNotFullyConsumed) {
tempFile.Add(currentFileRecord);
currentFileNotFullyConsumed = GetRecordFromReadBufferPage(currentFileRecord);
}
// Close both files
tempFile.Close();
dbFile.Close();
// remove the old file.
remove(dbFileName);
// rename the file name.
rename(tempFileName, dbFileName);
dbFile.Open(1, dbFileName);
}
int SortedDBFile::GetNextForSortedFile(Record &fetchme, CNF &cnf, Record &literal) {
if (queryOrderMaker->isEmpty()) {
while (GetRecordFromReadBufferPage(fetchme)) {
if (CheckForCNF(fetchme, cnf, literal)) return 1;
}
return 0;
}
if (!useSameQueryOrderMaker) {
useSameQueryOrderMaker = true;
bool doBinarySearch = true;
while (readBufferPage.GetFirst(&fetchme)) {
if (CheckForCNF(fetchme, cnf, literal)) return 1;
int comparisonValue = CheckForQuery(fetchme, literal);
if (comparisonValue == 1) return 0;
if (comparisonValue == 0) {
if (CheckForCNF(fetchme, cnf, literal)) return 1;
doBinarySearch = false;
break;
}
}
if (doBinarySearch) {
if (GetLengthInPages() - 1 <= currentlyBeingReadPageNumber + 1) return 0;
currentlyBeingReadPageNumber = BinarySearch(++currentlyBeingReadPageNumber, GetLengthInPages() - 1,
literal);
}
}
while (GetRecordFromReadBufferPage(fetchme)) {
if (CheckForCNF(fetchme, cnf, literal)) return 1;
if (CheckForQuery(fetchme, literal)) return 0;
}
return 0;
}
int SortedDBFile::CheckForQuery(Record &fetchme, Record &literal) {
return comparisonEngine.Compare(&literal, queryOrderMaker, &fetchme, sortInfo->myOrder);
}
bool SortedDBFile::CheckForCNF(Record &fetchme, CNF &cnf, Record &literal) {
return comparisonEngine.Compare(&fetchme, &literal, &cnf);
}
off_t SortedDBFile::BinarySearch(off_t low, off_t high, Record &literal) {
Page bufferPage;
Record temp;
// Base condition or Break condition
if (low == high) return low;
// Get first record of the middle page.
off_t mid = (low + high) / 2;
dbFile.GetPage(&bufferPage, mid);
bufferPage.GetFirst(&temp);
if (comparisonEngine.Compare(&literal, queryOrderMaker, &temp, sortInfo->myOrder) > 0) {
return BinarySearch(mid + 1, high, literal);
}
return BinarySearch(low, mid - 1, literal);
}