Search in sources :

Example 6 with TypeMismatchException

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

the class AddFreeItemToOrders method execute.

public void execute(FunctionContext context) {
    Region region = null;
    List<Object> vals = new ArrayList<Object>();
    List<Object> keys = new ArrayList<Object>();
    List<Object> argsList = new ArrayList<Object>();
    Object[] argsArray = null;
    if (context.getArguments() instanceof Boolean) {
    } else if (context.getArguments() instanceof String) {
        String arg = (String) context.getArguments();
    } else if (context.getArguments() instanceof Vector) {
    } else if (context.getArguments() instanceof Object[]) {
        argsArray = (Object[]) context.getArguments();
        argsList = Arrays.asList(argsArray);
    } else {
        System.out.println("AddFreeItemToOrders : Invalid Arguments");
    }
    InternalCache cache = null;
    try {
        cache = (InternalCache) CacheFactory.getAnyInstance();
        cache.getCacheConfig().setPdxReadSerialized(true);
        region = cache.getRegion("orders");
    } catch (CacheClosedException ex) {
        vals.add("NoCacheFoundResult");
        context.getResultSender().lastResult(vals);
    }
    String oql = "SELECT DISTINCT entry.key FROM /orders.entries entry WHERE entry.value.totalPrice > $1";
    Object[] queryArgs = new Object[1];
    queryArgs[0] = argsList.get(0);
    final Query query = cache.getQueryService().newQuery(oql);
    SelectResults result = null;
    try {
        result = (SelectResults) query.execute(queryArgs);
        int resultSize = result.size();
        if (result instanceof Collection<?>)
            for (Object item : result) {
                keys.add(item);
            }
    } catch (FunctionDomainException e) {
        if (cache != null)
            cache.getLogger().info("Caught FunctionDomainException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (TypeMismatchException e) {
        if (cache != null)
            cache.getLogger().info("Caught TypeMismatchException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (NameResolutionException e) {
        if (cache != null)
            cache.getLogger().info("Caught NameResolutionException while executing function AddFreeItemToOrders: " + e.getMessage());
    } catch (QueryInvocationTargetException e) {
        if (cache != null)
            cache.getLogger().info("Caught QueryInvocationTargetException while executing function AddFreeItemToOrders" + e.getMessage());
    }
    // class has to be in classpath.
    try {
        Item it = (Item) (argsList.get(1));
        for (Object key : keys) {
            Object obj = region.get(key);
            if (obj instanceof PdxInstance) {
                PdxInstance pi = (PdxInstance) obj;
                Order receivedOrder = (Order) pi.getObject();
                receivedOrder.addItem(it);
                region.put(key, receivedOrder);
            }
        }
        context.getResultSender().lastResult("success");
    } catch (ClassCastException e) {
        context.getResultSender().lastResult("failure");
    } catch (Exception e) {
        context.getResultSender().lastResult("failure");
    }
}
Also used : Query(org.apache.geode.cache.query.Query) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) SelectResults(org.apache.geode.cache.query.SelectResults) PdxInstance(org.apache.geode.pdx.PdxInstance) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Vector(java.util.Vector)

Example 7 with TypeMismatchException

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

the class MemoryIndexStore method updateMapping.

@Override
public void updateMapping(Object indexKey, Object oldKey, RegionEntry re, Object oldValue) throws IMQException {
    try {
        if (DefaultQuery.testHook != null) {
            DefaultQuery.testHook.doTestHook(3);
        }
        // Check if reverse-map is present.
        if (IndexManager.isObjectModificationInplace()) {
            // If reverse map get the old index key from reverse map.
            if (this.entryToValuesMap.containsKey(re)) {
                oldKey = this.entryToValuesMap.get(re);
            }
        } else {
            // forward map.
            if (oldValue != null && oldValue == getTargetObjectInVM(re)) {
                oldKey = getOldKey(indexKey, re);
            }
        }
        // No need to update the map if new and old index key are same.
        if (oldKey != null && oldKey.equals(TypeUtils.indexKeyFor(indexKey))) {
            return;
        }
        boolean retry = false;
        indexKey = TypeUtils.indexKeyFor(indexKey);
        if (indexKey.equals(QueryService.UNDEFINED)) {
            Object targetObject = getTargetObjectForUpdate(re);
            if (Token.isInvalidOrRemoved(targetObject)) {
                if (oldKey != null) {
                    basicRemoveMapping(oldKey, re, false);
                }
                return;
            }
        }
        do {
            retry = false;
            Object regionEntries = this.valueToEntriesMap.putIfAbsent(indexKey, re);
            if (regionEntries == TRANSITIONING_TOKEN) {
                retry = true;
                continue;
            } else if (regionEntries == null) {
                internalIndexStats.incNumKeys(1);
                numIndexKeys.incrementAndGet();
            } else if (regionEntries instanceof RegionEntry) {
                IndexElemArray elemArray = new IndexElemArray();
                if (DefaultQuery.testHook != null) {
                    DefaultQuery.testHook.doTestHook("BEGIN_TRANSITION_FROM_REGION_ENTRY_TO_ELEMARRAY");
                }
                elemArray.add(regionEntries);
                elemArray.add(re);
                if (!this.valueToEntriesMap.replace(indexKey, regionEntries, elemArray)) {
                    retry = true;
                }
                if (DefaultQuery.testHook != null) {
                    DefaultQuery.testHook.doTestHook("TRANSITIONED_FROM_REGION_ENTRY_TO_ELEMARRAY");
                }
                if (DefaultQuery.testHook != null) {
                    DefaultQuery.testHook.doTestHook("COMPLETE_TRANSITION_FROM_REGION_ENTRY_TO_ELEMARRAY");
                }
            } else if (regionEntries instanceof IndexConcurrentHashSet) {
                // basicRemoveMapping();
                synchronized (regionEntries) {
                    ((IndexConcurrentHashSet) regionEntries).add(re);
                }
                if (regionEntries != this.valueToEntriesMap.get(indexKey)) {
                    retry = true;
                }
            } else {
                IndexElemArray elemArray = (IndexElemArray) regionEntries;
                synchronized (elemArray) {
                    if (elemArray.size() >= IndexManager.INDEX_ELEMARRAY_THRESHOLD) {
                        IndexConcurrentHashSet set = new IndexConcurrentHashSet(IndexManager.INDEX_ELEMARRAY_THRESHOLD + 20, 0.75f, 1);
                        if (DefaultQuery.testHook != null) {
                            DefaultQuery.testHook.doTestHook("BEGIN_TRANSITION_FROM_ELEMARRAY_TO_CONCURRENT_HASH_SET");
                        }
                        // retry?
                        if (!this.valueToEntriesMap.replace(indexKey, regionEntries, TRANSITIONING_TOKEN)) {
                            retry = true;
                        } else {
                            if (DefaultQuery.testHook != null) {
                                DefaultQuery.testHook.doTestHook("TRANSITIONED_FROM_ELEMARRAY_TO_TOKEN");
                            }
                            set.add(re);
                            set.addAll(elemArray);
                            if (!this.valueToEntriesMap.replace(indexKey, TRANSITIONING_TOKEN, set)) {
                                // This should never happen. If we see this in the log, then something is wrong
                                // with the TRANSITIONING TOKEN and synchronization of changing collection types
                                // we should then just go from RE to CHS and completely remove the Elem Array.
                                region.getCache().getLogger().warning("Unable to transition from index elem to concurrent hash set.  Index needs to be recreated");
                                throw new IndexMaintenanceException("Unable to transition from index elem to concurrent hash set.  Index needs to be recreated");
                            }
                            if (DefaultQuery.testHook != null) {
                                DefaultQuery.testHook.doTestHook("COMPLETE_TRANSITION_FROM_ELEMARRAY_TO_CONCURRENT_HASH_SET");
                            }
                        }
                    } else {
                        elemArray.add(re);
                        if (regionEntries != this.valueToEntriesMap.get(indexKey)) {
                            retry = true;
                        }
                    }
                }
            }
            // Add to reverse Map with the new value.
            if (!retry) {
                // oldKey is not null only for an update
                if (oldKey != null) {
                    basicRemoveMapping(oldKey, re, false);
                }
                if (IndexManager.isObjectModificationInplace()) {
                    this.entryToValuesMap.put(re, indexKey);
                }
            }
        } while (retry);
    } catch (TypeMismatchException ex) {
        throw new IMQException("Could not add object of type " + indexKey.getClass().getName(), ex);
    }
    internalIndexStats.incNumValues(1);
}
Also used : TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) RegionEntry(org.apache.geode.internal.cache.RegionEntry) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException)

