Search in sources :

Example 11 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class AllGroupJunction method evaluateOrJunction.

/**
   * Evaluates the individual GroupJunctions and CompositeGroupJunctions and expands the individual
   * results so obtained to the query from clause iterator level ( i.e top level iterators). The
   * expanded results so obtained are then merged (union) to get the ORed results.The evaluated
   * result of an AllGroupJunction will always be of the query from clause iterator level (top
   * level) which can be ORed or ANDd with filter evaluable subtree CompiledJunction.
   * 
   * @param context ExecutionContext object
   * @return SelectResults object
   * @throws FunctionDomainException
   * @throws TypeMismatchException
   * @throws NameResolutionException
   * @throws QueryInvocationTargetException
   */
private SelectResults evaluateOrJunction(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    // int len = this.abstractGroupOrRangeJunctions.size();
    // Asif : Create an array of SelectResults for each of the GroupJunction
    // For each Group Junction there will be a corresponding array of
    // RuntimeIterators which will map to the fields of the ResultSet obtained
    // from Group Junction
    SelectResults[] grpResults = new SelectResults[1];
    List finalList = context.getCurrentIterators();
    RuntimeIterator[][] itrsForResultFields = new RuntimeIterator[1][];
    CompiledValue gj = null;
    Iterator junctionItr = this.abstractGroupOrRangeJunctions.iterator();
    List grpItrs = null;
    RuntimeIterator tempItr = null;
    SelectResults intermediateResults = null;
    while (junctionItr.hasNext()) {
        List expansionList = new LinkedList(finalList);
        gj = (CompiledValue) junctionItr.next();
        grpResults[0] = ((Filter) gj).filterEvaluate(context, null);
        grpItrs = (gj instanceof CompositeGroupJunction) ? QueryUtils.getDependentItrChainForIndpndntItrs(((CompositeGroupJunction) gj).getIndependentIteratorsOfCJ(), context) : context.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(((AbstractGroupOrRangeJunction) gj).getIndependentIteratorForGroup()[0]);
        itrsForResultFields[0] = new RuntimeIterator[grpItrs.size()];
        Iterator grpItr = grpItrs.iterator();
        int k = 0;
        while (grpItr.hasNext()) {
            tempItr = (RuntimeIterator) grpItr.next();
            itrsForResultFields[0][k++] = tempItr;
            expansionList.remove(tempItr);
        }
        SelectResults expandedResult = QueryUtils.cartesian(grpResults, itrsForResultFields, expansionList, finalList, context, null);
        intermediateResults = (intermediateResults == null) ? expandedResult : QueryUtils.union(expandedResult, intermediateResults, context);
    }
    return intermediateResults;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults)

Example 12 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class AllGroupJunction method evaluateAndJunction.

/**
   * Asif:Evaluates the individual GroupJunctions and CompositeGroupJunctions and does a cartesian
   * of the results so obtained and simultaneously expanding it to the query from clause level as
   * well as evaluating any iter evaluatable conditions. The evaluated result of an AllGroupJunction
   * will always be of the query from clause level which can be ORed or ANDd with filter evaluatable
   * subtree CompiledJunction
   * 
   * @param context ExecutionContext object
   * @return SelectResults
   * @throws FunctionDomainException
   * @throws TypeMismatchException
   * @throws NameResolutionException
   * @throws QueryInvocationTargetException
   */
