BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends Pages
Public Member Functions | Public Attributes | List of all members
badgerdb::BufMgr Class Reference

The central class which manages the buffer pool including frame allocation and deallocation to pages in the file. More...

#include <buffer.h>

Public Member Functions

 BufMgr (std::uint32_t bufs)
 
 ~BufMgr ()
 
void readPage (File *file, const PageId PageNo, Page *&page)
 
void unPinPage (File *file, const PageId PageNo, const bool dirty)
 
void allocPage (File *file, PageId &PageNo, Page *&page)
 
void flushFile (const File *file)
 
void disposePage (File *file, const PageId PageNo)
 
void printSelf ()
 
BufStatsgetBufStats ()
 
void clearBufStats ()
 

Public Attributes

PagebufPool
 

Detailed Description

The central class which manages the buffer pool including frame allocation and deallocation to pages in the file.

Definition at line 160 of file buffer.h.

Constructor & Destructor Documentation

badgerdb::BufMgr::BufMgr ( std::uint32_t  bufs)

Constructor of BufMgr class

Definition at line 19 of file buffer.cpp.

20  : numBufs(bufs) {
21  bufDescTable = new BufDesc[bufs];
22 
23  for (FrameId i = 0; i < bufs; i++)
24  {
25  bufDescTable[i].frameNo = i;
26  bufDescTable[i].valid = false;
27  }
28 
29  bufPool = new Page[bufs];
30 
31  int htsize = ((((int) (bufs * 1.2))*2)/2)+1;
32  hashTable = new BufHashTbl (htsize); // allocate the buffer hash table
33 
34  clockHand = bufs - 1;
35 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
Page * bufPool
Definition: buffer.h:205
badgerdb::BufMgr::~BufMgr ( )

Destructor of BufMgr class

Definition at line 38 of file buffer.cpp.

38  {
39 
40  // 1. Flush all dirty pages to disk
41  for ( std::uint32_t i = 0 ; i < numBufs; ++i ) {
42  if (bufDescTable[i].dirty) {
43  bufDescTable[i].file->writePage(bufPool[i]);
44  }
45  }
46  // 2. deallocate buffer pool
47  delete hashTable; // ?? not mentioned in the pdf
48  delete [] bufPool;
49  // 3. deallocate BufDesc table
50  delete [] bufDescTable;
51 
52 
53 }
void writePage(const Page &new_page)
Definition: file.cpp:169
Page * bufPool
Definition: buffer.h:205

Member Function Documentation

void badgerdb::BufMgr::allocPage ( File file,
PageId PageNo,
Page *&  page 
)

Allocates a new, empty page in the file and returns the Page object. The newly allocated page is also assigned a frame in the buffer pool.

Parameters
fileFile object
PageNoPage number. The number assigned to the page in the file is returned via this reference.
pageReference to page pointer. The newly allocated in-memory Page object is returned via this reference.

Definition at line 177 of file buffer.cpp.

178 {
179  FrameId frameNo;
180  allocBuf(frameNo);
181  bufPool[frameNo] = file->allocatePage();
182  pageNo = bufPool[frameNo].page_number();
183 // fprintf(stderr, "info: %s +%d %s\n", __FILE__, __LINE__, __func__);
184 
185  hashTable->insert(file, pageNo, frameNo);
186  bufDescTable[frameNo].Set(file, pageNo);
187  page = &bufPool[frameNo];
188 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
PageId page_number() const
Definition: page.h:193
Page * bufPool
Definition: buffer.h:205
void insert(const File *file, const PageId pageNo, const FrameId frameNo)
Definition: bufHashTbl.cpp:48
void badgerdb::BufMgr::clearBufStats ( )
inline

Clear buffer pool usage statistics

Definition at line 284 of file buffer.h.

285  {
286  bufStats.clear();
287  }
void badgerdb::BufMgr::disposePage ( File file,
const PageId  PageNo 
)

Delete page from file and also from buffer pool if present. Since the page is entirely deleted from file, its unnecessary to see if the page is dirty.

Parameters
fileFile object
PageNoPage number

Definition at line 191 of file buffer.cpp.

192 {
193  FrameId frameNo;
194  if ( hashTable->lookup(file, PageNo, frameNo) ) {
195  BufDesc* tmpbuf = &bufDescTable[frameNo];
196  hashTable->remove(tmpbuf->file, tmpbuf->pageNo);
197  tmpbuf->Clear();
198  }
199  file->deletePage(PageNo);
200 
201 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
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 badgerdb::BufMgr::flushFile ( const File file)

Writes out all dirty pages of the file to disk. All the frames assigned to the file need to be unpinned from buffer pool before this function can be successfully called. Otherwise Error returned.

Parameters
fileFile object
Exceptions
PagePinnedExceptionIf any page of the file is pinned in the buffer pool
BadBufferExceptionIf any frame allocated to the file is found to be invalid

Definition at line 148 of file buffer.cpp.

149 {
150 
151  BufDesc* tmpbuf;
152  for (std::uint32_t i = 0 ; i < numBufs ; ++i ) {
153  tmpbuf = &bufDescTable[i];
154  if ( tmpbuf->file == file ) { //Is this the way to check if belong to same file?
155  if ( tmpbuf->pinCnt > 0 ) {
156  throw PagePinnedException(file->filename(),
157  tmpbuf->pageNo,
158  tmpbuf->frameNo);
159  }
160  if ( ! tmpbuf->valid ) {
161  throw BadBufferException(tmpbuf->frameNo,
162  tmpbuf->dirty,
163  tmpbuf->valid,
164  tmpbuf->refbit);
165  }
166  if ( tmpbuf->dirty )
167  tmpbuf->file->writePage(bufPool[i]);
168  hashTable->remove(tmpbuf->file, tmpbuf->pageNo);
169  tmpbuf->Clear();
170 
171  }
172  }
173 
174 }
Page * bufPool
Definition: buffer.h:205
void remove(const File *file, const PageId pageNo)
Definition: bufHashTbl.cpp:85
BufStats& badgerdb::BufMgr::getBufStats ( )
inline

Get buffer pool usage statistics

Definition at line 276 of file buffer.h.

277  {
278  return bufStats;
279  }
void badgerdb::BufMgr::printSelf ( void  )

Print member variable values.

Definition at line 204 of file buffer.cpp.

205 {
206  BufDesc* tmpbuf;
207  int validFrames = 0;
208 
209  for (std::uint32_t i = 0; i < numBufs; i++)
210  {
211  tmpbuf = &(bufDescTable[i]);
212  std::cout << "FrameNo:" << i << " ";
213  tmpbuf->Print();
214 
215  if (tmpbuf->valid == true)
216  validFrames++;
217  }
218 
219  std::cout << "Total Number of Valid Frames:" << validFrames << "\n";
220 }
void badgerdb::BufMgr::readPage ( File file,
const PageId  PageNo,
Page *&  page 
)

Reads the given page from the file into a frame and returns the pointer to page. If the requested page is already present in the buffer pool pointer to that frame is returned otherwise a new frame is allocated from the buffer pool for reading the page.

Parameters
fileFile object
PageNoPage number in the file to be read
pageReference to page pointer. Used to fetch the Page object in which requested page from file is read in.

Definition at line 117 of file buffer.cpp.

118 {
119  FrameId frameNo;
120  if ( hashTable->lookup(file, pageNo, frameNo) ) { // found
121  bufDescTable[frameNo].refbit = true;
122  bufDescTable[frameNo].pinCnt++;
123  } else { // not found
124  allocBuf(frameNo);
125  bufPool[frameNo] = file->readPage(pageNo);
126  hashTable->insert(file, pageNo, frameNo);
127  bufDescTable[frameNo].Set(file, pageNo);
128  }
129  page = &bufPool[frameNo];
130 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
Page * bufPool
Definition: buffer.h:205
bool lookup(const File *file, const PageId pageNo, FrameId &frameNo)
Definition: bufHashTbl.cpp:70
void insert(const File *file, const PageId pageNo, const FrameId frameNo)
Definition: bufHashTbl.cpp:48
void badgerdb::BufMgr::unPinPage ( File file,
const PageId  PageNo,
const bool  dirty 
)

Unpin a page from memory since it is no longer required for it to remain in memory.

Parameters
fileFile object
PageNoPage number
dirtyTrue if the page to be unpinned needs to be marked dirty
Exceptions
PageNotPinnedExceptionIf the page is not already pinned

Definition at line 133 of file buffer.cpp.

134 {
135  FrameId frameNo;
136  if ( hashTable->lookup(file, pageNo, frameNo)) { // found
137  if ( dirty ) bufDescTable[frameNo].dirty = true;
138  if ( bufDescTable[frameNo].pinCnt > 0 ) {
139  bufDescTable[frameNo].pinCnt--;
140  } else {
141  throw PageNotPinnedException(file->filename(), pageNo, frameNo);
142  }
143  }
144 
145 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
bool lookup(const File *file, const PageId pageNo, FrameId &frameNo)
Definition: bufHashTbl.cpp:70

Member Data Documentation

Page* badgerdb::BufMgr::bufPool

Actual buffer pool from which frames are allocated

Definition at line 205 of file buffer.h.


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