Hash table class to keep track of pages in the buffer pool. More...
#include <bufHashTbl.h>
Public Member Functions | |
| BufHashTbl (const int htSize) | |
| ~BufHashTbl () | |
| void | insert (const File *file, const PageId pageNo, const FrameId frameNo) |
| void | lookup (const File *file, const PageId pageNo, FrameId &frameNo) |
| void | remove (const File *file, const PageId pageNo) |
Hash table class to keep track of pages in the buffer pool.
Definition at line 45 of file bufHashTbl.h.
| badgerdb::BufHashTbl::BufHashTbl | ( | const int | htSize | ) |
Constructor of BufHashTbl class
Definition at line 26 of file bufHashTbl.cpp.
: HTSIZE(htSize)
{
// allocate an array of pointers to hashBuckets
ht = new hashBucket* [htSize];
for(int i=0; i < HTSIZE; i++)
ht[i] = NULL;
}
Destructor of BufHashTbl class
Definition at line 35 of file bufHashTbl.cpp.
{
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;
}
| void badgerdb::BufHashTbl::insert | ( | const File * | file, |
| const PageId | pageNo, | ||
| const FrameId | frameNo | ||
| ) |
Insert entry into hash table mapping (file, pageNo) to frameNo.
| file | File object |
| pageNo | Page number in the file |
| frameNo | Frame number assigned to that page of the file |
| HashAlreadyPresentException | if the corresponding page already exists in the hash table |
| HashTableException | (optional) if could not create a new bucket as running of memory |
Definition at line 48 of file bufHashTbl.cpp.
{
int index = hash(file, pageNo);
hashBucket* tmpBuc = ht[index];
while (tmpBuc) {
if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
throw HashAlreadyPresentException(tmpBuc->file->filename(), tmpBuc->pageNo, tmpBuc->frameNo);
tmpBuc = tmpBuc->next;
}
tmpBuc = new hashBucket;
if (!tmpBuc)
throw HashTableException();
tmpBuc->file = (File*) file;
tmpBuc->pageNo = pageNo;
tmpBuc->frameNo = frameNo;
tmpBuc->next = ht[index];
ht[index] = tmpBuc;
}
| void badgerdb::BufHashTbl::lookup | ( | const File * | file, |
| const PageId | pageNo, | ||
| FrameId & | frameNo | ||
| ) |
Check if (file, pageNo) is currently in the buffer pool (ie. in the hash table).
| HashNotFoundException | if the page entry is not found in the hash table |
Definition at line 70 of file bufHashTbl.cpp.
{
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;
}
tmpBuc = tmpBuc->next;
}
throw HashNotFoundException(file->filename(), pageNo);
}
| void badgerdb::BufHashTbl::remove | ( | const File * | file, |
| const PageId | pageNo | ||
| ) |
Delete entry (file,pageNo) from hash table.
| HashNotFoundException | if the page entry is not found in the hash table |
Definition at line 86 of file bufHashTbl.cpp.
{
int index = hash(file, pageNo);
hashBucket* tmpBuc = ht[index];
hashBucket* prevBuc = NULL;
while (tmpBuc)
{
if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
{
if(prevBuc)
prevBuc->next = tmpBuc->next;
else
ht[index] = tmpBuc->next;
delete tmpBuc;
return;
}
else
{
prevBuc = tmpBuc;
tmpBuc = tmpBuc->next;
}
}
throw HashNotFoundException(file->filename(), pageNo);
}
1.7.6.1