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"
25 bufDescTable =
new BufDesc[bufs];
27 for (
FrameId i = 0; i < bufs; i++)
29 bufDescTable[i].frameNo = i;
30 bufDescTable[i].valid =
false;
35 int htsize = ((((int) (bufs * 1.2))*2)/2)+1;
44 for (std::uint32_t i = 0; i < numBufs; i++)
46 BufDesc* tmpbuf = &bufDescTable[i];
47 if (tmpbuf->valid ==
true && tmpbuf->dirty ==
true)
53 delete [] bufDescTable;
57 void BufMgr::allocBuf(
FrameId & frame)
62 std::uint32_t numScanned = 0;
65 while (numScanned < 2*numBufs)
72 if (! bufDescTable[clockHand].valid)
78 if (! bufDescTable[clockHand].refbit)
81 if (bufDescTable[clockHand].pinCnt == 0)
85 hashTable->
remove(bufDescTable[clockHand].file, bufDescTable[clockHand].pageNo);
94 bufDescTable[clockHand].refbit =
false;
99 if (!found && numScanned >= 2*numBufs)
101 throw BufferExceededException();
105 if (bufDescTable[clockHand].dirty)
109 bufDescTable[clockHand].file->
writePage(bufDescTable[clockHand].pageNo,
bufPool[clockHand]);
113 bufDescTable[clockHand].Clear();
127 hashTable->
lookup(file, pageNo, frameNo);
130 bufDescTable[frameNo].refbit =
true;
131 bufDescTable[frameNo].pinCnt++;
145 bufDescTable[frameNo].Set(file, pageNo);
149 hashTable->
insert(file, pageNo, frameNo);
159 hashTable->
lookup(file, pageNo, frameNo);
161 if (dirty ==
true) bufDescTable[frameNo].dirty = dirty;
164 if (bufDescTable[frameNo].pinCnt == 0)
168 else bufDescTable[frameNo].pinCnt--;
173 for (std::uint32_t i = 0; i < numBufs; i++)
175 BufDesc* tmpbuf = &(bufDescTable[i]);
176 if(tmpbuf->valid ==
true && tmpbuf->file == file)
178 if (tmpbuf->pinCnt > 0)
181 if (tmpbuf->dirty ==
true)
185 tmpbuf->dirty =
false;
188 hashTable->
remove(file,tmpbuf->pageNo);
191 else if (tmpbuf->valid ==
false && tmpbuf->file == file)
192 throw BadBufferException(tmpbuf->frameNo, tmpbuf->dirty, tmpbuf->valid, tmpbuf->refbit);
201 hashTable->
lookup(file, pageNo, frameNo);
204 bufDescTable[frameNo].Clear();
206 hashTable->
remove(file, pageNo);
226 bufDescTable[frameNo].Set(file, pageNo);
229 hashTable->
insert(file, pageNo, frameNo);
237 for (std::uint32_t i = 0; i < numBufs; i++)
239 tmpbuf = &(bufDescTable[i]);
240 std::cout <<
"FrameNo:" << i <<
" ";
243 if (tmpbuf->valid ==
true)
247 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...
virtual void writePage(const PageId page_number, const Page &new_page)=0
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.
virtual void deletePage(const PageId page_number)=0
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.
BufMgr(std::uint32_t bufs)
void flushFile(const File *file)
void lookup(const File *file, const PageId pageNo, FrameId &frameNo)
An exception that is thrown when a buffer is found whose valid is false but other variables in BufDes...
An exception that is thrown when a page which is not expected to be pinned in the buffer pool is foun...
virtual Page readPage(const PageId page_number) const =0
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)
virtual Page allocatePage(PageId &new_page_number)=0
void allocPage(File *file, PageId &PageNo, Page *&page)
An exception that is thrown when an entry being looked up in the hash table is not present in it...