-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanselect.cpp
executable file
·127 lines (109 loc) · 2.92 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
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
#include "catalog.h"
#include "query.h"
#include "index.h"
#include <string.h>
#include <cstdlib>
/*
* 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;
/* Your solution goes here */
Status temp;
// if there is a predicate, use constructor with predicate arguments
if(attrDesc!=NULL)
{
// construct scan
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 heapfile to store result
HeapFile result_heap(result, temp);
if (temp != OK)
{
return temp;
}
RID tempRID0;
Record tempRecord0;
RID tempRID1;
Record tempRecord1;
int tempOffset;
// traverse scanfile
while(!hf_scan.scanNext(tempRID0, tempRecord0))
{
tempOffset=0;
tempRecord1.length=reclen;
tempRecord1.data=malloc(reclen);
// project answer
for (int i = 0; i < projCnt; i++)
{
if (temp != OK)
{
return temp;
}
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 mmemory
free(tempRecord1.data);
}
}
// if there is no predicate, use the other construct
else
{
// coustruct scanfile
HeapFileScan hf_scan(projNames[0].relName, temp);
if (temp != OK)
{
return temp;
}
// construct result heapfile
HeapFile result_heap(result, temp);
if (temp != OK)
{
return temp;
}
RID tempRID0;
Record tempRecord0;
RID tempRID1;
Record tempRecord1;
int tempOffset;
while(!hf_scan.scanNext(tempRID0, tempRecord0))
{
tempOffset=0;
tempRecord1.length=reclen;
tempRecord1.data=malloc(reclen);
// project
for (int i = 0; i < projCnt; i++)
{
if (temp != OK)
{
return temp;
}
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);
}
}
return OK;
}