Search in sources :

Example 11 with PdxString

use of org.apache.geode.pdx.internal.PdxString in project geode by apache.

the class PDXUtils method convertPDX.

public static Object convertPDX(Object obj, boolean isStruct, boolean getDomainObjectForPdx, boolean getDeserializedObject, boolean localResults, boolean[] objectChangedMarker, boolean isDistinct) {
    objectChangedMarker[0] = false;
    if (isStruct) {
        StructImpl simpl = (StructImpl) obj;
        if (getDomainObjectForPdx) {
            try {
                if (simpl.isHasPdx()) {
                    obj = simpl.getPdxFieldValues();
                    objectChangedMarker[0] = true;
                } else {
                    obj = simpl.getFieldValues();
                }
            } catch (Exception ex) {
                throw new CacheException("Unable to retrieve domain object from PdxInstance while building the ResultSet. " + ex.getMessage()) {
                };
            }
        } else {
            Object[] values = simpl.getFieldValues();
            if (getDeserializedObject) {
                for (int i = 0; i < values.length; i++) {
                    if (values[i] instanceof VMCachedDeserializable) {
                        values[i] = ((VMCachedDeserializable) values[i]).getDeserializedForReading();
                    }
                }
            }
            /* This is to convert PdxString to String */
            if (simpl.isHasPdx() && isDistinct && localResults) {
                for (int i = 0; i < values.length; i++) {
                    if (values[i] instanceof PdxString) {
                        values[i] = ((PdxString) values[i]).toString();
                    }
                }
            }
            obj = values;
        }
    } else {
        if (getDomainObjectForPdx) {
            if (obj instanceof PdxInstance) {
                try {
                    obj = ((PdxInstance) obj).getObject();
                    objectChangedMarker[0] = true;
                } catch (Exception ex) {
                    throw new CacheException("Unable to retrieve domain object from PdxInstance while building the ResultSet. " + ex.getMessage()) {
                    };
                }
            } else if (obj instanceof PdxString) {
                obj = ((PdxString) obj).toString();
            }
        } else if (isDistinct && localResults && obj instanceof PdxString) {
            /* This is to convert PdxString to String */
            obj = ((PdxString) obj).toString();
        }
        if (getDeserializedObject && obj instanceof VMCachedDeserializable) {
            obj = ((VMCachedDeserializable) obj).getDeserializedForReading();
            objectChangedMarker[0] = true;
        }
    }
    return obj;
}
Also used : StructImpl(org.apache.geode.cache.query.internal.StructImpl) PdxInstance(org.apache.geode.pdx.PdxInstance) CacheException(org.apache.geode.cache.CacheException) PdxString(org.apache.geode.pdx.internal.PdxString) VMCachedDeserializable(org.apache.geode.internal.cache.VMCachedDeserializable) CacheException(org.apache.geode.cache.CacheException)

Example 12 with PdxString

use of org.apache.geode.pdx.internal.PdxString in project geode by apache.

the class PrimaryKeyIndex method lockedQuery.

@Override
void lockedQuery(Object key, int operator, Collection results, Set keysToRemove, ExecutionContext context) throws TypeMismatchException {
    assert keysToRemove == null;
    int limit = -1;
    // Key cannot be PdxString in a region
    if (key instanceof PdxString) {
        key = key.toString();
    }
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    QueryObserver observer = QueryObserverHolder.getInstance();
    if (limit != -1 && results.size() == limit) {
        observer.limitAppliedAtIndexLevel(this, limit, results);
        return;
    }
    switch(operator) {
        case OQLLexerTokenTypes.TOK_EQ:
            {
                if (key != null && key != QueryService.UNDEFINED) {
                    Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false);
                    if (entry != null) {
                        Object value = entry.getValue();
                        if (value != null) {
                            results.add(value);
                        }
                    }
                }
                break;
            }
        case OQLLexerTokenTypes.TOK_NE_ALT:
        case OQLLexerTokenTypes.TOK_NE:
            {
                // add all btree values
                Set values = (Set) getRegion().values();
                // Add data one more than the limit
                if (limit != -1) {
                    ++limit;
                }
                // results.addAll(values);
                Iterator iter = values.iterator();
                while (iter.hasNext()) {
                    // Check if query execution on this thread is canceled.
                    QueryMonitor.isQueryExecutionCanceled();
                    results.add(iter.next());
                    if (limit != -1 && results.size() == limit) {
                        observer.limitAppliedAtIndexLevel(this, limit, results);
                        return;
                    }
                }
                boolean removeOneRow = limit != -1;
                if (key != null && key != QueryService.UNDEFINED) {
                    Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false);
                    if (entry != null) {
                        if (entry.getValue() != null) {
                            results.remove(entry.getValue());
                            removeOneRow = false;
                        }
                    }
                }
                if (removeOneRow) {
                    Iterator itr = results.iterator();
                    if (itr.hasNext()) {
                        itr.next();
                        itr.remove();
                    }
                }
                break;
            }
        default:
            {
                throw new IllegalArgumentException(LocalizedStrings.PrimaryKeyIndex_INVALID_OPERATOR.toLocalizedString());
            }
    }
    // end switch
    numUses++;
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) RegionEntry(org.apache.geode.internal.cache.RegionEntry) Set(java.util.Set) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) Iterator(java.util.Iterator) PdxString(org.apache.geode.pdx.internal.PdxString)

