Search in sources :

Example 11 with OCompositeIndexDefinition

use of com.orientechnologies.orient.core.index.OCompositeIndexDefinition 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 12 with OCompositeIndexDefinition

use of com.orientechnologies.orient.core.index.OCompositeIndexDefinition 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)

Example 13 with OCompositeIndexDefinition

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

the class OQueryOperatorIs method executeIndexQuery.

@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    final OIndexInternal<?> internalIndex = index.getInternal();
    OIndexCursor cursor;
    if (!internalIndex.canBeUsedInEqualityOperators())
        return null;
    if (indexDefinition.getParamCount() == 1) {
        final Object key;
        if (indexDefinition instanceof OIndexDefinitionMultiValue)
            key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(keyParams.get(0));
        else
            key = indexDefinition.createValue(keyParams);
        final Object indexResult;
        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 we perform search
        // using part of composite key stored in index
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        final Object keyOne = compositeIndexDefinition.createSingleValue(keyParams);
        final Object keyTwo = compositeIndexDefinition.createSingleValue(keyParams);
        if (internalIndex.hasRangeQuerySupport()) {
            cursor = index.iterateEntriesBetween(keyOne, true, keyTwo, true, ascSortOrder);
        } else {
            if (indexDefinition.getParamCount() == keyParams.size()) {
                final Object indexResult;
                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) 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)

Example 14 with OCompositeIndexDefinition

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

the class OQueryOperatorMajor 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 key;
        if (indexDefinition instanceof OIndexDefinitionMultiValue)
            key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(keyParams.get(0));
        else
            key = indexDefinition.createValue(keyParams);
        if (key == null)
            return null;
        cursor = index.iterateEntriesMajor(key, false, ascSortOrder);
    } else {
        // if we have situation like "field1 = 1 AND field2 > 2"
        // then we fetch collection which left not included boundary is the smallest composite key in the
        // index that contains keys with values field1=1 and field2=2 and which right included boundary
        // is the biggest composite key in the index that contains key with value field1=1.
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        final Object keyOne = compositeIndexDefinition.createSingleValue(keyParams);
        if (keyOne == null)
            return null;
        final Object keyTwo = compositeIndexDefinition.createSingleValue(keyParams.subList(0, keyParams.size() - 1));
        if (keyTwo == null)
            return null;
        cursor = index.iterateEntriesBetween(keyOne, false, keyTwo, true, ascSortOrder);
    }
    updateProfiler(iContext, index, keyParams, indexDefinition);
    return cursor;
}
Also used : OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexDefinitionMultiValue(com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue)

Example 15 with OCompositeIndexDefinition

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

the class OQueryOperatorMajorEquals 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 key;
        if (indexDefinition instanceof OIndexDefinitionMultiValue)
            key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(keyParams.get(0));
        else
            key = indexDefinition.createValue(keyParams);
        if (key == null)
            return null;
        cursor = index.iterateEntriesMajor(key, true, ascSortOrder);
    } else {
        // if we have situation like "field1 = 1 AND field2 >= 2"
        // then we fetch collection which left included boundary is the smallest composite key in the
        // index that contains keys with values field1=1 and field2=2 and which right included boundary
        // is the biggest composite key in the index that contains key with value field1=1.
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        final Object keyOne = compositeIndexDefinition.createSingleValue(keyParams);
        if (keyOne == null)
            return null;
        final Object keyTwo = compositeIndexDefinition.createSingleValue(keyParams.subList(0, keyParams.size() - 1));
        if (keyTwo == null)
            return null;
        cursor = index.iterateEntriesBetween(keyOne, true, keyTwo, true, ascSortOrder);
    }
    updateProfiler(iContext, index, keyParams, indexDefinition);
    return cursor;
}
Also used : OCompositeIndexDefinition(com.orientechnologies.orient.core.index.OCompositeIndexDefinition) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndexCursor(com.orientechnologies.orient.core.index.OIndexCursor) OIndexDefinitionMultiValue(com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue)

Aggregations

OCompositeIndexDefinition (com.orientechnologies.orient.core.index.OCompositeIndexDefinition)29 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)26 OIndex (com.orientechnologies.orient.core.index.OIndex)16 Test (org.testng.annotations.Test)16 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)11 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)7 OPropertyIndexDefinition (com.orientechnologies.orient.core.index.OPropertyIndexDefinition)7 OIndexDefinitionMultiValue (com.orientechnologies.orient.core.index.OIndexDefinitionMultiValue)6 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)6 HashSet (java.util.HashSet)4 OIndexManager (com.orientechnologies.orient.core.index.OIndexManager)3 OPropertyMapIndexDefinition (com.orientechnologies.orient.core.index.OPropertyMapIndexDefinition)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OIndexCursorCollectionValue (com.orientechnologies.orient.core.index.OIndexCursorCollectionValue)2 OIndexCursorSingleValue (com.orientechnologies.orient.core.index.OIndexCursorSingleValue)2 OIndexManagerProxy (com.orientechnologies.orient.core.index.OIndexManagerProxy)2 OPropertyListIndexDefinition (com.orientechnologies.orient.core.index.OPropertyListIndexDefinition)2 OPropertyRidBagIndexDefinition (com.orientechnologies.orient.core.index.OPropertyRidBagIndexDefinition)2 OProgressListener (com.orientechnologies.common.listener.OProgressListener)1