-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBigQ.cc
More file actions
203 lines (173 loc) · 7.52 KB
/
BigQ.cc
File metadata and controls
203 lines (173 loc) · 7.52 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
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
#include "BigQ.h"
BigQ::BigQ(Pipe &in, Pipe &out, OrderMaker &sortOrder, int runlen) {
// Initialize worker thread data.
WorkerThreadData *my_data = new WorkerThreadData();
my_data->inputPipe = ∈
my_data->outputPipe = &out;
my_data->sortOrder = &sortOrder;
my_data->runLength = runlen;
// Create the worker thread.
pthread_create(&workerThread, NULL, TPMMS, (void *) my_data);
}
BigQ::~BigQ() {
}
void *TPMMS(void *threadData) {
WorkerThreadData *workerThreadData = (WorkerThreadData *) threadData;
InitializeWorkerThreadData(workerThreadData);
// Phase-1 (Generate sorted runs of runLength pages)
RunGeneration(workerThreadData);
// Phase-2 (Merge sorted runs)
MergeRuns(workerThreadData);
CleanUp(workerThreadData);
}
void InitializeWorkerThreadData(WorkerThreadData *threadData) {
// Create buffer page array to store current runs' records
Page *currentRunPages = new(std::nothrow) Page[threadData->runLength];
if (currentRunPages == NULL) {
cerr << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
threadData->currentRunPages = currentRunPages;
threadData->currentRunPageNumber = 0;
threadData->numberOfRuns = 0;
threadData->overflowIsThere = false;
// Create temporary file for storing runs.
sprintf(threadData->bigQFileName, "%d.bin", pthread_self());
threadData->bigQFile.Open(0, threadData->bigQFileName);
}
void RunGeneration(WorkerThreadData *threadData) {
Record *nextRecord = new Record();
while (threadData->inputPipe->Remove(nextRecord)) {
if (!AddRecordToCurrentRun(threadData, nextRecord)) {
CreateRun(threadData);
AddRecordToCurrentRun(threadData, nextRecord);
}
}
do {
CreateRun(threadData);
} while (threadData->overflowIsThere);
}
int AddRecordToCurrentRun(WorkerThreadData *threadData, Record *nextRecord) {
Page *currentRunPage = &threadData->currentRunPages[threadData->currentRunPageNumber];
if (!currentRunPage->Append(nextRecord)) {
if (threadData->currentRunPageNumber + 1 == threadData->runLength) {
return 0;
} else {
currentRunPage = &threadData->currentRunPages[++threadData->currentRunPageNumber];
}
currentRunPage->Append(nextRecord);
}
return 1;
}
void CreateRun(WorkerThreadData *workerThreadData) {
SortAndStoreCurrentRun(workerThreadData);
ClearCurrentRun(workerThreadData);
}
void SortAndStoreCurrentRun(WorkerThreadData *workerThreadData) {
// Using priority queue for in memory sort.
priority_queue<Record *, vector<Record *>, RecordComparator> pq(workerThreadData->sortOrder);
LoadCurrentRunPriorityQueue(workerThreadData, pq);
// Get the records from the priority queue and add sorted pages of records in the BigQFile.
WritePriorityQueueContentToBigQFile(workerThreadData, pq);
}
// Put all the current run's record to the priority queue.
void LoadCurrentRunPriorityQueue(WorkerThreadData *workerThreadData,
priority_queue<Record *, vector<Record *>, RecordComparator> &pq) {
for (int i = 0; i <= workerThreadData->currentRunPageNumber; i++) {
Page *currentRunPage = &workerThreadData->currentRunPages[i];
Record *temp = new Record();
while (currentRunPage->GetFirst(temp)) {
pq.push(temp);
temp = new Record();
}
currentRunPage->EmptyItOut();
}
}
void WritePriorityQueueContentToBigQFile(WorkerThreadData *workerThreadData,
priority_queue<Record *, vector<Record *>, RecordComparator> &pq) {
workerThreadData->overflowIsThere = false;
Page *bufferPage = new Page();
// Get next empty page offset of BigQFile.
off_t currentPageIndexOfBigQFile =
workerThreadData->bigQFile.GetLength() - 2 < 0 ? 0 : workerThreadData->bigQFile.GetLength() - 1;
off_t maxRunPageNumber = currentPageIndexOfBigQFile + workerThreadData->runLength - 1;
int currentRunPageNumberOfOverflowRecords = 0;
while (!pq.empty()) {
// Overflow condition
if (currentPageIndexOfBigQFile > maxRunPageNumber) {
if (!(&workerThreadData->currentRunPages[currentRunPageNumberOfOverflowRecords])->Append(pq.top())) {
currentRunPageNumberOfOverflowRecords++;
} else {
pq.pop();
}
}
// If the current buffer is full.
else if (!bufferPage->Append(pq.top())) {
workerThreadData->bigQFile.AddPage(bufferPage, currentPageIndexOfBigQFile++);
bufferPage->EmptyItOut();
} else {
pq.pop();
}
}
// If data is not over flow, store the last page in the BigQFile.
if (currentPageIndexOfBigQFile <= maxRunPageNumber) {
workerThreadData->bigQFile.AddPage(bufferPage, currentPageIndexOfBigQFile);
} else {
workerThreadData->overflowIsThere = true;
}
}
void ClearCurrentRun(WorkerThreadData *workerThreadData) {
workerThreadData->numberOfRuns++;
workerThreadData->currentRunPageNumber = 0;
}
void MergeRuns(struct WorkerThreadData *workerThreadData) {
priority_queue<PriorityQueueItem, vector<PriorityQueueItem>, RecordComparator> pq(workerThreadData->sortOrder);
LoadMergeRunPriorityQueue(workerThreadData, pq);
LoadOutputPipeWithPriorityQueueData(workerThreadData, pq);
}
void LoadMergeRunPriorityQueue(WorkerThreadData *workerThreadData,
priority_queue<PriorityQueueItem,
vector<PriorityQueueItem>,
RecordComparator> &pq) {
PriorityQueueItem *priorityQueueItem = new PriorityQueueItem[workerThreadData->numberOfRuns];
for (int i = 0; i < workerThreadData->numberOfRuns; i++) {
priorityQueueItem[i].currentPageNumber = i * workerThreadData->runLength;
priorityQueueItem[i].maxPageNumberOfCurrentRun =
std::min((off_t) priorityQueueItem[i].currentPageNumber + workerThreadData->runLength - 1,
workerThreadData->bigQFile.GetLength() - 2);
priorityQueueItem[i].page = new Page();
workerThreadData->bigQFile.GetPage(priorityQueueItem[i].page, priorityQueueItem[i].currentPageNumber);
priorityQueueItem[i].head = new Record();
priorityQueueItem[i].page->GetFirst(priorityQueueItem[i].head);
pq.push(priorityQueueItem[i]);
}
}
void LoadOutputPipeWithPriorityQueueData(WorkerThreadData *workerThreadData,
priority_queue<PriorityQueueItem,
vector<PriorityQueueItem>, RecordComparator> &pq) {
while (!pq.empty()) {
PriorityQueueItem item = pq.top();
pq.pop();
workerThreadData->outputPipe->Insert(item.head);
if (!item.page->GetFirst(item.head)) {
if (item.currentPageNumber + 1 <= item.maxPageNumberOfCurrentRun) {
item.currentPageNumber++;
workerThreadData->bigQFile.GetPage(item.page, item.currentPageNumber);
item.page->GetFirst(item.head);
pq.push(item);
}
} else {
pq.push(item);
}
}
}
void CleanUp(WorkerThreadData *workerThreadData) {
// Close and delete the file.
workerThreadData->bigQFile.Close();
remove(workerThreadData->bigQFileName);
delete[] workerThreadData->currentRunPages;
// Shut down output pipe.
if (workerThreadData->outputPipe) {
workerThreadData->outputPipe->ShutDown();
}
}