Search in sources :

Example 91 with CacheException

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;
    }
}
Also used : CacheException(javax.cache.CacheException) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 92 with CacheException

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);
    }
}
Also used : SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl) UUID(java.util.UUID)

Example 93 with CacheException

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);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException)

Example 94 with CacheException

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);
    }
}
Also used : IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException)

Example 95 with CacheException

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();
        }
    });
}
Also used : IgniteOutClosureX(org.apache.ignite.internal.util.lang.IgniteOutClosureX) CacheException(javax.cache.CacheException) SpiQuery(org.apache.ignite.cache.query.SpiQuery) CacheQueryFuture(org.apache.ignite.internal.processors.cache.query.CacheQueryFuture) CacheQuery(org.apache.ignite.internal.processors.cache.query.CacheQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

CacheException (javax.cache.CacheException)144 Ignite (org.apache.ignite.Ignite)42 IgniteException (org.apache.ignite.IgniteException)36 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 Transaction (org.apache.ignite.transactions.Transaction)25 ArrayList (java.util.ArrayList)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 IgniteCache (org.apache.ignite.IgniteCache)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 List (java.util.List)16 Map (java.util.Map)16 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)15 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)13 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)13 HashMap (java.util.HashMap)12 IgniteTransactions (org.apache.ignite.IgniteTransactions)12 CyclicBarrier (java.util.concurrent.CyclicBarrier)11 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)10 Cache (javax.cache.Cache)9