Search in sources :

Example 21 with TypeMismatchException

use of org.apache.geode.cache.query.TypeMismatchException in project geode by apache.

the class HashIndex method basicAddMapping.

/**
   * Add/Updates the index forward and reverse map. If index key for a RegionEntry is found same as
   * previous key no update is performed.
   *
   * This also updates the {@link IndexStatistics} numKeys and numValues as and when appropriate.
   * One thing to notice though is no increment in numValues is performed if old key and new index
   * key are found equal using {@link Object#equals(Object)}.
   */
private void basicAddMapping(Object key, RegionEntry entry) throws IMQException {
    try {
        if (DefaultQuery.testHook != null) {
            DefaultQuery.testHook.doTestHook(3);
        }
        Object newKey = TypeUtils.indexKeyFor(key);
        if (newKey.equals(QueryService.UNDEFINED)) {
            Object targetObject = getTargetObjectForUpdate(entry);
            if (Token.isInvalidOrRemoved(targetObject)) {
                // This should not happen as a token should only be added during gii
                // meaning we do not have an old mapping
                // we will continue to remove the old mapping to be safe and log a fine level message
                Object oldKey = null;
                if (IndexManager.isObjectModificationInplace() && this.entryToValuesMap.containsKey(entry)) {
                    oldKey = this.entryToValuesMap.get(entry);
                } else if (!IndexManager.isObjectModificationInplace() && this.entryToOldKeysMap != null) {
                    Map oldKeyMap = this.entryToOldKeysMap.get();
                    if (oldKeyMap != null) {
                        oldKey = TypeUtils.indexKeyFor(oldKeyMap.get(entry));
                    }
                }
                if (oldKey != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("A removed or invalid token was being added, and we had an old mapping.");
                    }
                    removeFromEntriesSet(oldKey, entry, true);
                }
                return;
            }
        }
        // Before adding the entry with new value, remove it from reverse map and
        // using the oldValue remove entry from the forward map.
        // Reverse-map is used based on the system property
        Object oldKey = getOldKey(entry);
        int indexSlot = this.entriesSet.add(newKey, entry);
        if (indexSlot >= 0) {
            // Update the reverse map
            if (IndexManager.isObjectModificationInplace()) {
                this.entryToValuesMap.put(entry, newKey);
            }
            if (newKey != null && oldKey != null) {
                removeFromEntriesSet(oldKey, entry, false, indexSlot);
            }
            // Update Stats after real addition
            internalIndexStats.incNumValues(1);
        }
    } catch (TypeMismatchException ex) {
        throw new IMQException("Could not add object of type " + key.getClass().getName(), ex);
    }
}
Also used : TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) StoredObject(org.apache.geode.internal.offheap.StoredObject) Object2ObjectOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 22 with TypeMismatchException

use of org.apache.geode.cache.query.TypeMismatchException in project geode by apache.

the class HashIndex method evaluate.

private void evaluate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, boolean applyOrderBy, List orderByAttribs) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    boolean multiColOrderBy = false;
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    key = TypeUtils.indexKeyFor(key);
    if (key == null) {
        key = IndexManager.NULL;
    }
    boolean asc = true;
    if (applyOrderBy) {
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttribs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttribs.size() > 1;
    }
    try {
        long iteratorCreationTime = GemFireCacheImpl.getInstance().cacheTimeMillis();
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                assert keysToRemove.isEmpty();
                addToResultsFromEntries(this.entriesSet.get(key), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
                break;
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    keysToRemove.add(key);
                    addToResultsFromEntries(this.entriesSet.getAllNotMatching(keysToRemove), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
                }
                break;
            default:
                throw new AssertionError("Operator = " + operator);
        }
    // end switch
    } catch (ClassCastException ex) {
        if (operator == OQLLexerTokenTypes.TOK_EQ) {
            // set
            return;
        } else if (operator == OQLLexerTokenTypes.TOK_NE || operator == OQLLexerTokenTypes.TOK_NE_ALT) {
            // put
            keysToRemove.add(key);
            long iteratorCreationTime = GemFireCacheImpl.getInstance().cacheTimeMillis();
            addToResultsFromEntries(this.entriesSet.getAllNotMatching(keysToRemove), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
        } else {
            // otherwise throw exception
            throw new TypeMismatchException("", ex);
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) HashSet(java.util.HashSet)

Example 23 with TypeMismatchException

use of org.apache.geode.cache.query.TypeMismatchException 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;
}
Also used : Index(org.apache.geode.cache.query.Index) AmbiguousNameException(org.apache.geode.cache.query.AmbiguousNameException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) ExecutionException(java.util.concurrent.ExecutionException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) FutureTask(java.util.concurrent.FutureTask) Iterator(java.util.Iterator)

Example 24 with TypeMismatchException

use of org.apache.geode.cache.query.TypeMismatchException in project geode by apache.

the class TypeUtils method indexTypeFor.

// indexes are allowed on primitive types except char,
// plus Strings, and temporals
/**
   * Return the type of the keys for a given path type.
   * 
   * @param pathType the Class of the last attribute in the path
   * @return the Class of the index keys
   * @throws TypeMismatchException if indexes are not allowed on this type
   */
public static Class indexTypeFor(Class pathType) throws TypeMismatchException {
    if (Character.class.isAssignableFrom(pathType) || Character.TYPE == pathType)
        return pathType;
    if (Byte.class.isAssignableFrom(pathType) || Short.class.isAssignableFrom(pathType) || Byte.TYPE == pathType || Short.TYPE == pathType)
        return Integer.class;
    Iterator i = _numericWrapperClasses.iterator();
    while (i.hasNext()) {
        Class cls = (Class) i.next();
        if (cls.isAssignableFrom(pathType))
            return pathType;
    }
    i = _numericPrimitiveClasses.iterator();
    while (i.hasNext()) {
        Class cls = (Class) i.next();
        if (cls == pathType)
            return pathType;
    }
    if (java.util.Date.class.isAssignableFrom(pathType) || pathType == String.class)
        return pathType;
    throw new TypeMismatchException(LocalizedStrings.TypeUtils_INDEXES_ARE_NOT_SUPPORTED_ON_PATHS_OF_TYPE_0.toLocalizedString(pathType.getName()));
}
Also used : TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) Iterator(java.util.Iterator) PdxString(org.apache.geode.pdx.internal.PdxString)

