Search in sources :

Example 1 with CqEntry

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

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

the class HashIndex method addValueToResultSet.

private void addValueToResultSet(RegionEntry re, Collection result, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, QueryObserver observer, long iteratorCreationTime) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    Object value = getTargetObject(re);
    if (value != null) {
        boolean ok = true;
        // we will reevaluate to be sure the value still matches the key
        if (re.isUpdateInProgress() || IndexManager.needsRecalculation(iteratorCreationTime, re.getLastModified())) {
            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, 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(re.getKey(), value));
            } else {
                applyProjection(projAttrib, context, result, value, intermediateResults, isIntersection);
            }
            if (limit != -1 && result.size() == limit) {
                observer.limitAppliedAtIndexLevel(this, limit, result);
                return;
            }
        }
    }
}
Also used : CqEntry(org.apache.geode.cache.query.internal.CqEntry) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) StoredObject(org.apache.geode.internal.offheap.StoredObject) IndexInfo(org.apache.geode.cache.query.internal.IndexInfo)

Example 3 with CqEntry

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

the class BaseCommandQuery method sendResultsAsObjectArray.

private void sendResultsAsObjectArray(SelectResults selectResults, int numberOfChunks, ServerConnection servConn, boolean isStructs, CollectionType collectionType, String queryString, ServerCQ cqQuery, boolean sendCqResultsWithKey, boolean sendResults) throws IOException {
    int resultIndex = 0;
    // For CQ only as we dont want CQEntries which have null values.
    int cqResultIndex = 0;
    Object[] objs = selectResults.toArray();
    for (int j = 0; j < numberOfChunks; j++) {
        boolean incompleteArray = false;
        if (logger.isTraceEnabled()) {
            logger.trace("{}: Creating chunk: {}", servConn.getName(), j);
        }
        Object[] results = new Object[MAXIMUM_CHUNK_SIZE];
        for (int i = 0; i < MAXIMUM_CHUNK_SIZE; i++) {
            if ((resultIndex) == selectResults.size()) {
                incompleteArray = true;
                break;
            }
            if (logger.isTraceEnabled()) {
                logger.trace("{}: Adding entry [{}] to query results: {}", servConn.getName(), resultIndex, objs[resultIndex]);
            }
            if (cqQuery != null) {
                CqEntry e = (CqEntry) objs[resultIndex];
                // The value may have become null because of entry invalidation.
                if (e.getValue() == null) {
                    resultIndex++;
                    // i will get incremented anyway so we need to decrement it back so
                    // that results[i] is not null.
                    i--;
                    continue;
                }
                // need to update the Results cache.
                if (!cqQuery.isPR()) {
                    cqQuery.addToCqResultKeys(e.getKey());
                }
                // Add to the Results object array.
                if (sendCqResultsWithKey) {
                    results[i] = e.getKeyValuePair();
                } else {
                    results[i] = e.getValue();
                }
            } else {
                // instance check added to fix bug 40516.
                if (isStructs && (objs[resultIndex] instanceof Struct)) {
                    results[i] = ((Struct) objs[resultIndex]).getFieldValues();
                } else {
                    results[i] = objs[resultIndex];
                }
            }
            resultIndex++;
            cqResultIndex++;
        }
        // number of entries in the result set.
        if (incompleteArray) {
            Object[] newResults;
            if (cqQuery != null) {
                newResults = new Object[cqResultIndex % MAXIMUM_CHUNK_SIZE];
            } else {
                newResults = new Object[resultIndex % MAXIMUM_CHUNK_SIZE];
            }
            for (int i = 0; i < newResults.length; i++) {
                newResults[i] = results[i];
            }
            results = newResults;
        }
        if (sendResults) {
            writeQueryResponseChunk(results, collectionType, (resultIndex == selectResults.size()), servConn);
            if (logger.isDebugEnabled()) {
                logger.debug("{}: Sent chunk ({} of {}) of query response for query: {}", servConn.getName(), (j + 1), numberOfChunks, queryString);
            }
        }
        // break out of loop here only.
        if (resultIndex == selectResults.size()) {
            break;
        }
    }
}
Also used : CqEntry(org.apache.geode.cache.query.internal.CqEntry) Struct(org.apache.geode.cache.query.Struct)

Example 4 with CqEntry

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

the class BaseCommandQuery method sendResultsAsObjectPartList.

private void sendResultsAsObjectPartList(int numberOfChunks, ServerConnection servConn, List objs, boolean isStructs, CollectionType collectionType, String queryString, ServerCQ cqQuery, boolean sendCqResultsWithKey, boolean sendResults) throws IOException {
    int resultIndex = 0;
    Object result = null;
    for (int j = 0; j < numberOfChunks; j++) {
        if (logger.isTraceEnabled()) {
            logger.trace("{}: Creating chunk: {}", servConn.getName(), j);
        }
        ObjectPartList serializedObjs = new ObjectPartList(MAXIMUM_CHUNK_SIZE, false);
        for (int i = 0; i < MAXIMUM_CHUNK_SIZE; i++) {
            if ((resultIndex) == objs.size()) {
                break;
            }
            if (logger.isTraceEnabled()) {
                logger.trace("{}: Adding entry [{}] to query results: {}", servConn.getName(), resultIndex, objs.get(resultIndex));
            }
            if (cqQuery != null) {
                CqEntry e = (CqEntry) objs.get(resultIndex);
                // The value may have become null because of entry invalidation.
                if (e.getValue() == null) {
                    resultIndex++;
                    continue;
                }
                // need to update the Results cache.
                if (!cqQuery.isPR()) {
                    cqQuery.addToCqResultKeys(e.getKey());
                }
                // Add to the Results object array.
                if (sendCqResultsWithKey) {
                    result = e.getKeyValuePair();
                } else {
                    result = e.getValue();
                }
            } else {
                result = objs.get(resultIndex);
            }
            if (sendResults) {
                addToObjectPartList(serializedObjs, result, collectionType, false, servConn, isStructs);
            }
            resultIndex++;
        }
        if (sendResults) {
            writeQueryResponseChunk(serializedObjs, collectionType, ((j + 1) == numberOfChunks), servConn);
            if (logger.isDebugEnabled()) {
                logger.debug("{}: Sent chunk ({} of {}) of query response for query: {}", servConn.getName(), (j + 1), numberOfChunks, queryString);
            }
        }
    }
}
Also used : CqEntry(org.apache.geode.cache.query.internal.CqEntry)

Aggregations

CqEntry (org.apache.geode.cache.query.internal.CqEntry)4 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)2 IndexInfo (org.apache.geode.cache.query.internal.IndexInfo)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Set (java.util.Set)1 EntryDestroyedException (org.apache.geode.cache.EntryDestroyedException)1 Struct (org.apache.geode.cache.query.Struct)1 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)1 RuntimeIterator (org.apache.geode.cache.query.internal.RuntimeIterator)1 IndexStoreEntry (org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry)1 MemoryIndexStoreEntry (org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry)1 CloseableIterator (org.apache.geode.internal.cache.persistence.query.CloseableIterator)1 StoredObject (org.apache.geode.internal.offheap.StoredObject)1