Search in sources :

Example 56 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OGraphCommandExecutorSQLFactory method runInConfiguredTxMode.

public static <T> T runInConfiguredTxMode(final GraphCallBack<T> callBack) {
    final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
    final ODatabaseDocumentInternal curDb = ODatabaseRecordThreadLocal.INSTANCE.get();
    final boolean txAlreadyBegun = curDb.getTransaction().isActive();
    OrientBaseGraph graph = null;
    try {
        if (isTxRequiredForSQLGraphOperations()) {
            graph = OGraphCommandExecutorSQLFactory.getGraph(true, shutdownFlag);
            return runInTx((OrientGraph) graph, callBack);
        } else {
            graph = getAnyGraph(shutdownFlag);
            return callBack.call(graph);
        }
    } finally {
        if (graph != null) {
            if (!txAlreadyBegun) {
                graph.commit();
                if (shutdownFlag.getValue())
                    graph.shutdown(false, false);
            }
        }
        ODatabaseRecordThreadLocal.INSTANCE.set(curDb);
    }
}
Also used : OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 57 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OGraphCommandExecutorSQLFactory method getAnyGraph.

/**
 * @return any graph if available, otherwise a Non Transactional OrientGraph implementation from the current database in thread
 *         local.
 */
public static OrientBaseGraph getAnyGraph(final OModifiableBoolean shouldBeShutDown) {
    final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
    if (result != null) {
        final ODatabaseDocument graphDb = result.getRawGraph();
        // CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
        if (canReuseActiveGraph(graphDb, database)) {
            if (!graphDb.isClosed()) {
                graphDb.activateOnCurrentThread();
                shouldBeShutDown.setValue(false);
                return result;
            }
        }
    }
    // Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
    shouldBeShutDown.setValue(true);
    ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
    return (OrientGraphNoTx) OrientGraphFactory.getNoTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 58 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OGraphCommandExecutorSQLFactory method runWithAnyGraph.

public static <T> T runWithAnyGraph(final GraphCallBack<T> callBack) {
    final OModifiableBoolean shutdownFlag = new OModifiableBoolean();
    final ODatabaseDocumentInternal curDb = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OrientBaseGraph graph = OGraphCommandExecutorSQLFactory.getAnyGraph(shutdownFlag);
    try {
        return callBack.call(graph);
    } finally {
        if (shutdownFlag.getValue())
            graph.shutdown(false, false);
        ODatabaseRecordThreadLocal.INSTANCE.set(curDb);
    }
}
Also used : OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 59 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OGraphCommandExecutorSQLFactory method getGraph.

/**
 * Returns a Transactional OrientGraph implementation from the current database in thread local.
 *
 * @param autoStartTx
 *          Whether returned graph will start transaction before each operation till commit automatically or user should do it
 *          explicitly be calling {@link OrientGraph#getRawGraph()} method {@link ODatabaseDocumentTx#begin()}.
 *
 * @return Transactional OrientGraph implementation from the current database in thread local.
 */
public static OrientGraph getGraph(final boolean autoStartTx, OModifiableBoolean shouldBeShutDown) {
    final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OrientBaseGraph result = OrientBaseGraph.getActiveGraph();
    if (result != null && (result instanceof OrientGraph)) {
        final ODatabaseDocumentTx graphDb = result.getRawGraph();
        // CHECK IF THE DATABASE + USER IN TL IS THE SAME IN ORDER TO USE IT
        if (canReuseActiveGraph(graphDb, database)) {
            if (!graphDb.isClosed()) {
                ODatabaseRecordThreadLocal.INSTANCE.set(graphDb);
                if (autoStartTx && autoTxStartRequired(graphDb))
                    ((OrientGraph) result).begin();
                shouldBeShutDown.setValue(false);
                return (OrientGraph) result;
            }
        }
    }
    // Set it again on ThreadLocal because the getRawGraph() may have set a closed db in the thread-local
    ODatabaseRecordThreadLocal.INSTANCE.set((ODatabaseDocumentInternal) database);
    shouldBeShutDown.setValue(true);
    final OrientGraph g = (OrientGraph) OrientGraphFactory.getTxGraphImplFactory().getGraph((ODatabaseDocumentTx) database, false);
    if (autoStartTx && autoTxStartRequired(database))
        g.begin();
    return g;
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Example 60 with OrientBaseGraph

use of com.tinkerpop.blueprints.impls.orient.OrientBaseGraph in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateEdge method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLCreateEdge 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);
        final ODatabaseDocument database = getDatabase();
        init((OCommandRequestText) iRequest);
        parserRequiredKeyword("CREATE");
        parserRequiredKeyword("EDGE");
        String className = null;
        String tempLower = parseOptionalWord(false);
        String temp = tempLower == null ? null : tempLower.toUpperCase(Locale.ENGLISH);
        while (temp != null) {
            if (temp.equals("CLUSTER")) {
                clusterName = parserRequiredWord(false);
            } else if (temp.equals(KEYWORD_FROM)) {
                from = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
            } else if (temp.equals("TO")) {
                to = parserRequiredWord(false, "Syntax error", " =><,\r\n", true);
            } else if (temp.equals(KEYWORD_SET)) {
                fields = new ArrayList<OPair<String, Object>>();
                parseSetFields(clazz, fields);
            } else if (temp.equals(KEYWORD_CONTENT)) {
                parseContent();
            } else if (temp.equals(KEYWORD_BATCH)) {
                temp = parserNextWord(true);
                if (temp != null)
                    batch = Integer.parseInt(temp);
            } else if (className == null && temp.length() > 0) {
                className = tempLower;
                OrientBaseGraph graph = OrientBaseGraph.getActiveGraph();
                if (graph != null && graph.isUseClassForEdgeLabel()) {
                    clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(temp);
                } else {
                    clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass("E");
                }
            }
            temp = parseOptionalWord(true);
            if (parserIsEnded())
                break;
        }
        if (className == null) {
            // ASSIGN DEFAULT CLASS
            className = OrientEdgeType.CLASS_NAME;
            clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(className);
        }
        // GET/CHECK CLASS NAME
        if (clazz == null)
            throw new OCommandSQLParsingException("Class '" + className + "' was not found");
        edgeLabel = className;
    } finally {
        textRequest.setText(originalQuery);
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)

Aggregations

OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)75 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)40 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)21 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)15 Vertex (com.tinkerpop.blueprints.Vertex)13 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)11 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)8 Edge (com.tinkerpop.blueprints.Edge)8 OrientGraphFactory (com.tinkerpop.blueprints.impls.orient.OrientGraphFactory)8 Test (org.junit.Test)8 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)6 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)6 OGraphRepair (com.tinkerpop.blueprints.impls.orient.OGraphRepair)5 ArrayList (java.util.ArrayList)5 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)4 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)4 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)4 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)4