Search in sources :

Example 6 with OCommandResultListener

use of com.orientechnologies.orient.core.command.OCommandResultListener in project orientdb by orientechnologies.

the class SQLSelectTest method queryAsynch.

@Test
public void queryAsynch() {
    final String sqlOne = "select * from company where id between 4 and 7";
    final String sqlTwo = "select $names let $names = (select EXPAND( addresses.city ) as city from Account where addresses.size() > 0 )";
    final List<ODocument> synchResultOne = database.command(new OSQLSynchQuery<ODocument>(sqlOne)).execute();
    final List<ODocument> synchResultTwo = database.command(new OSQLSynchQuery<ODocument>(sqlTwo)).execute();
    Assert.assertTrue(synchResultOne.size() > 0);
    Assert.assertTrue(synchResultTwo.size() > 0);
    final List<ODocument> asynchResultOne = new ArrayList<ODocument>();
    final List<ODocument> asynchResultTwo = new ArrayList<ODocument>();
    final AtomicBoolean endOneCalled = new AtomicBoolean();
    final AtomicBoolean endTwoCalled = new AtomicBoolean();
    database.command(new OSQLAsynchQuery<ODocument>(sqlOne, new OCommandResultListener() {

        @Override
        public boolean result(Object iRecord) {
            asynchResultOne.add((ODocument) iRecord);
            return true;
        }

        @Override
        public void end() {
            endOneCalled.set(true);
            database.command(new OSQLAsynchQuery<ODocument>(sqlTwo, new OCommandResultListener() {

                @Override
                public boolean result(Object iRecord) {
                    asynchResultTwo.add((ODocument) iRecord);
                    return true;
                }

                @Override
                public void end() {
                    endTwoCalled.set(true);
                }

                @Override
                public Object getResult() {
                    return null;
                }
            })).execute();
        }

        @Override
        public Object getResult() {
            return null;
        }
    })).execute();
    Assert.assertTrue(endOneCalled.get());
    Assert.assertTrue(endTwoCalled.get());
    Assert.assertTrue(ODocumentHelper.compareCollections(database, synchResultTwo, database, asynchResultTwo, null), "synchResultTwo=" + synchResultTwo.size() + " asynchResultTwo=" + asynchResultTwo.size());
    Assert.assertTrue(ODocumentHelper.compareCollections(database, synchResultOne, database, asynchResultOne, null), "synchResultOne=" + synchResultOne.size() + " asynchResultOne=" + asynchResultOne.size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 7 with OCommandResultListener

use of com.orientechnologies.orient.core.command.OCommandResultListener in project orientdb by orientechnologies.

the class SQLAsynchQuerySpeedTest method cycle.

@Override
@SuppressWarnings("unchecked")
public void cycle() throws UnsupportedEncodingException {
    System.out.println("1 -----------------------");
    OrientTest.printRecords((List<? extends ORecord>) database.command(new OSQLAsynchQuery<ODocument>("select * from animal where column(0) < 5 or column(0) >= 3 and column(5) < 7", new OCommandResultListener() {

        @Override
        public boolean result(Object iRecord) {
            OrientTest.printRecord(resultCount++, iRecord);
            return true;
        }

        @Override
        public void end() {
        }

        @Override
        public Object getResult() {
            return null;
        }
    })).execute());
}
Also used : OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)

Example 8 with OCommandResultListener

use of com.orientechnologies.orient.core.command.OCommandResultListener in project orientdb by orientechnologies.

the class OClassImpl method fireDatabaseMigration.

public void fireDatabaseMigration(final ODatabaseDocument database, final String propertyName, final OType type) {
    final boolean strictSQL = ((ODatabaseInternal) database).getStorage().getConfiguration().isStrictSql();
    database.query(new OSQLAsynchQuery<Object>("select from " + getEscapedName(name, strictSQL) + " where " + getEscapedName(propertyName, strictSQL) + ".type() <> \"" + type.name() + "\"", new OCommandResultListener() {

        @Override
        public boolean result(Object iRecord) {
            final ODocument record = ((OIdentifiable) iRecord).getRecord();
            record.field(propertyName, record.field(propertyName), type);
            database.save(record);
            return true;
        }

        @Override
        public void end() {
        }

        @Override
        public Object getResult() {
            return null;
        }
    }));
}
Also used : ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with OCommandResultListener

use of com.orientechnologies.orient.core.command.OCommandResultListener in project orientdb by orientechnologies.

the class GraphNonBlockingQueryRemote method testNonBlockingClose.

@Test
public void testNonBlockingClose() throws ExecutionException, InterruptedException {
    OrientGraph database = new OrientGraph("remote:localhost:3064/" + GraphNonBlockingQueryRemote.class.getSimpleName());
    database.createVertexType("Prod").createProperty("something", OType.STRING);
    for (int i = 0; i < 21; i++) {
        OrientVertex vertex = database.addVertex("class:Prod");
        vertex.setProperty("something", "value");
        vertex.save();
    }
    database.commit();
    final CountDownLatch ended = new CountDownLatch(21);
    try {
        OSQLNonBlockingQuery<Object> test = new OSQLNonBlockingQuery<Object>("select * from Prod ", new OCommandResultListener() {

            int resultCount = 0;

            @Override
            public boolean result(Object iRecord) {
                resultCount++;
                ODocument odoc = ((ODocument) iRecord);
                for (String name : odoc.fieldNames()) {
                    // <----------- PROBLEM
                    assertEquals("something", name);
                }
                ended.countDown();
                return resultCount > 20 ? false : true;
            }

            @Override
            public void end() {
                ended.countDown();
            }

            @Override
            public Object getResult() {
                return resultCount;
            }
        });
        database.command(test).execute();
        assertTrue(ended.await(10, TimeUnit.SECONDS));
    } finally {
        database.shutdown();
    }
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OSQLNonBlockingQuery(com.orientechnologies.orient.core.sql.query.OSQLNonBlockingQuery) OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) CountDownLatch(java.util.concurrent.CountDownLatch) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OrientGraphRemoteTest(com.tinkerpop.blueprints.impls.orient.OrientGraphRemoteTest) Test(org.junit.Test)

Example 10 with OCommandResultListener

use of com.orientechnologies.orient.core.command.OCommandResultListener in project orientdb by orientechnologies.

the class AbstractSelectTest method executeQuery.

protected List<ODocument> executeQuery(String sql, ODatabaseDocumentInternal db, Object... args) {
    final List<ODocument> synchResult = db.query(new OSQLSynchQuery<ODocument>(sql), args);
    final List<ODocument> asynchResult = new ArrayList<ODocument>();
    final AtomicBoolean endWasCalled = new AtomicBoolean();
    db.query(new OSQLAsynchQuery<ODocument>(sql, new OCommandResultListener() {

        @Override
        public boolean result(Object iRecord) {
            asynchResult.add((ODocument) iRecord);
            return true;
        }

        @Override
        public void end() {
            endWasCalled.set(true);
        }

        @Override
        public Object getResult() {
            return null;
        }
    }), args);
    Assert.assertTrue(endWasCalled.get());
    Assert.assertTrue(ODocumentHelper.compareCollections(db, synchResult, db, asynchResult, null), "Synch: " + synchResult.toString() + ", but asynch: " + asynchResult.toString());
    return synchResult;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OCommandResultListener (com.orientechnologies.orient.core.command.OCommandResultListener)10 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 Test (org.testng.annotations.Test)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 OSQLAsynchQuery (com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ODatabaseInternal (com.orientechnologies.orient.core.db.ODatabaseInternal)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 OCallable (com.orientechnologies.common.util.OCallable)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OLiveResultListener (com.orientechnologies.orient.core.sql.query.OLiveResultListener)1 OSQLNonBlockingQuery (com.orientechnologies.orient.core.sql.query.OSQLNonBlockingQuery)1 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)1 OrientGraphRemoteTest (com.tinkerpop.blueprints.impls.orient.OrientGraphRemoteTest)1 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)1