BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
bufHashTbl.cpp
1 
8 #include <memory>
9 #include <iostream>
10 #include "buffer.h"
11 #include "bufHashTbl.h"
12 #include "exceptions/hash_already_present_exception.h"
13 #include "exceptions/hash_not_found_exception.h"
14 #include "exceptions/hash_table_exception.h"
15 
16 namespace badgerdb {
17 
18 int BufHashTbl::hash(const File* file, const PageId pageNo)
19 {
20  int tmp, value;
21  tmp = (long)file; // cast of pointer to the file object to an integer
22  value = (tmp + pageNo) % HTSIZE;
23  return value;
24 }
25 
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 }
34 
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 }
47 
48 void BufHashTbl::insert(const File* file, const PageId pageNo, const FrameId frameNo)
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 }
69 
70 bool BufHashTbl::lookup(const File* file, const PageId pageNo, FrameId &frameNo)
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 }
84 
85 void BufHashTbl::remove(const File* file, const PageId pageNo) {
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 }
112 
113 }
An exception that is thrown when a new entry to be inserted in the hash table is already present in i...
const std::string & filename() const
Definition: file.h:179
BufHashTbl(const int htSize)
Definition: bufHashTbl.cpp:26
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
Declarations for buffer pool hash table.
Definition: bufHashTbl.h:17
hashBucket * next
Definition: bufHashTbl.h:36
Class which represents a file in the filesystem containing database pages.
Definition: file.h:73
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
An exception that is thrown when some unexpected error occurs in the hash table.
bool lookup(const File *file, const PageId pageNo, FrameId &frameNo)
Definition: bufHashTbl.cpp:70
void remove(const File *file, const PageId pageNo)
Definition: bufHashTbl.cpp:85
void insert(const File *file, const PageId pageNo, const FrameId frameNo)
Definition: bufHashTbl.cpp:48
An exception that is thrown when an entry being looked up in the hash table is not present in it...