use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CopyOnReadQueryJUnitTest method helpTestCopyOnReadFalse.
private void helpTestCopyOnReadFalse(String queryString, int expectedResultsSize, int numObjects, int objectsAndResultsMultiplier, boolean hasIndex, boolean isPR, boolean containsInnerQuery) throws Exception {
int numInstances = numObjects * objectsAndResultsMultiplier;
if (hasIndex && isPR) {
numInstances += numObjects * objectsAndResultsMultiplier;
}
assertEquals("Unexpected number of Portfolio instances" + queryString, numInstances, Portfolio.instanceCount.get());
// execute query
QueryService qs = utils.getCache().getQueryService();
Query query = qs.newQuery(queryString);
SelectResults results = (SelectResults) query.execute();
assertEquals("No results were found for query: " + queryString, expectedResultsSize * objectsAndResultsMultiplier, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
p.status = "discardStatus";
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
p.status = "discardStatus";
}
}
if (!hasIndex && isPR) {
// Copy on read is false, due to logic in local region
// when we deserialize, we end up caching the deserialized value
// This is why we don't have to worry about inner queries increasing the count
numInstances += numObjects * objectsAndResultsMultiplier;
}
assertEquals("Unexpected number of Portfolio instances" + queryString, numInstances, Portfolio.instanceCount.get());
results = (SelectResults) query.execute();
assertEquals("No results were found", expectedResultsSize * objectsAndResultsMultiplier, results.size());
for (Object o : results) {
if (o instanceof Portfolio) {
Portfolio p = (Portfolio) o;
assertEquals("status should have been changed", "discardStatus", p.status);
} else {
Struct struct = (Struct) o;
Portfolio p = (Portfolio) struct.getFieldValues()[0];
assertEquals("status should have been changed", "discardStatus", p.status);
}
}
// Unlike the copy on read case, we do not need to increase the instance count
// This is because of logic in LocalRegion where if copy on read is false, we cache the
// deserialized value
assertEquals("Unexpected number of Portfolio instances" + queryString, numInstances, Portfolio.instanceCount.get());
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CompiledJunction method auxIterateEvaluate.
/** invariant: the operand is known to be evaluated by iteration */
SelectResults auxIterateEvaluate(CompiledValue operand, ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// signifies that the junction cannot be evaluated as a filter.
if (intermediateResults == null)
throw new RuntimeException(LocalizedStrings.CompiledJunction_INTERMEDIATERESULTS_CAN_NOT_BE_NULL.toLocalizedString());
if (// short circuit
intermediateResults.isEmpty())
return intermediateResults;
List currentIters = context.getCurrentIterators();
RuntimeIterator[] rIters = new RuntimeIterator[currentIters.size()];
currentIters.toArray(rIters);
ObjectType elementType = intermediateResults.getCollectionType().getElementType();
SelectResults resultSet;
if (elementType.isStructType()) {
resultSet = QueryUtils.createStructCollection(context, (StructTypeImpl) elementType);
} else {
resultSet = QueryUtils.createResultCollection(context, elementType);
}
QueryObserver observer = QueryObserverHolder.getInstance();
try {
observer.startIteration(intermediateResults, operand);
Iterator iResultsIter = intermediateResults.iterator();
while (iResultsIter.hasNext()) {
Object tuple = iResultsIter.next();
if (tuple instanceof Struct) {
Object[] values = ((Struct) tuple).getFieldValues();
for (int i = 0; i < values.length; i++) {
rIters[i].setCurrent(values[i]);
}
} else {
rIters[0].setCurrent(tuple);
}
Object result = null;
try {
result = operand.evaluate(context);
} finally {
observer.afterIterationEvaluation(result);
}
if (result instanceof Boolean) {
if (((Boolean) result).booleanValue())
resultSet.add(tuple);
} else if (result != null && result != QueryService.UNDEFINED)
throw new TypeMismatchException(LocalizedStrings.CompiledJunction_ANDOR_OPERANDS_MUST_BE_OF_TYPE_BOOLEAN_NOT_TYPE_0.toLocalizedString(result.getClass().getName()));
}
} finally {
observer.endIteration(resultSet);
}
return resultSet;
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CompiledSelect method applyProjectionOnCollection.
private SelectResults applyProjectionOnCollection(SelectResults resultSet, ExecutionContext context, boolean ignoreOrderBy) throws TypeMismatchException, AmbiguousNameException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
List iterators = context.getCurrentIterators();
if (projAttrs == null && (this.orderByAttrs == null || ignoreOrderBy)) {
// as it is.
if (iterators.size() > 1) {
StructType type = createStructTypeForNullProjection(iterators, context);
((SelectResults) resultSet).setElementType(type);
}
return resultSet;
} else {
int numElementsAdded = 0;
SelectResults pResultSet = prepareEmptyResultSet(context, ignoreOrderBy);
boolean isStructType = resultSet.getCollectionType().getElementType() != null && resultSet.getCollectionType().getElementType().isStructType();
if (isStructType) {
Iterator resultsIter = resultSet.iterator();
// Apply limit if there is no order by.
Integer limitValue = evaluateLimitValue(context, this.limit);
while (((this.orderByAttrs != null && !ignoreOrderBy) || limitValue < 0 || (numElementsAdded < limitValue)) && resultsIter.hasNext()) {
// Check if query execution on this thread is canceled.
QueryMonitor.isQueryExecutionCanceled();
Object[] values = ((Struct) resultsIter.next()).getFieldValues();
for (int i = 0; i < values.length; i++) {
((RuntimeIterator) iterators.get(i)).setCurrent(values[i]);
}
int occurence = applyProjectionAndAddToResultSet(context, pResultSet, ignoreOrderBy);
if (occurence == 1 || (occurence > 1 && !this.distinct)) {
// (Unique i.e first time occurence) or subsequent occurence
// for non distinct query
++numElementsAdded;
}
}
// return pResultSet;
} else if (iterators.size() == 1) {
RuntimeIterator rIter = (RuntimeIterator) iterators.get(0);
Iterator resultsIter = ((SelectResults) resultSet).iterator();
// Apply limit if there is no order by.
Integer limitValue = evaluateLimitValue(context, this.limit);
while (((this.orderByAttrs != null && !ignoreOrderBy) || limitValue < 0 || (numElementsAdded < limitValue)) && resultsIter.hasNext()) {
rIter.setCurrent(resultsIter.next());
int occurence = applyProjectionAndAddToResultSet(context, pResultSet, ignoreOrderBy);
if (occurence == 1 || (occurence > 1 && !this.distinct)) {
// (Unique i.e first time occurence) or subsequent occurence
// for non distinct query
++numElementsAdded;
}
}
} else {
throw new RuntimeException(LocalizedStrings.CompiledSelect_RESULT_SET_DOES_NOT_MATCH_WITH_ITERATOR_DEFINITIONS_IN_FROM_CLAUSE.toLocalizedString());
}
return pResultSet;
}
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CompiledGroupBySelect method terminateAndAddToResults.
private boolean terminateAndAddToResults(boolean isStruct, SelectResults newResults, Aggregator[] aggregators, Object prev, ExecutionContext context, boolean isStrucFields, int limitValue) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
Object[] newRowArray = isStruct ? copyStruct((Struct) prev) : null;
Object newObject = null;
int bitstart = 0;
if (limitValue == 0) {
return false;
}
for (Aggregator aggregator : aggregators) {
if (isStruct) {
int pos = this.aggregateColsPos.nextSetBit(bitstart);
bitstart = pos + 1;
Object scalarResult = aggregator.terminate();
newRowArray[pos] = scalarResult;
} else {
newObject = aggregator.terminate();
}
}
if (isStruct) {
if (isStrucFields) {
((StructFields) newResults).addFieldValues(newRowArray);
} else {
newResults.add(new StructImpl((StructTypeImpl) ((Struct) prev).getStructType(), newRowArray));
}
} else {
newResults.add(newObject);
}
boolean keepAdding = true;
if (this.originalOrderByClause == null && limitValue > 0 && (context.getIsPRQueryNode() || context.getBucketList() == null) && newResults.size() == limitValue) {
keepAdding = false;
}
// rfresh the aggregators
refreshAggregators(aggregators, context);
return keepAdding;
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CompiledGroupBySelect method accumulate.
private void accumulate(boolean isStruct, Aggregator[] aggregators, Object current, boolean[] objectChangedMarker) {
int bitstart = 0;
for (Aggregator aggregator : aggregators) {
if (isStruct) {
int pos = this.aggregateColsPos.nextSetBit(bitstart);
bitstart = pos + 1;
Struct struct = (Struct) current;
Object scalar = PDXUtils.convertPDX(struct.getFieldValues()[pos], false, true, true, true, objectChangedMarker, isStruct);
aggregator.accumulate(scalar);
} else {
current = PDXUtils.convertPDX(current, false, true, true, true, objectChangedMarker, isStruct);
aggregator.accumulate(current);
}
}
}
Aggregations