-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelOp.cc
491 lines (397 loc) · 15 KB
/
RelOp.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "RelOp.h"
#include "RelOpStructs.h"
#include "BigQ.h"
#include <cmath>
void RelationalOp::WaitUntilDone() {
pthread_join(thread, nullptr);
// cerr << operation() << " Completed\n";
}
void RelationalOp::Use_n_Pages(int n) {
runLen = n;
}
void SelectFile::Run(DBFile &inFile, Pipe &outPipe, CNF &selOp, Record &literal) {
auto *data = new RelOpSelectFileData();
data->inFile = &inFile;
data->outPipe = &outPipe;
data->selOp = &selOp;
data->literal = &literal;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void *SelectFile::ThreadMethod(void *d) {
auto *threadData = (RelOpSelectFileData *) d;
Record temp;
if (threadData->selOp) {
while (threadData->inFile->GetNext(temp, *threadData->selOp, *threadData->literal))
threadData->outPipe->Insert(&temp);
} else {
while (threadData->inFile->GetNext(temp)) threadData->outPipe->Insert(&temp);
}
threadData->outPipe->ShutDown();
return nullptr;
}
string SelectFile::operation() {
return "SelecteFile";
}
void SelectPipe::Run(Pipe &inPipe, Pipe &outPipe, CNF &selOp, Record &literal) {
auto *data = new RelOpSelectPipeData();
data->inPipe = &inPipe;
data->outPipe = &outPipe;
data->selOp = &selOp;
data->literal = &literal;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void *SelectPipe::ThreadMethod(void *data) {
auto *threadData = (RelOpSelectPipeData *) data;
ComparisonEngine comp;
Record temp;
while (threadData->inPipe->Remove(&temp)) {
if (comp.Compare(&temp, threadData->literal, threadData->selOp))
threadData->outPipe->Insert(&temp);
}
threadData->outPipe->ShutDown();
return nullptr;
}
string SelectPipe::operation() {
return "SelectPipe";
}
void Project::Run(Pipe &inPipe, Pipe &outPipe, int *keepMe, int numAttsInput, int numAttsOutput) {
auto *data = new RelOpProjectData();
data->inPipe = &inPipe;
data->outPipe = &outPipe;
data->keepMe = keepMe;
data->numAttsInput = numAttsInput;
data->numAttsOutput = numAttsOutput;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void *Project::ThreadMethod(void *data) {
auto *threadData = (RelOpProjectData *) data;
Record temp;
while (threadData->inPipe->Remove(&temp)) {
temp.Project(threadData->keepMe, threadData->numAttsOutput, threadData->numAttsInput);
threadData->outPipe->Insert(&temp);
}
threadData->outPipe->ShutDown();
return nullptr;
}
string Project::operation() {
return "Project";
}
void Sum::Run(Pipe &inPipe, Pipe &outPipe, Function &computeMe, bool distinctFunc) {
auto *data = new RelOpSumData();
data->inPipe = &inPipe;
data->outPipe = &outPipe;
data->computeMe = &computeMe;
data->distinctFunc = distinctFunc;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void DistinctSum(RelOpSumData *data, Schema *outSchema) {
DuplicateRemoval duplicateRemoval;
Pipe *duplicateRemovalInPipe = new Pipe(100);
Pipe *duplicateRemovalOutPipe = new Pipe(100);
duplicateRemoval.Run(*duplicateRemovalInPipe, *duplicateRemovalOutPipe, *outSchema);
Record temp;
while (data->inPipe->Remove(&temp)) {
int tempIntResult = 0;
double tempDoubleResult = 0.0;
data->computeMe->Apply(temp, tempIntResult, tempDoubleResult);
std::string output = std::to_string(tempIntResult + tempDoubleResult) + "|";
temp.ComposeRecord(outSchema, const_cast<char *>(output.c_str()));
duplicateRemovalInPipe->Insert(&temp);
}
duplicateRemovalInPipe->ShutDown();
double result = 0.0;
while (duplicateRemovalOutPipe->Remove(&temp)) result += std::stod(temp.GetAtt(0, Double));
std::string output = std::to_string(result) + "|";
temp.ComposeRecord(outSchema, const_cast<char *>(output.c_str()));
data->outPipe->Insert(&temp);
data->outPipe->ShutDown();
}
void SimpleSum(RelOpSumData *data, Schema *outSchema) {
int intResult = 0;
double doubleResult = 0.0;
Record temp;
while (data->inPipe->Remove(&temp)) {
int tempIntResult = 0;
double tempDoubleResult = 0.0;
data->computeMe->Apply(temp, tempIntResult, tempDoubleResult);
intResult += tempIntResult;
doubleResult += tempDoubleResult;
}
std::string output = std::to_string(intResult + doubleResult) + "|";
temp.ComposeRecord(outSchema, const_cast<char *>(output.c_str()));
data->outPipe->Insert(&temp);
data->outPipe->ShutDown();
}
void *Sum::ThreadMethod(void *d) {
auto *data = (RelOpSumData *) d;
Attribute att = {"att1", Double};
Schema outSchema("out_schema", 1, &att);
if (data->distinctFunc) DistinctSum(data, &outSchema);
else SimpleSum(data, &outSchema);
return nullptr;
}
string Sum::operation() {
return "Sum";
}
void DuplicateRemoval::Run(Pipe &inPipe, Pipe &outPipe, Schema &mySchema) {
auto *data = new RelOpDuplicateRemovalData();
data->inPipe = &inPipe;
data->outPipe = &outPipe;
data->mySchema = &mySchema;
data->runLen = runLen;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void *DuplicateRemoval::ThreadMethod(void *d) {
ComparisonEngine comp;
auto *data = (RelOpDuplicateRemovalData *) d;
OrderMaker sortOrder(data->mySchema);
Pipe tempPipe(100);
BigQ bigQ(*data->inPipe, tempPipe, sortOrder, data->runLen);
Record temp1, temp2;
if (tempPipe.Remove(&temp1)) {
while (tempPipe.Remove(&temp2)) {
if (comp.Compare(&temp1, &temp2, &sortOrder) != 0) data->outPipe->Insert(&temp1);
temp1.Consume(&temp2);
}
data->outPipe->Insert(&temp1);
}
data->outPipe->ShutDown();
return nullptr;
}
string DuplicateRemoval::operation() {
return "DuplicateRemoval";
}
void WriteOut::Run(Pipe &inPipe, FILE *outFile, Schema &mySchema) {
auto *data = new RelOpWriteOutData();
data->inPipe = &inPipe;
data->outFile = outFile;
data->mySchema = &mySchema;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void *WriteOut::ThreadMethod(void *d) {
auto *data = (RelOpWriteOutData *) d;
Record temp;
while (data->inPipe->Remove(&temp)) temp.WriteToFile(data->mySchema, data->outFile);
fclose(data->outFile);
return nullptr;
}
string WriteOut::operation() {
return "WriteOut";
}
void Join::Run(Pipe &inPipeL, Pipe &inPipeR, Pipe &outPipe, CNF &selOp, Record &literal) {
auto *data = new RelOpJoinData();
data->inPipeL = &inPipeL;
data->inPipeR = &inPipeR;
data->outPipe = &outPipe;
data->selOp = &selOp;
data->literal = &literal;
data->runLen = runLen;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
void Join::writeToFile(std::string fileName, Pipe *pipe) {
File file;
file.Open(0, const_cast<char *>(fileName.c_str()));
int whichPage = 0;
Page page;
Record temp;
int count = 0;
while (pipe->Remove(&temp)) {
count++;
if (!page.Append(&temp)) {
file.AddPage(&page, whichPage++);
page.EmptyItOut();
page.Append(&temp);
}
}
file.AddPage(&page, whichPage++);
file.Close();
}
void Join::NestedBlockJoin(Pipe *pipeL, Pipe *pipeR, int runlen, Pipe *out) {
Record *outerRecord = new Record(), *innerRecord = new Record(), mergeRecord;
std::string fileNameL = "tmp_file_l_" + randomFileName(), fileNameR = "tmp_file_r_" + randomFileName();
writeToFile(fileNameL, pipeL);
writeToFile(fileNameR, pipeR);
File outer, inner;
outer.Open(1, const_cast<char *>(fileNameL.c_str()));
inner.Open(1, const_cast<char *> (fileNameR.c_str()));
int leftCount = -1, rightCount = -1;
int *attsToKeep = nullptr;
for (int outerBlockNumber = 0;
outerBlockNumber < std::ceil((outer.GetLength() - 1) / (double) runlen); outerBlockNumber++) {
SingleRun outerBlock(&outer, runlen, outerBlockNumber);
vector<Record *> outerRecords;
while (outerBlock.GetFirst(innerRecord)) {
outerRecords.push_back(innerRecord);
outerRecord = new Record();
}
for (int innerBlockNumber = 0;
innerBlockNumber < std::ceil((inner.GetLength() - 1) / (double) runlen); innerBlockNumber++) {
SingleRun innerBlock(&inner, runlen, innerBlockNumber);
vector<Record *> innerRecords;
while (innerBlock.GetFirst(innerRecord)) {
innerRecords.push_back(innerRecord);
innerRecord = new Record();
}
for (Record *o : outerRecords) {
for (Record *i : innerRecords) {
if (attsToKeep == nullptr) {
leftCount = o->NumberOfAtts();
rightCount = i->NumberOfAtts();
attsToKeep = new int[leftCount + rightCount];
int indexInArray = 0;
for (int index = 0; index < leftCount; index++) attsToKeep[indexInArray++] = index;
for (int index = 0; index < rightCount; index++) attsToKeep[indexInArray++] = index;
}
mergeRecord.MergeRecords(o, i, leftCount, rightCount, attsToKeep, leftCount + rightCount,
leftCount);
out->Insert(&mergeRecord);
}
}
}
}
outer.Close();
inner.Close();
remove(fileNameL.c_str());
remove(fileNameR.c_str());
out->ShutDown();
}
void Join::ComparisonBasedJoin(Pipe *pipeL, Pipe *pipeR, OrderMaker *orderL, OrderMaker *orderR, Pipe *out) {
ComparisonEngine comp;
Record *tempLeft = new Record(), *tempRight = new Record(), *firstLeft = nullptr;
int *attsToKeep = nullptr;
int leftIsPresent = 0, rightIsPresent = 0;
int leftCount = -1, rightCount = -1;
vector<Record *> leftRecords, rightRecords;
while (leftIsPresent || pipeL->Remove(tempLeft)) {
leftRecords.push_back(tempLeft);
leftIsPresent = 0;
firstLeft = new Record();
firstLeft->Copy(tempLeft);
tempLeft = new Record();
while (pipeL->Remove(tempLeft)) {
if (comp.Compare(firstLeft, tempLeft, orderL) == 0) {
leftRecords.push_back(tempLeft);
tempLeft = new Record();
} else {
leftIsPresent = 1;
break;
}
}
while (rightIsPresent || pipeR->Remove(tempRight)) {
rightIsPresent = 0;
int comparisonResult = comp.Compare(tempRight, orderR, firstLeft, orderL);
if (comparisonResult == 0) {
rightRecords.push_back(tempRight);
tempRight = new Record();
} else if (comparisonResult < 0) {
tempRight = new Record();
} else {
rightIsPresent = 1;
break;
}
}
for (Record *l : leftRecords) {
for (Record *r : rightRecords) {
if (attsToKeep == nullptr) {
leftCount = firstLeft->NumberOfAtts();
rightCount = tempRight->NumberOfAtts();
attsToKeep = new int[leftCount + rightCount];
int indexInArray = 0;
for (int i = 0; i < leftCount; i++) attsToKeep[indexInArray++] = i;
for (int i = 0; i < rightCount; i++) attsToKeep[indexInArray++] = i;
}
Record tempMerge;
tempMerge.MergeRecords(l, r, leftCount, rightCount, attsToKeep, leftCount + rightCount, leftCount);
out->Insert(&tempMerge);
}
}
leftRecords.clear();
rightRecords.clear();
}
out->ShutDown();
}
void *Join::ThreadMethod(void *d) {
auto *data = (RelOpJoinData *) d;
OrderMaker left, right;
data->selOp->GetSortOrders(left, right);
Pipe leftData(100), rightData(100);
BigQ bigQL(*data->inPipeL, leftData, left, data->runLen), bigQR(*data->inPipeR, rightData, right, data->runLen);
if (left.numAtts == 0) NestedBlockJoin(&leftData, &rightData, data->runLen, data->outPipe);
else ComparisonBasedJoin(&leftData, &rightData, &left, &right, data->outPipe);
return nullptr;
}
string Join::operation() {
return "Join";
}
void GroupBy::Run(Pipe &inPipe, Pipe &outPipe, OrderMaker &groupAtts, Function &computeMe, bool distinctFunc) {
auto *data = new RelOpGroupByData();
data->inPipe = &inPipe;
data->outPipe = &outPipe;
data->groupAtts = &groupAtts;
data->computeMe = &computeMe;
data->runLen = runLen;
data->distinctFunc = distinctFunc;
pthread_create(&thread, nullptr, ThreadMethod, (void *) data);
}
std::string AppendOrderAtts(std::string output, OrderMaker &o, Record &r) {
for (int index = 0; index < o.numAtts; index++) output += r.GetAtt(o.whichAtts[index], o.whichTypes[index]) + "|";
return output;
}
void *GroupBy::ThreadMethod(void *d) {
ComparisonEngine comp;
auto *data = (RelOpGroupByData *) d;
Attribute atts[1 + data->groupAtts->numAtts];
Attribute x = {"attNum", Double};
atts[0] = x;
for (int i = 0; i < data->groupAtts->numAtts; i++) {
Attribute y = {"att", data->groupAtts->whichTypes[i]};
atts[1 + i] = y;
}
Schema outSchema("out_schema", 1 + data->groupAtts->numAtts, atts);
Record outPipeTemp;
Pipe tempPipe(100);
BigQ bigQ(*data->inPipe, tempPipe, *data->groupAtts, data->runLen);
Record temp1, temp2;
if (!tempPipe.Remove(&temp1)) {
data->outPipe->ShutDown();
return nullptr;
}
Sum *sum = new Sum();
Pipe *sumInPipe = new Pipe(100), *sumOutPipe = new Pipe(100);
sum->Run(*sumInPipe, *sumOutPipe, *data->computeMe, data->distinctFunc);
while (tempPipe.Remove(&temp2)) {
int comparision = comp.Compare(&temp1, &temp2, data->groupAtts);
Record temp1Copy;
temp1Copy.Copy(&temp1);
sumInPipe->Insert(&temp1);
if (comparision == 0) {
temp1.Consume(&temp2);
continue;
}
sumInPipe->ShutDown();
Record outTemp;
sumOutPipe->Remove(&outTemp);
std::string output = outTemp.GetAtt(0, Double) + "|";
outPipeTemp.ComposeRecord(&outSchema, AppendOrderAtts(output, *data->groupAtts, temp1Copy).c_str());
data->outPipe->Insert(&outPipeTemp);
temp1.Consume(&temp2);
sum = new Sum();
sumInPipe = new Pipe(100);
sumOutPipe = new Pipe(100);
sum->Run(*sumInPipe, *sumOutPipe, *data->computeMe, data->distinctFunc);
}
Record temp1Copy;
temp1Copy.Copy(&temp1);
sumInPipe->Insert(&temp1);
sumInPipe->ShutDown();
Record outTemp;
sumOutPipe->Remove(&outTemp);
std::string output = outTemp.GetAtt(0, Double) + "|";
outPipeTemp.ComposeRecord(&outSchema, AppendOrderAtts(output, *data->groupAtts, temp1Copy).c_str());
data->outPipe->Insert(&outPipeTemp);
data->outPipe->ShutDown();
return nullptr;
}
string GroupBy::operation() {
return "GroupBy";
}