/**************************************************************************************
 * Function Name: FIFO
 *
 * Description:
 *		FIFO replacement strategy
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *		PageNumber pageNum: the page number of the requested page
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Xin Su <*****@*****.**>
 *
 * History:
 *		Date        Name                                Content
 *		----------  ----------------------------------  ------------------------
 *		2015-03-15  Xin Su <*****@*****.**>         Initialization.
 **************************************************************************************/
RC FIFO(BM_BufferPool * const bm, BM_PageHandle * const page,
		PageNumber pageNum) {
	// First search the page in the queue
	RC rc = -99; // init return code
	rc = searchPage(bm, page, pageNum);

	// If find the page, then simply return RC_OK
	if (rc != RC_PAGE_NOT_FOUND) {
		return rc;
	}

	/*
	 * If the code comes here, then the Buffer Manager doesn't have the page loaded.
	 * We have to read the page from the disk and load it into BM_PageHandle.
	 */

	PageList *queue = (PageList *) bm->mgmtData;

	// If the Buffer Manager has vacancy for the requested page,
	// then read the page from the disk and append it to the tail of the PageList
	if (queue->size < bm->numPages) {
		rc = -99; // reset return code
		rc = appendPage(bm, page, pageNum);

		return rc;
	}

	/*
	 * If the code comes here, then neither the Buffer Manager has the page loaded nor has vacancy for the requested page.
	 * The size of the PageList = bm->numPages
	 * We have to replace an existing page in the Buffer Manager with the requested page.
	 */

	// Find the first removable page, starting from head
	// Set the current pointer to the head of the queue to start the traversal
	queue->current = queue->head;

	while (queue->current != queue->tail && queue->current->fixCount != 0) {
		queue->current = queue->current->next;
	}

	// After the while loop, if the current pointer comes to the tail
	// then we still need to determine if the tail's fixCount = 0
	if (queue->current == queue->tail && queue->current->fixCount != 0) {
		return RC_NO_REMOVABLE_PAGE;
	}

	/*
	 * If the code comes here, then a removable page is found, and the current pointer is pointing to it
	 */

	// Replace the removable page with the requested page
	rc = -99; // reset the return value
	rc = replacePage(bm, page, pageNum);

	return rc;
}
Example #2
0
DbRecord DbFile::getNewRecord(const char* key, const char* value, RecordType type)
{
	size_t size = DbRecord::getSize(key, value, type);

	// Try creating a record in one of pages
	for (int i = pages.size() - 1; i >= 0; --i)
	{
		if (getPage(i).hasSpace(size))
			return getPage(i).newRecord(key, value, type);
	}

	// Create the record in a new page
	return appendPage()->newRecord(key, value, type);
}
std::shared_ptr<Page> Queue::writeFillToPage(AddressType Index,
                                             AddressType& Address) {
  while (Index > LastPage->Index) {
    bool WriteFillNextPage = writeFill(LastPage->getMinAddress(), PageSize);
    if (!WriteFillNextPage && Index > LastPage->Index) {
      // This should only happen if we reach eof. Verify,
      // If so, allow page wrap so that we can have a cursor pointing
      // to the eof position.
      if ((LastPage->getMinAddress() + PageSize != LastPage->getMaxAddress()) ||
          !appendPage())
        return failThenGetErrorPage(Address);
    }
  }
  return getDefinedPage(Index, Address);
}
bool Queue::writeFill(AddressType Address, AddressType WantedSize) {
  Address += WantedSize;
  // Expand till page exists.
  while (Address > LastPage->getMaxAddress()) {
    if (EofFrozen)
      return false;
    AddressType MaxLimit = LastPage->getMinAddress() + PageSize;
    if (Address >= MaxLimit) {
      LastPage->setMaxAddress(MaxLimit);
      if (!appendPage())
        return false;
    } else {
      LastPage->setMaxAddress(Address);
    }
  }
  return true;
}
Example #5
0
OutputViewWidget::OutputViewWidget(QWidget * parent) :
	TitledPanel(parent),
	MESSAGES_PAGE("messages"),
	LOG_PAGE("log"),
	PREVIEW_PAGE("preview"),
	SEARCH_RESULT_PAGE("search")
{
	setSelectorStyle(TabSelector);
	mToggleViewAction->setText(tr("Messages / Log File"));
	mToggleViewAction->setIcon(getRealIcon("logpanel"));


	// messages
	QFontMetrics fm(QApplication::font());
	OutputMessages = new LogEditor(this);
	OutputMessages->setFocusPolicy(Qt::ClickFocus);
	OutputMessages->setMinimumHeight(3*(fm.lineSpacing()+4));
	OutputMessages->setReadOnly(true);

	appendPage(new TitledPanelPage(OutputMessages, MESSAGES_PAGE, tr("Messages")), false);

	// log / errors
	logWidget = new LatexLogWidget(this);

	TitledPanelPage *page = new TitledPanelPage(logWidget, LOG_PAGE, tr("Log"));
	page->addToolbarActions(logWidget->displayActions());
	appendPage(page, false);

	// previewer
	previewWidget = new PreviewWidget(this);
	appendPage(new TitledPanelPage(previewWidget, PREVIEW_PAGE, tr("Preview")), false);

	// global search results
	searchResultModel = new SearchResultModel(this);

	SearchTreeDelegate *searchDelegate=new SearchTreeDelegate(this);

    QHBoxLayout *horz=new QHBoxLayout;
	horz->setContentsMargins(4,2,4,2);
	horz->setSpacing(8);

    searchScopeBox=new QComboBox;
    searchScopeBox->setEditable(false);
	searchScopeBox->addItem(tr("Current Doc"));
	searchScopeBox->addItem(tr("All Docs"));
	searchScopeBox->addItem(tr("Project"));
    connect(searchScopeBox,SIGNAL(currentIndexChanged(int)),SIGNAL(updateTheSearch(int)));


    QLabel *lbl=new QLabel;
    lbl->setText(tr("Search text:"));
    searchTextLabel=new QLabel;
    //searchTextEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    QPushButton *btn=new QPushButton(tr("Update Search"));
    connect(btn,SIGNAL(clicked()),this,SLOT(updateSearch()));
    QLabel *lbl2=new QLabel;
    lbl2->setText(tr("Replace by:"));
    replaceTextEdit=new QLineEdit;
    connect(replaceTextEdit,SIGNAL(textChanged(QString)),this,SLOT(replaceTextChanged(QString)));
    //replaceTextEdit->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Fixed);
    QPushButton *btn2=new QPushButton(tr("Replace all"));
    connect(btn2,SIGNAL(clicked()),this,SLOT(replaceAll()));

    horz->addWidget(searchScopeBox);
    horz->addWidget(lbl);
    horz->addWidget(searchTextLabel,1);
    horz->addWidget(btn);
    horz->addWidget(lbl2);
    horz->addWidget(replaceTextEdit,1);
    horz->addWidget(btn2);

	OutputSearchTree= new QTreeView(this);
    OutputSearchTree->header()->hide();
	OutputSearchTree->setUniformRowHeights(true);
	OutputSearchTree->setModel(searchResultModel);
    OutputSearchTree->setItemDelegate(searchDelegate);
	OutputSearchTree->setFrameShape(QFrame::NoFrame);

    QVBoxLayout *vert=new QVBoxLayout;
	vert->setContentsMargins(0,0,0,0);
	vert->setSpacing(0);

    vert->addLayout(horz);
	QFrame *hLine = new QFrame();
	hLine->setFrameShape(QFrame::HLine);
	vert->addWidget(hLine);
    vert->addWidget(OutputSearchTree,1);


    QWidget *wgt=new QWidget;
    wgt->setLayout(vert);

	connect(OutputSearchTree,SIGNAL(clicked(QModelIndex)),this,SLOT(clickedSearchResult(QModelIndex)));

    //appendPage(new TitledPanelPage(OutputSearchTree, SEARCH_RESULT_PAGE, tr("Search Results")));
    appendPage(new TitledPanelPage(wgt, SEARCH_RESULT_PAGE, tr("Search Results")));
}
/**************************************************************************************
 * Function Name: CLOCK
 *
 * Description:
 *		CLOCK replacement strategy
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *		PageNumber pageNum: the page number of the requested page
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Xin Su <*****@*****.**>
 *
 * History:
 *		Date        Name                                Content
 *		----------  ----------------------------------  ------------------------
 *		2015-03-15  Xin Su <*****@*****.**>         Initialization.
 **************************************************************************************/
