-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanselect.cpp
87 lines (66 loc) · 2.48 KB
/
scanselect.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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include <string.h>
#include <stdlib.h>
#include "utility.h"
/*
* A simple scan select using a heap file scan
*/
Status Operators::ScanSelect(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 result relation
{
cout << "Algorithm: File Scan" << endl;
Status status;
// Declare Heap to read From
HeapFileScan heapIn = HeapFileScan(projNames[0].relName, status);
if(status != OK) return status;
if(attrDesc) // If there is a predicate
{
// Declare Heap to read from
heapIn = HeapFileScan(attrDesc->relName, attrDesc->attrOffset, attrDesc->attrLen,
(Datatype)attrDesc->attrType, (const char*) attrValue, op, status);
if(status != OK) return status;
// Start the scan of the heapIn
status = heapIn.startScan(attrDesc->attrOffset, attrDesc->attrLen, (Datatype)attrDesc->attrType, (const char*) attrValue, op);
if(status != OK) return status;
}
// Declare heap to write to
HeapFile heapOut = HeapFile(result, status);
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;
}
// Scan through all of the heap until scanNext returns
RID rid;
Record record, newRecord;
while(heapIn.scanNext(rid, record) != FILEEOF)
{
// 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;
}
// End the scan of heapIn
status = heapIn.endScan();
if(status != OK) return status;
return OK;
}