Example 13 with PdxString

use of org.apache.geode.pdx.internal.PdxString in project geode by apache.

the class PrimaryKeyIndex method lockedQuery.

void lockedQuery(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, List projAttrib, SelectResults intermediateResults, boolean isIntersection) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    QueryObserver observer = QueryObserverHolder.getInstance();
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit.booleanValue()) {
        limit = ((Integer) context.cacheGet(CompiledValue.RESULT_LIMIT)).intValue();
    }
    if (limit != -1 && results.size() == limit) {
        observer.limitAppliedAtIndexLevel(this, limit, results);
        return;
    }
    // Key cannot be PdxString in a region
    if (key instanceof PdxString) {
        key = key.toString();
    }
    switch(operator) {
        case OQLLexerTokenTypes.TOK_EQ:
            {
                if (key != null && key != QueryService.UNDEFINED) {
                    Region.Entry entry = ((LocalRegion) getRegion()).accessEntry(key, false);
                    if (entry != null) {
                        Object value = entry.getValue();
                        if (value != null) {
                            boolean ok = true;
                            if (runtimeItr != null) {
                                runtimeItr.setCurrent(value);
                                ok = QueryUtils.applyCondition(iterOps, context);
                            }
                            if (ok) {
                                applyProjection(projAttrib, context, results, value, intermediateResults, isIntersection);
                            }
                        }
                    }
                }
                break;
            }
        case OQLLexerTokenTypes.TOK_NE_ALT:
        case OQLLexerTokenTypes.TOK_NE:
            {
                // add all btree values
                Set entries = (Set) getRegion().entrySet();
                Iterator itr = entries.iterator();
                while (itr.hasNext()) {
                    Map.Entry entry = (Map.Entry) itr.next();
                    if (key != null && key != QueryService.UNDEFINED && key.equals(entry.getKey())) {
                        continue;
                    }
                    Object val = entry.getValue();
                    // TODO: is this correct. What should be the behaviour of null values?
                    if (val != null) {
                        boolean ok = true;
                        if (runtimeItr != null) {
                            runtimeItr.setCurrent(val);
                            ok = QueryUtils.applyCondition(iterOps, context);
                        }
                        if (ok) {
                            applyProjection(projAttrib, context, results, val, intermediateResults, isIntersection);
                        }
                        if (limit != -1 && results.size() == limit) {
                            observer.limitAppliedAtIndexLevel(this, limit, results);
                            break;
                        }
                    }
                }
                break;
            }
        default:
            {
                throw new IllegalArgumentException("Invalid Operator");
            }
    }
    // end switch
    numUses++;
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) RegionEntry(org.apache.geode.internal.cache.RegionEntry) Set(java.util.Set) RuntimeIterator(org.apache.geode.cache.query.internal.RuntimeIterator) Iterator(java.util.Iterator) PdxString(org.apache.geode.pdx.internal.PdxString) Map(java.util.Map)

Example 14 with PdxString

use of org.apache.geode.pdx.internal.PdxString in project geode by apache.

the class CompiledSelect method applyProjectionAndAddToResultSet.

