Search in sources :

Example 1 with RuntimeIterator

use of org.apache.geode.cache.query.internal.RuntimeIterator 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 2 with RuntimeIterator

use of org.apache.geode.cache.query.internal.RuntimeIterator 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 3 with RuntimeIterator

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

the class FunctionalIndexCreationHelper method getModifiedDependentCompiledValue.

/**
   * This function is used to correct the complied value's dependency , in case the compiledValue is
   * dependent on the 0th RuntimeIterator in some way. Thus the dependent compiled value is prefixed
   * with the missing link so that it is derivable from the 0th iterator.
   */
private CompiledValue getModifiedDependentCompiledValue(ExecutionContext context, int currItrID, CompiledValue cv, boolean isDependent) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    if (cv instanceof CompiledIteratorDef) {
        CompiledIteratorDef iterDef = (CompiledIteratorDef) cv;
        RuntimeIterator rItr = (RuntimeIterator) context.getCurrentIterators().get(currItrID);
        String canonFrmClause = rItr.getDefinition();
        // TODO: original value of isDependent is always ignored
        isDependent = canonFrmClause.startsWith(this.canonicalizedIteratorNames[0]);
        return new CompiledIteratorDef(iterDef.getName(), rItr.getElementType(), getModifiedDependentCompiledValue(context, currItrID, iterDef.getCollectionExpr(), isDependent));
    } else if (cv instanceof CompiledPath) {
        CompiledPath path = (CompiledPath) cv;
        return new CompiledPath(getModifiedDependentCompiledValue(context, currItrID, path.getReceiver(), isDependent), path.getTailID());
    } else if (cv instanceof CompiledOperation) {
        CompiledOperation oper = (CompiledOperation) cv;
        List list = oper.getArguments();
        List newList = new ArrayList();
        for (Object aList : list) {
            CompiledValue cv1 = (CompiledValue) aList;
            StringBuilder sb = new StringBuilder();
            cv1.generateCanonicalizedExpression(sb, context);
            if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
                newList.add(getModifiedDependentCompiledValue(context, currItrID, cv1, true));
            } else {
                newList.add(getModifiedDependentCompiledValue(context, currItrID, cv1, false));
            }
        }
        // What if the receiver is null?
        CompiledValue rec = oper.getReceiver(context);
        if (rec == null) {
            if (isDependent) {
                rec = this.missingLink;
            }
            return new CompiledOperation(rec, oper.getMethodName(), newList);
        } else {
            return new CompiledOperation(getModifiedDependentCompiledValue(context, currItrID, rec, isDependent), oper.getMethodName(), newList);
        }
    } else if (cv instanceof CompiledFunction) {
        CompiledFunction cf = (CompiledFunction) cv;
        CompiledValue[] cvArray = cf.getArguments();
        int function = cf.getFunction();
        int len = cvArray.length;
        CompiledValue[] newCvArray = new CompiledValue[len];
        for (int i = 0; i < len; ++i) {
            CompiledValue cv1 = cvArray[i];
            StringBuilder sb = new StringBuilder();
            cv1.generateCanonicalizedExpression(sb, context);
            if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
                newCvArray[i] = getModifiedDependentCompiledValue(context, currItrID, cv1, true);
            } else {
                newCvArray[i] = getModifiedDependentCompiledValue(context, currItrID, cv1, false);
            }
        }
        return new CompiledFunction(newCvArray, function);
    } else if (cv instanceof CompiledID) {
        CompiledID id = (CompiledID) cv;
        RuntimeIterator rItr0 = (RuntimeIterator) context.getCurrentIterators().get(0);
        if (isDependent) {
            String name;
            if ((name = rItr0.getName()) != null && name.equals(id.getId())) {
                // The CompiledID is a RuneTimeIterator & so it needs to be replaced by the missing link
                return this.missingLink;
            } else {
                // The compiledID is a compiledPath
                return new CompiledPath(this.missingLink, id.getId());
            }
        } else {
            return cv;
        }
    } else if (cv instanceof CompiledIndexOperation) {
        CompiledIndexOperation co = (CompiledIndexOperation) cv;
        CompiledValue cv1 = co.getExpression();
        StringBuilder sb = new StringBuilder();
        cv1.generateCanonicalizedExpression(sb, context);
        if (sb.toString().startsWith(this.canonicalizedIteratorNames[0])) {
            cv1 = getModifiedDependentCompiledValue(context, currItrID, cv1, true);
        } else {
            cv1 = getModifiedDependentCompiledValue(context, currItrID, cv1, false);
        }
        return new CompiledIndexOperation(getModifiedDependentCompiledValue(context, currItrID, co.getReceiver(), isDependent), cv1);
    } else {
        return cv;
    }
}
Also used : CompiledIteratorDef(org.apache.geode.cache.query.internal.CompiledIteratorDef) CompiledID(org.apache.geode.cache.query.internal.CompiledID) CompiledFunction(org.apache.geode.cache.query.internal.CompiledFunction) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) ArrayList(java.util.ArrayList) CompiledPath(org.apache.geode.cache.query.internal.CompiledPath) CompiledIndexOperation(org.apache.geode.cache.query.internal.CompiledIndexOperation) CompiledOperation(org.apache.geode.cache.query.internal.CompiledOperation) ArrayList(java.util.ArrayList) List(java.util.List) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator)

