Search in sources :

Example 11 with CacheKeyConfiguration

use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.

the class CacheAffinityKeyConfigurationMismatchTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    CacheKeyConfiguration[] cacheKeyCfg;
    if (getTestIgniteInstanceName(0).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { new CacheKeyConfiguration(AKey.class) };
    } else if (getTestIgniteInstanceName(1).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { getCacheAKeyConfiguration("a") };
    } else if (getTestIgniteInstanceName(2).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { new CacheKeyConfiguration(AKey.class), getCacheAKeyConfiguration("a") };
    } else if (getTestIgniteInstanceName(3).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { new CacheKeyConfiguration(AKey.class), getCacheAKeyConfiguration("b") };
    } else if (getTestIgniteInstanceName(4).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { getCacheAKeyConfiguration("b") };
    } else if (getTestIgniteInstanceName(5).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {};
    } else if (getTestIgniteInstanceName(6).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { new CacheKeyConfiguration(AKey.class), new CacheKeyConfiguration(BKey.class) };
    } else if (getTestIgniteInstanceName(7).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] { new CacheKeyConfiguration(BKey.class) };
    } else {
        cacheKeyCfg = null;
    }
    if (cacheKeyCfg != null) {
        CacheConfiguration cacheCfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cacheCfg.setCacheMode(CacheMode.PARTITIONED);
        cacheCfg.setKeyConfiguration(cacheKeyCfg);
        cfg.setCacheConfiguration(cacheCfg);
    }
    return cfg;
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 12 with CacheKeyConfiguration

use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.

the class ClientUtils method cacheConfiguration.

/**
 * Deserialize configuration from stream.
 */
