Search in sources :

Example 26 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.

the class LiveQueryTest method checkLiveQuery1.

@Test(enabled = false)
public void checkLiveQuery1() throws IOException, InterruptedException {
    final String className1 = "LiveQueryTest1_1";
    final String className2 = "LiveQueryTest1_2";
    database.getMetadata().getSchema().createClass(className1);
    database.getMetadata().getSchema().createClass(className2);
    MyLiveQueryListener listener = new MyLiveQueryListener();
    OResultSet<ODocument> tokens = database.query(new OLiveQuery<ODocument>("live select from " + className1, listener));
    Assert.assertEquals(tokens.size(), 1);
    ODocument tokenDoc = tokens.get(0);
    int token = tokenDoc.field("token");
    Assert.assertNotNull(token);
    database.command(new OCommandSQL("insert into " + className1 + " set name = 'foo', surname = 'bar'")).execute();
    database.command(new OCommandSQL("insert into  " + className1 + " set name = 'foo', surname = 'baz'")).execute();
    database.command(new OCommandSQL("insert into " + className2 + " set name = 'foo'"));
    latch.await(1, TimeUnit.MINUTES);
    database.command(new OCommandSQL("live unsubscribe " + token)).execute();
    database.command(new OCommandSQL("insert into " + className1 + " set name = 'foo', surname = 'bax'")).execute();
    Assert.assertEquals(listener.ops.size(), 2);
    for (ORecordOperation doc : listener.ops) {
        Assert.assertEquals(doc.type, ORecordOperation.CREATED);
        Assert.assertEquals(((ODocument) doc.record).field("name"), "foo");
    }
    unLatch.await(1, TimeUnit.MINUTES);
    Assert.assertEquals(listener.unsubscribe, token);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 27 with ORecordOperation

use of com.orientechnologies.orient.core.db.record.ORecordOperation in project orientdb by orientechnologies.

the class SQLLiveSelectTest method liveQueryTestTX.

@Test
public void liveQueryTestTX() throws InterruptedException {
    int TOTAL_OPS = 6;
    final CountDownLatch latch = new CountDownLatch(TOTAL_OPS);
    final List<ORecordOperation> ops = Collections.synchronizedList(new ArrayList());
    OResultSet<ODocument> tokens = database.query(new OLiveQuery<Object>("live select from LiveClassTx", 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.begin();
    database.command(new OCommandSQL("insert into LiveClassTx set name = 'foo', surname = 'bar'")).execute();
    database.command(new OCommandSQL("insert into LiveClassTx set name = 'foo', surname = 'baz'")).execute();
    database.command(new OCommandSQL("insert into LiveClassTx set name = 'foo'")).execute();
    database.commit();
    database.begin();
    database.command(new OCommandSQL("update LiveClassTx set name = 'updated'")).execute();
    database.commit();
    latch.await();
    Assert.assertEquals(ops.size(), TOTAL_OPS);
    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();
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)27 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)13 ORecordId (com.orientechnologies.orient.core.id.ORecordId)11 ORecord (com.orientechnologies.orient.core.record.ORecord)11 OException (com.orientechnologies.common.exception.OException)7 IOException (java.io.IOException)7 OLiveResultListener (com.orientechnologies.orient.core.sql.query.OLiveResultListener)5 Test (org.testng.annotations.Test)5 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)4 ORID (com.orientechnologies.orient.core.id.ORID)4 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)3 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)2