Example #1
0
//--------------------------------------------------------------------
// BufMgr::FlushPage
//
// Input    : pid  - page id of a particular page 
// Output   : None
// Purpose  : Flush the page with the given pid to disk.
// Condition: The page with page id = pid must be in the buffer,
//            and is not pinned. pid cannot be INVALID_PAGE.
// PostCond : The page with page id = pid is written to disk if it's dirty. 
//            The frame where the page resides is empty.
// Return   : OK if operation is successful.  FAIL otherwise.
//--------------------------------------------------------------------
Status BufMgr::FlushPage(PageID pid)
{
	//std::cout << "Flush Page" << pid << std::endl;
	////std::cout << "Flush Page  " << pid << std::endl;
	int frameIndex = FindFrame(pid);
	if (frameIndex == INVALID_FRAME) return FAIL;

	Frame* targetFrame = &frames[frameIndex];
	if(!targetFrame->IsValid() || !targetFrame->NotPinned()) return FAIL;

	if (targetFrame->IsDirty()){
		if (targetFrame->Write() != OK) return FAIL;
		numDirtyPageWrites++;
	}
	
	replacer->RemoveFrame(targetFrame->GetPageID());
	targetFrame->EmptyIt();
	//std::cout << "Flush OK " << std::endl;
	return OK;
} 
Example #2
0
Status BufMgr::FlushAllPages()
{
	//std::cout << "Flush all " << std::endl;
	bool failedOnce = false;
	Frame* currFrame;
	for (int iter = 0; iter < numFrames; iter++) {
		currFrame = &frames[iter];
		if (currFrame->IsValid()) {
			// Check that the frame is not pinned
			if (!currFrame->NotPinned()){
				failedOnce = true;
			}

			if (currFrame->IsDirty()){
				if (currFrame->Write() != OK) failedOnce = true;
				numDirtyPageWrites++;
			}

			replacer->RemoveFrame(currFrame->GetPageID());
			currFrame->EmptyIt();
		}
	}
	return (failedOnce) ? FAIL : OK;
}