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;
}
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;
}
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");
}
}
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);
}
}
}
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);
}
}
}
}
}
Aggregations