Beispiel #1
0
RC PF_FileHandle::AllocatePage (PF_PageHandle &pageHandle) {
	if (!bOpen) {
		return PF_FHCLOSED;
	}

	PageNum i;
	/* Search for a free page using the PF header */
	for (i = 1; i <= pageCount; i++) {
		if (GetPageType(i) == FREE_PAGE) {
			break;
		}
	}
	/* If increasing file size exceeds limit, return error */
	if (i >= 8*(PF_PAGE_SIZE-PF_FILESUBHDR_SIZE)) {
		return PF_DBFULL;
	}

	Frame *pFrame;
	RC rc;
	/* If the paged file has no free pages, increase the file size */
	if (i > pageCount) {
 		/* Increment page count */
		pageCount += 1;
		/* Request buffer space for writing a page to disk */
		rc = bfm.Alloc (fileName, i, fileDesc, pFrame);
		if (rc != SUCCESS) {
			pageCount -= 1;
			return rc;
		}
		/* Append page to file */
		rc = bfm.WritePage (fileDesc, *pFrame);
		if (rc != SUCCESS) {
			bfm.Free (pFrame);
			pageCount -= 1;
			return rc;
		}
	}
	else {
		/* Allocated buffer space */
		rc = bfm.Alloc (fileName, i, fileDesc, pFrame);
		if (rc != SUCCESS) {
			bfm.Free (pFrame);
			return rc;
		}
	}

	/* Do not reverse the order of these operations as only
	 * SetPageType() writes the updated header to disk */
	/* Update sub-header information */
	fileSubHeader.nAllocatedPages++;
	/* Update bitmap */
	SetPageType (i, ALLOCATED_PAGE);

	/* Open page handle */
	rc = pageHandle.Open (*pFrame);
	if (rc != SUCCESS) {
		return rc;
	}
	return SUCCESS;
}
Beispiel #2
0
RC PF_FileHandle::GetThisPage (PageNum pageNum, PF_PageHandle &pageHandle) const {
	if (!bOpen) {
		return PF_FHCLOSED;
	}

	if (pageNum < 1 
		|| pageNum > pageCount 
		|| GetPageType(pageNum) == FREE_PAGE) {
		return PF_INVALIDPAGENUM;
	}

	Frame *pFrame;
	RC rc;
	/* Find frame in buffer that correspods to 
	   the given filename and page number */
	rc = bfm.Find (fileName, pageNum, pFrame);
	if (rc == SUCCESS) { /* If page is found in buffer, increment pin count */
		pFrame->pinCount++;
	}
	else { /* If page is not found in buffer, fetch it from disk */
		/* Request buffer space */
		rc = bfm.Alloc (fileName, pageNum, fileDesc, pFrame);
		if (rc != SUCCESS) {
			return rc;
		}
		/* Fetch page from disk */
		rc = bfm.ReadPage (fileDesc, *pFrame);
		if (rc != SUCCESS) {
			bfm.Free (pFrame);
			return rc;
		}
	}

	/* pFrame now points to the frame containing the request page.
	   We return a page handle to it. */
	return pageHandle.Open (*pFrame);
}