BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
Public Member Functions | List of all members
badgerdb::BufHashTbl Class Reference

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)
 
bool lookup (const File *file, const PageId pageNo, FrameId &frameNo)
 
void remove (const File *file, const PageId pageNo)
 

Detailed Description

Hash table class to keep track of pages in the buffer pool.

Warning
This class is not threadsafe.

Definition at line 45 of file bufHashTbl.h.

Constructor & Destructor Documentation

badgerdb::BufHashTbl::BufHashTbl ( const int  htSize)

Constructor of BufHashTbl class

Definition at line 26 of file bufHashTbl.cpp.

27  : HTSIZE(htSize)
28 {
29  // allocate an array of pointers to hashBuckets
30  ht = new hashBucket* [htSize];
31  for(int i=0; i < HTSIZE; i++)
32  ht[i] = NULL;
33 }
badgerdb::BufHashTbl::~BufHashTbl ( )

Destructor of BufHashTbl class

Definition at line 35 of file bufHashTbl.cpp.

36 {
37  for(int i = 0; i < HTSIZE; i++) {
38  hashBucket* tmpBuf = ht[i];
39  while (ht[i]) {
40  tmpBuf = ht[i];
41  ht[i] = ht[i]->next;
42  delete tmpBuf;
43  }
44  }
45  delete [] ht;
46 }
hashBucket * next
Definition: bufHashTbl.h:36

Member Function Documentation

void badgerdb::BufHashTbl::insert ( const File file,
const PageId  pageNo,
const FrameId  frameNo 
)

Insert entry into hash table mapping (file, pageNo) to frameNo.

Parameters
fileFile object
pageNoPage number in the file
frameNoFrame number assigned to that page of the file
Exceptions
HashAlreadyPresentExceptionif 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.

49 {
50  int index = hash(file, pageNo);
51 
52  hashBucket* tmpBuc = ht[index];
53  while (tmpBuc) {
54  if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
55  throw HashAlreadyPresentException(tmpBuc->file->filename(), tmpBuc->pageNo, tmpBuc->frameNo);
56  tmpBuc = tmpBuc->next;
57  }
58 
59  tmpBuc = new hashBucket;
60  if (!tmpBuc)
61  throw HashTableException();
62 
63  tmpBuc->file = (File*) file;
64  tmpBuc->pageNo = pageNo;
65  tmpBuc->frameNo = frameNo;
66  tmpBuc->next = ht[index];
67  ht[index] = tmpBuc;
68 }
hashBucket * next
Definition: bufHashTbl.h:36
bool 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). If so, assign the value to frameNo and return true.

Parameters
fileFile object
pageNoPage number in the file
frameNoFrame number reference that is assigned if (file, pageNo) is present
Returns
true if (file, pageNo) is present. Else false.

Definition at line 70 of file bufHashTbl.cpp.

71 {
72  int index = hash(file, pageNo);
73  hashBucket* tmpBuc = ht[index];
74  while (tmpBuc) {
75  if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
76  {
77  frameNo = tmpBuc->frameNo; // 'return' frameNo by reference
78  return true;
79  }
80  tmpBuc = tmpBuc->next;
81  }
82  return false;
83 }
void badgerdb::BufHashTbl::remove ( const File file,
const PageId  pageNo 
)

Delete entry (file,pageNo) from hash table.

Parameters
fileFile object
pageNoPage number in the file
Exceptions
HashNotFoundExceptionif the page entry is not found in the hash table

Definition at line 85 of file bufHashTbl.cpp.

85  {
86 
87  int index = hash(file, pageNo);
88  hashBucket* tmpBuc = ht[index];
89  hashBucket* prevBuc = NULL;
90 
91  while (tmpBuc)
92  {
93  if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
94  {
95  if(prevBuc)
96  prevBuc->next = tmpBuc->next;
97  else
98  ht[index] = tmpBuc->next;
99 
100  delete tmpBuc;
101  return;
102  }
103  else
104  {
105  prevBuc = tmpBuc;
106  tmpBuc = tmpBuc->next;
107  }
108  }
109 
110  throw HashNotFoundException(file->filename(), pageNo);
111 }
hashBucket * next
Definition: bufHashTbl.h:36

The documentation for this class was generated from the following files: