use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.
the class OLiveQueryShotdownTest method testShutDown.
@Test
public void testShutDown() throws Exception {
bootServer();
ODatabaseDocument db = new ODatabaseDocumentTx("remote:localhost/" + OLiveQueryShotdownTest.class.getSimpleName());
db.open("admin", "admin");
db.getMetadata().getSchema().createClass("Test");
final CountDownLatch error = new CountDownLatch(1);
try {
db.command(new OLiveQuery("live select from Test", new OLiveResultListener() {
@Override
public void onUnsubscribe(int iLiveToken) {
}
@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
}
@Override
public void onError(int iLiveToken) {
error.countDown();
}
})).execute();
shutdownServer();
assertTrue("onError method never called on shutdow", error.await(2, TimeUnit.SECONDS));
} finally {
// db.close();
}
}
use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.
the class OLiveCommandResultListenerTest method testNetworkError.
@Test
public void testNetworkError() throws IOException {
Mockito.when(channelBinary.writeInt(Mockito.anyInt())).thenThrow(new IOException("Mock Exception"));
OLiveCommandResultListener listener = new OLiveCommandResultListener(server, connection, 20, new TestResultListener());
OLiveQueryHook.subscribe(10, rawListener, db);
assertTrue(OLiveQueryHook.getOpsReference(db).getQueueThread().hasToken(10));
ORecordOperation op = new ORecordOperation(new ODocument(), ORecordOperation.CREATED);
listener.onLiveResult(10, op);
assertFalse(OLiveQueryHook.getOpsReference(db).getQueueThread().hasToken(10));
}
use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.
the class SQLLiveSelectTest method liveQueryTest.
@Test
public void liveQueryTest() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(6);
final List<ORecordOperation> ops = Collections.synchronizedList(new ArrayList());
OResultSet<ODocument> tokens = database.query(new OLiveQuery<Object>("live select from LiveClass", new OLiveResultListener() {
@Override
public void onLiveResult(int iLiveToken, ORecordOperation iOp) throws OException {
ops.add(iOp);
latch.countDown();
}
@Override
public void onError(int iLiveToken) {
}
@Override
public void onUnsubscribe(int iLiveToken) {
}
}));
Assert.assertEquals(tokens.size(), 1);
ODocument tokenDoc = tokens.get(0);
Integer token = tokenDoc.field("token");
Assert.assertNotNull(token);
database.command(new OCommandSQL("insert into liveclass set name = 'foo', surname = 'bar'")).execute();
database.command(new OCommandSQL("insert into liveclass set name = 'foo', surname = 'baz'")).execute();
database.command(new OCommandSQL("insert into liveclass set name = 'foo'")).execute();
database.command(new OCommandSQL("update liveclass set name = 'updated'")).execute();
latch.await();
Assert.assertEquals(ops.size(), 6);
for (ORecordOperation doc : ops) {
if (doc.type == ORecordOperation.CREATED) {
Assert.assertEquals(((ODocument) doc.record).field("name"), "foo");
} else if (doc.type == ORecordOperation.UPDATED) {
Assert.assertEquals(((ODocument) doc.record).field("name"), "updated");
} else {
Assert.fail();
}
}
}
use of com.orientechnologies.orient.core.db.record.ORecordOperation 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.db.record.ORecordOperation 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