Search in sources :

Example 6 with CompiledSortCriterion

use of org.apache.geode.cache.query.internal.CompiledSortCriterion in project geode by apache.

the class CompactRangeIndex method evaluate.

private void evaluate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, boolean applyOrderBy, List orderByAttribs) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    boolean multiColOrderBy = false;
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    key = TypeUtils.indexKeyFor(key);
    if (key == null) {
        key = IndexManager.NULL;
    }
    boolean asc = true;
    if (applyOrderBy) {
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttribs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttribs.size() > 1;
    }
    CloseableIterator<IndexStoreEntry> iterator = null;
    try {
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                assert keysToRemove.isEmpty();
                iterator = indexStore.get(key);
                addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                break;
            case OQLLexerTokenTypes.TOK_LT:
                {
                    if (asc) {
                        iterator = indexStore.iterator(null, true, key, false, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(null, true, key, false, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_LE:
                {
                    if (asc) {
                        iterator = indexStore.iterator(null, true, key, true, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(null, true, key, true, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_GT:
                {
                    if (asc) {
                        iterator = indexStore.iterator(key, false, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(key, false, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_GE:
                {
                    if (asc) {
                        iterator = indexStore.iterator(key, true, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(key, true, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    keysToRemove.add(key);
                    if (asc) {
                        iterator = indexStore.iterator(keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    // If the key is not null, then add the nulls to the results as this is a not equals query
                    if (!IndexManager.NULL.equals(key)) {
                        // we pass in the operator TOK_EQ because we want to add results where the key is equal
                        // to NULL
                        addToResultsFromEntries(IndexManager.NULL, OQLLexerTokenTypes.TOK_EQ, indexStore.get(IndexManager.NULL), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    }
                    // equals query
                    if (!QueryService.UNDEFINED.equals(key)) {
                        // we pass in the operator TOK_EQ because we want to add results where the key is equal
                        // to UNDEFINED
                        addToResultsFromEntries(QueryService.UNDEFINED, OQLLexerTokenTypes.TOK_EQ, indexStore.get(QueryService.UNDEFINED), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    }
                }
                break;
            default:
                throw new AssertionError("Operator = " + operator);
        }
    // end switch
    } catch (ClassCastException ex) {
        if (operator == OQLLexerTokenTypes.TOK_EQ) {
            // result is empty set
            return;
        } else if (operator == OQLLexerTokenTypes.TOK_NE || operator == OQLLexerTokenTypes.TOK_NE_ALT) {
            // put all in result
            keysToRemove.add(key);
            try {
                if (asc) {
                    iterator = indexStore.iterator(keysToRemove);
                } else {
                    iterator = indexStore.descendingIterator(keysToRemove);
                }
                addToResultsFromEntries(key, OQLLexerTokenTypes.TOK_NE, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
            } finally {
                if (iterator != null) {
                    iterator.close();
                }
            }
        } else {
            // otherwise throw exception
            throw new TypeMismatchException("", ex);
        }
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) MemoryIndexStoreEntry(org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) HashSet(java.util.HashSet)

Example 7 with CompiledSortCriterion

use of org.apache.geode.cache.query.internal.CompiledSortCriterion in project geode by apache.

the class CompactRangeIndex method lockedQuery.

/** Method called while appropriate lock held on index */
void lockedQuery(Object lowerBoundKey, int lowerBoundOperator, Object upperBoundKey, int upperBoundOperator, Collection results, Set keysToRemove, ExecutionContext context) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    lowerBoundKey = TypeUtils.indexKeyFor(lowerBoundKey);
    upperBoundKey = TypeUtils.indexKeyFor(upperBoundKey);
    boolean lowerBoundInclusive = lowerBoundOperator == OQLLexerTokenTypes.TOK_GE;
    boolean upperBoundInclusive = upperBoundOperator == OQLLexerTokenTypes.TOK_LE;
    // LowerBound Key inclusive , Upper bound key exclusive
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
    List orderByAttrs;
    boolean asc = true;
    boolean multiColOrderBy = false;
    if (orderByClause != null && orderByClause) {
        orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttrs.size() > 1;
    }
    // return if the index map is still empty at this stage
    if (isEmpty()) {
        return;
    }
    lowerBoundKey = getPdxStringForIndexedPdxKeys(lowerBoundKey);
    upperBoundKey = getPdxStringForIndexedPdxKeys(upperBoundKey);
    if (keysToRemove == null) {
        keysToRemove = new HashSet();
    }
    CloseableIterator<IndexStoreEntry> iterator = null;
    try {
        if (asc) {
            iterator = indexStore.iterator(lowerBoundKey, lowerBoundInclusive, upperBoundKey, upperBoundInclusive, keysToRemove);
        } else {
            iterator = indexStore.descendingIterator(lowerBoundKey, lowerBoundInclusive, upperBoundKey, upperBoundInclusive, keysToRemove);
        }
        addToResultsFromEntries(lowerBoundKey, upperBoundKey, lowerBoundOperator, upperBoundOperator, iterator, results, null, null, context, null, null, true, multiColOrderBy ? -1 : limit);
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) MemoryIndexStoreEntry(org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 8 with CompiledSortCriterion

use of org.apache.geode.cache.query.internal.CompiledSortCriterion in project geode by apache.

the class HashIndex method evaluate.

private void evaluate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, boolean applyOrderBy, List orderByAttribs) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    boolean multiColOrderBy = false;
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    key = TypeUtils.indexKeyFor(key);
    if (key == null) {
        key = IndexManager.NULL;
    }
    boolean asc = true;
    if (applyOrderBy) {
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttribs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttribs.size() > 1;
    }
    try {
        long iteratorCreationTime = GemFireCacheImpl.getInstance().cacheTimeMillis();
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                assert keysToRemove.isEmpty();
                addToResultsFromEntries(this.entriesSet.get(key), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
                break;
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    keysToRemove.add(key);
                    addToResultsFromEntries(this.entriesSet.getAllNotMatching(keysToRemove), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
                }
                break;
            default:
                throw new AssertionError("Operator = " + operator);
        }
    // end switch
    } catch (ClassCastException ex) {
        if (operator == OQLLexerTokenTypes.TOK_EQ) {
            // set
            return;
        } else if (operator == OQLLexerTokenTypes.TOK_NE || operator == OQLLexerTokenTypes.TOK_NE_ALT) {
            // put
            keysToRemove.add(key);
            long iteratorCreationTime = GemFireCacheImpl.getInstance().cacheTimeMillis();
            addToResultsFromEntries(this.entriesSet.getAllNotMatching(keysToRemove), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
        } else {
            // otherwise throw exception
            throw new TypeMismatchException("", ex);
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) HashSet(java.util.HashSet)

Example 9 with CompiledSortCriterion

use of org.apache.geode.cache.query.internal.CompiledSortCriterion in project geode by apache.

the class HashIndex method lockedQueryPrivate.

/** Method called while appropriate lock held on index */
private void lockedQueryPrivate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
    boolean applyOrderBy = false;
    List orderByAttrs = null;
    if (orderByClause != null && orderByClause) {
        orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
        applyOrderBy = true;
    }
    evaluate(key, operator, results, iterOps, runtimeItr, context, keysToRemove, projAttrib, intermediateResults, isIntersection, limit, applyOrderBy, orderByAttrs);
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 10 with CompiledSortCriterion

use of org.apache.geode.cache.query.internal.CompiledSortCriterion in project geode by apache.

the class RangeIndex method lockedQuery.

void lockedQuery(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, List projAttrib, SelectResults intermediateResults, boolean isIntersection) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
    boolean multiColOrderBy = false;
    List orderByAttrs = null;
    boolean asc = true;
    boolean applyOrderBy = false;
    if (orderByClause != null && orderByClause) {
        orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttrs.size() > 1;
        applyOrderBy = true;
    }
    if (key == null) {
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                {
                    nullMappedEntries.addValuesToCollection(results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit);
                    break;
                }
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    // add all btree values
                    NavigableMap sm = this.valueToEntriesMap;
                    if (!asc) {
                        sm = sm.descendingMap();
                    }
                    // keysToRemove should be null, meaning we aren't removing any keys
                    addValuesToResult(sm, results, null, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    undefinedMappedEntries.addValuesToCollection(results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit);
                    break;
                }
            default:
                {
                    throw new IllegalArgumentException("Invalid Operator");
                }
        }
    // end switch
    } else if (key == QueryService.UNDEFINED) {
        // do nothing
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                {
                    undefinedMappedEntries.addValuesToCollection(results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit);
                    break;
                }
            case OQLLexerTokenTypes.TOK_NE:
            case OQLLexerTokenTypes.TOK_NE_ALT:
                {
                    // add all btree values
                    NavigableMap sm = this.valueToEntriesMap;
                    if (!asc) {
                        sm = sm.descendingMap();
                    }
                    // keysToRemove should be null
                    addValuesToResult(sm, results, null, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    nullMappedEntries.addValuesToCollection(results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit);
                    break;
                }
            default:
                {
                    throw new IllegalArgumentException("Invalid Operator");
                }
        }
    // end switch
    } else {
        // return if the index map is still empty at this stage
        if (isEmpty()) {
            return;
        }
        key = getPdxStringForIndexedPdxKeys(key);
        evaluate(key, operator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit, applyOrderBy, orderByAttrs);
    }
// end else
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CompiledSortCriterion (org.apache.geode.cache.query.internal.CompiledSortCriterion)12 ArrayList (java.util.ArrayList)6 List (java.util.List)6 HashSet (java.util.HashSet)5 NavigableMap (java.util.NavigableMap)4 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)4 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)4 Struct (org.apache.geode.cache.query.Struct)3 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)3 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)3 Region (org.apache.geode.cache.Region)2 Query (org.apache.geode.cache.query.Query)2 QueryService (org.apache.geode.cache.query.QueryService)2 SelectResults (org.apache.geode.cache.query.SelectResults)2 Portfolio (org.apache.geode.cache.query.data.Portfolio)2 IndexStoreEntry (org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry)2 MemoryIndexStoreEntry (org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1