Example 25 with TypeMismatchException

use of org.apache.geode.cache.query.TypeMismatchException in project geode by apache.

the class RangeIndex method evaluate.

private void evaluate(Object key, int operator, Collection results, Set keysToRemove, int limit, ExecutionContext context) throws TypeMismatchException {
    key = TypeUtils.indexKeyFor(key);
    Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
    boolean multiColOrderBy = false;
    boolean asc = true;
    List orderByAttrs = null;
    if (orderByClause != null && orderByClause) {
        orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttrs.size() > 1;
    }
    limit = multiColOrderBy ? -1 : limit;
    try {
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                {
                    assert keysToRemove == null;
                    addValuesToResult(this.valueToEntriesMap.get(key), results, keysToRemove, limit, context);
                    break;
                }
            case OQLLexerTokenTypes.TOK_LT:
                {
                    NavigableMap sm = this.valueToEntriesMap.headMap(key, false);
                    sm = asc ? sm : sm.descendingMap();
                    addValuesToResult(sm, results, keysToRemove, limit, context);
                    break;
                }
            case OQLLexerTokenTypes.TOK_LE:
                {
                    NavigableMap sm = this.valueToEntriesMap.headMap(key, true);
                    sm = asc ? sm : sm.descendingMap();
                    addValuesToResult(sm, results, keysToRemove, limit, context);
                    break;
                }
            case OQLLexerTokenTypes.TOK_GT:
                {
                    // Asif:As tail Map returns the SortedMap vie which is greater than or
                    // equal to the key passed, the equal to key needs to be removed.
                    // However if the boundtary key is already part of the keysToRemove set
                    // then we do not have to remove it as it is already taken care of
                    NavigableMap sm = this.valueToEntriesMap.tailMap(key, false);
                    sm = asc ? sm : sm.descendingMap();
                    addValuesToResult(sm, results, keysToRemove, limit, context);
                    break;
                }
            case OQLLexerTokenTypes.TOK_GE:
                {
                    NavigableMap sm = this.valueToEntriesMap.tailMap(key, true);
                    sm = asc ? sm : sm.descendingMap();
                    addValuesToResult(sm, results, keysToRemove, limit, context);
                    break;
                }
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    NavigableMap sm = this.valueToEntriesMap;
                    if (!asc) {
                        sm = sm.descendingMap();
                    }
                    if (keysToRemove == null) {
                        addValuesToResultSingleKeyToRemove(sm, results, key, limit, context);
                    } else {
                        // TODO:Asif Somehow avoid this removal & then addition
                        keysToRemove.add(key);
                        addValuesToResult(sm, results, keysToRemove, limit, context);
                    }
                    nullMappedEntries.addValuesToCollection(results, limit, context);
                    undefinedMappedEntries.addValuesToCollection(results, limit, context);
                    break;
                }
            default:
                {
                    throw new IllegalArgumentException(LocalizedStrings.RangeIndex_OPERATOR_0.toLocalizedString(valueOf(operator)));
                }
        }
    // end switch
    } catch (ClassCastException ex) {
        if (operator == OQLLexerTokenTypes.TOK_EQ) {
            // result is empty set
            return;
        } else if (operator == OQLLexerTokenTypes.TOK_NE || operator == OQLLexerTokenTypes.TOK_NE_ALT) {
            // put
            // all
            // in
            // result
            NavigableMap sm = this.valueToEntriesMap;
            if (!asc) {
                sm = sm.descendingMap();
            }
            addValuesToResult(sm, results, keysToRemove, limit, context);
            nullMappedEntries.addValuesToCollection(results, limit, context);
            undefinedMappedEntries.addValuesToCollection(results, limit, context);
        } else {
            // otherwise throw exception
            throw new TypeMismatchException("", ex);
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)36 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)17 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)16 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)16 SelectResults (org.apache.geode.cache.query.SelectResults)15 Query (org.apache.geode.cache.query.Query)12 Iterator (java.util.Iterator)11 ArrayList (java.util.ArrayList)9 QueryService (org.apache.geode.cache.query.QueryService)9 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)9 Collection (java.util.Collection)7 List (java.util.List)6 QueryException (org.apache.geode.cache.query.QueryException)6 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)6 Host (org.apache.geode.test.dunit.Host)6 VM (org.apache.geode.test.dunit.VM)6 Map (java.util.Map)5 QueryExecutionTimeoutException (org.apache.geode.cache.query.QueryExecutionTimeoutException)5 ObjectType (org.apache.geode.cache.query.types.ObjectType)5 HashSet (java.util.HashSet)4