Search in sources :

Example 36 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class ByteArrayKeyTest method testAutomaticCompositeUsage.

public void testAutomaticCompositeUsage() {
    byte[] key1 = new byte[] { 1, 2, 3 };
    byte[] key2 = new byte[] { 4, 5, 6 };
    ODocument doc1 = new ODocument("CompositeByteArrayKeyTest");
    doc1.field("byteArrayKey", key1);
    doc1.field("intKey", 1);
    doc1.save();
    ODocument doc2 = new ODocument("CompositeByteArrayKeyTest");
    doc2.field("byteArrayKey", key2);
    doc2.field("intKey", 2);
    doc2.save();
    OIndex<?> index = database.getMetadata().getIndexManager().getIndex("compositeByteArrayKey");
    Assert.assertEquals(index.get(new OCompositeKey(key1, 1)), doc1);
    Assert.assertEquals(index.get(new OCompositeKey(key2, 2)), doc2);
}
Also used : OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 37 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class CollateTest method testCompositeIndexQueryCS.

public void testCompositeIndexQueryCS() {
    final OSchema schema = database.getMetadata().getSchema();
    OClass clazz = schema.createClass("CompositeIndexQueryCSTest");
    OProperty csp = clazz.createProperty("csp", OType.STRING);
    csp.setCollate(ODefaultCollate.NAME);
    OProperty cip = clazz.createProperty("cip", OType.STRING);
    cip.setCollate(OCaseInsensitiveCollate.NAME);
    clazz.createIndex("collateCompositeIndexCS", OClass.INDEX_TYPE.NOTUNIQUE, "csp", "cip");
    for (int i = 0; i < 10; i++) {
        ODocument document = new ODocument("CompositeIndexQueryCSTest");
        if (i % 2 == 0) {
            document.field("csp", "VAL");
            document.field("cip", "VAL");
        } else {
            document.field("csp", "val");
            document.field("cip", "val");
        }
        document.save();
    }
    String query = "select from CompositeIndexQueryCSTest where csp = 'VAL'";
    List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 5);
    for (ODocument document : result) Assert.assertEquals(document.field("csp"), "VAL");
    ODocument explain = database.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("collateCompositeIndexCS"));
    query = "select from CompositeIndexQueryCSTest where csp = 'VAL' and cip = 'VaL'";
    result = database.query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 5);
    for (ODocument document : result) {
        Assert.assertEquals(document.field("csp"), "VAL");
        Assert.assertEquals((document.<String>field("cip")).toUpperCase(), "VAL");
    }
    explain = database.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("collateCompositeIndexCS"));
    result = database.query(new OSQLSynchQuery<ODocument>("select from index:collateCompositeIndexCS where key = ['VAL', 'VaL']"));
    Assert.assertEquals(result.size(), 5);
    for (ODocument document : result) {
        final OCompositeKey key = document.field("key");
        final List keys = key.getKeys();
        Assert.assertEquals(keys.get(0), "VAL");
        Assert.assertTrue("val".compareToIgnoreCase((String) keys.get(1)) == 0);
        final ODocument record = document.<OIdentifiable>field("rid").getRecord();
        Assert.assertEquals(record.field("csp"), "VAL");
        Assert.assertEquals((record.<String>field("cip")).toUpperCase(), "VAL");
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) Set(java.util.Set) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) List(java.util.List) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 38 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class ClassIndexManagerTest method testCollectionCompositeUpdateCollectionWasChangedSimpleFieldWasAssigned.

public void testCollectionCompositeUpdateCollectionWasChangedSimpleFieldWasAssigned() {
    final ODocument doc = new ODocument("classIndexManagerTestCompositeCollectionClass");
    doc.field("prop1", "test1");
    doc.field("prop2", Arrays.asList(1, 2));
    doc.save();
    final OIndex<?> index = database.getMetadata().getIndexManager().getIndex("classIndexManagerTestIndexValueAndCollection");
    Assert.assertEquals(index.getSize(), 2);
    List<Integer> docList = doc.field("prop2");
    docList.add(3);
    docList.add(4);
    docList.add(5);
    docList.remove(0);
    doc.field("prop1", "test2");
    doc.save();
    Assert.assertEquals(index.getSize(), 4);
    Assert.assertEquals(index.get(new OCompositeKey("test2", 2)), doc.getIdentity());
    Assert.assertEquals(index.get(new OCompositeKey("test2", 3)), doc.getIdentity());
    Assert.assertEquals(index.get(new OCompositeKey("test2", 4)), doc.getIdentity());
    Assert.assertEquals(index.get(new OCompositeKey("test2", 5)), doc.getIdentity());
    doc.delete();
    Assert.assertEquals(index.getSize(), 0);
}
Also used : OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 39 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class DateIndexTest method testDateIndexes.

