/* * This method deletes a particular page from file. * * params: File* file, const PageId PageNo */ void BufMgr::disposePage(File* file, const PageId PageNo) { // Should we delete page if pincount is not zero? currently do not check pincount try{ FrameId frame = -1; // find if the page is in the buffer pool hashTable->lookup(file, PageNo, frame); // page is within the hashtable BufDesc* bufDesc = &(bufDescTable[frame]); /*if(bufDesc->pinCnt>0) { throw PagePinnedException("abc", PageNo, frame); }*/ // Clear the frame bufDesc->Clear(); // delete from buffer pool hashTable->remove(file, PageNo); } // Page is not in the buffer pool catch(HashNotFoundException) { } file->deletePage(PageNo); }
/* * Decrements the pinCnt of the frame containing (file, PageNo) * * Throws HashNotFoundException when page is not in the buffer pool, * on the hashtable to get a frame number. * params: File* file, const PageId pageNo, Page*& page */ void BufMgr::readPage(File* file, const PageId pageNo, Page*& page) { FrameId frame = -1; // look up hashtable try{ hashTable->lookup(file, pageNo, frame); // page is within the hashtable BufDesc* bufDesc = &(bufDescTable[frame]); bufDesc->refbit = true; bufDesc->pinCnt = bufDesc->pinCnt + 1; page = &(bufPool[frame]); } // Page is not in the buffer pool catch(HashNotFoundException) { // calling this first to see if the page is valid file->readPage(pageNo); // Allocate frame FrameId frame = -1; allocBuf(frame); page = &(bufPool[frame]); *page = file->readPage(pageNo); // insert into hash table hashTable->insert(file, pageNo, frame); // call Set() BufDesc* bufDesc = &(bufDescTable[frame]); bufDesc->Set(file, pageNo); } }
void BufMgr::flushFile(const File* file) { for(FrameId i = 0; i < numBufs; i++){ BufDesc* temp = &(bufDescTable[i]); if (temp->file == file){ if(temp->pinCnt > 0){ throw PagePinnedException(file->filename(), temp->pageNo, temp->frameNo); } if(temp->valid == false){ temp->pinCnt = 0; throw BadBufferException(temp->frameNo, temp->dirty, temp->valid, temp->refbit); } // (a) if (temp->dirty){ temp->file -> writePage(bufPool[i]); // flushes the page to disk temp->dirty = false; // sets dirty bit to false } //(b) try{ hashTable->remove(file, bufDescTable[i].pageNo); } catch(HashNotFoundException e){ return; } //(c) temp->Clear(); // clears frame //bufDescTable[i].valid = false; //bufDescTable[i].pinCnt = 0; } } }
//Flushes out all dirty pages and deallocates the buffer pool and the BufDesc table. BufMgr::~BufMgr() { BufDesc* bufdesc; File * tmpfile; Page * frame; for (std::uint32_t i = 0; i < numBufs; i++) { bufdesc = &(bufDescTable[i]); if(bufdesc->valid && bufdesc->dirty) { //write frame from buffer onto disk tmpfile = bufdesc->file; frame = &(bufPool[bufdesc->frameNo]); tmpfile->writePage(*frame); } //clear entry from BufDesc bufdesc->Clear(); } //deallocate BufDesc table delete [] &bufDescTable; //deallocates BufferPool for(std::uint32_t i = 0; i < numBufs; i++) { //guess there is no harm in leaving old information in pool } delete [] &bufPool; }
/* * print buffer management including total number of valid frames */ void BufMgr::printSelf(void) { BufDesc* tmpbuf; int validFrames = 0; for (std::uint32_t i = 0; i < numBufs; i++) { tmpbuf = &(bufDescTable[i]); std::cout << "FrameNo:" << i << " "; tmpbuf->Print(); if (tmpbuf->valid == true) validFrames++; } std::cout << "Total Number of Valid Frames:" << validFrames << "\n"; }
/* * This method is to to allocate an empty page in the specified file. * * params: File* file, PageId &pageNo, Page*& page */ void BufMgr::allocPage(File* file, PageId &pageNo, Page*& page) { // get a new page and allocate frame Page newPage = file->allocatePage(); pageNo = newPage.page_number(); FrameId frame = -1; allocBuf(frame); page = &(bufPool[frame]); *page = file->readPage(pageNo); // insert into hash table hashTable->insert(file, pageNo, frame); // call Set() BufDesc* bufDesc = &(bufDescTable[frame]); bufDesc->Set(file, pageNo); //std::cout << "Allocated new page: " << frame << "\n"; }