Search in sources :

Example 1 with OCommandExecutor

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

the class TransactionAtomicTest method testTransactionPreListenerRollback.

@Test
public void testTransactionPreListenerRollback() throws IOException {
    ODocument record1 = new ODocument();
    record1.field("value", "This is the first version").save();
    final ODatabaseListener listener = new ODatabaseListener() {

        @Override
        public void onAfterTxCommit(ODatabase iDatabase) {
        }

        @Override
        public void onAfterTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxBegin(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxCommit(ODatabase iDatabase) {
            throw new RuntimeException("Rollback test");
        }

        @Override
        public void onBeforeTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onClose(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeCommand(OCommandRequestText iCommand, OCommandExecutor executor) {
        }

        @Override
        public void onAfterCommand(OCommandRequestText iCommand, OCommandExecutor executor, Object result) {
        }

        @Override
        public void onCreate(ODatabase iDatabase) {
        }

        @Override
        public void onDelete(ODatabase iDatabase) {
        }

        @Override
        public void onOpen(ODatabase iDatabase) {
        }

        @Override
        public boolean onCorruptionRepairDatabase(ODatabase iDatabase, final String iReason, String iWhatWillbeFixed) {
            return true;
        }
    };
    database.registerListener(listener);
    database.begin();
    try {
        database.commit();
        Assert.assertTrue(false);
    } catch (OTransactionException e) {
        Assert.assertTrue(true);
    } finally {
        database.unregisterListener(listener);
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandExecutor(com.orientechnologies.orient.core.command.OCommandExecutor) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) ODatabaseListener(com.orientechnologies.orient.core.db.ODatabaseListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 2 with OCommandExecutor

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

the class OAbstractPaginatedStorage method command.

/**
   * Executes the command request and return the result back.
   */
public Object command(final OCommandRequestText iCommand) {
    while (true) {
        try {
            final OCommandExecutor executor = OCommandManager.instance().getExecutor(iCommand);
            // COPY THE CONTEXT FROM THE REQUEST
            executor.setContext(iCommand.getContext());
            executor.setProgressListener(iCommand.getProgressListener());
            executor.parse(iCommand);
            return executeCommand(iCommand, executor);
        } catch (ORetryQueryException e) {
            if (iCommand instanceof OQueryAbstract) {
                final OQueryAbstract query = (OQueryAbstract) iCommand;
                query.reset();
            }
            continue;
        }
    }
}
Also used : OCommandExecutor(com.orientechnologies.orient.core.command.OCommandExecutor) OQueryAbstract(com.orientechnologies.orient.core.query.OQueryAbstract)

Example 3 with OCommandExecutor

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

the class OServerCommandPostCommand method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: command/<database>/<language>/<command-text>[/limit][/<fetchPlan>]");
    // TRY TO GET THE COMMAND FROM THE URL, THEN FROM THE CONTENT
    final String language = urlParts.length > 2 ? urlParts[2].trim() : "sql";
    String text = urlParts.length > 3 ? urlParts[3].trim() : iRequest.content;
    final int limit = urlParts.length > 4 ? Integer.parseInt(urlParts[4].trim()) : -1;
    String fetchPlan = urlParts.length > 5 ? urlParts[5] : null;
    final String accept = iRequest.getHeader("accept");
    Object params = null;
    String mode = "resultset";
    if (iRequest.content != null && !iRequest.content.isEmpty()) {
        // CONTENT REPLACES TEXT
        if (iRequest.content.startsWith("{")) {
            // JSON PAYLOAD
            final ODocument doc = new ODocument().fromJSON(iRequest.content);
            text = doc.field("command");
            params = doc.field("parameters");
            if (doc.containsField("mode"))
                mode = doc.field("mode");
            if (params instanceof Collection) {
                final Object[] paramArray = new Object[((Collection) params).size()];
                ((Collection) params).toArray(paramArray);
                params = paramArray;
            }
        } else {
            text = iRequest.content;
        }
    }
    if (text == null)
        throw new IllegalArgumentException("text cannot be null");
    iRequest.data.commandInfo = "Command";
    iRequest.data.commandDetail = text;
    ODatabaseDocument db = null;
    Object response;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        final OCommandRequestText cmd = (OCommandRequestText) OCommandManager.instance().getRequester(language);
        cmd.setText(text);
        cmd.setLimit(limit);
        cmd.setFetchPlan(fetchPlan);
        final OCommandExecutor executor = OCommandManager.instance().getExecutor(cmd);
        executor.setContext(cmd.getContext());
        executor.setProgressListener(cmd.getProgressListener());
        executor.parse(cmd);
        if (!executor.isIdempotent() && iRequest.httpMethod.equals("GET"))
            throw new OCommandExecutionException("Cannot execute non idempotent command using HTTP GET");
        // REQUEST CAN'T MODIFY THE RESULT, SO IT'S CACHEABLE
        cmd.setCacheableResult(true);
        if (params == null) {
            response = db.command(cmd).execute();
        } else {
            response = db.command(cmd).execute(params);
        }
        fetchPlan = executor.getFetchPlan();
        String format = null;
        if (iRequest.parameters.get("format") != null)
            format = iRequest.parameters.get("format");
        if (fetchPlan != null)
            if (format != null)
                format += ",fetchPlan:" + fetchPlan;
            else
                format = "fetchPlan:" + fetchPlan;
        Map<String, Object> additionalContent = null;
        final List<String> tips = (List<String>) executor.getContext().getVariable("tips");
        if (tips != null) {
            additionalContent = new HashMap<String, Object>(1);
            additionalContent.put("warnings", tips);
        }
        if (iRequest.getHeader("TE") != null)
            iResponse.setStreaming(true);
        iResponse.writeResult(response, format, accept, additionalContent, mode);
    } finally {
        if (db != null) {
            db.activateOnCurrentThread();
            db.close();
        }
    }
    return false;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutor(com.orientechnologies.orient.core.command.OCommandExecutor) Collection(java.util.Collection) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OCommandExecutor (com.orientechnologies.orient.core.command.OCommandExecutor)3 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ODatabase (com.orientechnologies.orient.core.db.ODatabase)1 ODatabaseListener (com.orientechnologies.orient.core.db.ODatabaseListener)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 OQueryAbstract (com.orientechnologies.orient.core.query.OQueryAbstract)1 Collection (java.util.Collection)1 List (java.util.List)1 Test (org.testng.annotations.Test)1