-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufHash.C
More file actions
123 lines (101 loc) · 2.95 KB
/
bufHash.C
File metadata and controls
123 lines (101 loc) · 2.95 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
#include <memory.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <iostream>
#include <stdio.h>
#include "page.h"
#include "buf.h"
// buffer pool hash table implementation
int BufHashTbl::hash(const File* file, const int pageNo)
{
long tmp, value;
tmp = (int)(long)file; // cast of pointer to the file object to an integer
value = (tmp + pageNo) % HTSIZE;
return abs(value);
}
BufHashTbl::BufHashTbl(int htSize)
{
HTSIZE = htSize;
// allocate an array of pointers to hashBuckets
ht = new hashBucket* [htSize];
for(int i=0; i < HTSIZE; i++)
ht[i] = NULL;
}
BufHashTbl::~BufHashTbl()
{
for(int i = 0; i < HTSIZE; i++) {
hashBucket* tmpBuf = ht[i];
while (ht[i]) {
tmpBuf = ht[i];
ht[i] = ht[i]->next;
delete tmpBuf;
}
}
delete [] ht;
}
//---------------------------------------------------------------
// insert entry into hash table mapping (file,pageNo) to frameNo;
// returns OK if OK, HASHTBLERROR if an error occurred
//---------------------------------------------------------------
Status BufHashTbl::insert(const File* file, const int pageNo, const int frameNo) {
int index = hash(file, pageNo);
hashBucket* tmpBuc = ht[index];
while (tmpBuc) {
if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
return HASHTBLERROR;
tmpBuc = tmpBuc->next;
}
tmpBuc = new hashBucket;
if (!tmpBuc)
return HASHTBLERROR;
tmpBuc->file = (File*) file;
tmpBuc->pageNo = pageNo;
tmpBuc->frameNo = frameNo;
tmpBuc->next = ht[index];
ht[index] = tmpBuc;
return OK;
}
//-------------------------------------------------------------------
// Check if (file,pageNo) is currently in the buffer pool (ie. in
// the hash table). If so, return corresponding frameNo. else return
// HASHNOTFOUND
//-------------------------------------------------------------------
Status BufHashTbl::lookup(const File* file, const int pageNo, int& frameNo)
{
int index = hash(file, pageNo);
hashBucket* tmpBuc = ht[index];
while (tmpBuc) {
if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
{
frameNo = tmpBuc->frameNo; // return frameNo by reference
return OK;
}
tmpBuc = tmpBuc->next;
}
return HASHNOTFOUND;
}
//-------------------------------------------------------------------
// delete entry (file,pageNo) from hash table. REturn OK if page was
// found. Else return HASHTBLERROR
//-------------------------------------------------------------------
Status BufHashTbl::remove(const File* file, const int pageNo) {
int index = hash(file, pageNo);
hashBucket* tmpBuc = ht[index];
hashBucket* prevBuc = ht[index];
while (tmpBuc) {
if (tmpBuc->file == file && tmpBuc->pageNo == pageNo) {
if (tmpBuc == ht[index])
ht[index] = tmpBuc->next;
else
prevBuc->next = tmpBuc->next;
delete tmpBuc;
return OK;
} else {
prevBuc = tmpBuc;
tmpBuc = tmpBuc->next;
}
}
return HASHTBLERROR;
}