示例#1
0
/**
 * Returns a pointer or reference to the readonly record associated with tid
 *
 * @param tid: the tuple ID
 *
 * @return rtrn: the record
 */
Record* SPSegment::lookup(TID tid) {

	Record* rtrn;

	try {

		if (tid.pageId >= 0 && tid.slotId >= 0) {

			// read slotted page out of memory map
			if (spMap.count(tid.pageId) > 0) {

				SlottedPage* sp = spMap.at(tid.pageId);
				rtrn = sp->lookupRecord(tid.slotId);
			}
			// if not available read from frame
			else {

				// writes frame into map too
				SlottedPage* sp = readFromFrame(tid.pageId);

				if (sp != NULL) {
					rtrn = sp->lookupRecord(tid.slotId);
				} else {
					throw invalid_argument("No page found by given tid");
				}
			}

		} else {
			throw invalid_argument("Given tid is invalid");
		}

		// in case of record is a redirection, lookup for redirected data record
		if (!rtrn->isDataRecord()) {
			rtrn = lookup(rtrn->redirection);
		}

	} catch (const invalid_argument& e) {
		cerr << "Invalid argument @lookup segment: " << e.what() << endl;
		rtrn = NULL;
	}

	return rtrn;
}
示例#2
0
/**
 * Deletes the record pointed to by tid
 *
 * @param tid: the tuple ID
 *
 * @return rtrn: whether successfully or not
 */
bool SPSegment::remove(TID tid) {

	bool rtrn = true;

	try {

		if (tid.pageId >= 0 && tid.slotId >= 0) {

			if (spMap.count(tid.pageId) > 0) {

				SlottedPage* sp = spMap.at(tid.pageId);

				Record* recordToRemove = sp->lookupRecord(tid.slotId);

				// in case of record to remove is a redirection, first remove remote record
				if (!recordToRemove->isDataRecord()) {
					remove(recordToRemove->redirection);
				}

				sp->removeRecord(tid.slotId);

				// write changes back to disk
				if (!writeToFrame(sp, tid.pageId)) {
					cerr << "Cannot write slotted page into frame" << endl;
				}
			}

		} else {
			throw invalid_argument("Given tid is invalid");
		}

	} catch (const invalid_argument& e) {
		cerr << "Invalid argument @remove segment: " << e.what() << endl;
		rtrn = false;
	}

	return rtrn;
}