// Asif : For doing the Cartesian first evaluate the result of all Group
// Junction. Doing Cartesian of all the Results together is better than doing
// in pair
private SelectResults evaluateAndJunction(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    int len = this.abstractGroupOrRangeJunctions.size();
    // Asif : Create an array of SelectResults for each of the GroupJunction
    // For each Group Junction there will be a corresponding array of
    // RuntimeIterators which will map to the fields of the ResultSet obtained
    // from Group Junction.
    SelectResults[] results = new SelectResults[len];
    List finalList = context.getCurrentIterators();
    List expansionList = new LinkedList(finalList);
    RuntimeIterator[][] itrsForResultFields = new RuntimeIterator[len][];
    CompiledValue gj = null;
    Iterator junctionItr = this.abstractGroupOrRangeJunctions.iterator();
    List grpItrs = null;
    int j = 0;
    RuntimeIterator tempItr = null;
    while (junctionItr.hasNext()) {
        gj = (CompiledValue) junctionItr.next();
        SelectResults filterResults = ((Filter) gj).filterEvaluate(context, null);
        Support.Assert(filterResults != null, "FilterResults cannot be null here");
        if (filterResults.isEmpty()) {
            if (finalList.size() > 1) {
                StructType type = QueryUtils.createStructTypeForRuntimeIterators(finalList);
                return QueryUtils.createStructCollection(context, type);
            } else {
                ObjectType type = ((RuntimeIterator) finalList.iterator().next()).getElementType();
                if (type instanceof StructType) {
                    return QueryUtils.createStructCollection(context, (StructTypeImpl) type);
                } else {
                    return QueryUtils.createResultCollection(context, type);
                }
            }
        } else {
            results[j] = filterResults;
            grpItrs = (gj instanceof CompositeGroupJunction) ? QueryUtils.getDependentItrChainForIndpndntItrs(((CompositeGroupJunction) gj).getIndependentIteratorsOfCJ(), context) : context.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(((AbstractGroupOrRangeJunction) gj).getIndependentIteratorForGroup()[0]);
            itrsForResultFields[j] = new RuntimeIterator[grpItrs.size()];
            Iterator grpItr = grpItrs.iterator();
            int k = 0;
            while (grpItr.hasNext()) {
                tempItr = (RuntimeIterator) grpItr.next();
                itrsForResultFields[j][k++] = tempItr;
                expansionList.remove(tempItr);
            }
            ++j;
        }
    }
    SelectResults resultsSet = null;
    // Asif : Do the Cartesian of the different group junction results.
    CompiledValue iterOperandsToSend = null;
    if (!iterOperands.isEmpty()) {
        // TODO ASIF : Avoid creation of CompiledJunction by providing
        // functionality in AllGroupJunction for evaluating condition
        int size = iterOperands.size();
        CompiledValue[] cv = new CompiledValue[size];
        for (int k = 0; k < size; ++k) {
            cv[k] = (CompiledValue) this.iterOperands.get(k);
        }
        if (cv.length == 1) {
            iterOperandsToSend = cv[0];
        } else {
            iterOperandsToSend = new CompiledJunction(cv, this.operator);
        }
    }
    QueryObserver observer = QueryObserverHolder.getInstance();
    observer.beforeCartesianOfGroupJunctionsInAnAllGroupJunctionOfType_AND(results);
    resultsSet = QueryUtils.cartesian(results, itrsForResultFields, expansionList, finalList, context, iterOperandsToSend);
    observer.afterCartesianOfGroupJunctionsInAnAllGroupJunctionOfType_AND();
    Support.Assert(resultsSet != null, "ResultsSet obtained was NULL in AllGroupJunction");
    return resultsSet;
}
Also used : StructType(org.apache.geode.cache.query.types.StructType) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults)

Example 13 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class AddFreeItemToOrders method execute.

