Search in sources :

Example 71 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class IndexTest method testNullIndexKeysSupportInMiddleTx.

public void testNullIndexKeysSupportInMiddleTx() {
    if (database.getURL().startsWith("remote:"))
        return;
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass clazz = schema.createClass("NullIndexKeysSupportInMiddleTx", 1, null);
    clazz.createProperty("nullField", OType.STRING);
    ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("NullIndexKeysSupportInMiddleTxIndex", INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, new String[] { "nullField" });
    database.begin();
    for (int i = 0; i < 20; i++) {
        if (i % 5 == 0) {
            ODocument document = new ODocument("NullIndexKeysSupportInMiddleTx");
            document.field("nullField", (Object) null);
            document.save();
        } else {
            ODocument document = new ODocument("NullIndexKeysSupportInMiddleTx");
            document.field("nullField", "val" + i);
            document.save();
        }
    }
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupportInMiddleTx where nullField = 'val3'"));
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0).field("nullField"), "val3");
    final String query = "select from NullIndexKeysSupportInMiddleTx where nullField is null";
    result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupportInMiddleTx where nullField is null"));
    Assert.assertEquals(result.size(), 4);
    for (ODocument document : result) Assert.assertNull(document.field("nullField"));
    final ODocument explain = databaseDocumentTx.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("NullIndexKeysSupportInMiddleTxIndex"));
    database.commit();
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 72 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class IndexTxTest method beforeMethod.

@BeforeMethod
public void beforeMethod() throws Exception {
    super.beforeMethod();
    final OSchema schema = database.getMetadata().getSchema();
    schema.reload();
    database.getStorage().reload();
    schema.getClass("IndexTxTestClass").truncate();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema)

Example 73 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class IndexTest method testNullIndexKeysSupport.

