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 () |
BufStats & | getBufStats () |
void | clearBufStats () |
Public Attributes | |
Page * | bufPool |
The central class which manages the buffer pool including frame allocation and deallocation to pages in the file.
badgerdb::BufMgr::BufMgr | ( | std::uint32_t | bufs | ) |
Constructor of BufMgr class
Definition at line 23 of file buffer.cpp.
: numBufs(bufs) { bufDescTable = new BufDesc[bufs]; for (FrameId i = 0; i < bufs; i++) { bufDescTable[i].frameNo = i; bufDescTable[i].valid = false; } bufPool = new Page[bufs]; int htsize = ((((int) (bufs * 1.2))*2)/2)+1; hashTable = new BufHashTbl (htsize); // allocate the buffer hash table clockHand = bufs - 1; }
Destructor of BufMgr class
Definition at line 42 of file buffer.cpp.
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.
file | File object |
PageNo | Page number. The number assigned to the page in the file is returned via this reference. |
page | Reference to page pointer. The newly allocated in-memory Page object is returned via this reference. |
Definition at line 213 of file buffer.cpp.
{ FrameId frameNo; // alloc a new frame allocBuf(frameNo); // allocate a new page in the file //std::cerr << "buffer data size:" << bufPool[frameNo].data_.length() << "\n"; bufPool[frameNo] = file->allocatePage(pageNo); page = &bufPool[frameNo]; // set up the entry properly bufDescTable[frameNo].Set(file, pageNo); // insert in the hash table hashTable->insert(file, pageNo, frameNo); }
void badgerdb::BufMgr::clearBufStats | ( | ) | [inline] |
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.
Definition at line 196 of file buffer.cpp.
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.
file | File object |
PagePinnedException | If any page of the file is pinned in the buffer pool |
BadBufferException | If any frame allocated to the file is found to be invalid |
Definition at line 171 of file buffer.cpp.
{ for (std::uint32_t i = 0; i < numBufs; i++) { BufDesc* tmpbuf = &(bufDescTable[i]); if(tmpbuf->valid == true && tmpbuf->file == file) { if (tmpbuf->pinCnt > 0) throw PagePinnedException(file->filename(), tmpbuf->pageNo, tmpbuf->frameNo); if (tmpbuf->dirty == true) { //if ((status = tmpbuf->file->writePage(tmpbuf->pageNo, &(bufPool[i]))) != OK) tmpbuf->file->writePage(tmpbuf->pageNo, bufPool[i]); tmpbuf->dirty = false; } hashTable->remove(file,tmpbuf->pageNo); tmpbuf->Clear(); } else if (tmpbuf->valid == false && tmpbuf->file == file) throw BadBufferException(tmpbuf->frameNo, tmpbuf->dirty, tmpbuf->valid, tmpbuf->refbit); } }
BufStats& badgerdb::BufMgr::getBufStats | ( | ) | [inline] |
void badgerdb::BufMgr::printSelf | ( | void | ) |
Print member variable values.
Definition at line 232 of file buffer.cpp.
{ BufDesc* tmpbuf; int validFrames = 0; for (std::uint32_t i = 0; i < numBufs; i++) { tmpbuf = &(bufDescTable[i]); std::cout << "FrameNo:" << i << " "; tmpbuf->Print(); if (tmpbuf->valid == true) validFrames++; } std::cout << "Total Number of Valid Frames:" << validFrames << "\n"; }
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.
file | File object |
PageNo | Page number in the file to be read |
page | Reference 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.
{ // check to see if it is already in the buffer pool // std::cout << "readPage called on file.page " << file << "." << pageNo << endl; FrameId frameNo = 0; try { hashTable->lookup(file, pageNo, frameNo); // set the referenced bit bufDescTable[frameNo].refbit = true; bufDescTable[frameNo].pinCnt++; page = &bufPool[frameNo]; } catch(HashNotFoundException e) //not in the buffer pool, must allocate a new page { // alloc a new frame allocBuf(frameNo); // read the page into the new frame bufStats.diskreads++; //status = file->readPage(pageNo, &bufPool[frameNo]); bufPool[frameNo] = file->readPage(pageNo); // set up the entry properly bufDescTable[frameNo].Set(file, pageNo); page = &bufPool[frameNo]; // insert in the hash table hashTable->insert(file, pageNo, frameNo); } }
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.
file | File object |
PageNo | Page number |
dirty | True if the page to be unpinned needs to be marked dirty |
PageNotPinnedException | If the page is not already pinned |
Definition at line 154 of file buffer.cpp.
{ // lookup in hashtable FrameId frameNo = 0; hashTable->lookup(file, pageNo, frameNo); if (dirty == true) bufDescTable[frameNo].dirty = dirty; // make sure the page is actually pinned if (bufDescTable[frameNo].pinCnt == 0) { throw PageNotPinnedException(file->filename(), pageNo, frameNo); } else bufDescTable[frameNo].pinCnt--; }