public void testDateIndexes() {
    final Date dateOne = new Date();
    final Date dateTwo = new Date(dateOne.getTime() + 24 * 60 * 60 * 1000 + 100);
    final ODocument dateDoc = new ODocument("DateIndexTest");
    dateDoc.field("dateField", dateOne);
    dateDoc.field("dateTimeField", dateTwo);
    final List<Date> dateList = new ArrayList<Date>();
    final Date dateThree = new Date(dateOne.getTime() + 100);
    final Date dateFour = new Date(dateThree.getTime() + 24 * 60 * 60 * 1000 + 100);
    dateList.add(new Date(dateThree.getTime()));
    dateList.add(new Date(dateFour.getTime()));
    final List<Date> dateTimeList = new ArrayList<Date>();
    dateTimeList.add(new Date(dateThree.getTime()));
    dateTimeList.add(new Date(dateFour.getTime()));
    dateDoc.field("dateList", dateList);
    dateDoc.field("dateTimeList", dateTimeList);
    dateDoc.field("value", "v1");
    dateDoc.save();
    final OIndex dateIndexTestDateIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestDateIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestDateIndex.get(dateOne)).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestDateIndex.get(dateTwo));
    final OIndex dateIndexTestDateTimeIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestDateTimeIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestDateTimeIndex.get(dateTwo)).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestDateTimeIndex.get(dateOne));
    final OIndex dateIndexTestValueDateIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateIndex.get(new OCompositeKey("v1", dateOne))).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestValueDateIndex.get(new OCompositeKey("v1", dateTwo)));
    final OIndex dateIndexTestValueDateTimeIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateTimeIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateTimeIndex.get(new OCompositeKey("v1", dateTwo))).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestValueDateTimeIndex.get(new OCompositeKey("v1", dateOne)));
    final OIndex dateIndexTestValueDateListIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateListIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateListIndex.get(new OCompositeKey("v1", dateThree))).getIdentity(), dateDoc.getIdentity());
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateListIndex.get(new OCompositeKey("v1", dateFour))).getIdentity(), dateDoc.getIdentity());
    final OIndex dateIndexTestValueDateTimeListIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateListIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateTimeListIndex.get(new OCompositeKey("v1", dateThree))).getIdentity(), dateDoc.getIdentity());
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateTimeListIndex.get(new OCompositeKey("v1", dateFour))).getIdentity(), dateDoc.getIdentity());
    final OIndex dateIndexTestDateHashIndexIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestDateHashIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestDateHashIndexIndex.get(dateOne)).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestDateHashIndexIndex.get(dateTwo));
    final OIndex dateIndexTestDateTimeHashIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestDateTimeHashIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestDateTimeHashIndex.get(dateTwo)).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestDateTimeHashIndex.get(dateOne));
    final OIndex dateIndexTestValueDateHashIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateHashIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateHashIndex.get(new OCompositeKey("v1", dateOne))).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestValueDateHashIndex.get(new OCompositeKey("v1", dateTwo)));
    final OIndex dateIndexTestValueDateTimeHashIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateTimeHashIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateTimeHashIndex.get(new OCompositeKey("v1", dateTwo))).getIdentity(), dateDoc.getIdentity());
    Assert.assertNull(dateIndexTestValueDateTimeHashIndex.get(new OCompositeKey("v1", dateOne)));
    final OIndex dateIndexTestValueDateListHashIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateListHashIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateListHashIndex.get(new OCompositeKey("v1", dateThree))).getIdentity(), dateDoc.getIdentity());
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateListHashIndex.get(new OCompositeKey("v1", dateFour))).getIdentity(), dateDoc.getIdentity());
    final OIndex dateIndexTestValueDateTimeListHashIndex = database.getMetadata().getIndexManager().getIndex("DateIndexTestValueDateListHashIndex");
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateTimeListHashIndex.get(new OCompositeKey("v1", dateThree))).getIdentity(), dateDoc.getIdentity());
    Assert.assertEquals(((OIdentifiable) dateIndexTestValueDateTimeListHashIndex.get(new OCompositeKey("v1", dateFour))).getIdentity(), dateDoc.getIdentity());
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) ArrayList(java.util.ArrayList) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) Date(java.util.Date) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 40 with OCompositeKey

use of com.orientechnologies.orient.core.index.OCompositeKey in project orientdb by orientechnologies.

the class DefaultValuesTrivialTest method testPrepopulationMultivalueIndex.

@Test
public void testPrepopulationMultivalueIndex() throws Exception {
    // create example schema
    OSchema schema = database.getMetadata().getSchema();
    OClass classA = schema.createClass("ClassA");
    OProperty prop = classA.createProperty("name", OType.STRING);
    prop.setDefaultValue("default name");
    OProperty prop2 = classA.createProperty("value", OType.STRING);
    OIndex<?> index = classA.createIndex("multi", OClass.INDEX_TYPE.NOTUNIQUE, "value", "name");
    {
        ODocument doc = new ODocument(classA);
        assertEquals("default name", doc.field("name"));
        doc.field("value", "1");
        database.save(doc);
        assertEquals(1, ((Collection) index.get(new OCompositeKey("1"))).size());
    }
    {
        ODocument doc = new ODocument(classA);
        assertEquals("default name", doc.field("name"));
        doc.field("value", "2");
        database.save(doc);
        assertEquals(1, ((Collection) index.get(new OCompositeKey("2"))).size());
    }
    assertEquals(0, ((Collection) index.get(new OCompositeKey("3"))).size());
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Collection(java.util.Collection) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)40 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)22 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)11 ORecordId (com.orientechnologies.orient.core.id.ORecordId)10 ORID (com.orientechnologies.orient.core.id.ORID)8 OBinarySerializerFactory (com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)5 OBinarySerializer (com.orientechnologies.common.serialization.types.OBinarySerializer)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)4 OIndex (com.orientechnologies.orient.core.index.OIndex)3 Collection (java.util.Collection)3 Date (java.util.Date)3 List (java.util.List)3 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)2 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)2 OType (com.orientechnologies.orient.core.metadata.schema.OType)2 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)2 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)2