Search in sources :

Example 6 with OLiveResultListener

use of com.orientechnologies.orient.core.sql.query.OLiveResultListener 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();
        }
    }
}
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)

Example 7 with OLiveResultListener

use of com.orientechnologies.orient.core.sql.query.OLiveResultListener 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

OLiveResultListener (com.orientechnologies.orient.core.sql.query.OLiveResultListener)7 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)5 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OLiveQuery (com.orientechnologies.orient.core.sql.query.OLiveQuery)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Test (org.testng.annotations.Test)2 OException (com.orientechnologies.common.exception.OException)1 OIOException (com.orientechnologies.common.io.OIOException)1 OCallable (com.orientechnologies.common.util.OCallable)1 OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)1 OCommandRequestAsynch (com.orientechnologies.orient.core.command.OCommandRequestAsynch)1 OCommandResultListener (com.orientechnologies.orient.core.command.OCommandResultListener)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1