#include <BufferManager.h>
Manages a memory-based buffer pool of pages.
Public Member Functions | |
BufferManager (int poolSize) | |
Creates a buffer manager with the specified size. | |
~BufferManager () | |
Destructor. | |
int | getPoolSize () |
Returns the pool size. | |
Page * | pinPage (int pinPageId, string fileName, bool emptyPage) |
Checks if this page is in buffer pool. | |
void | unpinPage (int unpinPageId, string fileName, bool dirty) |
If the pin count for this page is greater than 0, it is decremented. | |
int | newPage (int numPages, string fileName, Page *&firstPage) |
Requests a run of pages from the underlying database, then finds a frame in the buffer pool for the first page and pins it. | |
void | freePage (int pageId, string fileName) |
Deallocates a page from the underlying database. | |
void | flushPage (int pageId, string fileName) |
Flushes page from the buffer pool to the underlying database if it is dirty. | |
void | flushAllPages () |
Flushes all dirty pages from the buffer pool to the underlying databases. | |
int | findFrame (int pageId, string fileName) |
Returns buffer pool location for a particular pageId. | |
Static Public Attributes | |
static const int | INVALID_PAGE = -1 |
Value to use for an invalid page id. | |
Classes | |
class | FrameDescriptor |
BufferManager::BufferManager | ( | int | poolSize | ) |
Creates a buffer manager with the specified size.
poolSize | the number of pages that the buffer pool can hold. |
int BufferManager::findFrame | ( | int | pageId, | |
string | fileName | |||
) |
Returns buffer pool location for a particular pageId.
This method is just used for testing purposes: it probably doesn't serve a real purpose in an actual database system.
pageId | the page id to be looked up. | |
fileName | the file name to be looked up. |
void BufferManager::flushAllPages | ( | ) |
Flushes all dirty pages from the buffer pool to the underlying databases.
If page is not dirty, it is not flushed, especially since an undirty page may hang around even after the underlying database has been erased.
void BufferManager::flushPage | ( | int | pageId, | |
string | fileName | |||
) |
Flushes page from the buffer pool to the underlying database if it is dirty.
If page is not dirty, it is not flushed, especially since an undirty page may hang around even after the underlying database has been erased. If the page is not in the buffer pool, do nothing, since the page is effectively flushed already.
pageId | the page id to be flushed. | |
fileName | the name of the database from where the page is to be deallocated. |
void BufferManager::freePage | ( | int | pageId, | |
string | fileName | |||
) |
Deallocates a page from the underlying database.
Verifies that page is not pinned.
pageId | the page id to be deallocated. | |
fileName | the name of the database from where the page is to be deallocated. |
PagePinnedException | if the page is pinned |
int BufferManager::getPoolSize | ( | ) |
Returns the pool size.
int BufferManager::newPage | ( | int | numPages, | |
string | fileName, | |||
Page *& | firstPage | |||
) |
Requests a run of pages from the underlying database, then finds a frame in the buffer pool for the first page and pins it.
If the buffer pool is full, no new pages are allocated from the database.
numPages | the number of pages in the run to be allocated. | |
fileName | the name of the database from where pages are to be allocated. | |
firstPage | a pointer to a page which will be replaced with a pointer to the actual page in the buffer pool. |
FileFullException | if there are not enough free pages (passed through from DBFile). |
Page* BufferManager::pinPage | ( | int | pinPageId, | |
string | fileName, | |||
bool | emptyPage | |||
) |
Checks if this page is in buffer pool.
If it is, returns a pointer to it. Otherwise, it finds an available frame for this page, reads the page, and pins it. Writes out the old page, if it is dirty, before reading.
pinPageId | the page id for the page to be pinned | |
fileName | the name of the database that contains the page to be pinned | |
emptyPage | determines if the page is known to be empty. If true, then the page is not actually read from disk since it is assumed to be empty. |
void BufferManager::unpinPage | ( | int | unpinPageId, | |
string | fileName, | |||
bool | dirty | |||
) |
If the pin count for this page is greater than 0, it is decremented.
If the pin count becomes zero, it is appropriately included in a group of replacement candidates.
unpinPageId | the page id for the page to be unpinned | |
fileName | the name of the database that contains the page to be unpinned | |
dirty | if false, then the page does not actually need to be written back to disk. |
InvalidPageException | if unpinPageNum is not valid. | |
PageNotPinnedException | if the page is not pinned, or if the page id is invalid in some other way. |