// resultSet could be a set or a bag (we have set constructor, or there
// could be a distinct subquery)
// in future, it would be good to simplify this to always work with a bag
// (converting all sets to bags) until the end when we enforce distinct
// The number returned indicates the occurence of the data in the SelectResults
// Thus if the SelectResults is of type ResultsSet or StructSet
// then 1 will indicate that data was added to the results & that was the
// first occurence. For this 0 will indicate that the data was not added
// because it was a duplicate
// If the SelectResults is an instance ResultsBag or StructsBag , the number will
// indicate the occurence. Thus 1 will indicate it being added for first time
// Currently orderBy is present only for StructSet & ResultSet which are
// unique object holders. So the occurence for them can be either 0 or 1 only
private int applyProjectionAndAddToResultSet(ExecutionContext context, SelectResults resultSet, boolean ignoreOrderBy) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    List currrentRuntimeIters = context.getCurrentIterators();
    int occurence = 0;
    ObjectType elementType = resultSet.getCollectionType().getElementType();
    boolean isStruct = elementType != null && elementType.isStructType();
    // TODO: Optimize this condition in some clean way
    boolean isLinkedStructure = resultSet instanceof Ordered && ((Ordered) resultSet).dataPreordered();
    ArrayList evaluatedOrderByClause = null;
    OrderByComparator comparator = null;
    boolean applyOrderBy = false;
    if (this.orderByAttrs != null && !ignoreOrderBy) {
        // In case PR order-by will get applied on the coordinator node
        // on the cumulative results. Apply the order-by on PR only if
        // limit is specified.
        Integer limitValue = evaluateLimitValue(context, this.limit);
        if (context.getPartitionedRegion() != null && limitValue < 0) {
            applyOrderBy = false;
        }
        applyOrderBy = true;
    }
    if (this.orderByAttrs != null && !ignoreOrderBy) {
        comparator = (OrderByComparator) ((Ordered) resultSet).comparator();
    }
    if (projAttrs == null) {
        int len = currrentRuntimeIters.size();
        Object[] values = new Object[len];
        for (int i = 0; i < len; i++) {
            RuntimeIterator iter = (RuntimeIterator) currrentRuntimeIters.get(i);
            values[i] = iter.evaluate(context);
            // case of all Pdx objects in cache
            if (this.distinct && !((DefaultQuery) context.getQuery()).isRemoteQuery() && !context.getCache().getPdxReadSerialized() && (values[i] instanceof PdxInstance)) {
                values[i] = ((PdxInstance) values[i]).getObject();
            }
        }
        // Don't care about Order By for count(*).
        if (isCount() && !this.distinct) {
            // Counter is local to CompileSelect and not available in ResultSet
            // until
            // the end of evaluate call to this CompiledSelect object.
            this.countStartQueryResult++;
            occurence = 1;
        } else {
            // if order by is present
            if (applyOrderBy) {
                StructImpl structImpl;
                if (this.distinct) {
                    if (isStruct) {
                        if (values.length == 1 && values[0] instanceof StructImpl) {
                            structImpl = (StructImpl) values[0];
                            comparator.addEvaluatedSortCriteria(structImpl.getFieldValues(), context);
                            occurence = resultSet.add(structImpl) ? 1 : 0;
                        } else {
                            comparator.addEvaluatedSortCriteria(values, context);
                            occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                        }
                    // TODO:Instead of a normal Map containing which holds
                    // StructImpl object
                    // use a THashObject with Object[] array hashing stragtegy as we
                    // are unnnecessarily
                    // creating objects of type Object[]
                    } else {
                        comparator.addEvaluatedSortCriteria(values[0], context);
                        occurence = resultSet.add(values[0]) ? 1 : 0;
                    }
                } else {
                    if (isStruct) {
                        if (values.length == 1 && values[0] instanceof StructImpl) {
                            structImpl = (StructImpl) values[0];
                            comparator.addEvaluatedSortCriteria(structImpl.getFieldValues(), context);
                            occurence = ((Bag) resultSet).addAndGetOccurence(structImpl.getFieldValues());
                        } else {
                            comparator.addEvaluatedSortCriteria(values, context);
                            occurence = ((Bag) resultSet).addAndGetOccurence(values);
                        }
                    } else {
                        comparator.addEvaluatedSortCriteria(values[0], context);
                        occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                    }
                }
            } else {
                if (isLinkedStructure) {
                    if (isStruct) {
                        StructImpl structImpl;
                        if (values.length == 1 && values[0] instanceof StructImpl) {
                            structImpl = (StructImpl) values[0];
                        } else {
                            structImpl = new StructImpl((StructTypeImpl) elementType, values);
                        }
                        if (this.distinct) {
                            occurence = resultSet.add(structImpl) ? 1 : 0;
                        } else {
                            occurence = ((Bag) resultSet).addAndGetOccurence(structImpl);
                        }
                    } else {
                        if (this.distinct) {
                            occurence = resultSet.add(values[0]) ? 1 : 0;
                        } else {
                            occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                        }
                    }
                } else {
                    if (this.distinct) {
                        if (isStruct) {
                            occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                        } else {
                            occurence = resultSet.add(values[0]) ? 1 : 0;
                        }
                    } else {
                        if (isStruct) {
                            occurence = ((Bag) resultSet).addAndGetOccurence(values);
                        } else {
                            boolean add = true;
                            if (context.isCqQueryContext()) {
                                if (values[0] instanceof Region.Entry) {
                                    Region.Entry e = (Region.Entry) values[0];
                                    if (!e.isDestroyed()) {
                                        try {
                                            values[0] = new CqEntry(e.getKey(), e.getValue());
                                        } catch (EntryDestroyedException ignore) {
                                            // Even though isDestory() check is made, the entry could throw
                                            // EntryDestroyedException if the value becomes null.
                                            add = false;
                                        }
                                    } else {
                                        add = false;
                                    }
                                }
                            }
                            if (add) {
                                occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                            }
                        }
                    }
                }
            }
        }
    } else {
        // One or more projection attributes
        int projCount = projAttrs.size();
        Object[] values = new Object[projCount];
        for (int i = 0; i < projCount; i++) {
            Object[] projDef = (Object[]) projAttrs.get(i);
            values[i] = ((CompiledValue) projDef[1]).evaluate(context);
            // for remote queries
            if (!((DefaultQuery) context.getQuery()).isRemoteQuery()) {
                if (this.distinct && values[i] instanceof PdxInstance && !context.getCache().getPdxReadSerialized()) {
                    values[i] = ((PdxInstance) values[i]).getObject();
                } else if (values[i] instanceof PdxString) {
                    values[i] = ((PdxString) values[i]).toString();
                }
            }
        }
        // if order by is present
        if (applyOrderBy) {
            if (distinct) {
                if (isStruct) {
                    comparator.addEvaluatedSortCriteria(values, context);
                    // Occurence field is used to identify the corrcet number of
                    // iterations
                    // required to implement the limit based on the presence or absence
                    // of distinct clause
                    occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                } else {
                    comparator.addEvaluatedSortCriteria(values[0], context);
                    occurence = resultSet.add(values[0]) ? 1 : 0;
                }
            } else {
                if (isStruct) {
                    comparator.addEvaluatedSortCriteria(values, context);
                    occurence = ((Bag) resultSet).addAndGetOccurence(values);
                } else {
                    comparator.addEvaluatedSortCriteria(values[0], context);
                    occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                }
            }
        } else {
            if (isLinkedStructure) {
                if (isStruct) {
                    StructImpl structImpl = new StructImpl((StructTypeImpl) elementType, values);
                    if (this.distinct) {
                        occurence = resultSet.add(structImpl) ? 1 : 0;
                    } else {
                        occurence = ((Bag) resultSet).addAndGetOccurence(structImpl);
                    }
                } else {
                    if (this.distinct) {
                        occurence = resultSet.add(values[0]) ? 1 : 0;
                    } else {
                        occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                    }
                }
            } else {
                if (this.distinct) {
                    if (isStruct) {
                        occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                    } else {
                        occurence = resultSet.add(values[0]) ? 1 : 0;
                    }
                } else {
                    if (isStruct) {
                        occurence = ((Bag) resultSet).addAndGetOccurence(values);
                    } else {
                        occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                    }
                }
            }
        }
    }
    return occurence;
}
Also used : EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) ArrayList(java.util.ArrayList) PdxString(org.apache.geode.pdx.internal.PdxString) ObjectType(org.apache.geode.cache.query.types.ObjectType) PdxInstance(org.apache.geode.pdx.PdxInstance) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Region(org.apache.geode.cache.Region) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with PdxString

