-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoinHT.C
More file actions
132 lines (117 loc) · 3.13 KB
/
joinHT.C
File metadata and controls
132 lines (117 loc) · 3.13 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "catalog.h"
#include "query.h"
#include "joinHT.h"
#include "stdio.h"
#include "stdlib.h"
joinHashTbl::joinHashTbl(const int size, const AttrDesc attr)
{
HTSIZE = size;
joinAttr = attr;
ht = new HTentry[HTSIZE]; // allocate the hash table
for(int i=0; i < HTSIZE; i++)
{
ht[i].chain = NULL;
ht[i].bucketCnt = 0;
}
}
joinHashTbl::~joinHashTbl()
{
joinhashBucket* tmpBuf;
for(int i = 0; i < HTSIZE; i++) {
while (ht[i].chain) {
tmpBuf = ht[i].chain;
if (joinAttr.attrType == STRING) delete tmpBuf->attrValue.sValue;
ht[i].chain = ht[i].chain->next;
delete tmpBuf;
}
}
delete [] ht;
}
int joinHashTbl::hash(const char* attrPtr, int attrType)
{
int value = 0;
switch (attrType) {
case INTEGER: value = (*(int *) attrPtr) * HTSIZE * 31; break;
case FLOAT: value = (int) ((*(float *) attrPtr) * HTSIZE * 31); break;
case STRING:
// must be a null terminated string
while (*attrPtr++) value = 31*value + (int)*attrPtr;
break;
default:
printf("illegal type in joinHT hash\n");
break;
}
value = abs(value % HTSIZE);
return value;
}
Status joinHashTbl::insert(const RID newRid, const char* tuple)
{
joinhashBucket* tmpBuc;
char* joinAttrPtr;
joinAttrPtr = (char*) tuple + joinAttr.attrOffset;
int index = hash(joinAttrPtr, joinAttr.attrType);
tmpBuc = new joinhashBucket;
if (!tmpBuc) return HASHTBLERROR;
tmpBuc->next = ht[index].chain;
ht[index].chain = tmpBuc;
ht[index].bucketCnt++; // keep track of how many buckets on this chain
tmpBuc->rid = newRid;
switch (joinAttr.attrType) {
case INTEGER:
tmpBuc->attrValue.iValue = *((int *) joinAttrPtr);
break;
case FLOAT:
tmpBuc->attrValue.fValue = *((float*) joinAttrPtr);
break;
case STRING:
tmpBuc->attrValue.sValue = new char[joinAttr.attrLen];
memcpy((void*) tmpBuc->attrValue.sValue, (void*) joinAttrPtr, joinAttr.attrLen);
break;
default:
printf("illegal type in joinHT insert\n");
break;
}
return OK;
}
Status joinHashTbl::lookup(const char* innerJoinAttrPtr, int & ridCnt, RID *&outRids)
{
joinhashBucket* tmpBuc;
ridCnt = 0;
int index = hash(innerJoinAttrPtr, joinAttr.attrType);
tmpBuc = ht[index].chain;
// allocate an array of RIDs. This array may be slightly too big in the case of "collisions"
// in which different join attribute values of the outer hash to the same chain
outRids = new RID[ht[index].bucketCnt];
while (tmpBuc != NULL)
{
// scan hash chain looking for matches
switch (joinAttr.attrType) {
case INTEGER:
if (tmpBuc->attrValue.iValue == *((int *)innerJoinAttrPtr))
{
outRids[ridCnt] = tmpBuc->rid;
ridCnt++;
}
break;
case FLOAT:
if (tmpBuc->attrValue.fValue == *((float *)innerJoinAttrPtr))
{
outRids[ridCnt] = tmpBuc->rid;
ridCnt++;
}
break;
case STRING:
if (strncmp(tmpBuc->attrValue.sValue, innerJoinAttrPtr, joinAttr.attrLen)==0)
{
outRids[ridCnt] = tmpBuc->rid;
ridCnt++;
}
break;
default:
printf("illegal type in joinHT lookup\n");
break;
}
tmpBuc = tmpBuc->next;
}
return OK;
}