/
indexselect.cpp
67 lines (58 loc) · 2.22 KB
/
indexselect.cpp
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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include <stdlib.h>
#include <string.h>
Status Operators::IndexSelect(const string& result, // Name of the output relation
const int projCnt, // Number of attributes in the projection
const AttrDesc projNames[], // Projection list (as AttrDesc)
const AttrDesc* attrDesc, // Attribute in the selection predicate
const Operator op, // Predicate operator
const void* attrValue, // Pointer to the literal value in the predicate
const int reclen) // Length of a tuple in the output relation
{
cout << "Algorithm: Index Select" << endl;
/* Your solution goes here */
Status Stat;
HeapFile heapfile = HeapFile(result, Stat);
if(Stat != OK) {
cerr << "Open heap file fail" << endl;
return Stat;
}
string relation = projNames[0].relName;
int unique = 0;
RID rid;
Record record;
Record tuple;
//Open index file
Index indScan(relation, attrDesc->attrOffset, attrDesc->attrLen, (Datatype)attrDesc->attrType, unique, Stat);
if(Stat != OK){
return Stat;
}
//Heapscan object to access the getRandomRecord() function
HeapFileScan heapScan=HeapFileScan(relation,attrDesc->attrOffset, attrDesc->attrLen,(Datatype)(attrDesc->attrType),(char *)attrValue,op,Stat);
if(Stat != OK){
return Stat;
}
//Start Index Scan
Stat = indScan.startScan(attrValue);
while(indScan.scanNext(rid)==OK){
heapScan.getRandomRecord(rid,record);
tuple.length = reclen;
tuple.data = malloc(reclen);
int offset = 0;
//Project record
for(int i=0; i<projCnt; ++i){
memcpy((char*)tuple.data + offset, (char*)record.data + projNames[i].attrOffset, projNames[i].attrLen);
offset += projNames[i].attrLen;
}
Stat = heapfile.insertRecord(tuple, rid);
free(tuple.data);
if(Stat != OK){
return Stat;
}
}
indScan.endScan();
heapScan.endScan();
return OK;
}