ClientCacheConfiguration cacheConfiguration(BinaryInputStream in, ProtocolContext protocolCtx) throws IOException {
    try (BinaryReaderExImpl reader = createBinaryReader(in)) {
        // Do not need length to read data. The protocol defines fixed configuration layout.
        reader.readInt();
        return // cache name is to be assigned later
        new ClientCacheConfiguration().setName("TBD").setAtomicityMode(CacheAtomicityMode.fromOrdinal(reader.readInt())).setBackups(reader.readInt()).setCacheMode(CacheMode.fromOrdinal(reader.readInt())).setCopyOnRead(reader.readBoolean()).setDataRegionName(reader.readString()).setEagerTtl(reader.readBoolean()).setStatisticsEnabled(reader.readBoolean()).setGroupName(reader.readString()).setDefaultLockTimeout(reader.readLong()).setMaxConcurrentAsyncOperations(reader.readInt()).setMaxQueryIteratorsCount(reader.readInt()).setName(reader.readString()).setOnheapCacheEnabled(reader.readBoolean()).setPartitionLossPolicy(PartitionLossPolicy.fromOrdinal((byte) reader.readInt())).setQueryDetailMetricsSize(reader.readInt()).setQueryParallelism(reader.readInt()).setReadFromBackup(reader.readBoolean()).setRebalanceBatchSize(reader.readInt()).setRebalanceBatchesPrefetchCount(reader.readLong()).setRebalanceDelay(reader.readLong()).setRebalanceMode(CacheRebalanceMode.fromOrdinal(reader.readInt())).setRebalanceOrder(reader.readInt()).setRebalanceThrottle(reader.readLong()).setRebalanceTimeout(reader.readLong()).setSqlEscapeAll(reader.readBoolean()).setSqlIndexMaxInlineSize(reader.readInt()).setSqlSchema(reader.readString()).setWriteSynchronizationMode(CacheWriteSynchronizationMode.fromOrdinal(reader.readInt())).setKeyConfiguration(ClientUtils.collection(in, unused -> new CacheKeyConfiguration(reader.readString(), reader.readString())).toArray(new CacheKeyConfiguration[0])).setQueryEntities(ClientUtils.collection(in, unused -> {
            QueryEntity qryEntity = new QueryEntity(reader.readString(), reader.readString()).setTableName(reader.readString()).setKeyFieldName(reader.readString()).setValueFieldName(reader.readString());
            boolean isPrecisionAndScaleSupported = protocolCtx.isFeatureSupported(QUERY_ENTITY_PRECISION_AND_SCALE);
            Collection<QueryField> qryFields = ClientUtils.collection(in, unused2 -> {
                String name = reader.readString();
                String typeName = reader.readString();
                boolean isKey = reader.readBoolean();
                boolean isNotNull = reader.readBoolean();
                Object dfltVal = reader.readObject();
                int precision = isPrecisionAndScaleSupported ? reader.readInt() : -1;
                int scale = isPrecisionAndScaleSupported ? reader.readInt() : -1;
                return new QueryField(name, typeName, isKey, isNotNull, dfltVal, precision, scale);
            });
            return qryEntity.setFields(qryFields.stream().collect(Collectors.toMap(QueryField::getName, QueryField::getTypeName, (a, b) -> a, LinkedHashMap::new))).setKeyFields(qryFields.stream().filter(QueryField::isKey).map(QueryField::getName).collect(Collectors.toCollection(LinkedHashSet::new))).setNotNullFields(qryFields.stream().filter(QueryField::isNotNull).map(QueryField::getName).collect(Collectors.toSet())).setDefaultFieldValues(qryFields.stream().filter(f -> f.getDefaultValue() != null).collect(Collectors.toMap(QueryField::getName, QueryField::getDefaultValue))).setFieldsPrecision(qryFields.stream().filter(f -> f.getPrecision() != -1).collect(Collectors.toMap(QueryField::getName, QueryField::getPrecision))).setFieldsScale(qryFields.stream().filter(f -> f.getScale() != -1).collect(Collectors.toMap(QueryField::getName, QueryField::getScale))).setAliases(ClientUtils.collection(in, unused3 -> new SimpleEntry<>(reader.readString(), reader.readString())).stream().collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue))).setIndexes(ClientUtils.collection(in, unused4 -> {
                String name = reader.readString();
                QueryIndexType type = QueryIndexType.fromOrdinal(reader.readByte());
                int inlineSize = reader.readInt();
                LinkedHashMap<String, Boolean> fields = ClientUtils.collection(in, unused5 -> new SimpleEntry<>(reader.readString(), reader.readBoolean())).stream().collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new));
                return new QueryIndex(fields, type).setName(name).setInlineSize(inlineSize);
            }));
        }).toArray(new QueryEntity[0])).setExpiryPolicy(!protocolCtx.isFeatureSupported(EXPIRY_POLICY) ? null : reader.readBoolean() ? new PlatformExpiryPolicy(reader.readLong(), reader.readLong(), reader.readLong()) : null);
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) PlatformExpiryPolicy.convertDuration(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy.convertDuration) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) MutableSingletonList(org.apache.ignite.internal.util.MutableSingletonList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) BinaryRawWriter(org.apache.ignite.binary.BinaryRawWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryUtils(org.apache.ignite.internal.binary.BinaryUtils) Map(java.util.Map) QueryEntity(org.apache.ignite.cache.QueryEntity) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) PartitionLossPolicy(org.apache.ignite.cache.PartitionLossPolicy) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) BiConsumer(java.util.function.BiConsumer) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) EXPIRY_POLICY(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.EXPIRY_POLICY) LinkedHashSet(java.util.LinkedHashSet) BinaryThreadLocalContext(org.apache.ignite.internal.binary.BinaryThreadLocalContext) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) QueryIndexType(org.apache.ignite.cache.QueryIndexType) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) IOException(java.io.IOException) BinaryReaderHandles(org.apache.ignite.internal.binary.BinaryReaderHandles) Consumer(java.util.function.Consumer) QUERY_ENTITY_PRECISION_AND_SCALE(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.QUERY_ENTITY_PRECISION_AND_SCALE) QueryIndex(org.apache.ignite.cache.QueryIndex) CacheMode(org.apache.ignite.cache.CacheMode) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryIndexType(org.apache.ignite.cache.QueryIndexType) QueryEntity(org.apache.ignite.cache.QueryEntity) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) LinkedHashMap(java.util.LinkedHashMap) Collection(java.util.Collection) QueryIndex(org.apache.ignite.cache.QueryIndex) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 13 with CacheKeyConfiguration

use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.

the class GridCacheUtils method validateKeyConfigiration.

/**
 * Validate affinity key configurations.
 * All fields are initialized and not empty (typeName and affKeyFieldName is defined).
 * Definition for the type does not repeat.
 *
 * @param groupName Cache group name.
 * @param cacheName Cache name.
 * @param cacheKeyCfgs keyConfiguration to validate.
 * @param log Logger used to log warning message (used only if fail flag is not set).
 * @param fail If true throws IgniteCheckedException in case of attribute values mismatch, otherwise logs warning.
 * @return Affinity key maps (typeName -> fieldName)
 * @throws IgniteCheckedException In case the affinity key configurations is not valid.
 */