Example 8 with TypeMismatchException

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

the class HashIndex method basicRemoveMapping.

/**
   * Remove an index entry for a RegionEntry when invalidate/destroy is called OR new index key is
   * inserted for the RegionEntry. In case of update only forward map is cleared of old key and NO
   * update is performed on reverse map as that has already been done during
   * {@link HashIndex#basicAddMapping(Object, RegionEntry)}.
   *
   * @param key - Index key.
   * @param entry RegionEntry for which is being updated by user.
   * @param updateReverseMap true only when RegionEntry is invalidated/destroyed.
   */
private void basicRemoveMapping(Object key, RegionEntry entry, boolean updateReverseMap) throws IMQException {
    // removal
    try {
        Object newKey = TypeUtils.indexKeyFor(key);
        removeFromEntriesSet(newKey, entry, updateReverseMap);
    } 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)

Example 9 with TypeMismatchException

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

the class IndexOperatorJUnitTest method testWithUNDEFINED.

@Test
public void testWithUNDEFINED() throws Exception {
    try {
        runQuery(QueryService.UNDEFINED, 0);
    } catch (TypeMismatchException e) {
        fail();
    }
    try {
        runQuery(QueryService.UNDEFINED, QueryService.UNDEFINED);
    } catch (TypeMismatchException e) {
        fail();
    }
    Object[] objectArray = { "a", "b" };
    try {
        runQuery(objectArray, QueryService.UNDEFINED);
        fail();
    } catch (TypeMismatchException e) {
    }
    HashMap map = new HashMap();
    map.put("0", new Integer(11));
    map.put("1", new Integer(12));
    Object result = runQuery(map, QueryService.UNDEFINED);
    if (result != null)
        fail();
}
Also used : HashMap(java.util.HashMap) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with TypeMismatchException

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

