BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations 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 161 of file buffer.h.

Constructor & Destructor Documentation

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

Constructor of BufMgr class

Definition at line 23 of file buffer.cpp.

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

Destructor of BufMgr class

Definition at line 42 of file buffer.cpp.

42  {
43  //Flush out all unwritten pages
44  for (std::uint32_t i = 0; i < numBufs; i++)
45  {
46  BufDesc* tmpbuf = &bufDescTable[i];
47  if (tmpbuf->valid == true && tmpbuf->dirty == true)
48  {
49  tmpbuf->file->writePage(tmpbuf->pageNo, bufPool[i]);
50  }
51  }
52 
53  delete [] bufDescTable;
54  delete [] bufPool;
55 }
virtual void writePage(const PageId page_number, const Page &new_page)=0
Page * bufPool
Definition: buffer.h:210

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 213 of file buffer.cpp.

214 {
215  FrameId frameNo;
216 
217  // alloc a new frame
218  allocBuf(frameNo);
219 
220  // allocate a new page in the file
221  //std::cerr << "buffer data size:" << bufPool[frameNo].data_.length() << "\n";
222  bufPool[frameNo] = file->allocatePage(pageNo);
223  page = &bufPool[frameNo];
224 
225  // set up the entry properly
226  bufDescTable[frameNo].Set(file, pageNo);
227 
228  // insert in the hash table
229  hashTable->insert(file, pageNo, frameNo);
230 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
Page * bufPool
Definition: buffer.h:210
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 289 of file buffer.h.

290  {
291  bufStats.clear();
292  }
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 196 of file buffer.cpp.

197 {
198  //Deallocate from file altogether
199  //See if it is in the buffer pool
200  FrameId frameNo = 0;
201  hashTable->lookup(file, pageNo, frameNo);
202 
203  // clear the page
204  bufDescTable[frameNo].Clear();
205 
206  hashTable->remove(file, pageNo);
207 
208  // deallocate it in the file
209  file->deletePage(pageNo);
210 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
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 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 171 of file buffer.cpp.

172 {
173  for (std::uint32_t i = 0; i < numBufs; i++)
174  {
175  BufDesc* tmpbuf = &(bufDescTable[i]);
176  if(tmpbuf->valid == true && tmpbuf->file == file)
177  {
178  if (tmpbuf->pinCnt > 0)
179  throw PagePinnedException(file->filename(), tmpbuf->pageNo, tmpbuf->frameNo);
180 
181  if (tmpbuf->dirty == true)
182  {
183  //if ((status = tmpbuf->file->writePage(tmpbuf->pageNo, &(bufPool[i]))) != OK)
184  tmpbuf->file->writePage(tmpbuf->pageNo, bufPool[i]);
185  tmpbuf->dirty = false;
186  }
187 
188  hashTable->remove(file,tmpbuf->pageNo);
189  tmpbuf->Clear();
190  }
191  else if (tmpbuf->valid == false && tmpbuf->file == file)
192  throw BadBufferException(tmpbuf->frameNo, tmpbuf->dirty, tmpbuf->valid, tmpbuf->refbit);
193  }
194 }
Page * bufPool
Definition: buffer.h:210
void remove(const File *file, const PageId pageNo)
Definition: bufHashTbl.cpp:86
BufStats& badgerdb::BufMgr::getBufStats ( )
inline

Get buffer pool usage statistics

Definition at line 281 of file buffer.h.

282  {
283  return bufStats;
284  }
void badgerdb::BufMgr::printSelf ( void  )

Print member variable values.

Definition at line 232 of file buffer.cpp.

233 {
234  BufDesc* tmpbuf;
235  int validFrames = 0;
236 
237  for (std::uint32_t i = 0; i < numBufs; i++)
238  {
239  tmpbuf = &(bufDescTable[i]);
240  std::cout << "FrameNo:" << i << " ";
241  tmpbuf->Print();
242 
243  if (tmpbuf->valid == true)
244  validFrames++;
245  }
246 
247  std::cout << "Total Number of Valid Frames:" << validFrames << "\n";
248 }
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 120 of file buffer.cpp.

121 {
122  // check to see if it is already in the buffer pool
123  // std::cout << "readPage called on file.page " << file << "." << pageNo << endl;
124  FrameId frameNo = 0;
125  try
126  {
127  hashTable->lookup(file, pageNo, frameNo);
128 
129  // set the referenced bit
130  bufDescTable[frameNo].refbit = true;
131  bufDescTable[frameNo].pinCnt++;
132  page = &bufPool[frameNo];
133  }
134  catch(HashNotFoundException e) //not in the buffer pool, must allocate a new page
135  {
136  // alloc a new frame
137  allocBuf(frameNo);
138 
139  // read the page into the new frame
140  bufStats.diskreads++;
141  //status = file->readPage(pageNo, &bufPool[frameNo]);
142  bufPool[frameNo] = file->readPage(pageNo);
143 
144  // set up the entry properly
145  bufDescTable[frameNo].Set(file, pageNo);
146  page = &bufPool[frameNo];
147 
148  // insert in the hash table
149  hashTable->insert(file, pageNo, frameNo);
150  }
151 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
void lookup(const File *file, const PageId pageNo, FrameId &frameNo)
Definition: bufHashTbl.cpp:70
Page * bufPool
Definition: buffer.h:210
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 154 of file buffer.cpp.

156 {
157  // lookup in hashtable
158  FrameId frameNo = 0;
159  hashTable->lookup(file, pageNo, frameNo);
160 
161  if (dirty == true) bufDescTable[frameNo].dirty = dirty;
162 
163  // make sure the page is actually pinned
164  if (bufDescTable[frameNo].pinCnt == 0)
165  {
166  throw PageNotPinnedException(file->filename(), pageNo, frameNo);
167  }
168  else bufDescTable[frameNo].pinCnt--;
169 }
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Definition: types.h:25
void 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 210 of file buffer.h.


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