SwatDB
|
#include <bufmgr.h>
Public Member Functions | |
BufferManager (DiskManager *disk_mgr) | |
BufferManager constructor. Initializes the buf_pool and frame_table, and stores a pointer to SwatDB's DiskManager. More... | |
~BufferManager () | |
BufferManager destructor. More... | |
std::pair< Page *, PageId > | allocatePage (FileId file_id) |
Allocates a Page for the file of given FileId. The Page is allocated both in the buffer pool, and on disk. More... | |
void | deallocatePage (PageId page_id) |
Removes the Page of the given PageId from the buffer pool, and deallocates the Page from the appopriate file on disk. More... | |
Page * | getPage (PageId page_id) |
Gets Page by page_id, pins the Page, and returns a pointer to the Page object. More... | |
void | releasePage (PageId page_id, bool dirty) |
Unpins a Page in the buffer pool. More... | |
void | setDirty (PageId page_id) |
Set the Page of the given PageId dirty. More... | |
void | flushPage (PageId page_id) |
Flushes the Page of the given PageId to disk. More... | |
void | createFile (FileId file_id) |
Calls createFile() method on the DiskManager to create new Unix file that corresponds to the given FileId. More... | |
void | removeFile (FileId file_id) |
Calls removeFile() method on the DiskManager. Checks that none of the file's pages are pinned in the buffer pool. Removes any of the file's pages from the buffer pool before removing from disk. More... | |
void | clearBuffer () |
THIS METHOD IS FOR DEBUGGING ONLY. Clears the entire buffer pool, resetting all frames and removing pages from the buffer_map even if pinned. Does not flush any dirty pages either. | |
BufferState | getBufferState () |
THIS METHOD IS FOR DEBUGGING ONLY. Returns the current state of the buffer pool. More... | |
void | printAllFrames () |
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of every Frame in the buffer pool, including pin count, valid bit, dirty bit, and ref_bit. If Page is valid, PageId is printed. | |
void | printValidFrames () |
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of every valid Frame in the buffer pool, including PageId, pin count, valid bit, dirty bit, and ref_bit. | |
void | printFrame (FrameId frame_id) |
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of given FrameId, including pin count, valid bit, dirty bit, and ref_bit. If Page is valid, PageId is printed. | |
void | printPage (PageId page_id) |
THIS METHOD IS FOR DEBUGGING ONLY. Prints Frame state of given PageId, including FrameId, pin count, valid bit, dirty bit, and ref_bit. If Page is not in the buffer map, prints "Page Not Found". | |
void | printBufferState () |
THIS METHOD IS FOR DEBUGGING ONLY. Prints current buffer state, including total number of pages, number of valid pages, number of pinned pages, number of dirty pages, number of pages whose ref bit is set and the current clock hand position. | |
Private Member Functions | |
FrameId | _allocateFrame () |
Allocates a Frame in the frame_table and returns the Frame index. More... | |
Private Attributes | |
BufferMap | buf_map |
Frame | frame_table [BUF_SIZE] |
Page | buf_pool [BUF_SIZE] |
DiskManager * | disk_mgr |
std::uint32_t | clock_hand |
std::mutex | buf_map_mtx |
SwatDb BufferManager Class. BufferManager manages in memory space of DBMS at page level granularity. At higher level, pages of data could be allocated, deallocated, retrieved to memory and fliushed to disk, using various methods.
BufferManager::BufferManager | ( | DiskManager * | disk_mgr | ) |
BufferManager constructor. Initializes the buf_pool and frame_table, and stores a pointer to SwatDB's DiskManager.
disk_mgr | A pointer to SwatDB's DiskManager object. (DiskManager*). |
BufferManager::~BufferManager | ( | ) |
BufferManager destructor.
|
private |
Allocates a Frame in the frame_table and returns the Frame index.
InsufficientSpaceBufMgr | If all pages in the buffer pool are pinned. |
Allocates a Page for the file of given FileId. The Page is allocated both in the buffer pool, and on disk.
file_id | A FileId to which a Page should be allocated. |
InsufficientSpaceBufMgr | If there is not enough space in buffer pool. |
InvalidFileIdDiskMgr | If file_id not valid. |
InsufficientSpaceDiskMgr | If there is not enough space in the Unix file. |
void BufferManager::createFile | ( | FileId | file_id | ) |
Calls createFile() method on the DiskManager to create new Unix file that corresponds to the given FileId.
file_id | FileId of the file to be created. |
void BufferManager::deallocatePage | ( | PageId | page_id | ) |
Removes the Page of the given PageId from the buffer pool, and deallocates the Page from the appopriate file on disk.
PagePinnedBufMgr | If the Page is pinned. |
InvalidPageNumDiskMg | If page_id.page_num is invalid (from DiskManager layer) |
InvalidFileIdDiskMgr | If page_id.file_id is invalid (from DiskManager layer) |
void BufferManager::flushPage | ( | PageId | page_id | ) |
Flushes the Page of the given PageId to disk.
PageNotFoundBufMgr | If page_id not in buf_map. |
InvalidFileIdDiskMgr | If page_id.file_id not valid. |
InvalidPageNumDiskMgr | If page_id.page_num not valid. |
BufferState BufferManager::getBufferState | ( | ) |
THIS METHOD IS FOR DEBUGGING ONLY. Returns the current state of the buffer pool.
Gets Page by page_id, pins the Page, and returns a pointer to the Page object.
page_id | A PageId corresponding to the pointer to be returned. |
InvalidPageIdBufMgr | If page_id is not valid. |
InsufficientSpaceBufMgr | If buffer pool is full. |
InvalidFileIdDiskMgr | from DiskManager if page_id.file_id invalid |
InvalidPageNumDiskMgr | from DiskManger if page_id.page_num invalid |
DiskErrorDiskMgr | frome DiskManager if file operation fails. |
void BufferManager::releasePage | ( | PageId | page_id, |
bool | dirty | ||
) |
Unpins a Page in the buffer pool.
PageNotPinnedBufMgr | If Page is not pinned. (pin_count is 0). |
PageNotFoundBufMgr | If page_id is not in buf_map. |
void BufferManager::removeFile | ( | FileId | file_id | ) |
Calls removeFile() method on the DiskManager. Checks that none of the file's pages are pinned in the buffer pool. Removes any of the file's pages from the buffer pool before removing from disk.
file_id | FileId of the file to be removed. |
PagePinnedBufMgr | If the are pinned pages of file_id. |
void BufferManager::setDirty | ( | PageId | page_id | ) |
|
private |
A wrapper for std::unordered_map<PageId, FrameId> that maps PageIds to Frame indices in buf_pool. Has different methods from std::unordered_map. Have get(), contains(), insert(), and remove() methods.
|
private |
std::mutex for synchronized access to buf_map.
|
private |
Clock hand which is a std::uint32_t, corresponding to the index of a Frame in frame_table. Used for the clock replacement policy.
|
private |
Pointer to SwatDB's DiskManager. Used for reading, writing, allocating, and deallocating pages to disk.