-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexselect.cpp
74 lines (59 loc) · 2.37 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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include <string.h>
#include <stdlib.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;
// Create/Find the heapfile with the name of the relation
Status status;
HeapFileScan heapIn = HeapFileScan(attrDesc->relName, status);
if(status != OK) return status;
HeapFile heapOut = HeapFile(result, status);
if(status != OK) return status;
// Create/find the index (checked to ensure indexed before entering this fn)
Index index = Index(attrDesc->relName, attrDesc->attrOffset, attrDesc->attrLen, (Datatype)attrDesc->attrType, 1, status);
if (status != OK) return status;
// Start the index scan
status = index.startScan(attrValue);
if(status != OK) return status;
// Reset size for the new relation and update the length
AttrDesc *r;
int len = 0, updatedLength = 0;
status = attrCat->getRelInfo(result, len, r);
if(status != OK) return status;
for(int i = 0; i < len; i++)
{
updatedLength += projNames[i].attrLen;
}
// Go through index and store records into the results heap page
RID rid;
Record record, newRecord;
while(index.scanNext(rid) != NOMORERECS)
{
// Return a reference to record with rid
status = heapIn.getRandomRecord(rid, record);
if(status != OK) return status;
// Copy memory into new Record
newRecord.data = malloc(updatedLength);
for(int i = 0; i < len; i++)
{
memcpy((char *) newRecord.data + r[i].attrOffset ,
(char *) record.data + projNames[i].attrOffset , r[i].attrLen);
}
newRecord.length = updatedLength;
// Store the new Record in the heap page
status = heapOut.insertRecord(newRecord, rid);
if(status != OK) return status;
}
status = index.endScan();
if(status != OK) return status;
return OK;
}