Search in sources :

Example 6 with OSQLAsynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery in project orientdb by orientechnologies.

the class OCommandExecutorToOStatementWrapper method parse.

@SuppressWarnings("unchecked")
@Override
public OCommandExecutorToOStatementWrapper parse(OCommandRequest iCommand) {
    final OCommandRequestText textRequest = (OCommandRequestText) iCommand;
    if (iCommand instanceof OSQLAsynchQuery) {
        request = (OSQLAsynchQuery<ODocument>) iCommand;
    } else {
        // BUILD A QUERY OBJECT FROM THE COMMAND REQUEST
        request = new OSQLSynchQuery<ODocument>(textRequest.getText());
        if (textRequest.getResultListener() != null) {
            request.setResultListener(textRequest.getResultListener());
        }
    }
    String queryText = textRequest.getText();
    statement = OStatementCache.get(queryText, getDatabase());
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 7 with OSQLAsynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery in project orientdb by orientechnologies.

the class ONetworkProtocolBinary method command.

protected void command(OClientConnection connection) throws IOException {
    setDataCommandInfo(connection, "Execute remote command");
    byte type = channel.readByte();
    final boolean live = type == 'l';
    final boolean asynch = type == 'a';
    if (connection == null && connection.getDatabase() == null)
        throw new IOException("Found invalid session");
    String dbSerializerName = connection.getDatabase().getSerializer().toString();
    String name = getRecordSerializerName(connection);
    if (!dbSerializerName.equals(name)) {
        ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
        ONetworkThreadLocalSerializer.setNetworkSerializer(ser);
    }
    OCommandRequestText command = (OCommandRequestText) OStreamSerializerAnyStreamable.INSTANCE.fromStream(channel.readBytes());
    ONetworkThreadLocalSerializer.setNetworkSerializer(null);
    final Map<Object, Object> params = command.getParameters();
    if (asynch && command instanceof OSQLSynchQuery) {
        // CONVERT IT IN ASYNCHRONOUS QUERY
        final OSQLAsynchQuery asynchQuery = new OSQLAsynchQuery(command.getText());
        asynchQuery.setFetchPlan(command.getFetchPlan());
        asynchQuery.setLimit(command.getLimit());
        asynchQuery.setTimeout(command.getTimeoutTime(), command.getTimeoutStrategy());
        asynchQuery.setUseCache(((OSQLSynchQuery) command).isUseCache());
        command = asynchQuery;
    }
    connection.getData().commandDetail = command.getText();
    beginResponse();
    try {
        connection.getData().command = command;
        OAbstractCommandResultListener listener = null;
        OLiveCommandResultListener liveListener = null;
        OCommandResultListener cmdResultListener = command.getResultListener();
        if (live) {
            liveListener = new OLiveCommandResultListener(server, connection, clientTxId, cmdResultListener);
            listener = new OSyncCommandResultListener(null);
            command.setResultListener(liveListener);
        } else if (asynch) {
            // IF COMMAND CACHE IS ENABLED, RESULT MUST BE COLLECTED
            final OCommandCache cmdCache = connection.getDatabase().getMetadata().getCommandCache();
            if (cmdCache.isEnabled())
                // CREATE E COLLECTOR OF RESULT IN RAM TO CACHE THE RESULT
                cmdResultListener = new OCommandCacheRemoteResultListener(cmdResultListener, cmdCache);
            listener = new OAsyncCommandResultListener(connection, this, clientTxId, cmdResultListener);
            command.setResultListener(listener);
        } else {
            listener = new OSyncCommandResultListener(null);
        }
        final long serverTimeout = OGlobalConfiguration.COMMAND_TIMEOUT.getValueAsLong();
        if (serverTimeout > 0 && command.getTimeoutTime() > serverTimeout)
            // FORCE THE SERVER'S TIMEOUT
            command.setTimeout(serverTimeout, command.getTimeoutStrategy());
        if (!isConnectionAlive(connection))
            return;
        // REQUEST CAN'T MODIFY THE RESULT, SO IT'S CACHEABLE
        command.setCacheableResult(true);
        // ASSIGNED THE PARSED FETCHPLAN
        final OCommandRequest commandImpl = connection.getDatabase().command(command);
        listener.setFetchPlan(commandImpl.getFetchPlan());
        final Object result;
        if (params == null)
            result = commandImpl.execute();
        else
            result = commandImpl.execute(params);
        // FETCHPLAN HAS TO BE ASSIGNED AGAIN, because it can be changed by SQL statement
        listener.setFetchPlan(commandImpl.getFetchPlan());
        if (asynch) {
            // ASYNCHRONOUS
            if (listener.isEmpty())
                try {
                    sendOk(connection, clientTxId);
                } catch (IOException ignored) {
                }
            // NO MORE RECORDS
            channel.writeByte((byte) 0);
        } else {
            // SYNCHRONOUS
            sendOk(connection, clientTxId);
            boolean isRecordResultSet = true;
            if (command instanceof OCommandRequestInternal)
                isRecordResultSet = command.isRecordResultSet();
            serializeValue(connection, listener, result, false, isRecordResultSet);
            if (listener instanceof OSyncCommandResultListener) {
                // SEND FETCHED RECORDS TO LOAD IN CLIENT CACHE
                for (ORecord rec : ((OSyncCommandResultListener) listener).getFetchedRecordsToSend()) {
                    // CLIENT CACHE RECORD. IT
                    channel.writeByte((byte) 2);
                    // ISN'T PART OF THE
                    // RESULT SET
                    writeIdentifiable(connection, rec);
                }
                // NO MORE RECORDS
                channel.writeByte((byte) 0);
            }
        }
    } finally {
        connection.getData().command = null;
        endResponse(connection);
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OCommandCache(com.orientechnologies.orient.core.cache.OCommandCache) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)

Example 8 with OSQLAsynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery in project orientdb by orientechnologies.

the class OCommunityEditionSingleNodeIT method testSearchOnField.

@Test
public void testSearchOnField() throws Exception {
    List<?> result = db.query(new OSQLAsynchQuery("SELECT from ArchaeologicalSites where Name LUCENE 'foro'"));
    assertThat(result).hasSize(2);
}
Also used : OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) Test(org.junit.Test)

Example 9 with OSQLAsynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery in project orientdb by orientechnologies.

the class OCommandExecutorSQLDeleteVertex method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLDeleteVertex parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        // System.out.println("NEW PARSER FROM: " + queryText);
        queryText = preParse(queryText, iRequest);
        // System.out.println("NEW PARSER TO: " + queryText);
        textRequest.setText(queryText);
        database = getDatabase();
        init((OCommandRequestText) iRequest);
        parserRequiredKeyword("DELETE");
        parserRequiredKeyword("VERTEX");
        OClass clazz = null;
        String where = null;
        int limit = -1;
        String word = parseOptionalWord(true);
        while (word != null) {
            if (word.startsWith("#")) {
                rid = new ORecordId(word);
            } else if (word.equalsIgnoreCase("from")) {
                final StringBuilder q = new StringBuilder();
                final int newPos = OStringSerializerHelper.getEmbedded(parserText, parserGetCurrentPosition(), -1, q);
                query = database.command(new OSQLAsynchQuery<ODocument>(q.toString(), this));
                parserSetCurrentPosition(newPos);
            } else if (word.equals(KEYWORD_WHERE)) {
                if (clazz == null)
                    // ASSIGN DEFAULT CLASS
                    clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(OrientVertexType.CLASS_NAME);
                where = parserGetCurrentPosition() > -1 ? " " + parserText.substring(parserGetPreviousPosition()) : "";
                query = database.command(new OSQLAsynchQuery<ODocument>("select from `" + clazz.getName() + "`" + where, this));
                break;
            } else if (word.equals(KEYWORD_LIMIT)) {
                word = parseOptionalWord(true);
                try {
                    limit = Integer.parseInt(word);
                } catch (Exception e) {
                    throw OException.wrapException(new OCommandSQLParsingException("Invalid LIMIT: " + word), e);
                }
            } else if (word.equals(KEYWORD_RETURN)) {
                returning = parseReturn();
            } else if (word.equals(KEYWORD_BATCH)) {
                word = parserNextWord(true);
                if (word != null)
                    batch = Integer.parseInt(word);
            } else if (word.length() > 0) {
                // GET/CHECK CLASS NAME
                clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(word);
                if (clazz == null)
                    throw new OCommandSQLParsingException("Class '" + word + "' was not found");
            }
            word = parseOptionalWord(true);
            if (parserIsEnded())
                break;
        }
        if (where == null)
            where = "";
        else
            where = " WHERE " + where;
        if (query == null && rid == null) {
            StringBuilder queryString = new StringBuilder();
            queryString.append("select from `");
            if (clazz == null) {
                queryString.append(OrientVertexType.CLASS_NAME);
            } else {
                queryString.append(clazz.getName());
            }
            queryString.append("`");
            queryString.append(where);
            if (limit > -1) {
                queryString.append(" LIMIT ").append(limit);
            }
            query = database.command(new OSQLAsynchQuery<ODocument>(queryString.toString(), this));
        }
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandSQLParsingException(com.orientechnologies.orient.core.sql.OCommandSQLParsingException) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) OCommandSQLParsingException(com.orientechnologies.orient.core.sql.OCommandSQLParsingException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 10 with OSQLAsynchQuery

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

Aggregations

OSQLAsynchQuery (com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)12 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)8 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)4 OCommandResultListener (com.orientechnologies.orient.core.command.OCommandResultListener)4 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.testng.annotations.Test)2 OException (com.orientechnologies.common.exception.OException)1 OIOException (com.orientechnologies.common.io.OIOException)1 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)1 OCommandCache (com.orientechnologies.orient.core.cache.OCommandCache)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1