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