use of org.apache.geode.cache.query.AmbiguousNameException in project geode by apache.
the class CompiledIteratorDef method getRuntimeIterator.
public RuntimeIterator getRuntimeIterator(ExecutionContext context) throws TypeMismatchException, AmbiguousNameException, NameResolutionException {
RuntimeIterator rIter = null;
// check cached in context
rIter = (RuntimeIterator) context.cacheGet(this);
if (rIter != null) {
return rIter;
}
ObjectType type = this.elementType;
if (type.equals(TypeUtils.OBJECT_TYPE)) {
// check to see if there's a typecast for this collection
ObjectType typc = getCollectionElementTypeCast();
if (typc != null) {
type = typc;
} else {
// Does not determine type of nested query
if (!(this.collectionExpr instanceof CompiledSelect)) {
type = computeElementType(context);
}
}
}
rIter = new RuntimeIterator(this, type);
// generate from clause should take care of bucket region substitution if
// necessary and then set the definition.
String fromClause = genFromClause(context);
rIter.setDefinition(fromClause);
/*
* If the type of RunTimeIterator is still ObjectType & if the RuneTimeIterator is independent
* of any iterator of the scopes less than or equal to its own scope, we can evaluate the
* collection via RuntimeIterator. This will initialize the Collection of RuntimeIterator ,
* which is OK. The code in RuntimeIterator will be rectified such that the ElementType of that
* RuntimeIterator is taken from the collection
*/
if (type.equals(TypeUtils.OBJECT_TYPE) && !this.isDependentOnAnyIteratorOfScopeLessThanItsOwn(context)) {
// the collection
try {
rIter.evaluateCollection(context);
} catch (QueryExecutionTimeoutException qet) {
throw qet;
} catch (RegionNotFoundException re) {
throw re;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Exception while getting runtime iterator.", e);
}
throw new TypeMismatchException(LocalizedStrings.CompiledIteratorDef_EXCEPTION_IN_EVALUATING_THE_COLLECTION_EXPRESSION_IN_GETRUNTIMEITERATOR_EVEN_THOUGH_THE_COLLECTION_IS_INDEPENDENT_OF_ANY_RUNTIMEITERATOR.toLocalizedString(), e);
}
}
// cache in context
context.cachePut(this, rIter);
return rIter;
}
use of org.apache.geode.cache.query.AmbiguousNameException in project geode by apache.
the class ExecutionContext method resolveImplicitOperationName.
/**
* returns implicit iterator receiver of operation with numArgs args, or null if cannot be
* resolved.
*
* SPECIAL CASE: If we are unable to resolve the name on any iterator, but there is only one
* iterator that we don't have type information for it (for now OBJECT_TYPE, this has to change),
* then return that one iterator under the assumption that the operation name must belong to it.
*/
RuntimeIterator resolveImplicitOperationName(String name, int numArgs, boolean mustBeMethod) throws AmbiguousNameException {
// iterate through all properties of iterator variables in scope
// to see if there is a unique resolution
RuntimeIterator oneUnknown = null;
List hits = new ArrayList(2);
boolean foundOneUnknown = false;
NEXT_SCOPE: for (int i = scopes.size() - 1; i >= 0; i--) {
QScope scope = (QScope) scopes.get(i);
Iterator iter = scope.getIterators().iterator();
while (iter.hasNext()) {
RuntimeIterator itr = (RuntimeIterator) iter.next();
Assert.assertTrue(itr != null);
// iterators in this scope
if (scope.getLimit() == itr) {
// don't go any farther in this scope
continue NEXT_SCOPE;
}
// method. This save lot of CPU time.
if (!TypeUtils.OBJECT_TYPE.equals(itr.getElementType()) && itr.containsProperty(name, numArgs, mustBeMethod)) {
hits.add(itr);
} else if (TypeUtils.OBJECT_TYPE.equals(itr.getElementType())) {
if (foundOneUnknown) {
// more than one
oneUnknown = null;
} else {
foundOneUnknown = true;
oneUnknown = itr;
}
}
}
}
if (hits.size() == 1) {
return (RuntimeIterator) hits.get(0);
}
if (hits.size() > 1) {
// ambiguous
if (mustBeMethod)
throw new AmbiguousNameException(LocalizedStrings.ExecutionContext_METHOD_NAMED_0_WITH_1_ARGUMENTS_IS_AMBIGUOUS_BECAUSE_IT_CAN_APPLY_TO_MORE_THAN_ONE_VARIABLE_IN_SCOPE.toLocalizedString(name, numArgs));
throw new AmbiguousNameException(LocalizedStrings.ExecutionContext_ATTRIBUTE_NAMED_0_IS_AMBIGUOUS_BECAUSE_IT_CAN_APPLY_TO_MORE_THAN_ONE_VARIABLE_IN_SCOPE.toLocalizedString(name));
}
Assert.assertTrue(hits.isEmpty());
// otherwise, returns null, unable to resolve here
return oneUnknown;
}
use of org.apache.geode.cache.query.AmbiguousNameException in project geode by apache.
the class IndexManager method getBestMatchIndex.
/**
* Asif : Returns the best available Index based on the available iterators in the Group
*
* TODO: Asif :Check if synchronization is needed while obtaining Array of Indexes as similar to
* what we have used during index updates
*
* @param indexType Primary or Range Index
* @param definitions String array containing the canonicalized definitions of the Iterators of
* the Group
* @param indexedExpression Index Expression path(CompiledValue) on which index needs to be
* created
* @param context ExecutionContext object
* @return IndexData object
*/
public IndexData getBestMatchIndex(IndexType indexType, String[] definitions, CompiledValue indexedExpression, ExecutionContext context) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
Index bestIndex = null;
Index bestPRIndex = null;
int[] bestMapping = null;
int qItrSize = definitions.length;
int bestIndexMatchLevel = qItrSize;
Iterator iter = this.indexes.values().iterator();
StringBuilder sb = new StringBuilder();
indexedExpression.generateCanonicalizedExpression(sb, context);
String indexExprStr = sb.toString();
PartitionedIndex prIndex = null;
Index prevBestPRIndex = null;
Index prevBestIndex = null;
Index index;
while (iter.hasNext()) {
Object ind = iter.next();
// the index is in create phase.
if (ind instanceof FutureTask) {
continue;
}
// If the index is still empty
if (!((AbstractIndex) ind).isPopulated()) {
continue;
}
index = (Index) ind;
if (index instanceof PartitionedIndex) {
prIndex = (PartitionedIndex) index;
// Get one of the bucket index. This index will be
// available on all the buckets.
index = prIndex.getBucketIndex();
if (index == null) {
continue;
}
}
// Use simple strategy just pick first compatible index
if (((IndexProtocol) index).isMatchingWithIndexExpression(indexedExpression, indexExprStr, context) && index.getType() == indexType) {
// For PR the matched index needs to be available on all the query buckets.
if (prIndex != null) {
try {
// Protect the PartitionedIndex from being removed when it is being used.
if (!prIndex.acquireIndexReadLockForRemove()) {
continue;
}
prIndex.verifyAndCreateMissingIndex(context.getBucketList());
} catch (Exception ignored) {
// Index is not there on all buckets.
// ignore this index.
prIndex.releaseIndexReadLockForRemove();
prIndex = null;
continue;
}
} else {
// For index on replicated regions
if (!((AbstractIndex) index).acquireIndexReadLockForRemove()) {
continue;
}
}
/*
* Asif : A match level of zero means exact match. A match level greater than 0 means the
* query from clauses have extra iterators as compared to Index resultset ( This does not
* neccessarily mean that Index resultset is not having extra fields. It is just that the
* criteria for match level is the absence or presence of extra iterators. The order of
* preference will be 0 , <0 , > 0 for first cut.
*/
String[] indexDefinitions = ((IndexProtocol) index).getCanonicalizedIteratorDefinitions();
int[] mapping = new int[qItrSize];
int matchLevel = getMatchLevel(definitions, indexDefinitions, mapping);
if (matchLevel == 0) {
prevBestPRIndex = bestPRIndex;
bestPRIndex = prIndex;
prevBestIndex = bestIndex;
bestIndex = index;
bestIndexMatchLevel = matchLevel;
bestMapping = mapping;
// chosen as bestIndex.
if (prIndex != null && prevBestPRIndex != null && prevBestPRIndex instanceof PartitionedIndex) {
((PartitionedIndex) prevBestPRIndex).releaseIndexReadLockForRemove();
prevBestPRIndex = null;
} else if (prevBestIndex != null) {
((AbstractIndex) prevBestIndex).releaseIndexReadLockForRemove();
prevBestIndex = null;
}
break;
} else if ((bestIndexMatchLevel > 0 && matchLevel < bestIndexMatchLevel) || (bestIndexMatchLevel < 0 && matchLevel < 0 && matchLevel > bestIndexMatchLevel)) {
prevBestPRIndex = bestPRIndex;
bestPRIndex = prIndex;
prevBestIndex = bestIndex;
bestIndex = index;
bestIndexMatchLevel = matchLevel;
bestMapping = mapping;
}
// release the lock if this index is not chosen as bestIndex.
if (prIndex != null && bestPRIndex != prIndex) {
prIndex.releaseIndexReadLockForRemove();
prIndex = null;
} else if (bestIndex != index) {
((AbstractIndex) index).releaseIndexReadLockForRemove();
index = null;
}
// chosen as bestIndex.
if (prevBestPRIndex != null && prevBestPRIndex instanceof PartitionedIndex) {
((PartitionedIndex) prevBestPRIndex).releaseIndexReadLockForRemove();
prevBestPRIndex = null;
} else if (prevBestIndex != null) {
((AbstractIndex) prevBestIndex).releaseIndexReadLockForRemove();
prevBestIndex = null;
}
}
}
if (bestIndex != null) {
if (logger.isDebugEnabled()) {
logger.debug("The best index found for index expression: {} is: {} with Match-level: {} and mapping: {}", indexExprStr, bestIndex, bestIndexMatchLevel, Arrays.toString(bestMapping));
}
}
return bestIndex != null ? new IndexData((IndexProtocol) bestIndex, bestIndexMatchLevel, bestMapping) : null;
}
Aggregations