public void execute(FunctionContext context) {
    Region region = null;
    List<Object> vals = new ArrayList<Object>();
    List<Object> keys = new ArrayList<Object>();
    List<Object> argsList = new ArrayList<Object>();
    Object[] argsArray = null;
    if (context.getArguments() instanceof Boolean) {
    } else if (context.getArguments() instanceof String) {
        String arg = (String) context.getArguments();
    } else if (context.getArguments() instanceof Vector) {
    } else if (context.getArguments() instanceof Object[]) {
        argsArray = (Object[]) context.getArguments();
        argsList = Arrays.asList(argsArray);
    } else {
        System.out.println("AddFreeItemToOrders : Invalid Arguments");
    }
    InternalCache cache = null;
    try {
        cache = (InternalCache) CacheFactory.getAnyInstance();
        cache.getCacheConfig().setPdxReadSerialized(true);
        region = cache.getRegion("orders");
    } catch (CacheClosedException ex) {
        vals.add("NoCacheFoundResult");
        context.getResultSender().lastResult(vals);
    }
    String oql = "SELECT DISTINCT entry.key FROM /orders.entries entry WHERE entry.value.totalPrice > $1";
    Object[] queryArgs = new Object[1];
    queryArgs[0] = argsList.get(0);
    final Query query = cache.getQueryService().newQuery(oql);
    SelectResults result = null;
    try {
        result = (SelectResults) query.execute(queryArgs);
        int resultSize = result.size();
        if (result instanceof Collection<?>)
            for (Object item : result) {
                keys.add(item);
            }
    } catch (FunctionDomainException e) {
        if (cache != null)
            cache.getLogger().info("Caught FunctionDomainException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (TypeMismatchException e) {
        if (cache != null)
            cache.getLogger().info("Caught TypeMismatchException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (NameResolutionException e) {
        if (cache != null)
            cache.getLogger().info("Caught NameResolutionException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (QueryInvocationTargetException e) {
        if (cache != null)
            cache.getLogger().info("Caught QueryInvocationTargetException while executing function AddFreeItemToOrders" + e.getMessage());
    }
    // class has to be in classpath.
    try {
        Item it = (Item) (argsList.get(1));
        for (Object key : keys) {
            Object obj = region.get(key);
            if (obj instanceof PdxInstance) {
                PdxInstance pi = (PdxInstance) obj;
                Order receivedOrder = (Order) pi.getObject();
                receivedOrder.addItem(it);
                region.put(key, receivedOrder);
            }
        }
        context.getResultSender().lastResult("success");
    } catch (ClassCastException e) {
        context.getResultSender().lastResult("failure");
    } catch (Exception e) {
        context.getResultSender().lastResult("failure");
    }
}
Also used : Query(org.apache.geode.cache.query.Query) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) SelectResults(org.apache.geode.cache.query.SelectResults) PdxInstance(org.apache.geode.pdx.PdxInstance) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Vector(java.util.Vector)

Example 14 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class AbstractIndex method applyProjection.

void applyProjection(List projAttrib, ExecutionContext context, Collection result, Object iterValue, SelectResults intermediateResults, boolean isIntersection) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    if (projAttrib == null) {
        iterValue = deserializePdxForLocalDistinctQuery(context, iterValue);
        this.addToResultsWithUnionOrIntersection(result, intermediateResults, isIntersection, iterValue);
    } else {
        boolean isStruct = result instanceof SelectResults && ((SelectResults) result).getCollectionType().getElementType() != null && ((SelectResults) result).getCollectionType().getElementType().isStructType();
        if (isStruct) {
            int projCount = projAttrib.size();
            Object[] values = new Object[projCount];
            Iterator projIter = projAttrib.iterator();
            int i = 0;
            while (projIter.hasNext()) {
                Object[] projDef = (Object[]) projIter.next();
                values[i] = deserializePdxForLocalDistinctQuery(context, ((CompiledValue) projDef[1]).evaluate(context));
                i++;
            }
            this.addToStructsWithUnionOrIntersection(result, intermediateResults, isIntersection, values);
        } else {
            Object[] temp = (Object[]) projAttrib.get(0);
            Object val = deserializePdxForLocalDistinctQuery(context, ((CompiledValue) temp[1]).evaluate(context));
            this.addToResultsWithUnionOrIntersection(result, intermediateResults, isIntersection, val);
        }
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator)

Example 15 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class AbstractIndex method addToStructsWithUnionOrIntersection.

private void addToStructsWithUnionOrIntersection(Collection results, SelectResults intermediateResults, boolean isIntersection, Object[] values) {
    for (int i = 0; i < values.length; i++) {
        values[i] = verifyAndGetPdxDomainObject(values[i]);
    }
    if (intermediateResults == null) {
        if (results instanceof StructFields) {
            ((StructFields) results).addFieldValues(values);
        } else {
            // The results could be LinkedStructSet or SortedResultsBag or StructSet
            SelectResults selectResults = (SelectResults) results;
            StructImpl structImpl = new StructImpl((StructTypeImpl) selectResults.getCollectionType().getElementType(), values);
            selectResults.add(structImpl);
        }
    } else {
        if (isIntersection) {
            if (results instanceof StructFields) {
                int occurrences = intermediateResults.occurrences(values);
                if (occurrences > 0) {
                    ((StructFields) results).addFieldValues(values);
                    ((StructFields) intermediateResults).removeFieldValues(values);
                }
            } else {
                // could be LinkedStructSet or SortedResultsBag
                SelectResults selectResults = (SelectResults) results;
                StructImpl structImpl = new StructImpl((StructTypeImpl) selectResults.getCollectionType().getElementType(), values);
                if (intermediateResults.remove(structImpl)) {
                    selectResults.add(structImpl);
                }
            }
        } else {
            if (results instanceof StructFields) {
                ((StructFields) results).addFieldValues(values);
            } else {
                // could be LinkedStructSet or SortedResultsBag
                SelectResults selectResults = (SelectResults) results;
                StructImpl structImpl = new StructImpl((StructTypeImpl) selectResults.getCollectionType().getElementType(), values);
                if (intermediateResults.remove(structImpl)) {
                    selectResults.add(structImpl);
                }
            }
        }
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) StructImpl(org.apache.geode.cache.query.internal.StructImpl) StructFields(org.apache.geode.cache.query.internal.StructFields)

Aggregations

SelectResults (org.apache.geode.cache.query.SelectResults)577 Test (org.junit.Test)423 Query (org.apache.geode.cache.query.Query)360 Region (org.apache.geode.cache.Region)336 QueryService (org.apache.geode.cache.query.QueryService)331 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)256 Portfolio (org.apache.geode.cache.query.data.Portfolio)249 Index (org.apache.geode.cache.query.Index)133 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 Host (org.apache.geode.test.dunit.Host)107 VM (org.apache.geode.test.dunit.VM)107 CacheException (org.apache.geode.cache.CacheException)105 Iterator (java.util.Iterator)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)101 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)89 Cache (org.apache.geode.cache.Cache)84 Struct (org.apache.geode.cache.query.Struct)80 LocalRegion (org.apache.geode.internal.cache.LocalRegion)67 ObjectType (org.apache.geode.cache.query.types.ObjectType)66