Search in sources :

Example 1 with QueryObserver

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

the class RangeIndex method addValuesToResult.

/**
   * 
   * @param entriesMap SortedMap object containing the indexed key as the key & the value being
   *        RegionEntryToValues Map object containing the indexed results
   * @param result Index Results holder Collection used for fetching the index results
   * @param keysToRemove Set containing the index keys for which index results should not be taken
   *        from the entriesMap
   */
private void addValuesToResult(Object entriesMap, Collection result, Set keysToRemove, int limit, ExecutionContext context) {
    if (entriesMap == null || result == null)
        return;
    QueryObserver observer = QueryObserverHolder.getInstance();
    if (verifyLimit(result, limit)) {
        observer.limitAppliedAtIndexLevel(this, limit, result);
        return;
    }
    if (entriesMap instanceof SortedMap) {
        if (((Map) entriesMap).isEmpty()) {
            // bug#40514
            return;
        }
        SortedMap sortedMap = (SortedMap) entriesMap;
        Iterator entriesIter = sortedMap.entrySet().iterator();
        Map.Entry entry = null;
        while (entriesIter.hasNext()) {
            entry = (Map.Entry) entriesIter.next();
            Object key = entry.getKey();
            if (keysToRemove == null || !keysToRemove.remove(key)) {
                RegionEntryToValuesMap rvMap = (RegionEntryToValuesMap) entry.getValue();
                rvMap.addValuesToCollection(result, limit, context);
                if (verifyLimit(result, limit)) {
                    observer.limitAppliedAtIndexLevel(this, limit, result);
                    return;
                }
            }
        }
    } else if (entriesMap instanceof RegionEntryToValuesMap) {
        // We have already been passed the collection to add, assuming keys to remove is null or
        // already been applied
        RegionEntryToValuesMap rvMap = (RegionEntryToValuesMap) entriesMap;
        rvMap.addValuesToCollection(result, limit, context);
        if (limit != -1 && result.size() == limit) {
            observer.limitAppliedAtIndexLevel(this, limit, result);
            return;
        }
    } else {
        throw new RuntimeException(LocalizedStrings.RangeIndex_PROBLEM_IN_INDEX_QUERY.toLocalizedString());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SortedMap(java.util.SortedMap) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) Iterator(java.util.Iterator) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) SortedMap(java.util.SortedMap)

Example 2 with QueryObserver

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

the class RangeIndex method addValuesToResult.

private void addValuesToResult(Object entriesMap, Collection result, Object keyToRemove, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    boolean limitApplied = false;
    if (entriesMap == null || result == null) {
        if (verifyLimit(result, limit)) {
            QueryObserver observer = QueryObserverHolder.getInstance();
            if (observer != null) {
                observer.limitAppliedAtIndexLevel(this, limit, result);
            }
        }
        return;
    }
    QueryObserver observer = QueryObserverHolder.getInstance();
    if (entriesMap instanceof SortedMap) {
        Iterator entriesIter = ((Map) entriesMap).entrySet().iterator();
        Map.Entry entry = null;
        boolean foundKeyToRemove = false;
        // null maps
        if (keyToRemove == null) {
            foundKeyToRemove = true;
        }
        while (entriesIter.hasNext()) {
            entry = (Map.Entry) entriesIter.next();
            // Object key = entry.getKey();
            if (foundKeyToRemove || !keyToRemove.equals(entry.getKey())) {
                RegionEntryToValuesMap rvMap = (RegionEntryToValuesMap) entry.getValue();
                rvMap.addValuesToCollection(result, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit);
                if (verifyLimit(result, limit)) {
                    observer.limitAppliedAtIndexLevel(this, limit, result);
                    break;
                }
            } else {
                foundKeyToRemove = true;
            }
        }
    } else if (entriesMap instanceof RegionEntryToValuesMap) {
        RegionEntryToValuesMap rvMap = (RegionEntryToValuesMap) entriesMap;
        rvMap.addValuesToCollection(result, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, limit);
    } else {
        throw new RuntimeException(LocalizedStrings.RangeIndex_PROBLEM_IN_INDEX_QUERY.toLocalizedString());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SortedMap(java.util.SortedMap) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) Iterator(java.util.Iterator) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) SortedMap(java.util.SortedMap)

Example 3 with QueryObserver

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

the class PrimaryKeyIndex method lockedQuery.

