-
Notifications
You must be signed in to change notification settings - Fork 0
/
qe.cc
347 lines (314 loc) · 10.9 KB
/
qe.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
#include "qe.h"
Filter::Filter(Iterator* input, const Condition &condition) {
}
// ... the rest of your implementations go here
GHJoin::GHJoin(Iterator *leftIn, // Iterator of input R
Iterator *rightIn, // Iterator of input S
const Condition &condition, // Join condition (CompOp is always EQ)
const unsigned numPartitions // # of partitions for each relation (decided by the optimizer)
)
{
//initial class variable
//partition R and S
rbfm = RecordBasedFileManager::instance();
this->leftIn=leftIn;
this->rightIn=rightIn;
this->condition=condition;
this->numPartitions=numPartitions;
string left="left";
string right="right";
//partition left & right
Partitons(this->numPartitions,this->condition);
}
RC GHJoin::getNextTuple(void *data)
{
//build hash table for smaller a relation
//match tuples
//join attributes left+right **different type of attributes will have different implement
//count partition, page number, slot number
FileHandle leftFileHandle;
FileHandle rightFileHandle;
void*dataPage=malloc(PAGE_SIZE);
string leftFileName;
string rightFileName;
string numOfPartition;
string numOfGHJoin;
//read out the partition to be joined
//get the name of the files,jPartition,suffixNum
memcpy(&numOfPartition, &jPartition, sizeof(int));
memcpy(&numOfGHJoin, &suffixNum, sizeof(int));
leftFileName="left_join"+numOfPartition+"_"+numOfGHJoin;//e.g left_join1_0
rightFileName="right_join"+numOfPartition+"_"+numOfGHJoin;
rbfm->openFile(leftFileName, leftFileHandle);
rbfm->openFile(rightFileName, rightFileHandle);
//
return QE_EOF;
}
void GHJoin:: getAttributes(vector<Attribute> &attrs) const
{
//return all attributes used in this join
vector<Attribute> leftAttrs;
vector<Attribute> rightAttrs;
string attriName="rel.attr";
//get attributes from left relation and right relation
leftIn->getAttributes(leftAttrs);
rightIn->getAttributes(rightAttrs);
for(int i = 0; i < leftAttrs.size(); ++i)
{
leftAttrs[i].name=attriName;
attrs.push_back(leftAttrs[i]);
}
for(int i = 0; i < rightAttrs.size(); ++i)
{
rightAttrs[i].name=attriName;
attrs.push_back(rightAttrs[i]);
}
}
RC GHJoin::Partitons(unsigned numPartitions,Condition condition)
{
//get joined attribute
//hash
//write to file
int hashValue;
vector<FileHandle>fileHandle;
string fileName;
int partitionNum=0;
int totalSlotNum=0;
int freeSpaceOffset=0;
vector<Attribute> attrs;
vector<void *>dataPage;
void*data=malloc(PAGE_SIZE);
void *attribute=malloc(PAGE_SIZE);
/********partition left relation*************************/
//initial space for temp page
for(int i=0;i<numPartitions;i++)
{
dataPage[i]=malloc(PAGE_SIZE);
memcpy((char*)dataPage[i]+PAGE_SIZE-2*sizeof(int), &totalSlotNum, sizeof(int));
memcpy((char*)dataPage[i]+PAGE_SIZE-sizeof(int), &freeSpaceOffset, sizeof(int));
}
//open files
for(int i=0;i<numPartitions;i++)
{
string numOfPartition;
string numOfGHJoin;
memcpy(&numOfPartition, &i, sizeof(int));
memcpy(&numOfGHJoin, &suffixNum, sizeof(int));
fileName="left_join"+numOfPartition+"_"+numOfGHJoin;//e.g left_join1_0
rbfm->openFile(fileName, fileHandle[i]);
}
//scan tuples from table
// iterator->setIterator();
leftIn->getAttributes(attrs);
while(leftIn->getNextTuple(data)!=-1)
{
int i=0;
//get attribute position
for(;i<attrs.size();i++)
{
if(condition.lhsAttr==attrs[i].name)
break;
}
if(i==attrs.size())
{
return -1;
}
//get the attribute value used to hash
getAttribute(i, attrs, data, condition.lhsAttr, attribute);
//hash the attribute get partition number
hashValue=partitionHash(attribute,attrs[i],numPartitions);
//insert attribute to temp page
insertAttrToTempPage(fileHandle[hashValue],hashValue,dataPage[hashValue],attribute,attrs[i]);
}
//close file
for(int i=0;i<numPartitions;i++)
{
rbfm->closeFile(fileHandle[i]);
}
//clear vector
attrs.clear();
/********partition right relation*************************/
//initial space for temp page
for(int i=0;i<numPartitions;i++)
{
memcpy((char*)dataPage[i]+PAGE_SIZE-2*sizeof(int), &totalSlotNum, sizeof(int));
memcpy((char*)dataPage[i]+PAGE_SIZE-sizeof(int), &freeSpaceOffset, sizeof(int));
}
//open files
for(int i=0;i<numPartitions;i++)
{
string numOfPartition;
string numOfGHJoin;
memcpy(&numOfPartition, &i, sizeof(int));
memcpy(&numOfGHJoin, &suffixNum, sizeof(int));
fileName="right_join"+numOfPartition+"_"+numOfGHJoin;//e.g right_join1_0
rbfm->openFile(fileName, fileHandle[i]);
}
//scan tuples from table
// iterator->setIterator();
rightIn->getAttributes(attrs);
while(rightIn->getNextTuple(data)!=-1)
{
int i=0;
//get attribute position
for(;i<attrs.size();i++)
{
if(condition.rhsAttr==attrs[i].name)
break;
}
if(i==attrs.size())
{
return -1;
}
//get the attribute value used to hash
getAttribute(i, attrs, data, condition.rhsAttr, attribute);
//hash the attribute get partition number
hashValue=partitionHash(attribute,attrs[i],numPartitions);
//insert attribute to temp page
insertAttrToTempPage(fileHandle[hashValue],hashValue,dataPage[hashValue],attribute,attrs[i]);
}
//close file
for(int i=0;i<numPartitions;i++)
{
rbfm->closeFile(fileHandle[i]);
}
//free page space
for(int i=0;i<numPartitions;i++)
{
free(dataPage[i]);
}
free(data);
free(attribute);
//clear vector
dataPage.clear();
fileHandle.clear();
attrs.clear();
//add suffixNum
suffixNum++;
return 0;
}
int GHJoin::partitionHash(const void *data, const Attribute attrs, unsigned numPartitions )
{
int hashValue=0;
int attrValue=0;
int varCharLength=0;
string attr;
switch (attrs.type) {
case 0:
memcpy(&attrValue, data, sizeof(int));
hashValue=attrValue%numPartitions;
break;
case 1:
memcpy(&attrValue, data, sizeof(int));
hashValue=attrValue%numPartitions;
case 2:
memcpy(&varCharLength, data, sizeof(int));
memcpy(&attr, (char*)data+sizeof(int), varCharLength);
attrValue=stoi(attr);
hashValue=attrValue%numPartitions;
default:
break;
}
return hashValue;
}
int GHJoin:: getAttribute(const unsigned int attriNum,const vector<Attribute> attrs,const void*data, const string attributeName, void *attribute)
{
int attriOffset=0;
int varCharLength=0;
for(int i=0;i<(attriNum-1);i++)
{
switch (attrs[i].type) {
case 0:
attriOffset=attriOffset+sizeof(int);
break;
case 1:
attriOffset=attriOffset+sizeof(int);
break;
case 2:
// attriLength=recordDescriptor[attriCount].length+sizeof(int);
{
memcpy(&varCharLength, (char*)data, attriOffset);
attriOffset=attriOffset+varCharLength+sizeof(int);
break;
}
default:
return -1;
// break;
}
}
switch (attrs[attriNum].type) {
case 0:
memcpy(attribute, (char*)data+attriOffset, sizeof(int));
break;
case 1:
memcpy(attribute, (char*)data+attriOffset, sizeof(int));
break;
case 2:
memcpy(&varCharLength, (char*)data+attriOffset, sizeof(int));
memcpy(attribute, (char*)data+attriOffset, varCharLength+sizeof(int));
break;
default:
break;
}
return 0;
}
int GHJoin:: insertAttrToTempPage(FileHandle filehandle,int partitionNum,void *dataPage,const void* attribute,const Attribute attr)
{
//int type : totalSlotNum
//float type:totalSlotNum
//varChar:<recordOffset,recordLength>,totalSlotNum,freeSpaceOffset
int totalSlotNum=0;
int recordOffset=0;
int recordLength=0;
int freeSpaceOffset=0;
int vaildSpce=0;
int varCharLength=0;
switch (attr.type) {
case 0:
memcpy(&totalSlotNum, (char*)dataPage+PAGE_SIZE-2*sizeof(int), sizeof(int));
if(totalSlotNum==MAX_ATTRI_NUM)
{
filehandle.writePage(partitionNum, dataPage);
totalSlotNum=0;
}
memcpy((char*)dataPage+totalSlotNum*sizeof(int), attribute, sizeof(int));
totalSlotNum=totalSlotNum+1;
memcpy((char*)dataPage+PAGE_SIZE-2*sizeof(int), &totalSlotNum, sizeof(int));
break;
case 1:
memcpy(&totalSlotNum, (char*)dataPage+PAGE_SIZE-2*sizeof(int), sizeof(int));
if(totalSlotNum==MAX_ATTRI_NUM)
{
filehandle.writePage(partitionNum, dataPage);
totalSlotNum=0;
}
memcpy((char*)dataPage+totalSlotNum*sizeof(int), attribute, sizeof(int));
totalSlotNum=totalSlotNum+1;
memcpy((char*)dataPage+PAGE_SIZE-2*sizeof(int), &totalSlotNum, sizeof(int));
break;
case 2:
memcpy(&varCharLength, attribute, sizeof(int));
memcpy(&freeSpaceOffset, (char*)dataPage+PAGE_SIZE-sizeof(int), sizeof(int));
memcpy(&totalSlotNum, (char*)dataPage+PAGE_SIZE-2*sizeof(int), sizeof(int));
vaildSpce=PAGE_SIZE-freeSpaceOffset-2*totalSlotNum-2*sizeof(int);
recordLength=varCharLength+sizeof(int);
if(vaildSpce<recordLength)
{
filehandle.writePage(partitionNum, dataPage);
totalSlotNum=0;
freeSpaceOffset=0;
}
memcpy((char*)dataPage+freeSpaceOffset, attribute, recordLength);
recordOffset=freeSpaceOffset;
memcpy((char*)dataPage+PAGE_SIZE-(2*totalSlotNum+1), &recordOffset, sizeof(int));
memcpy((char*)dataPage+PAGE_SIZE-(2*totalSlotNum+2), &recordLength, sizeof(int));
freeSpaceOffset=freeSpaceOffset+recordLength;
totalSlotNum=totalSlotNum+1;
memcpy((char*)dataPage+PAGE_SIZE-sizeof(int), &freeSpaceOffset, sizeof(int));
memcpy((char*)dataPage+PAGE_SIZE-2*sizeof(int), &totalSlotNum, sizeof(int));
break;
default:
return -1;
}
return 0;
}