// Read a page and put it in buffer. Mark frame as used. RC pinPage (BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum) { RC rc; BM_PageFrame *pf; BM_Pool_MgmtData *mgmtData= bm->mgmtData; BM_LOCK(); // Check if we already have a frame assigned to this page pf= findPageFrame(&mgmtData->pt_head, pageNum); if (pf) { // If fixCount==0, then remove it from LRU // Representing that frame is no more free if(pf->fixCount==0 && bm->strategy == RS_LRU) reuseLRUFrame(&mgmtData->stratData, pf); pf->fixCount++; page->pageNum= pageNum; page->data= (char*)&pf->data; if (bm->strategy == RS_CLOCK) { pf->clockReplaceFlag = FALSE; } BM_UNLOCK(); RETURN(RC_OK); } // Get free frame from pool pf= findFreeFrame(bm); if (pf==NULL) { BM_UNLOCK(); RETURN(RC_BUFFER_POOL_FULL); } // Read physical page and keep it in buffer if (pageNum >= mgmtData->fh.totalNumPages) { rc= ensureCapacity(pageNum+1, &mgmtData->fh); if (rc!=RC_OK) { BM_UNLOCK(); return rc; } } rc= readBlock(pageNum, &mgmtData->fh, &pf->data[0]); if (rc!=RC_OK) { BM_UNLOCK(); return rc; } mgmtData->io_reads++; // Mark page frame as used pf->fixCount++; pf->pn= page->pageNum= pageNum; page->data= &pf->data[0]; // Map page number to frame; setPageFrame(&mgmtData->pt_head, pageNum, pf); //Set the flag for the flag as false, which will prevent any replacement of this frame if (bm->strategy == RS_CLOCK) pf->clockReplaceFlag = FALSE; BM_UNLOCK(); RETURN(RC_OK); }
RC pinPage(BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum) { // Retrieve management data BM_MgmtData *mgmtData = bm->mgmtData; BM_Frame *frame = NULL; // Check for existing frame for this page int i; for (i = 0; i < bm->numPages; i++) { if (pageNum == (&mgmtData->framePool[i])->pageNum) { frame = &mgmtData->framePool[i]; break; } } //printf("Passed check for existing frame"); //printf("%s", frame); // Did we find a frame for this page? // If so, update the replacement strategy if (frame != NULL) { // Update LRU if (bm->strategy == RS_LRU) { movetoHead(&mgmtData->head, frame); } frame->fixCount++; page->pageNum = pageNum; page->data = &frame->memPage[0]; return RC_OK; } // If we made it this far, we need to find a free frame //printf("About to go into find free frame"); frame = findFreeFrame(bm); //printf("Passed find free frame"); //printf("%s", frame); // Check to see if the buffer is full if (frame == NULL) { //printf("Fail for # %d ", pageNum); return RC_BUFFER_FRAME_POOL_FULL; } //printf("passed buffer frame pool full"); // Add page to buffer if (pageNum >= mgmtData->fh.totalNumPages) { //printf("we had to check for capacity"); RC capacityCheck = ensureCapacity(pageNum + 1, &mgmtData->fh); if (capacityCheck != RC_OK) { return capacityCheck; } } //printf("passed buffer frame capacity check"); RC resultReadBlock = readBlock(pageNum, &mgmtData->fh, &frame->memPage[0]); if (resultReadBlock != RC_OK) { return resultReadBlock; } mgmtData->ioReads++; // Mark frame as used frame->fixCount++; frame->pageNum = pageNum; page->pageNum = pageNum; page->data = &frame->memPage[0]; if(bm->strategy == RS_LRU || bm->strategy == RS_FIFO){ insertAtHead(&mgmtData->head, frame); } return RC_OK; }