use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.
the class GridQueryParsingTest method cacheConfiguration.
/**
* @param name Cache name.
* @param clsK Key class.
* @param clsV Value class.
* @return Cache configuration.
*/
@SuppressWarnings("unchecked")
private CacheConfiguration cacheConfiguration(@NotNull String name, String sqlSchema, Class<?> clsK, Class<?> clsV) {
CacheConfiguration cc = defaultCacheConfiguration();
cc.setName(name);
cc.setCacheMode(CacheMode.PARTITIONED);
cc.setAtomicityMode(CacheAtomicityMode.ATOMIC);
cc.setNearConfiguration(null);
cc.setWriteSynchronizationMode(FULL_SYNC);
cc.setRebalanceMode(SYNC);
cc.setSqlSchema(sqlSchema);
cc.setSqlFunctionClasses(GridQueryParsingTest.class);
cc.setIndexedTypes(clsK, clsV);
if (!QueryUtils.isSqlType(clsK))
cc.setKeyConfiguration(new CacheKeyConfiguration(clsK));
return cc;
}
use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.
the class ClientCacheConfigurationSerializer method read.
/**
* Reads the cache configuration.
*
* @param reader Reader.
* @return Configuration.
*/
static CacheConfiguration read(BinaryRawReader reader) {
// Skip length.
reader.readInt();
short propCnt = reader.readShort();
CacheConfiguration cfg = new CacheConfiguration();
for (int i = 0; i < propCnt; i++) {
short code = reader.readShort();
switch(code) {
case ATOMICITY_MODE:
cfg.setAtomicityMode(CacheAtomicityMode.fromOrdinal(reader.readInt()));
break;
case BACKUPS:
cfg.setBackups(reader.readInt());
break;
case CACHE_MODE:
cfg.setCacheMode(CacheMode.fromOrdinal(reader.readInt()));
break;
case COPY_ON_READ:
cfg.setCopyOnRead(reader.readBoolean());
break;
case DATA_REGION_NAME:
cfg.setDataRegionName(reader.readString());
break;
case EAGER_TTL:
cfg.setEagerTtl(reader.readBoolean());
break;
case STATISTICS_ENABLED:
cfg.setStatisticsEnabled(reader.readBoolean());
break;
case GROUP_NAME:
cfg.setGroupName(reader.readString());
break;
case DEFAULT_LOCK_TIMEOUT:
cfg.setDefaultLockTimeout(reader.readLong());
break;
case MAX_CONCURRENT_ASYNC_OPERATIONS:
cfg.setMaxConcurrentAsyncOperations(reader.readInt());
break;
case MAX_QUERY_ITERATORS_COUNT:
cfg.setMaxQueryIteratorsCount(reader.readInt());
break;
case NAME:
cfg.setName(reader.readString());
break;
case ONHEAP_CACHE_ENABLED:
cfg.setOnheapCacheEnabled(reader.readBoolean());
break;
case PARTITION_LOSS_POLICY:
cfg.setPartitionLossPolicy(PartitionLossPolicy.fromOrdinal((byte) reader.readInt()));
break;
case QUERY_DETAIL_METRICS_SIZE:
cfg.setQueryDetailMetricsSize(reader.readInt());
break;
case QUERY_PARALLELISM:
cfg.setQueryParallelism(reader.readInt());
break;
case READ_FROM_BACKUP:
cfg.setReadFromBackup(reader.readBoolean());
break;
case REBALANCE_BATCH_SIZE:
cfg.setRebalanceBatchSize(reader.readInt());
break;
case REBALANCE_BATCHES_PREFETCH_COUNT:
cfg.setRebalanceBatchesPrefetchCount(reader.readLong());
break;
case REBALANCE_DELAY:
cfg.setRebalanceDelay(reader.readLong());
break;
case REBALANCE_MODE:
cfg.setRebalanceMode(CacheRebalanceMode.fromOrdinal(reader.readInt()));
break;
case REBALANCE_ORDER:
cfg.setRebalanceOrder(reader.readInt());
break;
case REBALANCE_THROTTLE:
cfg.setRebalanceThrottle(reader.readLong());
break;
case REBALANCE_TIMEOUT:
cfg.setRebalanceTimeout(reader.readLong());
break;
case SQL_ESCAPE_ALL:
cfg.setSqlEscapeAll(reader.readBoolean());
break;
case SQL_INDEX_MAX_INLINE_SIZE:
cfg.setSqlIndexMaxInlineSize(reader.readInt());
break;
case SQL_SCHEMA:
cfg.setSqlSchema(reader.readString());
break;
case WRITE_SYNCHRONIZATION_MODE:
cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.fromOrdinal(reader.readInt()));
break;
case KEY_CONFIGURATION:
int keyCnt = reader.readInt();
if (keyCnt > 0) {
CacheKeyConfiguration[] keys = new CacheKeyConfiguration[keyCnt];
for (int j = 0; j < keyCnt; j++) {
keys[j] = new CacheKeyConfiguration(reader.readString(), reader.readString());
}
cfg.setKeyConfiguration(keys);
}
break;
case QUERY_ENTITIES:
int qryEntCnt = reader.readInt();
if (qryEntCnt > 0) {
Collection<QueryEntity> entities = new ArrayList<>(qryEntCnt);
for (int j = 0; j < qryEntCnt; j++) entities.add(readQueryEntity(reader));
cfg.setQueryEntities(entities);
}
break;
}
}
return cfg;
}
use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.
the class ClientCacheConfigurationSerializer method write.
/**
* Writes the cache configuration.
* @param writer Writer.
* @param cfg Configuration.
*/
static void write(BinaryRawWriterEx writer, CacheConfiguration cfg) {
assert writer != null;
assert cfg != null;
// Reserve for length.
int pos = writer.reserveInt();
writeEnumInt(writer, cfg.getAtomicityMode(), CacheConfiguration.DFLT_CACHE_ATOMICITY_MODE);
writer.writeInt(cfg.getBackups());
writeEnumInt(writer, cfg.getCacheMode(), CacheConfiguration.DFLT_CACHE_MODE);
writer.writeBoolean(cfg.isCopyOnRead());
writer.writeString(cfg.getDataRegionName());
writer.writeBoolean(cfg.isEagerTtl());
writer.writeBoolean(cfg.isStatisticsEnabled());
writer.writeString(cfg.getGroupName());
writer.writeLong(cfg.getDefaultLockTimeout());
writer.writeInt(cfg.getMaxConcurrentAsyncOperations());
writer.writeInt(cfg.getMaxQueryIteratorsCount());
writer.writeString(cfg.getName());
writer.writeBoolean(cfg.isOnheapCacheEnabled());
writer.writeInt(cfg.getPartitionLossPolicy().ordinal());
writer.writeInt(cfg.getQueryDetailMetricsSize());
writer.writeInt(cfg.getQueryParallelism());
writer.writeBoolean(cfg.isReadFromBackup());
writer.writeInt(cfg.getRebalanceBatchSize());
writer.writeLong(cfg.getRebalanceBatchesPrefetchCount());
writer.writeLong(cfg.getRebalanceDelay());
writeEnumInt(writer, cfg.getRebalanceMode(), CacheConfiguration.DFLT_REBALANCE_MODE);
writer.writeInt(cfg.getRebalanceOrder());
writer.writeLong(cfg.getRebalanceThrottle());
writer.writeLong(cfg.getRebalanceTimeout());
writer.writeBoolean(cfg.isSqlEscapeAll());
writer.writeInt(cfg.getSqlIndexMaxInlineSize());
writer.writeString(cfg.getSqlSchema());
writeEnumInt(writer, cfg.getWriteSynchronizationMode());
CacheKeyConfiguration[] keys = cfg.getKeyConfiguration();
if (keys != null) {
writer.writeInt(keys.length);
for (CacheKeyConfiguration key : keys) {
writer.writeString(key.getTypeName());
writer.writeString(key.getAffinityKeyFieldName());
}
} else {
writer.writeInt(0);
}
// noinspection unchecked
Collection<QueryEntity> qryEntities = cfg.getQueryEntities();
if (qryEntities != null) {
writer.writeInt(qryEntities.size());
for (QueryEntity e : qryEntities) writeQueryEntity(writer, e);
} else
writer.writeInt(0);
// Write length (so that part of the config can be skipped).
writer.writeInt(pos, writer.out().position() - pos - 4);
}
use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.
the class GridQueryProcessor method dynamicTableCreate.
/**
* Create cache and table from given query entity.
*
* @param schemaName Schema name to create table in.
* @param entity Entity to create table from.
* @param templateName Template name.
* @param cacheName Cache name.
* @param cacheGroup Cache group name.
* @param dataRegion Data region name.
* @param affinityKey Affinity key column name.
* @param atomicityMode Atomicity mode.
* @param writeSyncMode Write synchronization mode.
* @param backups Backups.
* @param ifNotExists Quietly ignore this command if table already exists.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
public void dynamicTableCreate(String schemaName, QueryEntity entity, String templateName, String cacheName, String cacheGroup, @Nullable String dataRegion, String affinityKey, @Nullable CacheAtomicityMode atomicityMode, @Nullable CacheWriteSynchronizationMode writeSyncMode, int backups, boolean ifNotExists) throws IgniteCheckedException {
assert !F.isEmpty(templateName);
assert backups >= 0;
CacheConfiguration<?, ?> ccfg = ctx.cache().getConfigFromTemplate(templateName);
if (ccfg == null) {
if (QueryUtils.TEMPLATE_PARTITIONED.equalsIgnoreCase(templateName))
ccfg = new CacheConfiguration<>().setCacheMode(CacheMode.PARTITIONED);
else if (QueryUtils.TEMPLATE_REPLICATED.equalsIgnoreCase(templateName))
ccfg = new CacheConfiguration<>().setCacheMode(CacheMode.REPLICATED);
else
throw new SchemaOperationException(SchemaOperationException.CODE_CACHE_NOT_FOUND, templateName);
ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
}
if (!F.isEmpty(ccfg.getQueryEntities()))
throw new SchemaOperationException("Template cache already contains query entities which it should not: " + templateName);
if (!F.isEmpty(entity.getNotNullFields()))
QueryUtils.checkNotNullAllowed(ccfg);
if (F.isEmpty(cacheName))
cacheName = QueryUtils.createTableCacheName(schemaName, entity.getTableName());
ccfg.setName(cacheName);
if (!F.isEmpty(cacheGroup))
ccfg.setGroupName(cacheGroup);
if (!F.isEmpty(dataRegion))
ccfg.setDataRegionName(dataRegion);
if (atomicityMode != null)
ccfg.setAtomicityMode(atomicityMode);
if (writeSyncMode != null)
ccfg.setWriteSynchronizationMode(writeSyncMode);
ccfg.setBackups(backups);
ccfg.setSqlSchema(schemaName);
ccfg.setSqlEscapeAll(true);
ccfg.setQueryEntities(Collections.singleton(entity));
if (affinityKey != null)
ccfg.setKeyConfiguration(new CacheKeyConfiguration(entity.getKeyType(), affinityKey));
boolean res;
try {
res = ctx.grid().getOrCreateCache0(ccfg, true).get2();
} catch (CacheException e) {
if (e.getCause() instanceof SchemaOperationException)
throw (SchemaOperationException) e.getCause();
else
throw e;
}
if (!res && !ifNotExists)
throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_EXISTS, entity.getTableName());
}
use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.
the class CacheClientBinaryQueryExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Binary objects cache query example started.");
CacheConfiguration<Integer, Organization> orgCacheCfg = new CacheConfiguration<>();
orgCacheCfg.setCacheMode(CacheMode.PARTITIONED);
orgCacheCfg.setName(ORGANIZATION_CACHE_NAME);
orgCacheCfg.setQueryEntities(Arrays.asList(createOrganizationQueryEntity()));
CacheConfiguration<EmployeeKey, Employee> employeeCacheCfg = new CacheConfiguration<>();
employeeCacheCfg.setCacheMode(CacheMode.PARTITIONED);
employeeCacheCfg.setName(EMPLOYEE_CACHE_NAME);
employeeCacheCfg.setQueryEntities(Arrays.asList(createEmployeeQueryEntity()));
employeeCacheCfg.setKeyConfiguration(new CacheKeyConfiguration(EmployeeKey.class));
try (IgniteCache<Integer, Organization> orgCache = ignite.getOrCreateCache(orgCacheCfg);
IgniteCache<EmployeeKey, Employee> employeeCache = ignite.getOrCreateCache(employeeCacheCfg)) {
if (ignite.cluster().forDataNodes(orgCache.getName()).nodes().isEmpty()) {
System.out.println();
System.out.println(">>> This example requires remote cache nodes to be started.");
System.out.println(">>> Please start at least 1 remote cache node.");
System.out.println(">>> Refer to example's javadoc for details on configuration.");
System.out.println();
return;
}
// Populate cache with sample data entries.
populateCache(orgCache, employeeCache);
// Get cache that will work with binary objects.
IgniteCache<BinaryObject, BinaryObject> binaryCache = employeeCache.withKeepBinary();
// Run SQL fields query example.
sqlFieldsQuery(binaryCache);
// Run SQL query with join example.
sqlJoinQuery(binaryCache);
// Run full text query example.
textQuery(binaryCache);
System.out.println();
} finally {
// Delete caches with their content completely.
ignite.destroyCache(ORGANIZATION_CACHE_NAME);
ignite.destroyCache(EMPLOYEE_CACHE_NAME);
}
}
}
Aggregations