Search in sources :

Example 61 with ODatabaseDocument

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

the class OCommandCacheTest method testCommandCacheConfiguration.

@Test
public void testCommandCacheConfiguration() {
    OGlobalConfiguration.COMMAND_CACHE_ENABLED.setValue(true);
    OGlobalConfiguration.COMMAND_CACHE_MIN_EXECUTION_TIME.setValue(1);
    OGlobalConfiguration.COMMAND_CACHE_MAX_RESULSET_SIZE.setValue(10);
    OGlobalConfiguration.COMMAND_CACHE_EVICT_STRATEGY.setValue(OCommandCache.STRATEGY.INVALIDATE_ALL);
    String buildDirectory = System.getProperty("buildDirectory");
    if (buildDirectory == null)
        buildDirectory = ".";
    String dbPath = "plocal:" + buildDirectory + File.separator + OCommandCacheTest.class.getSimpleName();
    ODatabaseDocument db = new ODatabaseDocumentTx(dbPath);
    db.create();
    try {
        File commandCacheCfg = new File(buildDirectory + File.separator + OCommandCacheTest.class.getSimpleName() + "/command-cache.json");
        final String configurationContent = OIOUtils.readFileAsString(commandCacheCfg);
        ODocument cfg = new ODocument().fromJSON(configurationContent);
        Boolean enabled = cfg.field("enabled");
        String evict = cfg.field("evictStrategy");
        OCommandCache.STRATEGY evictStrategy = OCommandCache.STRATEGY.valueOf(evict);
        int minExecutionTime = cfg.field("minExecutionTime");
        int maxResultsetSize = cfg.field("maxResultsetSize");
        Assert.assertEquals(enabled, OGlobalConfiguration.COMMAND_CACHE_ENABLED.getValue());
        Assert.assertEquals(evictStrategy.toString(), OGlobalConfiguration.COMMAND_CACHE_EVICT_STRATEGY.getValue().toString());
        Assert.assertEquals(minExecutionTime, OGlobalConfiguration.COMMAND_CACHE_MIN_EXECUTION_TIME.getValue());
        Assert.assertEquals(maxResultsetSize, OGlobalConfiguration.COMMAND_CACHE_MAX_RESULSET_SIZE.getValue());
    } catch (IOException e) {
        Assert.fail("Cannot find file configuration", e);
    } finally {
        db.drop();
    }
    File f = new File(buildDirectory + File.separator + OCommandCacheTest.class.getSimpleName());
    Assert.assertEquals(f.exists(), false);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) IOException(java.io.IOException) File(java.io.File) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 62 with ODatabaseDocument

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

the class CheckHookCallCountTest method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + CheckHookCallCountTest.class.getSimpleName());
    db.create();
    try {
        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, doc.field("a"));
        assertEquals(2, 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", calculated.get(0).field("value"));
                }
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        doc.reload();
        assertEquals(2, doc.field("a"));
        assertEquals(2, doc.field("b"));
        assertEquals(4, doc.field("c"));
        doc = new ODocument(oClass);
        doc.field("a", 3);
        doc.field("b", 3);
        // FAILING here: infinite recursion
        doc.save();
        assertEquals(3, doc.field("a"));
        assertEquals(3, doc.field("b"));
        assertEquals(6, doc.field("c"));
    } finally {
        db.drop();
    }
}
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) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 63 with ODatabaseDocument

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

the class HookChangeValidationTest method testHookUpdateChangeTx.

@Test
public void testHookUpdateChangeTx() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        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() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        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");
        try {
            db.begin();
            doc.save();
            db.commit();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) 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) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 64 with ODatabaseDocument

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

the class HookChangeValidationTest method testHookCreateChange.

@Test
public void testHookCreateChange() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        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() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        try {
            doc.save();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) 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) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 65 with ODatabaseDocument

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

the class HookChangeValidationTest method testHookCreateChangeTx.

@Test
public void testHookCreateChangeTx() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + HookChangeValidationTest.class.getName());
    db.create();
    try {
        OSchema schema = db.getMetadata().getSchema();
        OClass classA = schema.createClass("TestClass");
        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() {

            @Override
            public RESULT onRecordBeforeCreate(ODocument doc) {
                doc.removeField("property1");
                doc.removeField("property2");
                doc.removeField("property3");
                return RESULT.RECORD_CHANGED;
            }

            @Override
            public RESULT onRecordBeforeUpdate(ODocument doc) {
                return RESULT.RECORD_NOT_CHANGED;
            }

            @Override
            public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
                return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
            }
        });
        ODocument doc = new ODocument(classA);
        doc.field("property1", "value1-create");
        doc.field("property2", "value2-create");
        doc.field("property3", "value3-create");
        try {
            db.begin();
            doc.save();
            db.commit();
            Assert.fail("The document save should fail for validation exception");
        } catch (OValidationException ex) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) 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) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)187 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)81 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)68 Test (org.testng.annotations.Test)53 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)47 ORecordId (com.orientechnologies.orient.core.id.ORecordId)23 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)17 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)16 ORecord (com.orientechnologies.orient.core.record.ORecord)14 Test (org.junit.Test)14 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)12 ORID (com.orientechnologies.orient.core.id.ORID)11 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)10 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)9 OValidationException (com.orientechnologies.orient.core.exception.OValidationException)9 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)9 ArrayList (java.util.ArrayList)9 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)8 HashSet (java.util.HashSet)8