Search in sources :

Example 21 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class CacheConfiguration method setIndexedTypes.

/**
     * Array of key and value type pairs to be indexed (thus array length must be always even).
     * It means each even (0,2,4...) class in the array will be considered as key type for cache entry,
     * each odd (1,3,5...) class will be considered as value type for cache entry.
     * <p>
     * The same key class can occur multiple times for different value classes, but each value class must be unique
     * because SQL table will be named as value class simple name.
     * <p>
     * To expose fields of these types onto SQL level and to index them you have to use annotations
     * from package {@link org.apache.ignite.cache.query.annotations}.
     *
     * @param indexedTypes Key and value type pairs.
     * @return {@code this} for chaining.
     */
public CacheConfiguration<K, V> setIndexedTypes(Class<?>... indexedTypes) {
    if (F.isEmpty(indexedTypes))
        return this;
    int len = indexedTypes.length;
    if (len == 0)
        return this;
    A.ensure((len & 1) == 0, "Number of indexed types is expected to be even. Refer to method javadoc for details.");
    if (this.indexedTypes != null)
        throw new CacheException("Indexed types can be set only once.");
    Class<?>[] newIndexedTypes = new Class<?>[len];
    for (int i = 0; i < len; i++) {
        if (indexedTypes[i] == null)
            throw new NullPointerException("Indexed types array contains null at index: " + i);
        newIndexedTypes[i] = U.box(indexedTypes[i]);
    }
    if (qryEntities == null)
        qryEntities = new ArrayList<>();
    for (int i = 0; i < len; i += 2) {
        Class<?> keyCls = newIndexedTypes[i];
        Class<?> valCls = newIndexedTypes[i + 1];
        TypeDescriptor desc = processKeyAndValueClasses(keyCls, valCls);
        QueryEntity converted = convert(desc);
        boolean dup = false;
        for (QueryEntity entity : qryEntities) {
            if (F.eq(entity.findValueType(), converted.findValueType())) {
                dup = true;
                break;
            }
        }
        if (!dup)
            qryEntities.add(converted);
    }
    return this;
}
Also used : CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) QueryEntity(org.apache.ignite.cache.QueryEntity)

Example 22 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class CacheConfiguration method processAnnotationsInClass.

/**
     * Process annotations for class.
     *
     * @param key If given class relates to key.
     * @param cls Class.
     * @param type Type descriptor.
     * @param parent Parent in case of embeddable.
     */
private static void processAnnotationsInClass(boolean key, Class<?> cls, TypeDescriptor type, @Nullable ClassProperty parent) {
    if (U.isJdk(cls) || QueryUtils.isGeometryClass(cls)) {
        if (parent == null && !key && QueryUtils.isSqlType(cls)) {
            // We have to index primitive _val.
            String idxName = cls.getSimpleName() + "_" + QueryUtils.VAL_FIELD_NAME + "_idx";
            type.addIndex(idxName, QueryUtils.isGeometryClass(cls) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
            type.addFieldToIndex(idxName, QueryUtils.VAL_FIELD_NAME, 0, false);
        }
        return;
    }
    if (parent != null && parent.knowsClass(cls))
        throw new CacheException("Recursive reference found in type: " + cls.getName());
    if (parent == null) {
        // Check class annotation at top level only.
        QueryTextField txtAnnCls = cls.getAnnotation(QueryTextField.class);
        if (txtAnnCls != null)
            type.valueTextIndex(true);
        QueryGroupIndex grpIdx = cls.getAnnotation(QueryGroupIndex.class);
        if (grpIdx != null)
            type.addIndex(grpIdx.name(), QueryIndexType.SORTED);
        QueryGroupIndex.List grpIdxList = cls.getAnnotation(QueryGroupIndex.List.class);
        if (grpIdxList != null && !F.isEmpty(grpIdxList.value())) {
            for (QueryGroupIndex idx : grpIdxList.value()) type.addIndex(idx.name(), QueryIndexType.SORTED);
        }
    }
    for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            QuerySqlField sqlAnn = field.getAnnotation(QuerySqlField.class);
            QueryTextField txtAnn = field.getAnnotation(QueryTextField.class);
            if (sqlAnn != null || txtAnn != null) {
                ClassProperty prop = new ClassProperty(field);
                prop.parent(parent);
                // Add parent property before its possible nested properties so that
                // resulting parent column comes before columns corresponding to those
                // nested properties in the resulting table - that way nested
                // properties override will happen properly (first parent, then children).
                type.addProperty(prop, key, true);
                processAnnotation(key, sqlAnn, txtAnn, cls, c, field.getType(), prop, type);
            }
        }
    }
}
Also used : QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) Field(java.lang.reflect.Field) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) QueryTextField(org.apache.ignite.cache.query.annotations.QueryTextField) QueryGroupIndex(org.apache.ignite.cache.query.annotations.QueryGroupIndex) CacheException(javax.cache.CacheException)

Example 23 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class IgniteReflectionFactory method createInstance.

/**
     * @return Initialized instance.
     */
private T createInstance() {
    if (cls == null)
        throw new IllegalStateException("Failed to create object (object type is not set).");
    try {
        T obj = cls.newInstance();
        injectProperties(obj);
        return obj;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new CacheException("Failed to instantiate factory object: " + cls.getName(), e);
    }
}
Also used : CacheException(javax.cache.CacheException)

Example 24 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class CacheLockImpl method tryLock.

/** {@inheritDoc} */
@Override
public boolean tryLock() {
    CacheOperationContext prev = gate.enter(opCtx);
    try {
        checkTx();
        boolean res = delegate.lockAll(keys, -1);
        if (res)
            incrementLockCounter();
        return res;
    } 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 25 with CacheException

use of javax.cache.CacheException in project ignite by apache.

the class CacheLockImpl method lock.

/** {@inheritDoc} */
@Override
public void lock() {
    CacheOperationContext prev = gate.enter(opCtx);
    try {
        checkTx();
        delegate.lockAll(keys, 0);
        incrementLockCounter();
    } catch (IgniteCheckedException e) {
        throw new CacheException(e.getMessage(), e);
    } finally {
        gate.leave(prev);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException)

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