Example #1
0
/*
 * bufTable for pages belonging to the file. For each page encountered:
 * (a) if the page is dirty, call file->writePage() to flush the page to disk 
 *     and then set the dirty bit for the page to false, 
 * (b) remove the page from the hashtable (whether the page is clean or dirty) 
 * (c) invoke the Clear() method of BufDesc for the page frame.
 *
 * Throws PagePinnedException if some page of the file is pinned. 
 * Throws BadBufferException if an invalid page belonging to the file is encountered.
 * params: File* file
 */
void BufMgr::flushFile(const File* file) 
{ 
  // Scan for pages belonging to the file
  for (FrameId i = 0; i < numBufs; i++) 
  {
	// Invalid page
    if(bufDescTable[i].file == file && bufDescTable[i].valid == false) {
		throw BadBufferException(i, bufDescTable[i].dirty, false, bufDescTable[i].refbit);
	}	  
	  
	// found a page belonging to the file
  	if (bufDescTable[i].file == file && bufDescTable[i].valid == true) {
	  // The page is pinned
	  if(bufDescTable[i].pinCnt > 0) {
	    throw PagePinnedException(bufDescTable[i].file->filename(), bufDescTable[i].pageNo, bufDescTable[i].frameNo);
	  }
	  // write to disk if dirty
	  if(bufDescTable[i].dirty == true) {
		bufDescTable[i].file->writePage(bufPool[bufDescTable[i].frameNo]);
		bufDescTable[i].dirty = false;
      }
      
      // delete from hashtable
      hashTable->remove(bufDescTable[i].file, bufDescTable[i].pageNo);
		
	  // clear the frame
	  bufDescTable[i].Clear();
	}
  	
  }
}
Example #2
0
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;
		}
	}
}
Example #3
0
/**
 * Writes out all dirty pages of the file to disk.
 * All the frames assigned to the file need to be unpinned from buffer pool before this function can be successfully called.
 * Otherwise Error returned.
 *
 * @throws  PagePinnedException If any page of the file is pinned in the buffer pool 
 * @throws BadBufferException If any frame allocated to the file is found to be invalid
 */
void BufMgr::flushFile(const File* file) 
{
	
		int pages_found = 0;
		for(std::uint32_t i = 0; i<numBufs; i++){
			if(bufDescTable[i].file == file){
				pages_found++;
				if(bufDescTable[i].pinCnt > 0){
					throw PagePinnedException(bufDescTable[i].file->filename(), bufDescTable[i].pageNo, i);
				}
				else if(bufPool[i].page_number() != bufDescTable[i].pageNo || bufDescTable[i].valid == false){
					throw BadBufferException(i, bufDescTable[i].dirty, bufDescTable[i].valid, bufDescTable[i].refbit);
				}

				if(bufDescTable[i].dirty == true){
					bufDescTable[i].file->writePage(bufPool[i]);
				}
				hashTable->remove(file, bufDescTable[i].pageNo);
				bufDescTable[i].Clear();
			}
		}	
}
Example #4
0
/**
* Writes out all dirty pages of the file to disk.
* All the frames assigned to the file need to be unpinned from buffer pool before this function can be successfully called.
* Otherwise Error returned.
*
* @param file   	File object
* @throws  PagePinnedException If any page of the file is pinned in the buffer pool 
* @throws BadBufferException If any frame allocated to the file is found to be invalid
*/
void BufMgr::flushFile(const File* file) 
{
	for(FrameId i = 0; i < numBufs; i++){
		if(bufDescTable[i].file == file){
			if(bufDescTable[i].pinCnt != 0){
				//page is already pinned, throw an exception
				throw PagePinnedException("pagePinned", bufDescTable[i].pageNo, i);
			}
			if(!bufDescTable[i].valid){
				//Not a valid page, throw an exception
				throw BadBufferException(i, bufDescTable[i].dirty, bufDescTable[i].valid, bufDescTable[i].refbit);
			}

			if(bufDescTable[i].dirty){
				//write to disk if the frame is dirty
				bufDescTable[i].file->writePage(bufPool[i]);
				bufDescTable[i].dirty = false;
			}
			hashTable->remove(bufDescTable[i].file, bufDescTable[i].pageNo);
			bufDescTable[i].Clear();
		}
	}
}