public static Map<String, String> validateKeyConfigiration(String groupName, String cacheName, CacheKeyConfiguration[] cacheKeyCfgs, IgniteLogger log, boolean fail) throws IgniteCheckedException {
    Map<String, String> keyConfigurations = new HashMap<>();
    if (cacheKeyCfgs != null) {
        for (CacheKeyConfiguration cacheKeyCfg : cacheKeyCfgs) {
            String typeName = cacheKeyCfg.getTypeName();
            A.notNullOrEmpty(typeName, "typeName");
            String fieldName = cacheKeyCfg.getAffinityKeyFieldName();
            A.notNullOrEmpty(fieldName, "affKeyFieldName");
            String oldFieldName = keyConfigurations.put(typeName, fieldName);
            if (oldFieldName != null && !oldFieldName.equals(fieldName)) {
                final String msg = "Cache key configuration contains conflicting definitions: [" + (groupName != null ? "cacheGroup=" + groupName + ", " : "") + "cacheName=" + cacheName + ", " + "typeName=" + typeName + ", " + "affKeyFieldName1=" + oldFieldName + ", " + "affKeyFieldName2=" + fieldName + "].";
                throwIgniteCheckedException(log, fail, msg);
            }
        }
    }
    return keyConfigurations;
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 14 with CacheKeyConfiguration

use of org.apache.ignite.cache.CacheKeyConfiguration 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];
        QueryEntity newEntity = new QueryEntity(keyCls, valCls);
        boolean dup = false;
        for (QueryEntity entity : qryEntities) {
            if (F.eq(entity.findValueType(), newEntity.findValueType())) {
                dup = true;
                break;
            }
        }
        if (!dup)
            qryEntities.add(newEntity);
        // Set key configuration if needed.
        String affFieldName = BinaryContext.affinityFieldName(keyCls);
        if (affFieldName != null) {
            CacheKeyConfiguration newKeyCfg = new CacheKeyConfiguration(newEntity.getKeyType(), affFieldName);
            if (F.isEmpty(keyCfg))
                keyCfg = new CacheKeyConfiguration[] { newKeyCfg };
            else {
                boolean keyCfgDup = false;
                for (CacheKeyConfiguration oldKeyCfg : keyCfg) {
                    if (F.eq(oldKeyCfg.getTypeName(), newKeyCfg.getTypeName())) {
                        keyCfgDup = true;
                        break;
                    }
                }
                if (!keyCfgDup) {
                    CacheKeyConfiguration[] keyCfg0 = new CacheKeyConfiguration[keyCfg.length + 1];
                    System.arraycopy(keyCfg, 0, keyCfg0, 0, keyCfg.length);
                    keyCfg0[keyCfg0.length - 1] = newKeyCfg;
                    keyCfg = keyCfg0;
                }
            }
        }
    }
    return this;
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) QueryEntity(org.apache.ignite.cache.QueryEntity)

Example 15 with CacheKeyConfiguration

use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.

the class GridBinaryAffinityKeySelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    BinaryTypeConfiguration typeCfg = new BinaryTypeConfiguration();
    typeCfg.setTypeName(TestObject.class.getName());
    BinaryConfiguration bCfg = new BinaryConfiguration();
    bCfg.setTypeConfigurations(Collections.singleton(typeCfg));
    cfg.setBinaryConfiguration(bCfg);
    CacheKeyConfiguration keyCfg = new CacheKeyConfiguration(TestObject.class.getName(), "affKey");
    CacheKeyConfiguration keyCfg2 = new CacheKeyConfiguration("TestObject2", "affKey");
    cfg.setCacheKeyConfiguration(keyCfg, keyCfg2);
    cfg.setMarshaller(new BinaryMarshaller());
    if (!igniteInstanceName.equals(getTestIgniteInstanceName(GRID_CNT))) {
        CacheConfiguration cacheCfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
        cacheCfg.setCacheMode(PARTITIONED);
        cfg.setCacheConfiguration(cacheCfg);
    }
    return cfg;
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)41 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)24 QueryEntity (org.apache.ignite.cache.QueryEntity)19 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)17 ArrayList (java.util.ArrayList)11 LinkedHashMap (java.util.LinkedHashMap)7 QueryIndex (org.apache.ignite.cache.QueryIndex)7 Ignite (org.apache.ignite.Ignite)6 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)6 HashMap (java.util.HashMap)5 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)4 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 Collectors (java.util.stream.Collectors)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 CacheAtomicityMode (org.apache.ignite.cache.CacheAtomicityMode)3 CacheMode (org.apache.ignite.cache.CacheMode)3 CacheRebalanceMode (org.apache.ignite.cache.CacheRebalanceMode)3