RC CLOCK(BM_BufferPool * const bm, BM_PageHandle * const page,
		PageNumber pageNum) {
	PageList *queue = (PageList *) bm->mgmtData;

	// First search the page in the queue.
	RC rc = -99; // init return code
	rc = searchPage(bm, page, pageNum);

	// if find the page, set the current page's clockFlag to TRUE
	// else if other errors happen, return rc
	if (rc == RC_OK) {
		queue->current->clockFlag = TRUE;
		return rc;
	} else if (rc != RC_PAGE_NOT_FOUND) {
		return rc;
	}

	/*
	 * If the code comes here, then the Buffer Manager doesn't have the page loaded.
	 * We have to read the page from the disk and load it into BM_PageHandle.
	 */

	// If the Buffer Manager has vacancy for the requested page,
	// then read the page from the disk and append it to the tail of the PageList
	if (queue->size < bm->numPages) {
		rc = -99; // reset return code
		rc = appendPage(bm, page, pageNum);

		if (rc == RC_OK) {
			// Set the clock pointer to the next to the current pointer
			// if the PageList is not full, set the clock pointer to the next to the current pointer
			// else, set the clock pointer to the head
			if (queue->size < bm->numPages) {
				queue->clock = queue->current->next;
			} else if (queue->size == bm->numPages) {
				queue->clock = queue->head;
			}
		}

		return rc;
	}

	/*
	 * If the code comes here, then neither the Buffer Manager has the page loaded nor has vacancy for the requested page.
	 * The size of the PageList = bm->numPages
	 * We have to replace an existing page in the Buffer Manager with the requested page.
	 */

	// Logic of CLOCK replacement strategy
	// Now the current points to should point to the page that was requested last time
	while (queue->clock->clockFlag != FALSE) {
		queue->clock->clockFlag = FALSE;

		if (queue->clock == queue->tail) {
			queue->clock = queue->head;
		} else {
			queue->clock = queue->clock->next;
		}
	}

	// We find the first PageFrame whose clockFlag is FALSE
	// Set the current pointer to the clock pointer
	queue->current = queue->clock;
	/*RC*/rc = -99;// do not need to redefine RC
	rc = replacePage(bm, page, pageNum);

	if (rc == RC_OK) {
		// After the replacement of the requested page, set its clockFlag to TRUE
		queue->clock->clockFlag = TRUE;

		// TO DO - Whether we need to set the clock pointer to the next to the current pointer?
		if (queue->clock == queue->tail) {
			queue->clock = queue->head;
		} else {
			queue->clock = queue->clock->next;
		}
	}

	return rc;
}
/**************************************************************************************
 * Function Name: CLOCK
 *
 * Description:
 *		CLOCK replacement strategy
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *		PageNumber pageNum: the page number of the requested page
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Xin Su <*****@*****.**>
 *
 * History:
 *		Date        Name                                Content
 *		----------  ----------------------------------  ------------------------
 *		2015-03-15  Xin Su <*****@*****.**>         Initialization
 **************************************************************************************/