public void testNullIndexKeysSupport() {
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass clazz = schema.createClass("NullIndexKeysSupport", 1, null);
    clazz.createProperty("nullField", OType.STRING);
    ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("NullIndexKeysSupportIndex", INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, new String[] { "nullField" });
    for (int i = 0; i < 20; i++) {
        if (i % 5 == 0) {
            ODocument document = new ODocument("NullIndexKeysSupport");
            document.field("nullField", (Object) null);
            document.save();
        } else {
            ODocument document = new ODocument("NullIndexKeysSupport");
            document.field("nullField", "val" + i);
            document.save();
        }
    }
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupport where nullField = 'val3'"));
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0).field("nullField"), "val3");
    final String query = "select from NullIndexKeysSupport where nullField is null";
    result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupport where nullField is null"));
    Assert.assertEquals(result.size(), 4);
    for (ODocument document : result) Assert.assertNull(document.field("nullField"));
    final ODocument explain = databaseDocumentTx.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("NullIndexKeysSupportIndex"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 74 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema in project orientdb by orientechnologies.

the class SQLSelectIndexReuseTest method beforeClass.

@BeforeClass
public void beforeClass() throws Exception {
    super.beforeClass();
    if (database.isClosed()) {
        database.open("admin", "admin");
    }
    final OSchema schema = database.getMetadata().getSchema();
    final OClass oClass = schema.createClass("sqlSelectIndexReuseTestClass");
    oClass.createProperty("prop1", OType.INTEGER);
    oClass.createProperty("prop2", OType.INTEGER);
    oClass.createProperty("prop3", OType.INTEGER);
    oClass.createProperty("prop4", OType.INTEGER);
    oClass.createProperty("prop5", OType.INTEGER);
    oClass.createProperty("prop6", OType.INTEGER);
    oClass.createProperty("prop7", OType.STRING);
    oClass.createProperty("prop8", OType.INTEGER);
    oClass.createProperty("prop9", OType.INTEGER);
    oClass.createProperty("fEmbeddedMap", OType.EMBEDDEDMAP, OType.INTEGER);
    oClass.createProperty("fEmbeddedMapTwo", OType.EMBEDDEDMAP, OType.INTEGER);
    oClass.createProperty("fLinkMap", OType.LINKMAP);
    oClass.createProperty("fEmbeddedList", OType.EMBEDDEDLIST, OType.INTEGER);
    oClass.createProperty("fEmbeddedListTwo", OType.EMBEDDEDLIST, OType.INTEGER);
    oClass.createProperty("fLinkList", OType.LINKLIST);
    oClass.createProperty("fEmbeddedSet", OType.EMBEDDEDSET, OType.INTEGER);
    oClass.createProperty("fEmbeddedSetTwo", OType.EMBEDDEDSET, OType.INTEGER);
    oClass.createIndex("indexone", OClass.INDEX_TYPE.UNIQUE, "prop1", "prop2");
    oClass.createIndex("indextwo", OClass.INDEX_TYPE.UNIQUE, "prop3");
    oClass.createIndex("indexthree", OClass.INDEX_TYPE.NOTUNIQUE, "prop1", "prop2", "prop4");
    oClass.createIndex("indexfour", OClass.INDEX_TYPE.NOTUNIQUE, "prop4", "prop1", "prop3");
    oClass.createIndex("indexfive", OClass.INDEX_TYPE.NOTUNIQUE, "prop6", "prop1", "prop3");
    oClass.createIndex("indexsix", OClass.INDEX_TYPE.FULLTEXT, "prop7");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedMapByKey", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedMap");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedMapByValue", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedMap by value");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedList", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedList");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedMapByKeyProp8", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedMapTwo", "prop8");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedMapByValueProp8", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedMapTwo by value", "prop8");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedSetProp8", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedSetTwo", "prop8");
    oClass.createIndex("sqlSelectIndexReuseTestProp9EmbeddedSetProp8", OClass.INDEX_TYPE.NOTUNIQUE, "prop9", "fEmbeddedSetTwo", "prop8");
    oClass.createIndex("sqlSelectIndexReuseTestEmbeddedListTwoProp8", OClass.INDEX_TYPE.NOTUNIQUE, "fEmbeddedListTwo", "prop8");
    schema.save();
    final String[] fullTextIndexStrings = { "Alice : What is the use of a book, without pictures or conversations?", "Rabbit : Oh my ears and whiskers, how late it's getting!", "Alice : If it had grown up, it would have made a dreadfully ugly child; but it makes rather a handsome pig, I think", "The Cat : We're all mad here.", "The Hatter : Why is a raven like a writing desk?", "The Hatter : Twinkle, twinkle, little bat! How I wonder what you're at.", "The Queen : Off with her head!", "The Duchess : Tut, tut, child! Everything's got a moral, if only you can find it.", "The Duchess : Take care of the sense, and the sounds will take care of themselves.", "The King : Begin at the beginning and go on till you come to the end: then stop." };
    for (int i = 0; i < 10; i++) {
        final Map<String, Integer> embeddedMap = new HashMap<String, Integer>();
        embeddedMap.put("key" + (i * 10 + 1), i * 10 + 1);
        embeddedMap.put("key" + (i * 10 + 2), i * 10 + 2);
        embeddedMap.put("key" + (i * 10 + 3), i * 10 + 3);
        embeddedMap.put("key" + (i * 10 + 4), i * 10 + 1);
        final List<Integer> embeddedList = new ArrayList<Integer>(3);
        embeddedList.add(i * 3);
        embeddedList.add(i * 3 + 1);
        embeddedList.add(i * 3 + 2);
        final Set<Integer> embeddedSet = new HashSet<Integer>();
        embeddedSet.add(i * 10);
        embeddedSet.add(i * 10 + 1);
        embeddedSet.add(i * 10 + 2);
        for (int j = 0; j < 10; j++) {
            final ODocument document = new ODocument("sqlSelectIndexReuseTestClass");
            document.field("prop1", i);
            document.field("prop2", j);
            document.field("prop3", i * 10 + j);
            document.field("prop4", i);
            document.field("prop5", i);
            document.field("prop6", j);
            document.field("prop7", fullTextIndexStrings[i]);
            document.field("prop8", j);
            document.field("prop9", j % 2);
            document.field("fEmbeddedMap", embeddedMap);
            document.field("fEmbeddedMapTwo", embeddedMap);
            document.field("fEmbeddedList", embeddedList);
            document.field("fEmbeddedListTwo", embeddedList);
            document.field("fEmbeddedSet", embeddedSet);
            document.field("fEmbeddedSetTwo", embeddedSet);
            document.save();
        }
    }
    database.close();
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 75 with OSchema

use of com.orientechnologies.orient.core.metadata.schema.OSchema 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)

Aggregations

OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)203 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)165 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)122 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)54 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)46 Test (org.testng.annotations.Test)35 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)31 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)23 Test (org.junit.Test)19 Before (org.junit.Before)16 ORID (com.orientechnologies.orient.core.id.ORID)15 OIndex (com.orientechnologies.orient.core.index.OIndex)15 Collection (java.util.Collection)15 Set (java.util.Set)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)9 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 BeforeClass (org.testng.annotations.BeforeClass)9 OStorage (com.orientechnologies.orient.core.storage.OStorage)6 ArrayList (java.util.ArrayList)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4