Ejemplo n.º 1
0
void CreateRoot(void) {
    struct PageHdr *PagePtr;

    /* Install the header of new page */
    PagePtr = (struct PageHdr *) malloc(sizeof(struct PageHdr));
    ck_malloc(PagePtr, "PagePtr");
    PagePtr->PgTypeID = LeafSymbol;
    PagePtr->PgNum = ROOT;
    PagePtr->PgNumOfNxtLfPg = NULLPAGENO;
    PagePtr->KeyListPtr = NULL; /* no keys yet */

    FlushPage(PagePtr); /* fills in #bytes & #keys */
}
Ejemplo n.º 2
0
//--------------------------------------------------------------------
// BufMgr::FreePage
//
// Input    : pid     - page id of a particular page 
// Output   : None
// Purpose  : Free the memory allocated for the page with 
//            page id = pid  
// Condition: Either the page is already in the buffer and is pinned
//            no more than once, or the page is not in the buffer.
// PostCond : The page is unpinned, and the frame where it resides in
//            the buffer pool is freed.  Also the page is deallocated
//            from the database. 
// Return   : OK if operation is successful.  FAIL otherwise.
// Note     : You can call MINIBASE_DB->DeallocatePage(pid) to
//            deallocate a page.
//--------------------------------------------------------------------
Status BufMgr::FreePage(PageID pid)
{
	//std::cout << "Free PageID " << pid << std::endl;
	////std::cout << "Free page:  " << pid << std::endl;

	Frame* targetFrame;
	int frameIndex = FindFrame(pid);
	if (frameIndex != INVALID_FRAME) {
		targetFrame = &frames[frameIndex];

		if (targetFrame->GetPinCount() > 1) return FAIL;
		
		UnpinPage(pid, true);
		FlushPage(pid);
	}
	
	return MINIBASE_DB->DeallocatePage(pid);
}
Ejemplo n.º 3
0
void PageManager::RemovePage(TimePoint const & tp, std::shared_ptr<Page> page) {
	m_pageCache.erase(page->GetID());
	m_pagesOrderByAccessTime.erase(tp);
	m_pagesLastAccessTime.erase(page);
	FlushPage(page);
}
Ejemplo n.º 4
0
PageManager::~PageManager() {
	for (auto const & it: m_pageCache)
		FlushPage(it.second);
}
Ejemplo n.º 5
0
//--------------------------------------------------------------------
// BufMgr::PinPage
//
// Input    : pid     - page id of a particular page 
//            isEmpty - (optional, default to false) if true indicate
//                      that the page to be pinned is an empty page.
// Output   : page - a pointer to a page in the buffer pool. (NULL
//            if fail)
// Purpose  : Pin the page with page id = pid to the buffer.  
//            Read the page from disk unless isEmpty is true or unless
//            the page is already in the buffer.
// Condition: Either the page is already in the buffer, or there is at
//            least one frame available in the buffer pool for the 
//            page.
// PostCond : The page with page id = pid resides in the buffer and 
//            is pinned. The number of pin on the page increase by
//            one.
// Return   : OK if operation is successful.  FAIL otherwise.
//--------------------------------------------------------------------
Status BufMgr::PinPage(PageID pid, Page*& page, bool isEmpty)
{
	if(pid == INVALID_PAGE) return FAIL;

	totalCall++;

	// Check if the page is in the buffer pool
	bool inPool = false;
	Frame* currFrame;
	for (int iter = 0; iter < numFrames; iter++) {
		currFrame = &frames[iter];
		if (currFrame->GetPageID() == pid){
			inPool = true;
			totalHit++;
			break;
		}
	}

	if (inPool){
		// Increase its pin count and set output page pointer
		currFrame->Pin();
		page = currFrame->GetPage();
	}
	else {
		
		// Find the first free frame if there is one
		bool foundEmptyFrame = false;
		for (int iter = 0; iter < numFrames; iter++) {
			currFrame = &frames[iter];
			if (!currFrame->IsValid()){
				foundEmptyFrame = true;
				break;
			}
		}
		
		if (!foundEmptyFrame) {
		
			// Find a page to evict based on our replacement policy
			int replacedPageID = replacer->PickVictim();

			// Get a pointer to the frame we will flush
			for (int iter = 0; iter < numFrames; iter++) {
				currFrame = &frames[iter];
				if (currFrame->GetPageID() == replacedPageID){
					break;
				}
			}

			if(FlushPage(replacedPageID) != OK) { 
				page = NULL;
				return FAIL;
			}
		}

		currFrame->SetPageID(pid);
		currFrame->Pin();

		// If the page is not empty, read it in from disk
		if (!isEmpty && currFrame->Read(pid) != OK) {
			page = NULL;
			return FAIL;
		}
		

		page = currFrame->GetPage();
	}

	// Now that the frame is pinned we need to remove it from the ones that can be evicted
	replacer->RemoveFrame(currFrame->GetPageID());
	////std::cout << "pinned page: " << currFrame->GetPageID() <<std::endl;
	return OK;
}