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");
}
}
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);
}
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);
}
}
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();
}
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;
}
Aggregations