Search in sources :

Example 31 with PdxString

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;
}
Also used : TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) Iterator(java.util.Iterator) Collection(java.util.Collection) PdxString(org.apache.geode.pdx.internal.PdxString) PdxString(org.apache.geode.pdx.internal.PdxString) Map(java.util.Map)

Example 32 with PdxString

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;
}
Also used : EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) PdxString(org.apache.geode.pdx.internal.PdxString) PdxString(org.apache.geode.pdx.internal.PdxString) PdxInstance(org.apache.geode.pdx.PdxInstance) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 33 with PdxString

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);
}
Also used : ArrayList(java.util.ArrayList) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) PdxString(org.apache.geode.pdx.internal.PdxString) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 34 with PdxString

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();
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) PdxString(org.apache.geode.pdx.internal.PdxString) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 35 with PdxString

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;
        }
    }
}
Also used : CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) IndexInfo(org.apache.geode.cache.query.internal.IndexInfo) PdxString(org.apache.geode.pdx.internal.PdxString) PdxString(org.apache.geode.pdx.internal.PdxString)

Aggregations

PdxString (org.apache.geode.pdx.internal.PdxString)36 Test (org.junit.Test)22 Region (org.apache.geode.cache.Region)13 CacheException (org.apache.geode.cache.CacheException)11 CompactRangeIndex (org.apache.geode.cache.query.internal.index.CompactRangeIndex)11 RangeIndex (org.apache.geode.cache.query.internal.index.RangeIndex)11 SelectResults (org.apache.geode.cache.query.SelectResults)10 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)10 Host (org.apache.geode.test.dunit.Host)10 VM (org.apache.geode.test.dunit.VM)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 QueryService (org.apache.geode.cache.query.QueryService)9 PdxInstance (org.apache.geode.pdx.PdxInstance)9 IOException (java.io.IOException)8 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)8 IgnoredException (org.apache.geode.test.dunit.IgnoredException)8 AttributesFactory (org.apache.geode.cache.AttributesFactory)7 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)7 Index (org.apache.geode.cache.query.Index)7