Search in sources :

Example 81 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OCommandExecutorScript method executeJsr223Script.

protected Object executeJsr223Script(final String language, final OCommandContext iContext, final Map<Object, Object> iArgs) {
    ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OScriptManager scriptManager = Orient.instance().getScriptManager();
    CompiledScript compiledScript = request.getCompiledScript();
    final OPartitionedObjectPool.PoolEntry<ScriptEngine> entry = scriptManager.acquireDatabaseEngine(db.getName(), language);
    final ScriptEngine scriptEngine = entry.object;
    try {
        if (compiledScript == null) {
            if (!(scriptEngine instanceof Compilable))
                throw new OCommandExecutionException("Language '" + language + "' does not support compilation");
            final Compilable c = (Compilable) scriptEngine;
            try {
                compiledScript = c.compile(parserText);
            } catch (ScriptException e) {
                scriptManager.throwErrorMessage(e, parserText);
            }
            request.setCompiledScript(compiledScript);
        }
        final Bindings binding = scriptManager.bind(compiledScript.getEngine().getBindings(ScriptContext.ENGINE_SCOPE), (ODatabaseDocumentTx) db, iContext, iArgs);
        try {
            final Object ob = compiledScript.eval(binding);
            return OCommandExecutorUtility.transformResult(ob);
        } catch (ScriptException e) {
            throw OException.wrapException(new OCommandScriptException("Error on execution of the script", request.getText(), e.getColumnNumber()), e);
        } finally {
            scriptManager.unbind(binding, iContext, iArgs);
        }
    } finally {
        scriptManager.releaseDatabaseEngine(language, db.getName(), entry);
    }
}
Also used : OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OPartitionedObjectPool(com.orientechnologies.common.concur.resource.OPartitionedObjectPool)

Example 82 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OContentRecordConflictStrategy method onUpdate.

@Override
public byte[] onUpdate(OStorage storage, final byte iRecordType, final ORecordId rid, final int iRecordVersion, final byte[] iRecordContent, final AtomicInteger iDatabaseVersion) {
    final boolean hasSameContent;
    if (iRecordType == ODocument.RECORD_TYPE) {
        // No need lock, is already inside a lock.
        OStorageOperationResult<ORawBuffer> res = storage.readRecord(rid, null, false, false, null);
        final ODocument storedRecord = new ODocument(rid).fromStream(res.getResult().getBuffer());
        final ODocument newRecord = new ODocument().fromStream(iRecordContent);
        final ODatabaseDocumentInternal currentDb = ODatabaseRecordThreadLocal.INSTANCE.get();
        hasSameContent = ODocumentHelper.hasSameContentOf(storedRecord, currentDb, newRecord, currentDb, null, false);
    } else {
        // CHECK BYTE PER BYTE
        final ORecordAbstract storedRecord = rid.getRecord();
        hasSameContent = Arrays.equals(storedRecord.toStream(), iRecordContent);
    }
    if (hasSameContent)
        // OK
        iDatabaseVersion.set(Math.max(iDatabaseVersion.get(), iRecordVersion));
    else
        // NO DOCUMENT, CANNOT MERGE SO RELY TO THE VERSION CHECK
        checkVersions(rid, iRecordVersion, iDatabaseVersion.get());
    return null;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 83 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OGraphCommandExecutorSQLFactory method runInTx.

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

Example 84 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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 85 with ODatabaseDocumentInternal

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

Aggregations

ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)139 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)42 OStorage (com.orientechnologies.orient.core.storage.OStorage)31 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)26 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)20 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)18 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)17 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)16 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 ORID (com.orientechnologies.orient.core.id.ORID)13 IOException (java.io.IOException)13 OException (com.orientechnologies.common.exception.OException)12 ORecordId (com.orientechnologies.orient.core.id.ORecordId)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)10 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)10 ORecord (com.orientechnologies.orient.core.record.ORecord)7 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)5 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)5