Example 4 with RuntimeIterator

use of org.apache.geode.cache.query.internal.RuntimeIterator 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)

Example 5 with RuntimeIterator

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

the class CompactRangeIndex method verifyInnerAndOuterEntryValues.

/**
   * This evaluates the left and right side of a EQUI-JOIN where condition for which this Index was
   * used. Like, if condition is "p.ID = e.ID", {@link IndexInfo} will contain Left as p.ID, Right
   * as e.ID and operator as TOK_EQ. This method will evaluate p.ID OR e.ID based on if it is inner
   * or outer RegionEntry, and verify the p.ID = e.ID.
   * 
   * This method is called only for Memory indexstore
   * 
   * @return true if entry value and index value are consistent.
   */
protected boolean verifyInnerAndOuterEntryValues(IndexStoreEntry entry, ExecutionContext context, IndexInfo indexInfo, Object keyVal) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    // Verify index key in value only for memory index store
    CompactRangeIndex index = (CompactRangeIndex) indexInfo._getIndex();
    RuntimeIterator runtimeItr = index.getRuntimeIteratorForThisIndex(context, indexInfo);
    if (runtimeItr != null) {
        runtimeItr.setCurrent(((MemoryIndexStoreEntry) entry).getDeserializedValue());
    }
    return evaluateEntry(indexInfo, context, keyVal);
}
Also used : RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator)

Aggregations

RuntimeIterator (org.apache.geode.cache.query.internal.RuntimeIterator)13 List (java.util.List)7 ArrayList (java.util.ArrayList)5 CompiledIteratorDef (org.apache.geode.cache.query.internal.CompiledIteratorDef)4 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)4 PdxString (org.apache.geode.pdx.internal.PdxString)4 Iterator (java.util.Iterator)3 CompiledValue (org.apache.geode.cache.query.internal.CompiledValue)3 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)3 Collection (java.util.Collection)2 Map (java.util.Map)2 Set (java.util.Set)2 Region (org.apache.geode.cache.Region)2 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)2 CompiledID (org.apache.geode.cache.query.internal.CompiledID)2 CompiledIndexOperation (org.apache.geode.cache.query.internal.CompiledIndexOperation)2 CompiledOperation (org.apache.geode.cache.query.internal.CompiledOperation)2 CompiledPath (org.apache.geode.cache.query.internal.CompiledPath)2 QRegion (org.apache.geode.cache.query.internal.QRegion)2 BucketRegion (org.apache.geode.internal.cache.BucketRegion)2