Search in sources :

Example 11 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class OrientGraphHookCall method testHook.

@Test
public void testHook() {
    OrientBaseGraph graph = new OrientGraph("memory:" + OrientGraphHookCall.class.getSimpleName());
    graph.getRawGraph().registerHook(new ORecordHook() {

        @Override
        public void onUnregister() {
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            switch(iType) {
                case AFTER_CREATE:
                    {
                        if (((ODocument) iRecord).getSchemaClass().isSubClassOf(OrientEdgeType.CLASS_NAME)) {
                            edgeCreatedCnt++;
                        } else {
                            vertexCreatedCnt++;
                        }
                        break;
                    }
                case AFTER_UPDATE:
                    {
                        if (((ODocument) iRecord).getSchemaClass().isSubClassOf(OrientEdgeType.CLASS_NAME)) {
                            edgeUpdatedCnt++;
                        } else {
                            vertexUpdatedCnt++;
                        }
                        break;
                    }
                default:
                    {
                    }
            }
            return null;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return null;
        }
    });
    try {
        Vertex v1 = graph.addVertex("v", (String) null);
        graph.commit();
        assertEquals(1, vertexCreatedCnt);
        assertEquals(0, vertexUpdatedCnt);
        assertEquals(0, edgeCreatedCnt);
        assertEquals(0, edgeUpdatedCnt);
        v1.setProperty("p1a", "v1a");
        graph.commit();
        assertEquals(1, vertexCreatedCnt);
        assertEquals(1, vertexUpdatedCnt);
        assertEquals(0, edgeCreatedCnt);
        assertEquals(0, edgeUpdatedCnt);
        Vertex v2 = graph.addVertex("v", (String) null);
        graph.commit();
        assertEquals(2, vertexCreatedCnt);
        assertEquals(1, vertexUpdatedCnt);
        assertEquals(0, edgeCreatedCnt);
        assertEquals(0, edgeUpdatedCnt);
        v2.setProperty("p2a", "v2a");
        graph.commit();
        assertEquals(2, vertexCreatedCnt);
        assertEquals(2, vertexUpdatedCnt);
        assertEquals(0, edgeCreatedCnt);
        assertEquals(0, edgeUpdatedCnt);
        v1.addEdge("e", v2);
        graph.commit();
        assertEquals(2, vertexCreatedCnt);
        assertEquals(4, vertexUpdatedCnt);
        assertEquals(1, edgeCreatedCnt);
        assertEquals(0, edgeUpdatedCnt);
    } finally {
        graph.shutdown();
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) Test(org.junit.Test)

Example 12 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method callbackHooks.

/**
 * Callback the registered hooks if any.
 *
 * @param type Hook type. Define when hook is called.
 * @param id   Record received in the callback
 *
 * @return True if the input record is changed, otherwise false
 */
public ORecordHook.RESULT callbackHooks(final ORecordHook.TYPE type, final OIdentifiable id) {
    if (id == null || hooks.isEmpty() || id.getIdentity().getClusterId() == 0)
        return ORecordHook.RESULT.RECORD_NOT_CHANGED;
    final ORecordHook.SCOPE scope = ORecordHook.SCOPE.typeToScope(type);
    final int scopeOrdinal = scope.ordinal();
    ORID identity = id.getIdentity().copy();
    if (!pushInHook(identity))
        return ORecordHook.RESULT.RECORD_NOT_CHANGED;
    try {
        final ORecord rec = id.getRecord();
        if (rec == null)
            return ORecordHook.RESULT.RECORD_NOT_CHANGED;
        final OScenarioThreadLocal.RUN_MODE runMode = OScenarioThreadLocal.INSTANCE.getRunMode();
        boolean recordChanged = false;
        for (ORecordHook hook : hooksByScope[scopeOrdinal]) {
            switch(runMode) {
                case // NON_DISTRIBUTED OR PROXIED DB
                DEFAULT:
                    if (getStorage().isDistributed() && hook.getDistributedExecutionMode() == ORecordHook.DISTRIBUTED_EXECUTION_MODE.TARGET_NODE)
                        // SKIP
                        continue;
                    // TARGET NODE
                    break;
                case RUNNING_DISTRIBUTED:
                    if (hook.getDistributedExecutionMode() == ORecordHook.DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE)
                        continue;
            }
            final ORecordHook.RESULT res = hook.onTrigger(type, rec);
            if (res == ORecordHook.RESULT.RECORD_CHANGED)
                recordChanged = true;
            else if (res == ORecordHook.RESULT.SKIP_IO)
                // SKIP IO OPERATION
                return res;
            else if (res == ORecordHook.RESULT.SKIP)
                // SKIP NEXT HOOKS AND RETURN IT
                return res;
            else if (res == ORecordHook.RESULT.RECORD_REPLACED)
                return res;
        }
        return recordChanged ? ORecordHook.RESULT.RECORD_CHANGED : ORecordHook.RESULT.RECORD_NOT_CHANGED;
    } finally {
        popInHook(identity);
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ORID(com.orientechnologies.orient.core.id.ORID) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook)

Example 13 with ORecordHook

use of com.orientechnologies.orient.core.hook.ORecordHook in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method compileHooks.

private void compileHooks() {
    final List<ORecordHook>[] intermediateHooksByScope = new List[ORecordHook.SCOPE.values().length];
    for (ORecordHook.SCOPE scope : ORecordHook.SCOPE.values()) intermediateHooksByScope[scope.ordinal()] = new ArrayList<ORecordHook>();
    for (ORecordHook hook : hooks.keySet()) for (ORecordHook.SCOPE scope : hook instanceof ORecordHook.Scoped ? ((ORecordHook.Scoped) hook).getScopes() : ORecordHook.SCOPE.values()) intermediateHooksByScope[scope.ordinal()].add(hook);
    for (ORecordHook.SCOPE scope : ORecordHook.SCOPE.values()) {
        final int ordinal = scope.ordinal();
        final List<ORecordHook> scopeHooks = intermediateHooksByScope[ordinal];
        hooksByScope[ordinal] = scopeHooks.toArray(new ORecordHook[scopeHooks.size()]);
    }
}
Also used : ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook)

Aggregations

ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)13 ORecord (com.orientechnologies.orient.core.record.ORecord)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 Test (org.junit.Test)5 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 ArrayList (java.util.ArrayList)3 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 ODatabaseImport (com.orientechnologies.orient.core.db.tool.ODatabaseImport)2 File (java.io.File)2 Test (org.testng.annotations.Test)2 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)1 ODatabase (com.orientechnologies.orient.core.db.ODatabase)1 ODatabaseInternal (com.orientechnologies.orient.core.db.ODatabaseInternal)1 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)1 ODatabaseExport (com.orientechnologies.orient.core.db.tool.ODatabaseExport)1 DISTRIBUTED_EXECUTION_MODE (com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE)1 RESULT (com.orientechnologies.orient.core.hook.ORecordHook.RESULT)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1