Ejemplo n.º 1
0
bool BANDB::remove(int transID, int key) {
	int pageIndex = bm->findKey(key);
	if (pageIndex == -1) { return false; }
	int pageNum = bm->getNumAtIndex(pageIndex);

	Record* oldRecP = bm->locateRecord(pageIndex, key);
	Record oldRec = *oldRecP;
	char* oldData = (char*) &oldRec;

	char empty[16] = {};
	for (int i = 0; i < 16; i++) { empty[i] = 0; }

	bool result = bm->removeRecord(pageIndex, key);
	lm->writeWriteRecord(transID, pageNum, oldData, empty);
	return result;
}
Ejemplo n.º 2
0
void BANDB::rollbackWork(int transID, int savepoint) {
	int undoNext = lm->txnTable.locateTxnRecord(transID)->lastLSN;
	while (undoNext > savepoint) {
		LogRecord lr = lm->log[undoNext-1];
		if (lr.type == 2) {
			// first, we recover the old record, which gives us the key and data
			Record* r = new Record(lr.oldData);
			
			// now we write over the data in the page:
			int pageIndex = bm->findKey(r->key);
			if (pageIndex == -1) { pageIndex = bm->findKey(0); }
			int pageNum = bm->getNumAtIndex(pageIndex);


			// If this was an insert, old data will be null, so we need to recover
			// the key from "new data" and remove it from the db
			if (r->key == 0) {
				Record* r2 = new Record(lr.newData);
				pageIndex = bm->findKey(r2->key);
				pageNum = bm->getNumAtIndex(pageIndex);
				bm->removeRecord(pageIndex, r2->key);
			} else {
				bm->insertRecord(pageIndex, r->key, r->data);
			}

			// then write the CLR
			lm->writeRollbackRecord(transID, undoNext, r->data);
		}
		undoNext = lr.prevLSN;
	}
	// If we rolled it back all the way, we are in the abort state
	TxnRecord* rec = lm->txnTable.locateTxnRecord(transID);
	if (undoNext == 0) {
		rec->state = 4;
	} else {
		rec->state = 1;
	}
}