@Override
void lockedQuery(Object key, int operator, Collection results, Set keysToRemove, ExecutionContext context) throws TypeMismatchException {
    assert keysToRemove == null;
    int limit = -1;
    // Key cannot be PdxString in a region
    if (key instanceof PdxString) {
        key = key.toString();
    }
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    QueryObserver observer = QueryObserverHolder.getInstance();
    if (limit != -1 && results.size() == limit) {
        observer.limitAppliedAtIndexLevel(this, limit, results);
        return;
    }
    switch(operator) {
        case OQLLexerTokenTypes.TOK_EQ:
            {
                if (key != null && key != QueryService.UNDEFINED) {
                    Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false);
                    if (entry != null) {
                        Object value = entry.getValue();
                        if (value != null) {
                            results.add(value);
                        }
                    }
                }
                break;
            }
        case OQLLexerTokenTypes.TOK_NE_ALT:
        case OQLLexerTokenTypes.TOK_NE:
            {
                // add all btree values
                Set values = (Set) getRegion().values();
                // Add data one more than the limit
                if (limit != -1) {
                    ++limit;
                }
                // results.addAll(values);
                Iterator iter = values.iterator();
                while (iter.hasNext()) {
                    // Check if query execution on this thread is canceled.
                    QueryMonitor.isQueryExecutionCanceled();
                    results.add(iter.next());
                    if (limit != -1 && results.size() == limit) {
                        observer.limitAppliedAtIndexLevel(this, limit, results);
                        return;
                    }
                }
                boolean removeOneRow = limit != -1;
                if (key != null && key != QueryService.UNDEFINED) {
                    Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false);
                    if (entry != null) {
                        if (entry.getValue() != null) {
                            results.remove(entry.getValue());
                            removeOneRow = false;
                        }
                    }
                }
                if (removeOneRow) {
                    Iterator itr = results.iterator();
                    if (itr.hasNext()) {
                        itr.next();
                        itr.remove();
                    }
                }
                break;
            }
        default:
            {
                throw new IllegalArgumentException(LocalizedStrings.PrimaryKeyIndex_INVALID_OPERATOR.toLocalizedString());
            }
    }
    // end switch
    numUses++;
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) RegionEntry(org.apache.geode.internal.cache.RegionEntry) Set(java.util.Set) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) Iterator(java.util.Iterator) PdxString(org.apache.geode.pdx.internal.PdxString)

Example 4 with QueryObserver

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

the class PrimaryKeyIndex 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 {
    QueryObserver observer = QueryObserverHolder.getInstance();
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit.booleanValue()) {
        limit = ((Integer) context.cacheGet(CompiledValue.RESULT_LIMIT)).intValue();
    }
    if (limit != -1 && results.size() == limit) {
        observer.limitAppliedAtIndexLevel(this, limit, results);
        return;
    }
    // Key cannot be PdxString in a region
    if (key instanceof PdxString) {
        key = key.toString();
    }
    switch(operator) {
        case OQLLexerTokenTypes.TOK_EQ:
            {
                if (key != null && key != QueryService.UNDEFINED) {
                    Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false);
                    if (entry != null) {
                        Object value = entry.getValue();
                        if (value != null) {
                            boolean ok = true;
                            if (runtimeItr != null) {
                                runtimeItr.setCurrent(value);
                                ok = QueryUtils.applyCondition(iterOps, context);
                            }
                            if (ok) {
                                applyProjection(projAttrib, context, results, value, intermediateResults, isIntersection);
                            }
                        }
                    }
                }
                break;
            }
        case OQLLexerTokenTypes.TOK_NE_ALT:
        case OQLLexerTokenTypes.TOK_NE:
            {
                // add all btree values
                Set entries = (Set) getRegion().entrySet();
                Iterator itr = entries.iterator();
                while (itr.hasNext()) {
                    Map.Entry entry = (Map.Entry) itr.next();
                    if (key != null && key != QueryService.UNDEFINED && key.equals(entry.getKey())) {
                        continue;
                    }
                    Object val = entry.getValue();
                    // TODO: is this correct. What should be the behaviour of null values?
                    if (val != null) {
                        boolean ok = true;
                        if (runtimeItr != null) {
                            runtimeItr.setCurrent(val);
                            ok = QueryUtils.applyCondition(iterOps, context);
                        }
                        if (ok) {
                            applyProjection(projAttrib, context, results, val, intermediateResults, isIntersection);
                        }
                        if (limit != -1 && results.size() == limit) {
                            observer.limitAppliedAtIndexLevel(this, limit, results);
                            break;
                        }
                    }
                }
                break;
            }
        default:
            {
                throw new IllegalArgumentException("Invalid Operator");
            }
    }
    // end switch
    numUses++;
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) RegionEntry(org.apache.geode.internal.cache.RegionEntry) Set(java.util.Set) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) Iterator(java.util.Iterator) PdxString(org.apache.geode.pdx.internal.PdxString) Map(java.util.Map)

Example 5 with QueryObserver

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

the class CompactRangeIndex method addToResultsFromEntries.

/*
   * 
   * @param lowerBoundKey the index key to match on for a lower bound on a ranged query, otherwise
   * the key to match on
   * 
   * @param upperBoundKey the index key to match on for an upper bound on a ranged query, otherwise
   * null
   * 
   * @param lowerBoundOperator the operator to use to determine a match against the lower bound
   * 
   * @param upperBoundOperator the operator to use to determine a match against the upper bound
   */
