BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
buffer.h
1 
8 #pragma once
9 
10 #include "file.h"
11 #include "bufHashTbl.h"
12 #include <iostream>
13 
14 namespace badgerdb {
15 
19 class BufMgr;
20 
24 class BufDesc {
25 
26  friend class BufMgr;
27 
28  private:
32  File* file;
33 
37  PageId pageNo;
38 
42  FrameId frameNo;
43 
47  int pinCnt;
48 
52  bool dirty;
53 
57  bool valid;
58 
62  bool refbit;
63 
67  void Clear()
68  {
69  pinCnt = 0;
70  file = NULL;
71  pageNo = Page::INVALID_NUMBER;
72  dirty = false;
73  refbit = false;
74  valid = false;
75  };
76 
84  void Set(File* filePtr, PageId pageNum)
85  {
86  file = filePtr;
87  pageNo = pageNum;
88  pinCnt = 1;
89  dirty = false;
90  valid = true;
91  refbit = true;
92  }
93 
94  void Print()
95  {
96  if(file != NULL)
97  {
98  std::cout << "file:" << file->filename() << " ";
99  std::cout << "pageNo:" << pageNo << " ";
100  }
101  else
102  std::cout << "file:NULL ";
103 
104  std::cout << "valid:" << valid << " ";
105  std::cout << "pinCnt:" << pinCnt << " ";
106  std::cout << "dirty:" << dirty << " ";
107  std::cout << "refbit:" << refbit << "\n";
108  }
109 
113  BufDesc()
114  {
115  Clear();
116  }
117 };
118 
119 
123 struct BufStats
124 {
128  int accesses;
129 
134 
139 
143  void clear()
144  {
145  accesses = diskreads = diskwrites = 0;
146  }
147 
152  {
153  clear();
154  }
155 };
156 
157 
161 class BufMgr
162 {
163  private:
167  FrameId clockHand;
168 
172  std::uint32_t numBufs;
173 
177  BufHashTbl *hashTable;
178 
182  BufDesc *bufDescTable;
183 
187  BufStats bufStats;
188 
195  void allocBuf(FrameId & frame);
196 
200  void advanceClock()
201  {
202  clockHand = (clockHand + 1) % numBufs;
203  }
204 
205 
206  public:
211 
215  BufMgr(std::uint32_t bufs);
216 
220  ~BufMgr();
221 
231  void readPage(File* file, const PageId PageNo, Page*& page);
232 
241  void unPinPage(File* file, const PageId PageNo, const bool dirty);
242 
251  void allocPage(File* file, PageId &PageNo, Page*& page);
252 
262  void flushFile(const File* file);
263 
271  void disposePage(File* file, const PageId PageNo);
272 
276  void printSelf();
277 
282  {
283  return bufStats;
284  }
285 
290  {
291  bufStats.clear();
292  }
293 };
294 
295 }
const std::string & filename() const
Definition: file.h:158
The central class which manages the buffer pool including frame allocation and deallocation to pages ...
Definition: buffer.h:161
void readPage(File *file, const PageId PageNo, Page *&page)
Definition: buffer.cpp:120
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
void clearBufStats()
Definition: buffer.h:289
Class which represents a file in the filesystem containing database pages.
Definition: file.h:75
void unPinPage(File *file, const PageId PageNo, const bool dirty)
Definition: buffer.cpp:154
Class to maintain statistics of buffer usage.
Definition: buffer.h:123
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
Class which represents a fixed-size database page containing records.
Definition: page.h:108
Class for maintaining information about buffer pool frames.
Definition: buffer.h:24
BufMgr(std::uint32_t bufs)
Definition: buffer.cpp:23
void flushFile(const File *file)
Definition: buffer.cpp:171
Page * bufPool
Definition: buffer.h:210
void printSelf()
Definition: buffer.cpp:232
void disposePage(File *file, const PageId PageNo)
Definition: buffer.cpp:196
Hash table class to keep track of pages in the buffer pool.
Definition: bufHashTbl.h:45
static const PageId INVALID_NUMBER
Definition: page.h:124
void allocPage(File *file, PageId &PageNo, Page *&page)
Definition: buffer.cpp:213
BufStats & getBufStats()
Definition: buffer.h:281