BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
buffer.h
1 
8 #pragma once
9 
10 #include "file.h"
11 #include "bufHashTbl.h"
12 
13 namespace badgerdb {
14 
18 class BufMgr;
19 
23 class BufDesc {
24 
25  friend class BufMgr;
26 
27  private:
31  File* file;
32 
36  PageId pageNo;
37 
41  FrameId frameNo;
42 
46  int pinCnt;
47 
51  bool dirty;
52 
56  bool valid;
57 
61  bool refbit;
62 
66  void Clear()
67  {
68  pinCnt = 0;
69  file = NULL;
70  pageNo = Page::INVALID_NUMBER;
71  dirty = false;
72  refbit = false;
73  valid = false;
74  };
75 
83  void Set(File* filePtr, PageId pageNum)
84  {
85  file = filePtr;
86  pageNo = pageNum;
87  pinCnt = 1;
88  dirty = false;
89  valid = true;
90  refbit = true;
91  }
92 
93  void Print()
94  {
95  if(file)
96  {
97  std::cout << "file:" << file->filename() << " ";
98  std::cout << "pageNo:" << pageNo << " ";
99  }
100  else
101  std::cout << "file:NULL ";
102 
103  std::cout << "valid:" << valid << " ";
104  std::cout << "pinCnt:" << pinCnt << " ";
105  std::cout << "dirty:" << dirty << " ";
106  std::cout << "refbit:" << refbit << "\n";
107  }
108 
112  BufDesc()
113  {
114  Clear();
115  }
116 };
117 
118 
122 struct BufStats
123 {
127  int accesses;
128 
133 
138 
142  void clear()
143  {
144  accesses = diskreads = diskwrites = 0;
145  }
146 
151  {
152  clear();
153  }
154 };
155 
156 
160 class BufMgr
161 {
162  private:
166  FrameId clockHand;
167 
171  std::uint32_t numBufs;
172 
176  BufHashTbl *hashTable;
177 
181  BufDesc *bufDescTable;
182 
186  BufStats bufStats;
187 
191  void advanceClock();
192 
199  void allocBuf(FrameId & frame);
200 
201  public:
206 
210  BufMgr(std::uint32_t bufs);
211 
215  ~BufMgr();
216 
226  void readPage(File* file, const PageId PageNo, Page*& page);
227 
236  void unPinPage(File* file, const PageId PageNo, const bool dirty);
237 
246  void allocPage(File* file, PageId &PageNo, Page*& page);
247 
257  void flushFile(const File* file);
258 
266  void disposePage(File* file, const PageId PageNo);
267 
271  void printSelf();
272 
277  {
278  return bufStats;
279  }
280 
285  {
286  bufStats.clear();
287  }
288 };
289 
290 }
const std::string & filename() const
Definition: file.h:179
The central class which manages the buffer pool including frame allocation and deallocation to pages ...
Definition: buffer.h:160
void readPage(File *file, const PageId PageNo, Page *&page)
Definition: buffer.cpp:117
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
void clearBufStats()
Definition: buffer.h:284
Class which represents a file in the filesystem containing database pages.
Definition: file.h:73
void unPinPage(File *file, const PageId PageNo, const bool dirty)
Definition: buffer.cpp:133
Class to maintain statistics of buffer usage.
Definition: buffer.h:122
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:107
Class for maintaining information about buffer pool frames.
Definition: buffer.h:23
BufMgr(std::uint32_t bufs)
Definition: buffer.cpp:19
void flushFile(const File *file)
Definition: buffer.cpp:148
Page * bufPool
Definition: buffer.h:205
void printSelf()
Definition: buffer.cpp:204
void disposePage(File *file, const PageId PageNo)
Definition: buffer.cpp:191
Hash table class to keep track of pages in the buffer pool.
Definition: bufHashTbl.h:45
static const PageId INVALID_NUMBER
Definition: page.h:123
void allocPage(File *file, PageId &PageNo, Page *&page)
Definition: buffer.cpp:177
BufStats & getBufStats()
Definition: buffer.h:276