Search in sources :

Example 41 with OIndexCursor

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

the class IndexTxAwareOneValueGetValuesTest method testClear.

@Test
public void testClear() {
    database.getMetadata().getIndexManager().reload();
    database.begin();
    final OIndex<?> index = database.getMetadata().getIndexManager().getIndex("idxTxAwareOneValueGetValuesTest");
    Assert.assertTrue(index instanceof OIndexTxAwareOneValue);
    final int clusterId = database.getDefaultClusterId();
    index.put(1, new ORecordId(clusterId, 1));
    index.put(2, new ORecordId(clusterId, 2));
    database.commit();
    Assert.assertNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    Set<OIdentifiable> resultOne = new HashSet<OIdentifiable>();
    OIndexCursor cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultOne);
    Assert.assertEquals(resultOne.size(), 2);
    database.begin();
    index.clear();
    Assert.assertNotNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    Set<OIdentifiable> resultTwo = new HashSet<OIdentifiable>();
    cursorToSet(cursor, resultTwo);
    Assert.assertEquals(resultTwo.size(), 0);
    database.rollback();
    Assert.assertNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    Set<OIdentifiable> resultThree = new HashSet<OIdentifiable>();
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultThree);
    Assert.assertEquals(resultThree.size(), 2);
}
Also used : OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexTxAwareOneValue(com.orientechnologies.orient.core.index.OIndexTxAwareOneValue) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 42 with OIndexCursor

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

the class IndexTxAwareOneValueGetValuesTest method testClearAndPut.

@Test
public void testClearAndPut() {
    database.getMetadata().getIndexManager().reload();
    database.begin();
    final OIndex<?> index = database.getMetadata().getIndexManager().getIndex("idxTxAwareOneValueGetValuesTest");
    Assert.assertTrue(index instanceof OIndexTxAwareOneValue);
    final int clusterId = database.getDefaultClusterId();
    index.put(1, new ORecordId(clusterId, 1));
    index.put(2, new ORecordId(clusterId, 2));
    database.commit();
    Assert.assertNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    Set<OIdentifiable> resultOne = new HashSet<OIdentifiable>();
    OIndexCursor cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultOne);
    Assert.assertEquals(resultOne.size(), 2);
    database.begin();
    index.clear();
    index.put(2, new ORecordId(clusterId, 2));
    Assert.assertNotNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    Set<OIdentifiable> resultTwo = new HashSet<OIdentifiable>();
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultTwo);
    Assert.assertEquals(resultTwo.size(), 1);
    database.rollback();
    Assert.assertNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    Set<OIdentifiable> resultThree = new HashSet<OIdentifiable>();
    cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, resultThree);
    Assert.assertEquals(resultThree.size(), 2);
}
Also used : OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexTxAwareOneValue(com.orientechnologies.orient.core.index.OIndexTxAwareOneValue) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 43 with OIndexCursor

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

the class IndexTxAwareOneValueGetValuesTest method testPutAfterTransaction.

@Test
public void testPutAfterTransaction() {
    database.getMetadata().getIndexManager().reload();
    database.begin();
    final OIndex<?> index = database.getMetadata().getIndexManager().getIndex("idxTxAwareOneValueGetValuesTest");
    Assert.assertTrue(index instanceof OIndexTxAwareOneValue);
    final int clusterId = database.getDefaultClusterId();
    index.put(1, new ORecordId(clusterId, 1));
    index.put(2, new ORecordId(clusterId, 2));
    Assert.assertNotNull(database.getTransaction().getIndexChanges("idxTxAwareOneValueGetValuesTest"));
    Set<OIdentifiable> result = new HashSet<OIdentifiable>();
    OIndexCursor cursor = index.iterateEntries(Arrays.asList(1, 2), true);
    cursorToSet(cursor, result);
    Assert.assertEquals(result.size(), 2);
    database.commit();
    index.put(3, new ORecordId(clusterId, 3));
    cursor = index.iterateEntries(Arrays.asList(1, 2, 3), true);
    cursorToSet(cursor, result);
    Assert.assertEquals(result.size(), 3);
}
Also used : OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexTxAwareOneValue(com.orientechnologies.orient.core.index.OIndexTxAwareOneValue) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 44 with OIndexCursor

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

the class OQueryOperatorBetween method executeIndexQuery.

