Search in sources :

Example 1 with DISTRIBUTED_EXECUTION_MODE

use of com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    OClass oClass = schema.createClass("TestInHook");
    oClass.createProperty("a", OType.INTEGER);
    oClass.createProperty("b", OType.INTEGER);
    oClass.createProperty("c", OType.INTEGER);
    ODocument doc = new ODocument(oClass);
    doc.field("a", 2);
    doc.field("b", 2);
    doc.save();
    doc.reload();
    assertEquals(2, (Object) doc.field("a"));
    assertEquals(2, (Object) doc.field("b"));
    assertNull(doc.field("c"));
    db.registerHook(new ODocumentHookAbstract(db) {

        {
            setIncludeClasses("TestInHook");
        }

        @Override
        public void onRecordAfterCreate(ODocument iDocument) {
            onRecordAfterRead(iDocument);
        }

        @Override
        public void onRecordAfterRead(ODocument iDocument) {
            String script = "select sum(a, b) as value from " + iDocument.getIdentity();
            List<ODocument> calculated = database.query(new OSQLSynchQuery<Object>(script));
            if (calculated != null && !calculated.isEmpty()) {
                iDocument.field("c", (Object) calculated.get(0).field("value"));
            }
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    doc.reload();
    assertEquals(2, (Object) doc.field("a"));
    assertEquals(2, (Object) doc.field("b"));
    assertEquals(4, (Object) doc.field("c"));
    doc = new ODocument(oClass);
    doc.field("a", 3);
    doc.field("b", 3);
    doc.save();
    assertEquals(3, (Object) doc.field("a"));
    assertEquals(3, (Object) doc.field("b"));
    assertEquals(6, (Object) doc.field("c"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) Test(org.junit.Test)

Example 2 with DISTRIBUTED_EXECUTION_MODE

use of com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testCreationInHook.

@Test
public void testCreationInHook() {
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    final OClass classA = schema.createClass("TestCreationInHookMain");
    final OClass classB = schema.createClass("TestCreationInHookReflect");
    classA.createProperty("name", OType.STRING);
    classA.createProperty("mirror", OType.LINK).setLinkedClass(classB);
    classB.createProperty("name", OType.STRING);
    db.registerHook(new ODocumentHookAbstract(db) {

        {
            setIncludeClasses(classA.getName());
        }

        @Override
        public RESULT onRecordBeforeCreate(ODocument iDocument) {
            ODocument mirror = new ODocument(classB);
            mirror.field("name", (Object) iDocument.field("name"));
            iDocument.field("mirror", mirror);
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    for (int i = 0; i < 10; i++) {
        String name = "name-" + RANDOM.nextLong();
        ODocument doc = new ODocument(classA);
        doc.field("name", name);
        doc.save();
    }
    db.commit();
    db.close();
    db = wicket.getTester().getDatabase();
    for (ODocument doc : db.browseClass(classA.getName())) {
        String name = doc.field("name");
        assertNotNull(name);
        ODocument mirror = doc.field("mirror");
        assertNotNull(mirror);
        assertEquals(name, mirror.field("name"));
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) RESULT(com.orientechnologies.orient.core.hook.ORecordHook.RESULT) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) Test(org.junit.Test)

Example 3 with DISTRIBUTED_EXECUTION_MODE

use of com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testDocumentTrackingSimple.

@Test
public void testDocumentTrackingSimple() {
    final String className = "TestDocumentTrackingSimple";
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    final OClass classA = schema.createClass(className);
    classA.createProperty("a", OType.STRING);
    db.registerHook(new ORecordHook() {

        @Override
        public void onUnregister() {
        // NOP
        }

        @Override
        public RESULT onTrigger(TYPE iType, ORecord iRecord) {
            if (iRecord instanceof ODocument) {
                ODocument doc = (ODocument) iRecord;
                if (classA.isSuperClassOf(doc.getSchemaClass())) {
                    // System.out.println("During "+iType+" document should track changes: "+doc.isTrackingChanges());
                    assertTrue("During " + iType + " document should track changes", doc.isTrackingChanges());
                }
            }
            return RESULT.RECORD_NOT_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    db.commit();
    ODocument doc = new ODocument(classA);
    doc.field("a", "test");
    doc.save();
    doc.reload();
    doc.field("a", "test2");
    doc.save();
    doc.reload();
    doc.delete();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) RESULT(com.orientechnologies.orient.core.hook.ORecordHook.RESULT) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) Test(org.junit.Test)

Example 4 with DISTRIBUTED_EXECUTION_MODE

use of com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testValidationAndHooksOrder.

@Test
@Ignore
public void testValidationAndHooksOrder() {
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    OClass classA = schema.createClass(TEST_VALIDATION_AND_HOOKS_CLASS);
    classA.createProperty("property1", OType.STRING).setNotNull(true);
    classA.createProperty("property2", OType.STRING).setReadonly(true);
    classA.createProperty("property3", OType.STRING).setMandatory(true);
    db.registerHook(new ODocumentHookAbstract(db) {

        @Override
        public RESULT onRecordBeforeCreate(ODocument doc) {
            doc.field("property1", "value1-create");
            doc.field("property2", "value2-create");
            doc.field("property3", "value3-create");
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public RESULT onRecordBeforeUpdate(ODocument doc) {
            doc.undo("property2");
            if (doc.field("property3") == null)
                doc.field("property3", "value3-update");
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    ODocument doc = new ODocument(classA);
    // doc.field("property3", "value3-create"); //Magic line #1
    doc.save();
    assertEquals("value1-create", doc.field("property1"));
    assertEquals("value2-create", doc.field("property2"));
    assertEquals("value3-create", doc.field("property3"));
    doc.field("property1", "value1-update");
    doc.field("property2", "value2-update");
    doc.removeField("property3");
    // doc.undo("property2"); //Magic line #2
    // doc.field("property3", "value3-update"); //Magic line #3
    doc.save();
    assertEquals("value1-update", doc.field("property1"));
    assertEquals("value2-create", doc.field("property2"));
    assertEquals("value3-update", doc.field("property3"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) RESULT(com.orientechnologies.orient.core.hook.ORecordHook.RESULT) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with DISTRIBUTED_EXECUTION_MODE

use of com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testUpdatesInHooks.

@Test
@Ignore
public void testUpdatesInHooks() {
    final String className = "TestUpdatesInHook";
    ODatabaseDocument db = wicket.getTester().getDatabase();
    db.registerHook(new ODocumentHookAbstract(db) {

        {
            setIncludeClasses(className);
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }

        @Override
        public RESULT onRecordBeforeCreate(ODocument iDocument) {
            return onRecordBeforeUpdate(iDocument);
        }

        @Override
        public RESULT onRecordBeforeUpdate(ODocument iDocument) {
            iDocument.undo("b");
            return RESULT.RECORD_CHANGED;
        }

        @Override
        public void onRecordAfterCreate(ODocument iDocument) {
            onRecordAfterUpdate(iDocument);
        }

        @Override
        public void onRecordAfterUpdate(ODocument iDocument) {
            onRecordAfterRead(iDocument);
        }

        @Override
        public void onRecordAfterRead(ODocument iDocument) {
            if (!iDocument.containsField("b"))
                iDocument.field("b", (Object) iDocument.field("a"));
        }
    });
    OSchema schema = db.getMetadata().getSchema();
    OClass classA = schema.createClass(className);
    classA.createProperty("a", OType.STRING);
    classA.createProperty("b", OType.STRING);
    db.commit();
    ODocument doc = new ODocument(classA);
    /*doc.field("a", "test1");
		doc.save();
		doc.reload();
		assertEquals(doc.field("a"), doc.field("b"));
		doc.field("a", "test2");
		doc.save();
		doc.reload();
		assertEquals(doc.field("a"), doc.field("b"));*/
    db.begin();
    doc.field("a", "test3");
    doc.save();
    doc.reload();
    // doc = db.reload(doc, null, true);
    assertEquals((Object) doc.field("a"), (Object) doc.field("b"));
    doc.field("a", "test4");
    doc.save();
    doc.reload();
    // doc = db.reload(doc, null, true);
    assertEquals((Object) doc.field("a"), (Object) doc.field("b"));
    db.commit();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) RESULT(com.orientechnologies.orient.core.hook.ORecordHook.RESULT) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)6 DISTRIBUTED_EXECUTION_MODE (com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)6 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 Test (org.junit.Test)6 ODocumentHookAbstract (com.orientechnologies.orient.core.hook.ODocumentHookAbstract)5 RESULT (com.orientechnologies.orient.core.hook.ORecordHook.RESULT)5 Ignore (org.junit.Ignore)3 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 List (java.util.List)1