BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations 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 void 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;
79  }
80  tmpBuc = tmpBuc->next;
81  }
82 
83  throw HashNotFoundException(file->filename(), pageNo);
84 }
85 
86 void BufHashTbl::remove(const File* file, const PageId pageNo) {
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 }
113 
114 }
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:158
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:75
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.
void lookup(const File *file, const PageId pageNo, FrameId &frameNo)
Definition: bufHashTbl.cpp:70
void remove(const File *file, const PageId pageNo)
Definition: bufHashTbl.cpp:86
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...