private void addToResultsFromEntries(Object lowerBoundKey, Object upperBoundKey, int lowerBoundOperator, int upperBoundOperator, CloseableIterator<IndexStoreEntry> entriesIter, Collection result, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    QueryObserver observer = QueryObserverHolder.getInstance();
    boolean limitApplied = false;
    if (entriesIter == null || (limitApplied = verifyLimit(result, limit))) {
        if (limitApplied) {
            if (observer != null) {
                observer.limitAppliedAtIndexLevel(this, limit, result);
            }
        }
        return;
    }
    Set seenKey = null;
    if (IndexManager.IS_TEST_EXPANSION) {
        seenKey = new HashSet();
    }
    while (entriesIter.hasNext()) {
        try {
            // Check if query execution on this thread is canceled.
            QueryMonitor.isQueryExecutionCanceled();
            if (IndexManager.testHook != null) {
                if (this.region.getCache().getLogger().fineEnabled()) {
                    this.region.getCache().getLogger().fine("IndexManager TestHook is set in addToResultsFromEntries.");
                }
                IndexManager.testHook.hook(11);
            }
            IndexStoreEntry indexEntry = null;
            try {
                indexEntry = entriesIter.next();
            } catch (NoSuchElementException ignore) {
                // Continue from while.
                continue;
            }
            Object value = indexEntry.getDeserializedValue();
            if (IndexManager.IS_TEST_EXPANSION) {
                Object rk = indexEntry.getDeserializedRegionKey();
                if (seenKey.contains(rk)) {
                    continue;
                }
                seenKey.add(rk);
                List expandedResults = expandValue(context, lowerBoundKey, upperBoundKey, lowerBoundOperator, upperBoundOperator, value);
                Iterator iterator = ((Collection) expandedResults).iterator();
                while (iterator.hasNext()) {
                    value = iterator.next();
                    if (value != null) {
                        boolean ok = true;
                        if (runtimeItr != null) {
                            runtimeItr.setCurrent(value);
                        }
                        if (ok && runtimeItr != null && iterOps != null) {
                            ok = QueryUtils.applyCondition(iterOps, context);
                        }
                        if (ok) {
                            if (context != null && context.isCqQueryContext()) {
                                result.add(new CqEntry(indexEntry.getDeserializedRegionKey(), value));
                            } else {
                                applyProjection(projAttrib, context, result, value, intermediateResults, isIntersection);
                            }
                            if (verifyLimit(result, limit)) {
                                observer.limitAppliedAtIndexLevel(this, limit, result);
                                return;
                            }
                        }
                    }
                }
            } else {
                if (value != null) {
                    boolean ok = true;
                    if (indexEntry.isUpdateInProgress() || TEST_ALWAYS_UPDATE_IN_PROGRESS) {
                        IndexInfo indexInfo = (IndexInfo) context.cacheGet(CompiledValue.INDEX_INFO);
                        if (runtimeItr == null) {
                            runtimeItr = getRuntimeIteratorForThisIndex(context, indexInfo);
                            if (runtimeItr == null) {
                                // could not match index with iterator
                                throw new QueryInvocationTargetException("Query alias's must be used consistently");
                            }
                        }
                        runtimeItr.setCurrent(value);
                        // Verify index key in region entry value.
                        ok = evaluateEntry((IndexInfo) indexInfo, context, null);
                    }
                    if (runtimeItr != null) {
                        runtimeItr.setCurrent(value);
                    }
                    if (ok && runtimeItr != null && iterOps != null) {
                        ok = QueryUtils.applyCondition(iterOps, context);
                    }
                    if (ok) {
                        if (context != null && context.isCqQueryContext()) {
                            result.add(new CqEntry(indexEntry.getDeserializedRegionKey(), value));
                        } else {
                            if (IndexManager.testHook != null) {
                                IndexManager.testHook.hook(200);
                            }
                            applyProjection(projAttrib, context, result, value, intermediateResults, isIntersection);
                        }
                        if (verifyLimit(result, limit)) {
                            observer.limitAppliedAtIndexLevel(this, limit, result);
                            return;
                        }
                    }
                }
            }
        } catch (ClassCastException | EntryDestroyedException ignore) {
        // ignore it
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) MemoryIndexStoreEntry(org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IndexInfo(org.apache.geode.cache.query.internal.IndexInfo) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) CqEntry(org.apache.geode.cache.query.internal.CqEntry) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Aggregations

QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)71 SelectResults (org.apache.geode.cache.query.SelectResults)55 Test (org.junit.Test)48 Region (org.apache.geode.cache.Region)47 Query (org.apache.geode.cache.query.Query)42 Portfolio (org.apache.geode.cache.query.data.Portfolio)42 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)41 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)38 Index (org.apache.geode.cache.query.Index)31 QueryService (org.apache.geode.cache.query.QueryService)25 LocalRegion (org.apache.geode.internal.cache.LocalRegion)23 Collection (java.util.Collection)17 Cache (org.apache.geode.cache.Cache)13 AttributesFactory (org.apache.geode.cache.AttributesFactory)12 Position (org.apache.geode.cache.query.data.Position)9 Iterator (java.util.Iterator)8 RegionAttributes (org.apache.geode.cache.RegionAttributes)8 Set (java.util.Set)7 Host (org.apache.geode.test.dunit.Host)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7