use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class JavaThinCompatibilityTest method testCacheConfiguration.
/**
*/
private void testCacheConfiguration(boolean checkFieldsPrecessionAndScale, boolean checkExpiryPlc) throws Exception {
X.println(">>>> Testing cache configuration");
try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
String cacheName = "testCacheConfiguration";
ClientCacheConfiguration ccfg = new ClientCacheConfiguration();
ccfg.setName(cacheName);
ccfg.setBackups(3);
ccfg.setGroupName("cache");
ccfg.setCacheMode(CacheMode.PARTITIONED);
QueryEntity qryEntity = new QueryEntity(int.class.getName(), "Entity").setTableName("ENTITY").setFields(new LinkedHashMap<>(F.asMap("id", Integer.class.getName(), "rate", Double.class.getName())));
if (checkFieldsPrecessionAndScale) {
qryEntity.setFieldsPrecision(F.asMap("rate", 5));
qryEntity.setFieldsScale(F.asMap("rate", 2));
}
ccfg.setQueryEntities(qryEntity);
if (checkExpiryPlc)
ccfg.setExpiryPolicy(new PlatformExpiryPolicy(10, 20, 30));
client.createCache(ccfg);
ClientCacheConfiguration ccfg1 = client.cache(cacheName).getConfiguration();
assertEquals(ccfg.getName(), ccfg1.getName());
assertEquals(ccfg.getBackups(), ccfg1.getBackups());
assertEquals(ccfg.getGroupName(), ccfg1.getGroupName());
assertEquals(ccfg.getCacheMode(), ccfg1.getCacheMode());
assertEquals(ccfg.getQueryEntities().length, ccfg1.getQueryEntities().length);
assertEquals(ccfg.getQueryEntities()[0].getTableName(), ccfg1.getQueryEntities()[0].getTableName());
assertEquals(ccfg.getQueryEntities()[0].getFields(), ccfg1.getQueryEntities()[0].getFields());
if (checkFieldsPrecessionAndScale) {
assertEquals(ccfg.getQueryEntities()[0].getFieldsPrecision(), ccfg1.getQueryEntities()[0].getFieldsPrecision());
assertEquals(ccfg.getQueryEntities()[0].getFieldsScale(), ccfg1.getQueryEntities()[0].getFieldsScale());
}
if (checkExpiryPlc) {
assertEquals(ccfg.getExpiryPolicy().getExpiryForCreation(), ccfg1.getExpiryPolicy().getExpiryForCreation());
assertEquals(ccfg.getExpiryPolicy().getExpiryForAccess(), ccfg1.getExpiryPolicy().getExpiryForAccess());
assertEquals(ccfg.getExpiryPolicy().getExpiryForUpdate(), ccfg1.getExpiryPolicy().getExpiryForUpdate());
}
}
}
use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class GridQueryProcessor method prepareChangeOnNotStartedCache.
/**
* Prepare operation on non-started cache.
*
* @param op Operation.
* @param desc Dynamic cache descriptor.
* @return Result: nop flag, error.
*/
private T2<Boolean, SchemaOperationException> prepareChangeOnNotStartedCache(SchemaAbstractOperation op, DynamicCacheDescriptor desc) {
boolean nop = false;
SchemaOperationException err = null;
if (op instanceof SchemaAddQueryEntityOperation) {
if (cacheSupportSql(desc.cacheConfiguration()))
err = new SchemaOperationException(SchemaOperationException.CODE_CACHE_ALREADY_INDEXED, desc.cacheName());
return new T2<>(nop, err);
}
// Build table and index maps.
QuerySchema schema = desc.schema();
Map<String, QueryEntity> tblMap = new HashMap<>();
Map<String, T2<QueryEntity, QueryIndex>> idxMap = new HashMap<>();
for (QueryEntity entity : schema.entities()) {
String tblName = entity.getTableName();
QueryEntity oldEntity = tblMap.put(tblName, entity);
if (oldEntity != null) {
err = new SchemaOperationException("Invalid schema state (duplicate table found): " + tblName);
break;
}
for (QueryIndex entityIdx : entity.getIndexes()) {
String idxName = entityIdx.getName();
T2<QueryEntity, QueryIndex> oldIdxEntity = idxMap.put(idxName, new T2<>(entity, entityIdx));
if (oldIdxEntity != null) {
err = new SchemaOperationException("Invalid schema state (duplicate index found): " + idxName);
break;
}
}
if (err != null)
break;
}
// Now check whether operation can be applied to schema.
if (op instanceof SchemaIndexCreateOperation) {
SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation) op;
String idxName = op0.indexName();
T2<QueryEntity, QueryIndex> oldIdxEntity = idxMap.get(idxName);
if (oldIdxEntity == null) {
String tblName = op0.tableName();
QueryEntity oldEntity = tblMap.get(tblName);
if (oldEntity == null)
err = new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, tblName);
else {
for (String fieldName : op0.index().getFields().keySet()) {
Set<String> oldEntityFields = new HashSet<>(oldEntity.getFields().keySet());
for (Map.Entry<String, String> alias : oldEntity.getAliases().entrySet()) {
oldEntityFields.remove(alias.getKey());
oldEntityFields.add(alias.getValue());
}
if (!oldEntityFields.contains(fieldName)) {
err = new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, fieldName);
break;
}
}
}
} else {
if (op0.ifNotExists())
nop = true;
else
err = new SchemaOperationException(SchemaOperationException.CODE_INDEX_EXISTS, idxName);
}
} else if (op instanceof SchemaIndexDropOperation) {
SchemaIndexDropOperation op0 = (SchemaIndexDropOperation) op;
String idxName = op0.indexName();
T2<QueryEntity, QueryIndex> oldIdxEntity = idxMap.get(idxName);
if (oldIdxEntity == null) {
if (op0.ifExists())
nop = true;
else
err = new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, idxName);
}
} else if (op instanceof SchemaAlterTableAddColumnOperation) {
SchemaAlterTableAddColumnOperation op0 = (SchemaAlterTableAddColumnOperation) op;
QueryEntity e = tblMap.get(op0.tableName());
if (e == null) {
if (op0.ifTableExists())
nop = true;
else
err = new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, op0.tableName());
} else {
for (QueryField fld : op0.columns()) {
if (e.getFields().containsKey(fld.name())) {
if (op0.ifNotExists()) {
assert op0.columns().size() == 1;
nop = true;
} else
err = new SchemaOperationException(CODE_COLUMN_EXISTS, fld.name());
}
}
}
} else if (op instanceof SchemaAlterTableDropColumnOperation) {
SchemaAlterTableDropColumnOperation op0 = (SchemaAlterTableDropColumnOperation) op;
QueryEntity e = tblMap.get(op0.tableName());
if (e == null) {
if (op0.ifTableExists())
nop = true;
else
err = new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, op0.tableName());
} else {
for (String colName : op0.columns()) {
if (err != null)
break;
String fldName = QueryUtils.fieldNameByAlias(e, colName);
if (!e.getFields().containsKey(fldName)) {
if (op0.ifExists()) {
assert op0.columns().size() == 1;
nop = true;
} else
err = new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, fldName);
break;
}
err = QueryUtils.validateDropColumn(e, fldName, colName);
}
}
} else
err = new SchemaOperationException("Unsupported operation: " + op);
return new T2<>(nop, err);
}
use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class GridQueryProcessor method dynamicAddQueryEntity.
/**
* Enable dynamically indexing of existing cache.
*
* @param cacheName Cache name
* @param schemaName Target schema name.
* @param entity Instance of {@code QueryEntity}.
* @param qryParallelism Query parallelism.
* @param sqlEscape Escape flag, see{@link QueryUtils#normalizeQueryEntity}.
*/
public IgniteInternalFuture<?> dynamicAddQueryEntity(String cacheName, String schemaName, QueryEntity entity, Integer qryParallelism, boolean sqlEscape) {
assert qryParallelism == null || qryParallelism > 0;
CacheConfiguration cfg = ctx.cache().cacheConfiguration(cacheName);
if (qryParallelism != null && qryParallelism > 1 && cfg.getCacheMode() != PARTITIONED)
throw new IgniteSQLException("Segmented indices are supported for PARTITIONED mode only.");
QueryEntity entity0 = QueryUtils.normalizeQueryEntity(ctx, entity, sqlEscape);
SchemaAddQueryEntityOperation op = new SchemaAddQueryEntityOperation(UUID.randomUUID(), cacheName, schemaName, Collections.singletonList(entity0), qryParallelism != null ? qryParallelism : CacheConfiguration.DFLT_QUERY_PARALLELISM, sqlEscape);
return startIndexOperationDistributed(op);
}
use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class QuerySchema method finish.
/**
* Process operation.
*
* @param op Operation for handle.
*/
public void finish(SchemaAbstractOperation op) {
synchronized (mux) {
if (op instanceof SchemaIndexCreateOperation) {
SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation) op;
for (QueryEntity entity : entities) {
String tblName = entity.getTableName();
if (F.eq(tblName, op0.tableName())) {
boolean exists = false;
for (QueryIndex idx : entity.getIndexes()) {
if (F.eq(idx.getName(), op0.indexName())) {
exists = true;
break;
}
}
if (!exists) {
List<QueryIndex> idxs = new ArrayList<>(entity.getIndexes());
idxs.add(op0.index());
entity.setIndexes(idxs);
}
break;
}
}
} else if (op instanceof SchemaIndexDropOperation) {
SchemaIndexDropOperation op0 = (SchemaIndexDropOperation) op;
for (QueryEntity entity : entities) {
Collection<QueryIndex> idxs = entity.getIndexes();
QueryIndex victim = null;
for (QueryIndex idx : idxs) {
if (F.eq(idx.getName(), op0.indexName())) {
victim = idx;
break;
}
}
if (victim != null) {
List<QueryIndex> newIdxs = new ArrayList<>(entity.getIndexes());
newIdxs.remove(victim);
entity.setIndexes(newIdxs);
break;
}
}
} else if (op instanceof SchemaAlterTableAddColumnOperation) {
SchemaAlterTableAddColumnOperation op0 = (SchemaAlterTableAddColumnOperation) op;
int targetIdx = -1;
for (int i = 0; i < entities.size(); i++) {
QueryEntity entity = ((List<QueryEntity>) entities).get(i);
if (F.eq(entity.getTableName(), op0.tableName())) {
targetIdx = i;
break;
}
}
if (targetIdx == -1)
return;
boolean replaceTarget = false;
QueryEntity target = ((List<QueryEntity>) entities).get(targetIdx);
for (QueryField field : op0.columns()) {
target.getFields().put(field.name(), field.typeName());
if (!field.isNullable()) {
if (!(target instanceof QueryEntityEx)) {
target = new QueryEntityEx(target);
replaceTarget = true;
}
QueryEntityEx target0 = (QueryEntityEx) target;
Set<String> notNullFields = target0.getNotNullFields();
if (notNullFields == null) {
notNullFields = new HashSet<>();
target0.setNotNullFields(notNullFields);
}
notNullFields.add(field.name());
}
}
if (replaceTarget)
((List<QueryEntity>) entities).set(targetIdx, target);
} else if (op instanceof SchemaAlterTableDropColumnOperation) {
SchemaAlterTableDropColumnOperation op0 = (SchemaAlterTableDropColumnOperation) op;
int targetIdx = -1;
for (int i = 0; i < entities.size(); i++) {
QueryEntity entity = ((List<QueryEntity>) entities).get(i);
if (F.eq(entity.getTableName(), op0.tableName())) {
targetIdx = i;
break;
}
}
if (targetIdx == -1)
return;
QueryEntity entity = ((List<QueryEntity>) entities).get(targetIdx);
for (String field : op0.columns()) {
boolean rmv = QueryUtils.removeField(entity, field);
assert rmv || op0.ifExists() : "Invalid operation state [removed=" + rmv + ", ifExists=" + op0.ifExists() + ']';
}
} else {
assert op instanceof SchemaAddQueryEntityOperation : "Unsupported schema operation [" + op.toString() + "]";
assert entities.isEmpty();
for (QueryEntity opEntity : ((SchemaAddQueryEntityOperation) op).entities()) entities.add(QueryUtils.copy(opEntity));
}
}
}
use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class ExternalStorage method cacheJdbcPojoStoreExample.
public static void cacheJdbcPojoStoreExample() {
// tag::pojo[]
IgniteConfiguration igniteCfg = new IgniteConfiguration();
CacheConfiguration<Integer, Person> personCacheCfg = new CacheConfiguration<>();
personCacheCfg.setName("PersonCache");
personCacheCfg.setCacheMode(CacheMode.PARTITIONED);
personCacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
personCacheCfg.setReadThrough(true);
personCacheCfg.setWriteThrough(true);
CacheJdbcPojoStoreFactory<Integer, Person> factory = new CacheJdbcPojoStoreFactory<>();
factory.setDialect(new MySQLDialect());
factory.setDataSourceFactory((Factory<DataSource>) () -> {
MysqlDataSource mysqlDataSrc = new MysqlDataSource();
mysqlDataSrc.setURL("jdbc:mysql://[host]:[port]/[database]");
mysqlDataSrc.setUser("YOUR_USER_NAME");
mysqlDataSrc.setPassword("YOUR_PASSWORD");
return mysqlDataSrc;
});
JdbcType personType = new JdbcType();
personType.setCacheName("PersonCache");
personType.setKeyType(Integer.class);
personType.setValueType(Person.class);
// Specify the schema if applicable
// personType.setDatabaseSchema("MY_DB_SCHEMA");
personType.setDatabaseTable("PERSON");
personType.setKeyFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
personType.setValueFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
personType.setValueFields(new JdbcTypeField(java.sql.Types.VARCHAR, "name", String.class, "name"));
factory.setTypes(personType);
personCacheCfg.setCacheStoreFactory(factory);
QueryEntity qryEntity = new QueryEntity();
qryEntity.setKeyType(Integer.class.getName());
qryEntity.setValueType(Person.class.getName());
qryEntity.setKeyFieldName("id");
Set<String> keyFields = new HashSet<>();
keyFields.add("id");
qryEntity.setKeyFields(keyFields);
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("id", "java.lang.Integer");
fields.put("name", "java.lang.String");
qryEntity.setFields(fields);
personCacheCfg.setQueryEntities(Collections.singletonList(qryEntity));
igniteCfg.setCacheConfiguration(personCacheCfg);
// end::pojo[]
}
Aggregations