BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends
Public Member Functions | Public Attributes
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>

List of all members.

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.

  : 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.

                {
  //Flush out all unwritten pages
  for (std::uint32_t i = 0; i < numBufs; i++) 
  {
    BufDesc* tmpbuf = &bufDescTable[i];
    if (tmpbuf->valid == true && tmpbuf->dirty == true)
    {
      tmpbuf->file->writePage(tmpbuf->pageNo, bufPool[i]);
    }
  }

  delete [] bufDescTable;
  delete [] bufPool;
}

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.

{
  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);
}

Clear buffer pool usage statistics

Definition at line 289 of file buffer.h.

  {
    bufStats.clear();
  }
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.

{
  //Deallocate from file altogether
  //See if it is in the buffer pool
  FrameId frameNo = 0;
  hashTable->lookup(file, pageNo, frameNo);

  // clear the page
  bufDescTable[frameNo].Clear();

  hashTable->remove(file, pageNo);

  // deallocate it in the file  
  file->deletePage(pageNo);
}
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.

{
  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);
  }
}

Get buffer pool usage statistics

Definition at line 281 of file buffer.h.

  {
    return bufStats;
  }
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.

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.

{
  // 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.

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.

{
  // 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--;
}

Member Data Documentation

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:
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends