Search in sources :

Example 1 with OCommandRequestText

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

the class OClientConnectionManager method shutdown.

public void shutdown() {
    timerTask.cancel();
    List<ONetworkProtocol> toWait = new ArrayList<ONetworkProtocol>();
    final Iterator<Entry<Integer, OClientConnection>> iterator = connections.entrySet().iterator();
    while (iterator.hasNext()) {
        final Entry<Integer, OClientConnection> entry = iterator.next();
        final ONetworkProtocol protocol = entry.getValue().getProtocol();
        if (protocol != null)
            protocol.sendShutdown();
        OLogManager.instance().debug(this, "Sending shutdown to thread %s", protocol);
        OCommandRequestText command = entry.getValue().getData().command;
        if (command != null && command.isIdempotent()) {
            protocol.interrupt();
        } else {
            if (protocol instanceof ONetworkProtocolBinary && ((ONetworkProtocolBinary) protocol).getRequestType() == OChannelBinaryProtocol.REQUEST_SHUTDOWN) {
                continue;
            }
            final Socket socket;
            if (protocol == null || protocol.getChannel() == null)
                socket = null;
            else
                socket = protocol.getChannel().socket;
            if (socket != null && !socket.isClosed() && !socket.isInputShutdown()) {
                try {
                    OLogManager.instance().debug(this, "Closing input socket of thread %s", protocol);
                    if (// An SSLSocket will throw an UnsupportedOperationException.
                    !(socket instanceof SSLSocket))
                        socket.shutdownInput();
                } catch (IOException e) {
                    OLogManager.instance().debug(this, "Error on closing connection of %s client during shutdown", e, entry.getValue().getRemoteAddress());
                }
            }
            if (protocol.isAlive()) {
                if (protocol instanceof ONetworkProtocolBinary && ((ONetworkProtocolBinary) protocol).getRequestType() == -1) {
                    try {
                        OLogManager.instance().debug(this, "Closing socket of thread %s", protocol);
                        protocol.getChannel().close();
                    } catch (Exception e) {
                        OLogManager.instance().debug(this, "Error during chanel close at shutdown", e);
                    }
                    OLogManager.instance().debug(this, "Sending interrupt signal to thread %s", protocol);
                    protocol.interrupt();
                }
                toWait.add(protocol);
            }
        }
    }
    for (ONetworkProtocol protocol : toWait) {
        try {
            protocol.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Entry(java.util.Map.Entry) ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) ONetworkProtocol(com.orientechnologies.orient.server.network.protocol.ONetworkProtocol) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 2 with OCommandRequestText

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

the class OCommandExecutorSQLGrant method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLGrant parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        queryText = preParse(queryText, iRequest);
        textRequest.setText(queryText);
        init((OCommandRequestText) iRequest);
        privilege = ORole.PERMISSION_NONE;
        resource = null;
        role = null;
        StringBuilder word = new StringBuilder();
        int oldPos = 0;
        int pos = nextWord(parserText, parserTextUpperCase, oldPos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_GRANT))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_GRANT + " not found. Use " + getSyntax(), parserText, oldPos);
        pos = nextWord(parserText, parserTextUpperCase, pos, word, true);
        if (pos == -1)
            throw new OCommandSQLParsingException("Invalid privilege", parserText, oldPos);
        parsePrivilege(word, oldPos);
        pos = nextWord(parserText, parserTextUpperCase, pos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_ON))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_ON + " not found. Use " + getSyntax(), parserText, oldPos);
        pos = nextWord(parserText, parserText, pos, word, true);
        if (pos == -1)
            throw new OCommandSQLParsingException("Invalid resource", parserText, oldPos);
        resource = word.toString();
        pos = nextWord(parserText, parserTextUpperCase, pos, word, true);
        if (pos == -1 || !word.toString().equals(KEYWORD_TO))
            throw new OCommandSQLParsingException("Keyword " + KEYWORD_TO + " not found. Use " + getSyntax(), parserText, oldPos);
        pos = nextWord(parserText, parserText, pos, word, true);
        if (pos == -1)
            throw new OCommandSQLParsingException("Invalid role", parserText, oldPos);
        final String roleName = word.toString();
        role = getDatabase().getMetadata().getSecurity().getRole(roleName);
        if (role == null)
            throw new OCommandSQLParsingException("Invalid role: " + roleName);
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText)

Example 3 with OCommandRequestText

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

the class OCommandExecutorSQLLiveSelect method parse.

@Override
public OCommandExecutorSQLSelect parse(final OCommandRequest iRequest) {
    final OCommandRequestText requestText = (OCommandRequestText) iRequest;
    final String originalText = requestText.getText();
    final String remainingText = requestText.getText().trim().substring(5).trim();
    requestText.setText(remainingText);
    try {
        return super.parse(iRequest);
    } finally {
        requestText.setText(originalText);
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText)

Example 4 with OCommandRequestText

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

the class OCommandExecutorSQLTraverse method parse.

/**
 * Compile the filter conditions only the first time.
 */
public OCommandExecutorSQLTraverse 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);
        super.parse(iRequest);
        final int pos = parseFields();
        if (pos == -1)
            throw new OCommandSQLParsingException("Traverse must have the field list. Use " + getSyntax());
        parserSetCurrentPosition(pos);
        int endPosition = parserText.length();
        parsedTarget = OSQLEngine.getInstance().parseTarget(parserText.substring(pos, endPosition), getContext());
        if (parsedTarget.parserIsEnded())
            parserSetCurrentPosition(endPosition);
        else
            parserMoveCurrentPosition(parsedTarget.parserGetCurrentPosition());
        if (!parserIsEnded()) {
            parserNextWord(true);
            if (parserGetLastWord().equalsIgnoreCase(KEYWORD_WHERE))
                // // TODO Remove the additional management of WHERE for TRAVERSE after a while
                warnDeprecatedWhere();
            if (parserGetLastWord().equalsIgnoreCase(KEYWORD_WHERE) || parserGetLastWord().equalsIgnoreCase(KEYWORD_WHILE)) {
                compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition(), endPosition), getContext(), KEYWORD_WHILE);
                traverse.predicate(compiledFilter);
                optimize();
                parserSetCurrentPosition(compiledFilter.parserIsEnded() ? endPosition : compiledFilter.parserGetCurrentPosition() + parserGetCurrentPosition());
            } else
                parserGoBack();
        }
        parserSkipWhiteSpaces();
        while (!parserIsEnded()) {
            if (parserOptionalKeyword(KEYWORD_LIMIT, KEYWORD_SKIP, KEYWORD_OFFSET, KEYWORD_TIMEOUT, KEYWORD_MAXDEPTH, KEYWORD_STRATEGY)) {
                final String w = parserGetLastWord();
                if (w.equals(KEYWORD_LIMIT))
                    parseLimit(w);
                else if (w.equals(KEYWORD_SKIP) || w.equals(KEYWORD_OFFSET))
                    parseSkip(w);
                else if (w.equals(KEYWORD_TIMEOUT))
                    parseTimeout(w);
                else if (w.equals(KEYWORD_MAXDEPTH))
                    parseMaxDepth(w);
                else if (w.equals(KEYWORD_STRATEGY))
                    parseStrategy(w);
            }
        }
        if (limit == 0 || limit < -1)
            throw new IllegalArgumentException("Limit must be > 0 or = -1 (no limit)");
        else
            traverse.limit(limit);
        traverse.getContext().setParent(iRequest.getContext());
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText)

Example 5 with OCommandRequestText

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

