use of javax.cache.CacheException in project ignite by apache.
the class CacheJdbcPojoStore method prepareBuilders.
/**
* Prepare internal store specific builders for provided types metadata.
*
* @param cacheName Cache name to prepare builders for.
* @param types Collection of types.
* @throws CacheException If failed to prepare internal builders for types.
*/
@Override
protected void prepareBuilders(@Nullable String cacheName, Collection<JdbcType> types) throws CacheException {
Map<String, PojoPropertiesCache> pojoProps = U.newHashMap(types.size() * 2);
for (JdbcType type : types) {
String keyTypeName = type.getKeyType();
TypeKind keyKind = kindForName(keyTypeName);
if (keyKind == TypeKind.POJO) {
if (pojoProps.containsKey(keyTypeName))
throw new CacheException("Found duplicate key type [cache=" + U.maskName(cacheName) + ", keyType=" + keyTypeName + "]");
pojoProps.put(keyTypeName, new PojoPropertiesCache(keyTypeName, type.getKeyFields()));
}
String valTypeName = type.getValueType();
TypeKind valKind = kindForName(valTypeName);
if (valKind == TypeKind.POJO)
pojoProps.put(valTypeName, new PojoPropertiesCache(valTypeName, type.getValueFields()));
}
if (!pojoProps.isEmpty()) {
Map<String, Map<String, PojoPropertiesCache>> newPojosProps = new HashMap<>(pojosProps);
newPojosProps.put(cacheName, pojoProps);
pojosProps = newPojosProps;
}
}
use of javax.cache.CacheException in project ignite by apache.
the class CacheAbstractJdbcStore method fillParameter.
/**
* Sets the value of the designated parameter using the given object.
*
* @param stmt Prepare statement.
* @param idx Index for parameters.
* @param field Field descriptor.
* @param fieldVal Field value.
* @throws CacheException If failed to set statement parameter.
*/
protected void fillParameter(PreparedStatement stmt, int idx, JdbcTypeField field, @Nullable Object fieldVal) throws CacheException {
try {
if (fieldVal != null) {
if (field.getJavaFieldType() == UUID.class) {
switch(field.getDatabaseFieldType()) {
case Types.BINARY:
fieldVal = U.uuidToBytes((UUID) fieldVal);
break;
case Types.CHAR:
case Types.VARCHAR:
fieldVal = fieldVal.toString();
break;
default:
}
} else if (field.getJavaFieldType().isEnum()) {
if (fieldVal instanceof Enum) {
Enum val = (Enum) fieldVal;
fieldVal = NUMERIC_TYPES.contains(field.getDatabaseFieldType()) ? val.ordinal() : val.name();
} else if (fieldVal instanceof BinaryEnumObjectImpl) {
BinaryEnumObjectImpl val = (BinaryEnumObjectImpl) fieldVal;
fieldVal = val.enumOrdinal();
}
}
stmt.setObject(idx, fieldVal);
} else
stmt.setNull(idx, field.getDatabaseFieldType());
} catch (SQLException e) {
throw new CacheException("Failed to set statement parameter name: " + field.getDatabaseFieldName(), e);
}
}
use of javax.cache.CacheException in project ignite by apache.
the class CacheLockImpl method unlock.
/** {@inheritDoc} */
@Override
public void unlock() {
CacheOperationContext prev = gate.enter(opCtx);
try {
if (lockedThread != Thread.currentThread()) {
throw new IllegalStateException("Failed to unlock keys (did current thread acquire lock " + "with this lock instance?).");
}
assert cntr > 0;
cntr--;
if (cntr == 0)
lockedThread = null;
delegate.unlockAll(keys);
} catch (IgniteCheckedException e) {
throw new CacheException(e.getMessage(), e);
} finally {
gate.leave(prev);
}
}
use of javax.cache.CacheException in project ignite by apache.
the class CacheLockImpl method tryLock.
/** {@inheritDoc} */
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (time <= 0)
return tryLock();
CacheOperationContext prev = gate.enter(opCtx);
try {
checkTx();
IgniteInternalFuture<Boolean> fut = delegate.lockAllAsync(keys, unit.toMillis(time));
try {
boolean res = fut.get();
if (res)
incrementLockCounter();
return res;
} catch (IgniteInterruptedCheckedException e) {
if (!fut.cancel()) {
if (fut.isDone()) {
Boolean res = fut.get();
Thread.currentThread().interrupt();
if (res)
incrementLockCounter();
return res;
}
}
if (e.getCause() instanceof InterruptedException)
throw (InterruptedException) e.getCause();
throw new InterruptedException();
}
} catch (IgniteCheckedException e) {
throw new CacheException(e.getMessage(), e);
} finally {
gate.leave(prev);
}
}
use of javax.cache.CacheException in project ignite by apache.
the class IgniteCacheProxy method query.
/**
* @param filter Filter.
* @param grp Optional cluster group.
* @return Cursor.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> query(final Query filter, @Nullable ClusterGroup grp) throws IgniteCheckedException {
final CacheQuery qry;
boolean isKeepBinary = opCtx != null && opCtx.isKeepBinary();
final CacheQueryFuture fut;
if (filter instanceof TextQuery) {
TextQuery p = (TextQuery) filter;
qry = ctx.queries().createFullTextQuery(p.getType(), p.getText(), isKeepBinary);
if (grp != null)
qry.projection(grp);
fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.TEXT, p.getText(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {
@Override
public CacheQueryFuture<Map.Entry<K, V>> applyx() {
return qry.execute();
}
}, false);
} else if (filter instanceof SpiQuery) {
qry = ctx.queries().createSpiQuery(isKeepBinary);
if (grp != null)
qry.projection(grp);
fut = ctx.kernalContext().query().executeQuery(GridCacheQueryType.SPI, filter.getClass().getSimpleName(), ctx, new IgniteOutClosureX<CacheQueryFuture<Map.Entry<K, V>>>() {
@Override
public CacheQueryFuture<Map.Entry<K, V>> applyx() {
return qry.execute(((SpiQuery) filter).getArgs());
}
}, false);
} else {
if (filter instanceof SqlFieldsQuery)
throw new CacheException("Use methods 'queryFields' and 'localQueryFields' for " + SqlFieldsQuery.class.getSimpleName() + ".");
throw new CacheException("Unsupported query type: " + filter);
}
return new QueryCursorImpl<>(new GridCloseableIteratorAdapter<Entry<K, V>>() {
/** */
private Cache.Entry<K, V> cur;
@Override
protected Entry<K, V> onNext() throws IgniteCheckedException {
if (!onHasNext())
throw new NoSuchElementException();
Cache.Entry<K, V> e = cur;
cur = null;
return e;
}
@Override
protected boolean onHasNext() throws IgniteCheckedException {
if (cur != null)
return true;
Object next = fut.next();
// instead of Iterator<Map.Entry> due to IndexingSpi interface.
if (next == null)
return false;
if (next instanceof Cache.Entry)
cur = (Cache.Entry) next;
else {
Map.Entry e = (Map.Entry) next;
cur = new CacheEntryImpl(e.getKey(), e.getValue());
}
return true;
}
@Override
protected void onClose() throws IgniteCheckedException {
fut.cancel();
}
});
}
Aggregations