-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexselect.cpp
executable file
·98 lines (83 loc) · 2.32 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
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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include <string.h>
#include <cstdlib>
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 temp;
if(attrDesc!=NULL)
{
// construct heapfilescan to get random record
HeapFileScan hf_scan(attrDesc->relName, attrDesc->attrOffset, attrDesc->attrLen, (Datatype)attrDesc->attrType, (static_cast<const char*> (attrValue)), op, temp);
if (temp != OK)
{
return temp;
}
// construct index
Index idx(attrDesc->relName, attrDesc->attrOffset, attrDesc->attrLen, (Datatype)attrDesc->attrType, 0, temp);
if (temp != OK)
{
return temp;
}
// result heapfile
HeapFile result_heap(result, temp);
if (temp != OK)
{
return temp;
}
RID tempRID0;
Record tempRecord0;
RID tempRID1;
Record tempRecord1;
int tempOffset;
temp=idx.startScan(attrValue);
if (temp != OK)
{
return temp;
}
while(!idx.scanNext(tempRID0))
{
// get random record
temp=hf_scan.getRandomRecord(tempRID0, tempRecord0);
if (temp != OK)
{
return temp;
}
tempOffset=0;
tempRecord1.length=reclen;
tempRecord1.data=malloc(reclen);
// project
for (int i = 0; i < projCnt; i++)
{
memcpy ((char*)tempRecord1.data+tempOffset, (char*)tempRecord0.data+projNames[i].attrOffset, projNames[i].attrLen);
tempOffset+=projNames[i].attrLen;
}
temp=result_heap.insertRecord(tempRecord1, tempRID1);
if (temp != OK)
{
return temp;
}
free(tempRecord1.data);
}
temp=idx.endScan();
if (temp != OK)
{
return temp;
}
}
// if there is no predicate, cannot use index select, cerr
else
{
return NOTUSED1;
}
return OK;
}