Search in sources :

Example 1 with OCommandCache

use of com.orientechnologies.orient.core.cache.OCommandCache 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)

Aggregations

OIOException (com.orientechnologies.common.io.OIOException)1 OCommandCache (com.orientechnologies.orient.core.cache.OCommandCache)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 ORecordSerializer (com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)1 OSQLAsynchQuery (com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 IOException (java.io.IOException)1