the class OCommandExecutorSQLUpdate method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLUpdate parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
        queryText = preParse(queryText, iRequest);
        if (isUpdateEdge()) {
            // work-around to use UPDATE syntax without having to
            queryText = queryText.replaceFirst("EDGE ", "");
        }
        textRequest.setText(queryText);
        final ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        setEntries.clear();
        addEntries.clear();
        putEntries.clear();
        removeEntries.clear();
        incrementEntries.clear();
        content = null;
        merge = null;
        query = null;
        parserRequiredKeyword(KEYWORD_UPDATE);
        subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
        if (subjectName == null) {
            throwSyntaxErrorException("Invalid subject name. Expected cluster, class, index or sub-query");
        }
        if (subjectName.equalsIgnoreCase("EDGE")) {
            updateEdge = true;
            subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
        }
        clazz = extractClassFromTarget(subjectName);
        String word = parserNextWord(true);
        if (parserIsEnded() || (!word.equals(KEYWORD_SET) && !word.equals(KEYWORD_ADD) && !word.equals(KEYWORD_PUT) && !word.equals(KEYWORD_REMOVE) && !word.equals(KEYWORD_INCREMENT) && !word.equals(KEYWORD_CONTENT) && !word.equals(KEYWORD_MERGE) && !word.equals(KEYWORD_LOCK) && !word.equals(KEYWORD_RETURN) && !word.equals(KEYWORD_UPSERT) && !word.equals(KEYWORD_EDGE)))
            throwSyntaxErrorException("Expected keyword " + KEYWORD_SET + "," + KEYWORD_ADD + "," + KEYWORD_CONTENT + "," + KEYWORD_MERGE + "," + KEYWORD_PUT + "," + KEYWORD_REMOVE + "," + KEYWORD_INCREMENT + "," + KEYWORD_LOCK + " or " + KEYWORD_RETURN + " or " + KEYWORD_UPSERT + " or " + KEYWORD_EDGE);
        while ((!parserIsEnded() && !parserGetLastWord().equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE)) || parserGetLastWord().equals(KEYWORD_UPSERT)) {
            word = parserGetLastWord();
            if (word.equals(KEYWORD_CONTENT))
                parseContent();
            else if (word.equals(KEYWORD_MERGE))
                parseMerge();
            else if (word.equals(KEYWORD_SET))
                parseSetFields(clazz, setEntries);
            else if (word.equals(KEYWORD_ADD))
                parseAddFields(clazz);
            else if (word.equals(KEYWORD_PUT))
                parsePutFields();
            else if (word.equals(KEYWORD_REMOVE))
                parseRemoveFields();
            else if (word.equals(KEYWORD_INCREMENT))
                parseIncrementFields();
            else if (word.equals(KEYWORD_LOCK))
                lockStrategy = parseLock();
            else if (word.equals(KEYWORD_UPSERT))
                upsertMode = true;
            else if (word.equals(KEYWORD_RETURN))
                parseReturn();
            else if (word.equals(KEYWORD_RETRY)) {
                OLogManager.instance().warn(this, "RETRY keyword will be ignored in " + originalQuery);
                parseRetry();
            } else
                break;
            parserNextWord(true);
        }
        final String additionalStatement = parserGetLastWord();
        if (subjectName.startsWith("(")) {
            subjectName = subjectName.trim();
            query = database.command(new OSQLAsynchQuery<ODocument>(subjectName.substring(1, subjectName.length() - 1), this).setContext(context));
            if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT))
                compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(), KEYWORD_WHERE);
        } else if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT) || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LET) || additionalStatement.equals(KEYWORD_LOCK)) {
            if (this.preParsedStatement != null) {
                Map<Object, Object> params = ((OCommandRequestText) iRequest).getParameters();
                OUpdateStatement updateStm = (OUpdateStatement) preParsedStatement;
                StringBuilder selectString = new StringBuilder();
                selectString.append("select from ");
                updateStm.target.toString(params, selectString);
                if (updateStm.let != null) {
                    selectString.append(" ");
                    updateStm.let.toString(params, selectString);
                }
                if (updateStm.whereClause != null) {
                    selectString.append(" WHERE ");
                    updateStm.whereClause.toString(params, selectString);
                }
                if (updateStm.limit != null) {
                    selectString.append(" ");
                    updateStm.limit.toString(params, selectString);
                }
                if (updateStm.timeout != null) {
                    selectString.append(" ");
                    updateStm.timeout.toString(params, selectString);
                }
                if (updateStm.lockRecord != null) {
                    selectString.append(" LOCK ");
                    switch(updateStm.lockRecord) {
                        case DEFAULT:
                            selectString.append("DEFAULT");
                            break;
                        case EXCLUSIVE_LOCK:
                            selectString.append("RECORD");
                            break;
                        case SHARED_LOCK:
                            selectString.append("SHARED");
                            break;
                        case NONE:
                            selectString.append("NONE");
                            break;
                    }
                }
                query = new OSQLAsynchQuery<ODocument>(selectString.toString(), this);
            } else {
                query = new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget() + " " + additionalStatement + " " + parserText.substring(parserGetCurrentPosition()), this);
            }
            isUpsertAllowed = (((OMetadataInternal) getDatabase().getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName) != null);
        } else if (!additionalStatement.isEmpty())
            throwSyntaxErrorException("Invalid keyword " + additionalStatement);
        else
            query = new OSQLAsynchQuery<ODocument>("select from " + getSelectTarget(), this);
        if (upsertMode && !isUpsertAllowed)
            throwSyntaxErrorException("Upsert only works with class names ");
        if (upsertMode && !additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE))
            throwSyntaxErrorException("Upsert only works with WHERE keyword");
        if (upsertMode && updateEdge)
            throwSyntaxErrorException("Upsert is not supported with UPDATE EDGE");
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OUpdateStatement(com.orientechnologies.orient.core.sql.parser.OUpdateStatement)

Aggregations

OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)44 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)12 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)9 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)6 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)4 OSQLAsynchQuery (com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)4 OException (com.orientechnologies.common.exception.OException)3 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)3 OCommandExecutor (com.orientechnologies.orient.core.command.OCommandExecutor)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)2 OIdentifier (com.orientechnologies.orient.core.sql.parser.OIdentifier)2 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)2 IOException (java.io.IOException)2 Socket (java.net.Socket)2 Entry (java.util.Map.Entry)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2