Search code examples
javatriggershookorientdb

OrientDB register Hooks


I'm just trying out all the hooks on orientdb, but i'm not sure how to register the ORecordHooks on my graph.

OrientGraph graph = new OrientGraph("remote:localhost/test");
MyHook hook = new MyHook();

The MyHook class looks like this:

public class MyHook extends ORecordHookAbstract implements
        ODatabaseLifecycleListener {

    public MyHook() {
        Orient.instance().addDbLifecycleListener(this);
    }
    @Override
    public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
        System.out.println("0");
        return null;
    }
    @Override
    public PRIORITY getPriority() {
        System.out.println("1");
        return PRIORITY.FIRST;
    }
    @Override
    public void onRecordAfterCreate(ORecord iRecord) {
        System.out.println("2");
    }
    @Override
    public RESULT onRecordBeforeCreate(ORecord iRecord) {
        System.out.println("3");
        return ORecordHook.RESULT.RECORD_CHANGED;
    }
    @Override
    public RESULT onRecordBeforeRead(ORecord iRecord) {
        System.out.println("4");
        return ORecordHook.RESULT.RECORD_CHANGED;
    }
    @Override
    public void onRecordAfterRead(ORecord iRecord) {
        System.out.println("5");
    }
    @Override
    public RESULT onRecordBeforeUpdate(ORecord iRecord) {
        System.out.println("6");
        return ORecordHook.RESULT.RECORD_CHANGED;
    }
    @Override
    public void onRecordAfterUpdate(ORecord iRecord) {
        System.out.println("7");
    }
    @Override
    public RESULT onRecordBeforeDelete(ORecord iRecord) {
        System.out.println("8");
        return ORecordHook.RESULT.RECORD_CHANGED;
    }
    @Override
    public void onRecordAfterDelete(ORecord iRecord) {
        System.out.println("9");
    }
    @Override
    public void onCreate(ODatabaseInternal iDatabase) {e
        System.out.println("10");
        iDatabase.registerHook(this);
    }
    @Override
    public void onOpen(ODatabaseInternal iDatabase) {
        System.out.println("11");
        iDatabase.registerHook(this);
    }
    @Override
    public void onClose(ODatabaseInternal iDatabase) {
        System.out.println("12");
        iDatabase.unregisterHook(this);
    }
    @Override
    public void onCreateClass(ODatabaseInternal iDatabase, OClass iClass) {
        System.out.println("13");
    }
    @Override
    public void onDropClass(ODatabaseInternal iDatabase, OClass iClass) {
        System.out.println("14");
    }
}

After creating and deleting a bunch of vertices and edges only Hooks 1, 13 and 12 fire, so that would be getPriority(), onCreateClass() and onClose(). Why don't any of my ORecordHooks fire?


Solution

  • To register a Hook on a OrientGraph instance you can do the following:

     graph.getRawGraph().registerHook(hook);