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();
}
}
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);
}
}
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()]);
}
}
Aggregations