RC CLOCK(BM_BufferPool * const bm, BM_PageHandle * const page,
		PageNumber pageNum) {
	PageList *queue = (PageList *) bm->mgmtData;
	RC rc; // init return code

	// First search the page in the PageList
	printf("searchPage: Page-%d\n", pageNum);
	rc = -99;
	rc = searchPage(bm, page, pageNum);

	// if find the page, set the current page's clockFlag to TRUE
	// else if other errors happen, return rc
	if (rc == RC_PAGE_FOUND) {
		queue->current->fixCount++;
		queue->current->clockFlag = TRUE;
		printf("Page-%d found\n", pageNum);

		return rc;
	} else if (rc != RC_PAGE_NOT_FOUND) {
		return rc;
	}
	printf("Page-%d not found\n", pageNum);

	/*
	 * If the code comes here, then the Buffer Manager doesn't have the page loaded.
	 * We have to read the page from the disk and load it into BM_PageHandle.
	 */

	// if the Buffer Manager has vacancy for the requested page,
	// then read the page from the disk and append it to the next to the tail of the PageList
	if (queue->size < bm->numPages) {
		printf("appendPage: Page-%d\n", pageNum);
		rc = -99; // reset return code
		rc = appendPage(bm, page, pageNum);

		// if the PageList is not full, set the clock pointer to the next to the current pointer
		// else, now the current pointer points to the tail, then set the clock pointer points back to the head
		if (rc == RC_OK) {
			if (queue->size < bm->numPages) {
				queue->clock = queue->current->next;
			} else if (queue->size == bm->numPages) {
				queue->clock = queue->head;
			}

			printf("Page-%d appended\n", pageNum);
		}

		return rc;
	}

	/*
	 * If the code comes here, then neither the Buffer Manager has the page loaded nor vacancy for the requested page
	 * Now the PageList is full: the size of the PageList = bm->numPages
	 * We have to replace an existing page in the Buffer Manager with the requested page
	 */

	// Logic of CLOCK replacement strategy
	// Now the current pointer points to the page that was requested last time,
	// and the clock pointer points to the next to the current pointer (or back to the head if the current pointers points to the tail)
	// Find the first page with clockFlag = TRUE and fixCount = 0
	while (queue->clock->clockFlag == TRUE || queue->clock->fixCount != 0
			|| queue->clock->numReadIO != 0 || queue->clock->numWriteIO != 0) {
		queue->clock->clockFlag = FALSE;

		// Into the while loop means this page does not fit the requirement of the removable page
		// Move the clock pointer to the next. If it points to the tail, move it back to the head
		if (queue->clock == queue->tail) {
			queue->clock = queue->head;
		} else {
			queue->clock = queue->clock->next;
		}
	}

	// We find the first PageFrame whose clockFlag = FALSE and fixCount = 0
	// Set the current pointer to the clock pointer, so that we can call replacePage
	queue->current = queue->clock;

	// Replace the removable page with the requested page
	printf("replacePage: Page-%d\n", pageNum);
	rc = -99;
	rc = replacePage(bm, page, pageNum);

	if (rc == RC_OK) {
		// After the replacement of the requested page, set its clockFlag to TRUE
		queue->clock->clockFlag = TRUE;

		// Set the clock pointer to the next to the current pointer
		if (queue->clock == queue->tail) {
			queue->clock = queue->head;
		} else {
			queue->clock = queue->clock->next;
		}

		printf("Page-%d replaced\n", pageNum);
	}

	return rc;
} // CLOCK
/**************************************************************************************
 * Function Name: FIFO
 *
 * Description:
 *		FIFO replacement strategy
 *
 * Parameters:
 *		BM_BufferPool * const bm: Buffer Pool Handler
 *		BM_PageHandle * const page: Buffer Page Handler
 *		PageNumber pageNum: the page number of the requested page
 *
 * Return:
 *		RC: return code
 *
 * Author:
 *		Xin Su <*****@*****.**>
 *
 * History:
 *		Date        Name                                Content
 *		----------  ----------------------------------  ------------------------
 *		2015-03-15  Xin Su <*****@*****.**>         Initialization.
 **************************************************************************************/