@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    OIndexCursor cursor;
    final OIndexInternal<?> internalIndex = index.getInternal();
    if (!internalIndex.canBeUsedInEqualityOperators() || !internalIndex.hasRangeQuerySupport())
        return null;
    if (indexDefinition.getParamCount() == 1) {
        final Object[] betweenKeys = (Object[]) keyParams.get(0);
        final Object keyOne = indexDefinition.createValue(Collections.singletonList(OSQLHelper.getValue(betweenKeys[0])));
        final Object keyTwo = indexDefinition.createValue(Collections.singletonList(OSQLHelper.getValue(betweenKeys[2])));
        if (keyOne == null || keyTwo == null)
            return null;
        cursor = index.iterateEntriesBetween(keyOne, leftInclusive, keyTwo, rightInclusive, ascSortOrder);
    } else {
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        final Object[] betweenKeys = (Object[]) keyParams.get(keyParams.size() - 1);
        final Object betweenKeyOne = OSQLHelper.getValue(betweenKeys[0]);
        if (betweenKeyOne == null)
            return null;
        final Object betweenKeyTwo = OSQLHelper.getValue(betweenKeys[2]);
        if (betweenKeyTwo == null)
            return null;
        final List<Object> betweenKeyOneParams = new ArrayList<Object>(keyParams.size());
        betweenKeyOneParams.addAll(keyParams.subList(0, keyParams.size() - 1));
        betweenKeyOneParams.add(betweenKeyOne);
        final List<Object> betweenKeyTwoParams = new ArrayList<Object>(keyParams.size());
        betweenKeyTwoParams.addAll(keyParams.subList(0, keyParams.size() - 1));
        betweenKeyTwoParams.add(betweenKeyTwo);
        final Object keyOne = compositeIndexDefinition.createSingleValue(betweenKeyOneParams);
        if (keyOne == null)
            return null;
        final Object keyTwo = compositeIndexDefinition.createSingleValue(betweenKeyTwoParams);
        if (keyTwo == null)
            return null;
        cursor = index.iterateEntriesBetween(keyOne, leftInclusive, keyTwo, rightInclusive, ascSortOrder);
    }
    updateProfiler(iContext, index, keyParams, indexDefinition);
    return cursor;
}
Also used : OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) ArrayList(java.util.ArrayList) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor)

Example 45 with OIndexCursor

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

the class OQueryOperatorContainsKey method executeIndexQuery.

@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    OIndexCursor cursor;
    final OIndexInternal<?> internalIndex = index.getInternal();
    if (!internalIndex.canBeUsedInEqualityOperators())
        return null;
    if (indexDefinition.getParamCount() == 1) {
        if (!((indexDefinition instanceof OPropertyMapIndexDefinition) && ((OPropertyMapIndexDefinition) indexDefinition).getIndexBy() == OPropertyMapIndexDefinition.INDEX_BY.KEY))
            return null;
        final Object key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(keyParams.get(0));
        if (key == null)
            return null;
        final Object indexResult = index.get(key);
        if (indexResult == null || indexResult instanceof OIdentifiable)
            cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, key);
        else
            cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, key);
    } else {
        // in case of composite keys several items can be returned in case of we perform search
        // using part of composite key stored in index.
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        if (!((compositeIndexDefinition.getMultiValueDefinition() instanceof OPropertyMapIndexDefinition) && ((OPropertyMapIndexDefinition) compositeIndexDefinition.getMultiValueDefinition()).getIndexBy() == OPropertyMapIndexDefinition.INDEX_BY.KEY))
            return null;
        final Object keyOne = compositeIndexDefinition.createSingleValue(keyParams);
        if (keyOne == null)
            return null;
        if (internalIndex.hasRangeQuerySupport()) {
            final Object keyTwo = compositeIndexDefinition.createSingleValue(keyParams);
            cursor = index.iterateEntriesBetween(keyOne, true, keyTwo, true, ascSortOrder);
        } else {
            if (indexDefinition.getParamCount() == keyParams.size()) {
                final Object indexResult = index.get(keyOne);
                if (indexResult == null || indexResult instanceof OIdentifiable)
                    cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, keyOne);
                else
                    cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, keyOne);
            } else
                return null;
        }
    }
    updateProfiler(iContext, index, keyParams, indexDefinition);
    return cursor;
}
Also used : OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OPropertyMapIndexDefinition(com.orientechnologies.orient.core.index.OPropertyMapIndexDefinition) OIndexCursorCollectionValue(com.orientechnologies.orient.core.index.OIndexCursorCollectionValue) OIndexCursorSingleValue(com.orientechnologies.orient.core.index.OIndexCursorSingleValue) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexDefinitionMultiValue(com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Aggregations

OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)53 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)47 ORecordId (com.orientechnologies.orient.core.id.ORecordId)42 Test (org.testng.annotations.Test)41 HashSet (java.util.HashSet)40 OIndexTxAwareMultiValue (com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue)20 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)20 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)8 OCompositeIndexDefinition (com.orientechnologies.orient.core.index.OCompositeIndexDefinition)7 OIndexDefinitionMultiValue (com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue)6 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 OIndex (com.orientechnologies.orient.core.index.OIndex)3 OIndexCursorCollectionValue (com.orientechnologies.orient.core.index.OIndexCursorCollectionValue)3 OIndexCursorSingleValue (com.orientechnologies.orient.core.index.OIndexCursorSingleValue)3 Map (java.util.Map)3 Set (java.util.Set)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)1 OIndexFullText (com.orientechnologies.orient.core.index.OIndexFullText)1