11 #include "exceptions/buffer_exceeded_exception.h"
12 #include "exceptions/page_not_pinned_exception.h"
13 #include "exceptions/page_pinned_exception.h"
14 #include "exceptions/bad_buffer_exception.h"
15 #include "exceptions/hash_not_found_exception.h"
21 bufDescTable =
new BufDesc[bufs];
23 for (
FrameId i = 0; i < bufs; i++)
25 bufDescTable[i].frameNo = i;
26 bufDescTable[i].valid =
false;
31 int htsize = ((((int) (bufs * 1.2))*2)/2)+1;
41 for ( std::uint32_t i = 0 ; i < numBufs; ++i ) {
42 if (bufDescTable[i].dirty) {
50 delete [] bufDescTable;
55 void BufMgr::advanceClock()
57 clockHand = (clockHand+1)%numBufs;
61 void BufMgr::allocBuf(
FrameId & frame)
89 for ( i = 0 ; i < numBufs*2; ++i ) {
91 tmpbuf = &bufDescTable[clockHand];
92 if ( ! tmpbuf->valid )
94 if ( tmpbuf->refbit ) {
95 tmpbuf->refbit =
false;
98 if ( tmpbuf->pinCnt > 0 )
102 tmpbuf->file->writePage(
bufPool[clockHand]);
105 hashTable->
remove(tmpbuf->file, tmpbuf->pageNo);
109 throw BufferExceededException();
112 frame = tmpbuf->frameNo;
120 if ( hashTable->
lookup(file, pageNo, frameNo) ) {
121 bufDescTable[frameNo].refbit =
true;
122 bufDescTable[frameNo].pinCnt++;
126 hashTable->
insert(file, pageNo, frameNo);
127 bufDescTable[frameNo].Set(file, pageNo);
136 if ( hashTable->
lookup(file, pageNo, frameNo)) {
137 if ( dirty ) bufDescTable[frameNo].dirty =
true;
138 if ( bufDescTable[frameNo].pinCnt > 0 ) {
139 bufDescTable[frameNo].pinCnt--;
152 for (std::uint32_t i = 0 ; i < numBufs ; ++i ) {
153 tmpbuf = &bufDescTable[i];
154 if ( tmpbuf->file == file ) {
155 if ( tmpbuf->pinCnt > 0 ) {
160 if ( ! tmpbuf->valid ) {
168 hashTable->
remove(tmpbuf->file, tmpbuf->pageNo);
185 hashTable->
insert(file, pageNo, frameNo);
186 bufDescTable[frameNo].Set(file, pageNo);
194 if ( hashTable->
lookup(file, PageNo, frameNo) ) {
195 BufDesc* tmpbuf = &bufDescTable[frameNo];
196 hashTable->
remove(tmpbuf->file, tmpbuf->pageNo);
209 for (std::uint32_t i = 0; i < numBufs; i++)
211 tmpbuf = &(bufDescTable[i]);
212 std::cout <<
"FrameNo:" << i <<
" ";
215 if (tmpbuf->valid ==
true)
219 std::cout <<
"Total Number of Valid Frames:" << validFrames <<
"\n";
An exception that is thrown when a page which is expected to be pinned in the buffer pool is found to...
const std::string & filename() const
void readPage(File *file, const PageId PageNo, Page *&page)
std::uint32_t FrameId
Identifier for a frame in buffer pool.
Class which represents a file in the filesystem containing database pages.
void unPinPage(File *file, const PageId PageNo, const bool dirty)
std::uint32_t PageId
Identifier for a page in a file.
Class which represents a fixed-size database page containing records.
Class for maintaining information about buffer pool frames.
void writePage(const Page &new_page)
BufMgr(std::uint32_t bufs)
void flushFile(const File *file)
An exception that is thrown when a buffer is found whose valid is false but other variables in BufDes...
PageId page_number() const
bool lookup(const File *file, const PageId pageNo, FrameId &frameNo)
An exception that is thrown when a page which is not expected to be pinned in the buffer pool is foun...
Page readPage(const PageId page_number) const
void disposePage(File *file, const PageId PageNo)
Hash table class to keep track of pages in the buffer pool.
void remove(const File *file, const PageId pageNo)
void insert(const File *file, const PageId pageNo, const FrameId frameNo)
void allocPage(File *file, PageId &PageNo, Page *&page)
void deletePage(const PageId page_number)