Search in sources :

Example 31 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL in project orientdb by orientechnologies.

the class IndexTest method testCreateIndexAbstractClass.

public void testCreateIndexAbstractClass() {
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    OClass abstractClass = schema.createAbstractClass("TestCreateIndexAbstractClass");
    abstractClass.createProperty("value", OType.STRING).setMandatory(true).createIndex(INDEX_TYPE.UNIQUE);
    schema.createClass("TestCreateIndexAbstractClassChildOne", abstractClass);
    schema.createClass("TestCreateIndexAbstractClassChildTwo", abstractClass);
    ODocument docOne = new ODocument("TestCreateIndexAbstractClassChildOne");
    docOne.field("value", "val1");
    docOne.save();
    ODocument docTwo = new ODocument("TestCreateIndexAbstractClassChildTwo");
    docTwo.field("value", "val2");
    docTwo.save();
    final String queryOne = "select from TestCreateIndexAbstractClass where value = 'val1'";
    List<ODocument> resultOne = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(queryOne));
    Assert.assertEquals(resultOne.size(), 1);
    Assert.assertEquals((Object) resultOne.get(0), (Object) docOne);
    ODocument explain = databaseDocumentTx.command(new OCommandSQL("explain " + queryOne)).execute();
    Assert.assertTrue(explain.<Collection<String>>field("involvedIndexes").contains("TestCreateIndexAbstractClass.value"));
    final String queryTwo = "select from TestCreateIndexAbstractClass where value = 'val2'";
    List<ODocument> resultTwo = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>(queryTwo));
    Assert.assertEquals(resultTwo.size(), 1);
    Assert.assertEquals((Object) resultTwo.get(0), (Object) docTwo);
    explain = databaseDocumentTx.command(new OCommandSQL("explain " + queryTwo)).execute();
    Assert.assertTrue(explain.<Collection<String>>field("involvedIndexes").contains("TestCreateIndexAbstractClass.value"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) 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 32 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL 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 33 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL in project orientdb by orientechnologies.

the class IndexTest method testNullIndexKeysSupportInTx.

public void testNullIndexKeysSupportInTx() {
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass clazz = schema.createClass("NullIndexKeysSupportInTx", 1, null);
    clazz.createProperty("nullField", OType.STRING);
    ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("NullIndexKeysSupportInTxIndex", 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("NullIndexKeysSupportInTx");
            document.field("nullField", (Object) null);
            document.save();
        } else {
            ODocument document = new ODocument("NullIndexKeysSupportInTx");
            document.field("nullField", "val" + i);
            document.save();
        }
    }
    database.commit();
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupportInTx where nullField = 'val3'"));
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0).field("nullField"), "val3");
    final String query = "select from NullIndexKeysSupportInTx where nullField is null";
    result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupportInTx 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("NullIndexKeysSupportInTxIndex"));
}
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 34 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL in project orientdb by orientechnologies.

the class MapIndexTest method testIndexMapRemoveItemInTx.

public void testIndexMapRemoveItemInTx() throws Exception {
    Mapper mapper = new Mapper();
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("key1", 10);
    map.put("key2", 20);
    map.put("key3", 30);
    mapper.setIntMap(map);
    mapper = database.save(mapper);
    try {
        database.begin();
        Mapper loadedMapper = (Mapper) database.load(new ORecordId(mapper.getId()));
        loadedMapper.getIntMap().remove("key2");
        loadedMapper = database.save(loadedMapper);
        database.commit();
    } catch (Exception e) {
        database.rollback();
        throw e;
    }
    final List<ODocument> resultByKey = database.command(new OCommandSQL("select key, rid from index:mapIndexTestKey")).execute();
    Assert.assertNotNull(resultByKey);
    Assert.assertEquals(resultByKey.size(), 2);
    for (ODocument d : resultByKey) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (!d.field("key").equals("key1") && !d.field("key").equals("key3")) {
            Assert.fail("Unknown key found: " + d.field("key"));
        }
    }
    final List<ODocument> resultByValue = database.command(new OCommandSQL("select key, rid from index:mapIndexTestValue")).execute();
    Assert.assertNotNull(resultByValue);
    Assert.assertEquals(resultByValue.size(), 2);
    for (ODocument d : resultByValue) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (!d.field("key").equals(10) && !d.field("key").equals(30)) {
            Assert.fail("Unknown key found: " + d.field("key"));
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Mapper(com.orientechnologies.orient.test.domain.whiz.Mapper) HashMap(java.util.HashMap) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 35 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL in project orientdb by orientechnologies.

the class MapIndexTest method testIndexMapRemoveInTx.

public void testIndexMapRemoveInTx() throws Exception {
    Mapper mapper = new Mapper();
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("key1", 10);
    map.put("key2", 20);
    mapper.setIntMap(map);
    mapper = database.save(mapper);
    try {
        database.begin();
        database.delete(mapper);
        database.commit();
    } catch (Exception e) {
        database.rollback();
        throw e;
    }
    final List<ODocument> resultByKey = database.command(new OCommandSQL("select key, rid from index:mapIndexTestKey")).execute();
    Assert.assertNotNull(resultByKey);
    Assert.assertEquals(resultByKey.size(), 0);
    final List<ODocument> resultByValue = database.command(new OCommandSQL("select key, rid from index:mapIndexTestValue")).execute();
    Assert.assertNotNull(resultByValue);
    Assert.assertEquals(resultByValue.size(), 0);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Mapper(com.orientechnologies.orient.test.domain.whiz.Mapper) HashMap(java.util.HashMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)646 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)440 Test (org.junit.Test)109 Test (org.testng.annotations.Test)93 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)85 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)83 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)81 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)65 ORID (com.orientechnologies.orient.core.id.ORID)65 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)54 Set (java.util.Set)53 HashMap (java.util.HashMap)48 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)42 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)42 List (java.util.List)27 Before (org.junit.Before)25 OStorage (com.orientechnologies.orient.core.storage.OStorage)23 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)23 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)22 ORecordId (com.orientechnologies.orient.core.id.ORecordId)21