the class LocalRegion method query.

@Override
public SelectResults query(String predicate) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    if (predicate == null) {
        throw new IllegalArgumentException("The input query predicate is null. A null predicate is not allowed.");
    }
    predicate = predicate.trim();
    SelectResults results;
    if (hasServerProxy()) {
        // Trim whitespace
        String queryString = constructRegionQueryString(predicate.trim());
        try {
            results = getServerProxy().query(queryString, null);
        } catch (Exception e) {
            Throwable cause = e.getCause();
            if (cause == null) {
                cause = e;
            }
            throw new QueryInvocationTargetException(e.getMessage(), cause);
        }
    } else {
        // TODO: params size is always zero so this whole block is wasted
        Object[] params = new Object[0];
        QueryService qs = getGemFireCache().getLocalQueryService();
        String queryStr = constructRegionQueryString(predicate.trim());
        DefaultQuery query = (DefaultQuery) qs.newQuery(queryStr);
        if (query.getRegionsInQuery(params).size() != 1) {
            throw new QueryInvalidException("Prevent multiple region query from being executed through region.query()");
        }
        results = (SelectResults) query.execute(params);
    }
    return results;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) StoredObject(org.apache.geode.internal.offheap.StoredObject) TimeoutException(org.apache.geode.cache.TimeoutException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InternalGemFireException(org.apache.geode.InternalGemFireException) ConflictingPersistentDataException(org.apache.geode.cache.persistence.ConflictingPersistentDataException) CacheRuntimeException(org.apache.geode.cache.CacheRuntimeException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ExecutionException(java.util.concurrent.ExecutionException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) EntryExistsException(org.apache.geode.cache.EntryExistsException) PartitionedRegionStorageException(org.apache.geode.cache.PartitionedRegionStorageException) StatisticsDisabledException(org.apache.geode.cache.StatisticsDisabledException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) FailedSynchronizationException(org.apache.geode.cache.FailedSynchronizationException) NoSuchElementException(java.util.NoSuchElementException) QueryException(org.apache.geode.cache.query.QueryException) RedundancyAlreadyMetException(org.apache.geode.internal.cache.partitioned.RedundancyAlreadyMetException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) SystemException(javax.transaction.SystemException) SubscriptionNotEnabledException(org.apache.geode.cache.client.SubscriptionNotEnabledException) RegionExistsException(org.apache.geode.cache.RegionExistsException) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CancelException(org.apache.geode.CancelException) DiskAccessException(org.apache.geode.cache.DiskAccessException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IndexMaintenanceException(org.apache.geode.cache.query.IndexMaintenanceException) TransactionException(org.apache.geode.cache.TransactionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) RollbackException(javax.transaction.RollbackException) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) DeltaSerializationException(org.apache.geode.DeltaSerializationException)

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