SwatDB
Public Member Functions | Private Member Functions | Private Attributes | List of all members
BufferManager Class Reference

#include <bufmgr.h>

Collaboration diagram for BufferManager:
Collaboration graph
[legend]

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 *, PageIdallocatePage (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...
 
PagegetPage (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]
 
DiskManagerdisk_mgr
 
std::uint32_t clock_hand
 
std::mutex buf_map_mtx
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ BufferManager()

BufferManager::BufferManager ( DiskManager disk_mgr)

BufferManager constructor. Initializes the buf_pool and frame_table, and stores a pointer to SwatDB's DiskManager.

Precondition
disk_mgr points to an initialized DiskManager object.
Postcondition
A BufferManager object will be initialized with an empty buffer pool. disk_mgr is set to the given DiskManager* and clock_hand is set to 0.
Parameters
disk_mgrA pointer to SwatDB's DiskManager object. (DiskManager*).

◆ ~BufferManager()

BufferManager::~BufferManager ( )

BufferManager destructor.

Precondition
None.
Postcondition
Every valid and dirty Page in buffer pool is written to disk.

Member Function Documentation

◆ _allocateFrame()

FrameId BufferManager::_allocateFrame ( )
private

Allocates a Frame in the frame_table and returns the Frame index.

Precondition
Not every Page in the buffer pool is pinned. buf_map mutex is locked.
Postcondition
If there is a Page that is not valid, its Frame is reset and FrameId is returned. If there is a Page that is valid, and is not referenced, it is selected for eviction. If the victim Page is dirty, it is written to disk. Then, the Frame is reset, PageId of the victim Page is removed from buf_map and FrameId is returned. In the process of performing the replacement algorithm, ref_bit of some valid pages that are not pinned may be set to false.
Returns
FrameId of the allocated Frame in frame_table.
Exceptions
InsufficientSpaceBufMgrIf all pages in the buffer pool are pinned.

◆ allocatePage()

std::pair<Page*,PageId> BufferManager::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.

Precondition
A valid FileId is provided and there is a free Page on disk or there is enough space in Unix file. There is also free space in the buffer pool, or a Page which can be evicted from the buffer pool.
Postcondition
A Frame is allocated, and a corresponding Page in the buffer pool is allocated. The Frame's page_id is set, valid is set to true, and the pin_count is set to 1. The PageId is added to the BufferMap. Finally, a pair of a pointer to the allocated Page and PageId is returned.
Parameters
file_idA FileId to which a Page should be allocated.
Returns
std::pair of Page* and PageId of the allocated Page.
Exceptions
InsufficientSpaceBufMgrIf there is not enough space in buffer pool.
InvalidFileIdDiskMgrIf file_id not valid.
InsufficientSpaceDiskMgrIf there is not enough space in the Unix file.

◆ createFile()

void BufferManager::createFile ( FileId  file_id)

Calls createFile() method on the DiskManager to create new Unix file that corresponds to the given FileId.

Precondition
FileId is valid.
Postcondition
Unix file that corresponds to the file_id is created.
Parameters
file_idFileId of the file to be created.
See also
DiskManager::createFile()

◆ deallocatePage()

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.

Precondition
A valid PageId of an unpinned Page is provided as a parameter.
Postcondition
If the Page is in the buffer pool, the Frame is reset, and the Page is removed from the buffer pool. The Page is deallocated from disk.
Parameters
pageIdPageId of the Page to be deallocated
Exceptions
PagePinnedBufMgrIf the Page is pinned.
InvalidPageNumDiskMgIf page_id.page_num is invalid (from DiskManager layer)
InvalidFileIdDiskMgrIf page_id.file_id is invalid (from DiskManager layer)

◆ flushPage()

void BufferManager::flushPage ( PageId  page_id)

Flushes the Page of the given PageId to disk.

Precondition
A PageId of a pinned Page is provided as input.
Postcondition
If the Page is set dirty, the Page is written to disk through the disk_mgr. Page is still pinned.
Parameters
page_idPageId of the Page to set dirty.
Exceptions
PageNotFoundBufMgrIf page_id not in buf_map.
InvalidFileIdDiskMgrIf page_id.file_id not valid.
InvalidPageNumDiskMgrIf page_id.page_num not valid.

◆ getBufferState()

BufferState BufferManager::getBufferState ( )

THIS METHOD IS FOR DEBUGGING ONLY. Returns the current state of the buffer pool.

See also
BufferState

◆ getPage()

Page* BufferManager::getPage ( PageId  page_id)

Gets Page by page_id, pins the Page, and returns a pointer to the Page object.

Precondition
A PageId of an allocated Page is provided as a parameter and buffer pool is not full of pinned pages.
Postcondition
If the page_id is in buf_map, the Page is pinned and its pointer is returned. Else, a Frame is allocated in the buffer pool according to the page replacement policy, the Page is read from disk_mgr into the buffer pool, and the page_id, pin count, and valid bit are set. Page* is returned.
Parameters
page_idA PageId corresponding to the pointer to be returned.
Returns
Pointer to the Page with page_id.
Exceptions
InvalidPageIdBufMgrIf page_id is not valid.
InsufficientSpaceBufMgrIf buffer pool is full.
InvalidFileIdDiskMgrfrom DiskManager if page_id.file_id invalid
InvalidPageNumDiskMgrfrom DiskManger if page_id.page_num invalid
DiskErrorDiskMgrfrome DiskManager if file operation fails.

◆ releasePage()

void BufferManager::releasePage ( PageId  page_id,
bool  dirty 
)

Unpins a Page in the buffer pool.

Precondition
A PageId of a pinned Page is provided as input. The Page is in the buffer pool and is pinned by the executing thread/process.
Postcondition
The pin count of the Page is decremented. ref_bit is set to true. Dirty bit is set if the dirty parameter is true.
Parameters
page_idPageId of the Page to be released.
Exceptions
PageNotPinnedBufMgrIf Page is not pinned. (pin_count is 0).
PageNotFoundBufMgrIf page_id is not in buf_map.

◆ removeFile()

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.

Precondition
A valid FileId is given as a parameter. None of the file's pages are pinned in the buffer pool.
Postcondition
If the file has pages in the buffer pool, the corresponding frames are reset and pages are removed from buf_map. The file is removed from disk via DiskManager->removeFile().
Parameters
file_idFileId of the file to be removed.
Exceptions
PagePinnedBufMgrIf the are pinned pages of file_id.
See also
DiskManager::removeFile()

◆ setDirty()

void BufferManager::setDirty ( PageId  page_id)

Set the Page of the given PageId dirty.

Precondition
A PageId of a pinned Page is provided as input.
Postcondition
The Page is set dirty.
Parameters
page_idPageId of the Page to set dirty.
Exceptions
PageNotFoundBufMgrIf page_id is not in the buffer pool.

Member Data Documentation

◆ buf_map

BufferMap BufferManager::buf_map
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.

◆ buf_map_mtx

std::mutex BufferManager::buf_map_mtx
private

std::mutex for synchronized access to buf_map.

◆ buf_pool

Page BufferManager::buf_pool[BUF_SIZE]
private

Array of Page objects. Represents the buffer pool.

◆ clock_hand

std::uint32_t BufferManager::clock_hand
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.

◆ disk_mgr

DiskManager* BufferManager::disk_mgr
private

Pointer to SwatDB's DiskManager. Used for reading, writing, allocating, and deallocating pages to disk.

◆ frame_table

Frame BufferManager::frame_table[BUF_SIZE]
private

Array of Frame objects. Frames store metadata about each Page in the buffer pool.


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