use of com.orientechnologies.orient.core.storage.OStorageOperationResult in project orientdb by orientechnologies.
the class ODistributedTransactionManager method createUndoTasksFromTx.
/**
* Create undo content for distributed 2-phase rollback. This list of undo tasks is sent to all the nodes to revert a transaction
* and it's also applied locally.
*
* @param iTx Current transaction
*
* @return List of remote undo tasks
*/
protected List<OAbstractRemoteTask> createUndoTasksFromTx(final OTransaction iTx) {
final List<OAbstractRemoteTask> undoTasks = new ArrayList<OAbstractRemoteTask>();
for (ORecordOperation op : iTx.getAllRecordEntries()) {
OAbstractRemoteTask undoTask = null;
final ORecord record = op.getRecord();
switch(op.type) {
case ORecordOperation.CREATED:
// CREATE UNDO TASK LATER ONCE THE RID HAS BEEN ASSIGNED
break;
case ORecordOperation.UPDATED:
case ORecordOperation.DELETED:
// CREATE UNDO TASK WITH THE PREVIOUS RECORD CONTENT/VERSION
final ORecordId rid = (ORecordId) record.getIdentity();
final AtomicReference<ORecord> previousRecord = new AtomicReference<ORecord>();
OScenarioThreadLocal.executeAsDefault(new Callable<Object>() {
@Override
public Object call() throws Exception {
final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
final ORecordOperation txEntry = db.getTransaction().getRecordEntry(rid);
if (txEntry != null && txEntry.type == ORecordOperation.DELETED)
// GET DELETED RECORD FROM TX
previousRecord.set(txEntry.getRecord());
else {
final OStorageOperationResult<ORawBuffer> loadedBuffer = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying().readRecord(rid, null, true, false, null);
if (loadedBuffer != null) {
// LOAD THE RECORD FROM THE STORAGE AVOIDING USING THE DB TO GET THE TRANSACTIONAL CHANGES
final ORecord loaded = Orient.instance().getRecordFactoryManager().newInstance(loadedBuffer.getResult().recordType);
ORecordInternal.fill(loaded, rid, loadedBuffer.getResult().version, loadedBuffer.getResult().getBuffer(), false);
previousRecord.set(loaded);
} else
// RECORD NOT FOUND ON LOCAL STORAGE, ASK TO DB BECAUSE IT COULD BE SHARDED AND RESIDE ON ANOTHER SERVER
previousRecord.set(db.load(rid));
}
return null;
}
});
if (previousRecord.get() == null)
throw new ORecordNotFoundException(rid);
if (op.type == ORecordOperation.UPDATED)
undoTask = new OFixUpdateRecordTask(previousRecord.get(), ORecordVersionHelper.clearRollbackMode(previousRecord.get().getVersion()));
else
undoTask = new OResurrectRecordTask(previousRecord.get());
break;
default:
continue;
}
if (undoTask != null)
undoTasks.add(undoTask);
}
return undoTasks;
}
Aggregations