use of org.apache.geode.pdx.internal.PdxString in project geode by apache.

the class CompiledComparison method evaluate.

public Object evaluate(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    Object left = _left.evaluate(context);
    Object right = _right.evaluate(context);
    if (context.isCqQueryContext() && left instanceof Region.Entry) {
        left = ((Region.Entry) left).getValue();
    }
    if (context.isCqQueryContext() && right instanceof Region.Entry) {
        right = ((Region.Entry) right).getValue();
    }
    if (left == null || right == null) {
        return TypeUtils.compare(left, right, _operator);
    }
    // only if it is of the same class as that of the object being compared
    if (!context.getCache().getPdxReadSerialized()) {
        if (left instanceof PdxInstance && !(right instanceof PdxInstance) && ((PdxInstance) left).getClassName().equals(right.getClass().getName())) {
            left = ((PdxInstance) left).getObject();
        } else if (right instanceof PdxInstance && !(left instanceof PdxInstance) && ((PdxInstance) right).getClassName().equals(left.getClass().getName())) {
            right = ((PdxInstance) right).getObject();
        }
    }
    if (left instanceof PdxString) {
        if (right instanceof String) {
            switch(_right.getType()) {
                case LITERAL:
                    right = ((CompiledLiteral) _right).getSavedPdxString();
                    break;
                case QUERY_PARAM:
                    right = ((CompiledBindArgument) _right).getSavedPdxString(context);
                    break;
                case FUNCTION:
                case PATH:
                    right = new PdxString((String) right);
            }
        }
    } else if (right instanceof PdxString) {
        switch(_left.getType()) {
            case LITERAL:
                left = ((CompiledLiteral) _left).getSavedPdxString();
                break;
            case QUERY_PARAM:
                left = ((CompiledBindArgument) _left).getSavedPdxString(context);
                break;
            case FUNCTION:
            case PATH:
                left = new PdxString((String) left);
        }
    }
    return TypeUtils.compare(left, right, _operator);
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) Region(org.apache.geode.cache.Region) 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