use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OSBTreeBonsaiLocalTest method testKeyDelete.
public void testKeyDelete() throws Exception {
for (int i = 0; i < KEYS_COUNT; i++) {
sbTree.put(i, new ORecordId(i % 32000, i));
}
for (int i = 0; i < KEYS_COUNT; i++) {
if (i % 3 == 0)
Assert.assertEquals(sbTree.remove(i), new ORecordId(i % 32000, i));
}
Assert.assertEquals((int) sbTree.firstKey(), 1);
Assert.assertEquals((int) sbTree.lastKey(), (KEYS_COUNT - 1) % 3 == 0 ? KEYS_COUNT - 2 : KEYS_COUNT - 1);
for (int i = 0; i < KEYS_COUNT; i++) {
if (i % 3 == 0)
Assert.assertNull(sbTree.get(i));
else
Assert.assertEquals(sbTree.get(i), new ORecordId(i % 32000, i));
}
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OSBTreeBonsaiLocalTest method testKeyAddDelete.
public void testKeyAddDelete() throws Exception {
for (int i = 0; i < KEYS_COUNT; i++) {
sbTree.put(i, new ORecordId(i % 32000, i));
Assert.assertEquals(sbTree.get(i), new ORecordId(i % 32000, i));
}
for (int i = 0; i < KEYS_COUNT; i++) {
if (i % 3 == 0)
Assert.assertEquals(sbTree.remove(i), new ORecordId(i % 32000, i));
if (i % 2 == 0)
sbTree.put(KEYS_COUNT + i, new ORecordId((KEYS_COUNT + i) % 32000, KEYS_COUNT + i));
}
Assert.assertEquals((int) sbTree.firstKey(), 1);
Assert.assertEquals((int) sbTree.lastKey(), 2 * KEYS_COUNT - 2);
for (int i = 0; i < KEYS_COUNT; i++) {
if (i % 3 == 0)
Assert.assertNull(sbTree.get(i));
else
Assert.assertEquals(sbTree.get(i), new ORecordId(i % 32000, i));
if (i % 2 == 0)
Assert.assertEquals(sbTree.get(KEYS_COUNT + i), new ORecordId((KEYS_COUNT + i) % 32000, KEYS_COUNT + i));
}
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class ODistributedStorage method executeRecordOperationInLock.
private Object executeRecordOperationInLock(final boolean iUnlockAtTheEnd, final ORecordId rid, final OCallable<Object, OCallable<Void, ODistributedRequestId>> callback) throws Exception {
final ORecordId rid2Lock;
if (!rid.isPersistent())
// CREATE A COPY TO MAINTAIN THE LOCK ON THE CLUSTER AVOIDING THE RID IS TRANSFORMED IN PERSISTENT. THIS ALLOWS TO HAVE
// PARALLEL TX BECAUSE NEW RID LOCKS THE ENTIRE CLUSTER.
rid2Lock = new ORecordId(rid.getClusterId(), -1l);
else
rid2Lock = rid;
ODistributedRequestId requestId = null;
final OLogSequenceNumber lastLSN = wrapped.getLSN();
final AtomicBoolean lockReleased = new AtomicBoolean(false);
try {
requestId = acquireRecordLock(rid2Lock);
final ODistributedRequestId finalReqId = requestId;
final OCallable<Void, ODistributedRequestId> unlockCallback = new OCallable<Void, ODistributedRequestId>() {
@Override
public Void call(final ODistributedRequestId requestId) {
// UNLOCK AS SOON AS THE REQUEST IS SENT
if (lockReleased.compareAndSet(false, true)) {
releaseRecordLock(rid2Lock, finalReqId);
lockReleased.set(true);
}
return null;
}
};
return OScenarioThreadLocal.executeAsDistributed(new Callable() {
@Override
public Object call() throws Exception {
return callback.call(unlockCallback);
}
});
} finally {
if (iUnlockAtTheEnd) {
if (lockReleased.compareAndSet(false, true)) {
releaseRecordLock(rid2Lock, requestId);
}
}
final OLogSequenceNumber currentLSN = wrapped.getLSN();
if (!lastLSN.equals(currentLSN))
// SAVE LAST LSN
try {
localDistributedDatabase.getSyncConfiguration().setLastLSN(getDistributedManager().getLocalNodeName(), ((OLocalPaginatedStorage) getUnderlying()).getLSN(), true);
} catch (IOException e) {
ODistributedServerLog.debug(this, dManager != null ? dManager.getLocalNodeName() : "?", null, ODistributedServerLog.DIRECTION.NONE, "Error on updating local LSN configuration for database '%s'", wrapped.getName());
}
}
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class ODistributedTransactionManager method getInvolvedClusters.
protected Set<String> getInvolvedClusters(final List<ORecordOperation> uResult) {
final Set<String> involvedClusters = new HashSet<String>();
for (ORecordOperation op : uResult) {
final ORecord record = op.getRecord();
involvedClusters.add(storage.getClusterNameByRID((ORecordId) record.getIdentity()));
}
return involvedClusters;
}
use of com.orientechnologies.orient.core.id.ORecordId 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