RC FIFO(BM_BufferPool * const bm, BM_PageHandle * const page,
		PageNumber pageNum) {
	PageList *queue = (PageList *) bm->mgmtData;
	RC rc; // init return code

	// First search the requested page in the PageList
	printf("searchPage: Page-%d\n", pageNum);
	rc = -99;
	rc = searchPage(bm, page, pageNum);

	// First search the requested page in the queue
	// if the requested page is found, then set statistics and return rc (RC_PAGE_FOUND)
	// else if something unexpected error happens, then return rc
	if (rc == RC_PAGE_FOUND) {
		queue->current->fixCount++;
		printf("Page-%d found\n", pageNum);

		return rc;
	} else if (rc != RC_PAGE_NOT_FOUND) {
		return rc;
	}
	printf("Page-%d not found\n", pageNum);

	/*
	 * If the code comes here, then the Buffer Manager doesn't have the requested page
	 * We have to read the page from the disk and load it into BM_PageHandle.
	 */

	// if the Buffer Manager has vacancy for the requested page,
	// then read the page from the disk and append it to the next of the tail of the PageList
	if (queue->size < bm->numPages) {
		printf("appendPage: Page-%d\n", pageNum);
		rc = -99; // reset return code
		rc = appendPage(bm, page, pageNum);

		if (rc == RC_OK) {
			printf("Page-%d appended\n", pageNum);
		}

		return rc;
	}

	/*
	 * If the code comes here, then neither the Buffer Manager has the requested page loaded nor vacancy for the requested page
	 * Now the PageList is full: the size of the PageList = bm->numPages
	 * We have to replace an existing page in the Buffer Manager with the requested page
	 */

	// Find the first removable page, starting from head
	// Set the current pointer to the head of the queue to start the traversal
	queue->current = queue->head;

	// Find the first page with fixCount = 0
	while (queue->current != queue->tail
			&& (queue->current->fixCount != 0 || queue->current->numReadIO != 0
					|| queue->current->numWriteIO != 0)) {
		queue->current = queue->current->next;
	}

	// After the while loop, if the current pointer comes to the tail,
	// then we still need to determine if the tail's fixCount = 0
	if (queue->current == queue->tail
			&& (queue->current->fixCount != 0 || queue->current->numReadIO != 0
					|| queue->current->numWriteIO != 0)) {
		return RC_NO_REMOVABLE_PAGE;
	}

	/*
	 * If the code comes here, then a removable page is found
	 * The current pointer is pointing to it
	 */

	// Replace the removable page with the requested page
	printf("replacePage: Page-%d\n", pageNum);
	rc = -99; // reset the return value
	rc = replacePage(bm, page, pageNum);

	// if replacePage completes without error and the requested page is not in the tail spot
	// then move it to the tail of the PageList
	if (rc == RC_OK && queue->current != queue->tail) {
		// Remove the current PageFrame
		// The steps of the remove are different depending on if the requested page is the head or not
		if (queue->current == queue->head) {
			queue->head = queue->head->next;
			queue->current->next->previous = NULL;
		} else {
			queue->current->previous->next = queue->current->next;
			queue->current->next->previous = queue->current->previous;
		}

		// Add the requested page to the tail of the PageList
		// step 1 - connect tail and current
		queue->current->previous = queue->tail;
		queue->tail->next = queue->current;

		// step 2 - set current's next
		queue->current->next = NULL;

		// step 3 - set tail
		queue->tail = queue->tail->next;

		printf("Page-%d replaced\n", pageNum);

		// Now the current pointer still points to the requested page
	}

	return rc;
} // FIFO