BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations 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)
 
void 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
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).

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

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;
79  }
80  tmpBuc = tmpBuc->next;
81  }
82 
83  throw HashNotFoundException(file->filename(), pageNo);
84 }
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 86 of file bufHashTbl.cpp.

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

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