use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class CompiledIn method evaluate.
public Object evaluate(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
Object evalElm = this.elm.evaluate(context);
Object evalColln = evaluateColln(context);
if (evalColln == null || evalColln == QueryService.UNDEFINED) {
return QueryService.UNDEFINED;
}
// handle each type of collection that we support
if (evalColln instanceof Map) {
evalColln = ((Map) evalColln).entrySet();
}
if (evalColln instanceof Collection) {
Iterator iterator = ((Iterable) evalColln).iterator();
while (iterator.hasNext()) {
Object evalObj = evalElm;
Object collnObj = iterator.next();
if (evalElm instanceof PdxString && collnObj instanceof String) {
evalObj = ((PdxString) evalElm).toString();
} else if (collnObj instanceof PdxString && evalElm instanceof String) {
collnObj = ((PdxString) collnObj).toString();
}
if (TypeUtils.compare(evalObj, collnObj, OQLLexerTokenTypes.TOK_EQ).equals(Boolean.TRUE)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
if (!evalColln.getClass().isArray()) {
throw new TypeMismatchException(LocalizedStrings.CompiledIn_OPERAND_OF_IN_CANNOT_BE_INTERPRETED_AS_A_COLLECTION_IS_INSTANCE_OF_0.toLocalizedString(evalColln.getClass().getName()));
}
if (evalColln.getClass().getComponentType().isPrimitive()) {
if (evalElm == null) {
throw new TypeMismatchException(LocalizedStrings.CompiledIn_IN_OPERATOR_CHECK_FOR_NULL_IN_PRIMITIVE_ARRAY.toLocalizedString());
}
}
int numElements = Array.getLength(evalColln);
for (int i = 0; i < numElements; i++) {
Object o = Array.get(evalColln, i);
if (TypeUtils.compare(evalElm, o, TOK_EQ).equals(Boolean.TRUE)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class CompiledOperation method evaluate.
public Object evaluate(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
CompiledValue rcvr = getReceiver(context);
Object result;
Object evalRcvr;
if (rcvr == null) {
// must be intended as implicit iterator operation
// see if it's an implicit operation name
RuntimeIterator rcvrItr = context.resolveImplicitOperationName(this.methodName, this.args.size(), true);
evalRcvr = rcvrItr.evaluate(context);
/*
* // evaluate on current iteration of collection if (rcvrItr != null) { result =
* eval0(rcvrItr.evaluate(context), rcvrItr.getElementType().resolveClass(), context); }
*
* // function call: no functions implemented except keywords in the grammar throw new
* TypeMismatchException(LocalizedStrings.CompiledOperation_COULD_NOT_RESOLVE_METHOD_NAMED_0.
* toLocalizedString(this.methodName));
*/
} else {
// if not null, then explicit receiver
evalRcvr = rcvr.evaluate(context);
}
// short circuit null immediately
if (evalRcvr == null) {
return QueryService.UNDEFINED;
}
if (context.isCqQueryContext() && evalRcvr instanceof Region.Entry) {
Region.Entry re = (Region.Entry) evalRcvr;
if (re.isDestroyed()) {
return QueryService.UNDEFINED;
}
try {
evalRcvr = re.getValue();
} catch (EntryDestroyedException ede) {
// throw EntryDestroyedException if the value becomes null.
return QueryService.UNDEFINED;
}
}
// check if the receiver is the iterator, in which
// case we resolve the method on the constraint rather
// than the runtime type of the receiver
Class resolveClass = null;
// if (resolveClass == null)
if (evalRcvr instanceof PdxInstance) {
String className = ((PdxInstance) evalRcvr).getClassName();
try {
resolveClass = InternalDataSerializer.getCachedClass(className);
} catch (ClassNotFoundException cnfe) {
throw new QueryInvocationTargetException(cnfe);
}
} else if (evalRcvr instanceof PdxString) {
resolveClass = String.class;
} else {
resolveClass = evalRcvr.getClass();
}
result = eval0(evalRcvr, resolveClass, context);
// }
// check for PR substitution
// check for BucketRegion substitution
PartitionedRegion pr = context.getPartitionedRegion();
if (pr != null && (result instanceof Region)) {
if (pr.getFullPath().equals(((Region) result).getFullPath())) {
result = context.getBucketRegion();
}
}
return result;
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class CompiledOperation method eval0.
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED", justification = "Does not matter if the methodDispatch that isn't stored in the map is used")
private Object eval0(Object receiver, Class resolutionType, ExecutionContext context) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
if (receiver == null || receiver == QueryService.UNDEFINED)
return QueryService.UNDEFINED;
List args = new ArrayList();
List argTypes = new ArrayList();
Iterator i = this.args.iterator();
while (i.hasNext()) {
CompiledValue arg = (CompiledValue) i.next();
Object o = arg.evaluate(context);
// undefined arg produces undefines method result
if (o == QueryService.UNDEFINED)
return QueryService.UNDEFINED;
args.add(o);
// pass in null for the type if the runtime value is null
if (o == null)
argTypes.add(null);
else
// commented out because we currently always use the runtime type for args
// else if (arg.getType() == Identifier)
// {
// CompiledValue resolved = context.resolve(((CompiledID)arg).getId());
// if (resolved != null && resolved.getType() == ITERATOR)
// argTypes.add(((RuntimeIterator)resolved).getBaseCollection().getConstraint());
// else
// argTypes.add(o.getClass());
// }
// otherwise use the runtime type
argTypes.add(o.getClass());
}
// see if in cache
MethodDispatch methodDispatch;
List key = Arrays.asList(new Object[] { resolutionType, this.methodName, argTypes });
methodDispatch = (MethodDispatch) CompiledOperation.cache.get(key);
if (methodDispatch == null) {
try {
methodDispatch = new MethodDispatch(resolutionType, this.methodName, argTypes);
} catch (NameResolutionException nre) {
if (!org.apache.geode.cache.query.Struct.class.isAssignableFrom(resolutionType) && (DefaultQueryService.QUERY_HETEROGENEOUS_OBJECTS || DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS)) {
return QueryService.UNDEFINED;
} else {
throw nre;
}
}
// cache
CompiledOperation.cache.putIfAbsent(key, methodDispatch);
}
if (receiver instanceof PdxInstance) {
try {
if (receiver instanceof PdxInstanceImpl) {
receiver = ((PdxInstanceImpl) receiver).getCachedObject();
} else {
receiver = ((PdxInstance) receiver).getObject();
}
} catch (PdxSerializationException ex) {
throw new QueryInvocationTargetException(ex);
}
} else if (receiver instanceof PdxString) {
receiver = ((PdxString) receiver).toString();
}
return methodDispatch.invoke(receiver, args);
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class PdxStringQueryJUnitTest method testQueriesWithRangeIndexWithREUpdateInProgress.
@Test
public void testQueriesWithRangeIndexWithREUpdateInProgress() throws Exception {
Index index = qs.createIndex("index2", "p.secId", "/exampleRegion p, p.positions.values");
assertTrue(index instanceof RangeIndex);
PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 111);
pf.writeString("secId", "IBM");
pf.writeString("status", "active");
HashMap positions = new HashMap();
positions.put("price", "50");
positions.put("price", "60");
pf.writeObject("positions", positions);
PdxInstance pi = pf.create();
r.put("IBM", pi);
positions = new HashMap();
positions.put("price", "100");
positions.put("price", "120");
r.put("YHOO", new TestObject(222, "YHOO", positions, "inactive"));
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 333);
pf.writeString("secId", "GOOGL");
pf.writeString("status", "active");
positions = new HashMap();
positions.put("price", "130");
positions.put("price", "150");
pf.writeObject("positions", positions);
pi = pf.create();
positions = new HashMap();
positions.put("price", "200");
positions.put("price", "220");
r.put("VMW", new TestObject(111, "VMW", positions, "inactive"));
r.put("GOOGL", pi);
makeREUpdateInProgress();
Map map = ((RangeIndex) index).getValueToEntriesMap();
for (Object key : map.keySet()) {
assertTrue(key instanceof PdxString);
}
DefaultQuery.setPdxReadSerialized(true);
executeQueriesValidateResults(INDEX_TYPE_RANGE);
qs.removeIndex(index);
r.clear();
}
use of org.apache.geode.pdx.internal.PdxString in project geode by apache.
the class CompactRangeIndex method evaluateEntry.
/**
* This evaluates the left and right side of a where condition for which this Index was used.
* Like, if condition is "ID > 1", {@link IndexInfo} will contain Left as ID, Right as '1' and
* operator as TOK_GT. This method will evaluate ID from region entry value and verify the ID > 1.
*
* Note: IndexInfo is created for each query separately based on the condition being evaluated
* using the Index.
*
* @return true if RegionEntry value satisfies the where condition (contained in IndexInfo).
*/
protected boolean evaluateEntry(IndexInfo indexInfo, ExecutionContext context, Object keyVal) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
CompiledValue path = ((IndexInfo) indexInfo)._path();
Object left = path.evaluate(context);
CompiledValue key = ((IndexInfo) indexInfo)._key();
Object right = null;
// For CompiledUndefined indexInfo has null key.
if (keyVal == null && key == null) {
if (left == QueryService.UNDEFINED) {
return true;
} else {
return false;
}
}
if (key != null) {
right = key.evaluate(context);
// a tuple. In other cases it does not
if (null != right && indexInfo._getIndex() instanceof CompactMapRangeIndex && right instanceof Object[]) {
right = ((Object[]) right)[0];
}
} else {
right = keyVal;
}
int operator = indexInfo._operator();
if (left == null && right == null) {
return Boolean.TRUE;
} else {
if (left instanceof PdxString) {
if (right instanceof String) {
switch(key.getType()) {
case CompiledValue.LITERAL:
right = ((CompiledLiteral) key).getSavedPdxString();
break;
case OQLLexerTokenTypes.QUERY_PARAM:
right = ((CompiledBindArgument) key).getSavedPdxString(context);
break;
case CompiledValue.FUNCTION:
case CompiledValue.PATH:
right = new PdxString((String) right);
}
}
}
Object result = TypeUtils.compare(left, right, operator);
// either of them is null and operator is other than == or !=
if (result == QueryService.UNDEFINED) {
// Undefined is added to results for != conditions only
if (operator != OQLLexerTokenTypes.TOK_NE || operator != OQLLexerTokenTypes.TOK_NE_ALT) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
} else {
return (Boolean) result;
}
}
}
Aggregations