use of org.apache.ignite.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation 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.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation 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.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation in project ignite by apache.
the class QuerySchema method makePatch.
/**
* Make query schema patch.
*
* @param targetCfg Cache configuration when it should be changed (enabling indexing dynamically).
* @param target Query entity list to which current schema should be expanded.
* @return Patch to achieve entity which is a result of merging current one and target.
* @see QuerySchemaPatch
*/
public QuerySchemaPatch makePatch(CacheConfiguration<?, ?> targetCfg, Collection<QueryEntity> target) {
synchronized (mux) {
if (entities.isEmpty() && targetCfg != null) {
SchemaAddQueryEntityOperation op = new SchemaAddQueryEntityOperation(UUID.randomUUID(), targetCfg.getName(), targetCfg.getSqlSchema(), target, targetCfg.getQueryParallelism(), targetCfg.isSqlEscapeAll());
return new QuerySchemaPatch(Collections.singletonList(op), Collections.emptyList(), "");
}
Map<String, QueryEntity> localEntities = new HashMap<>();
for (QueryEntity entity : entities) {
if (localEntities.put(entity.getTableName(), entity) != null)
throw new IllegalStateException("Duplicate key");
}
Collection<SchemaAbstractOperation> patchOperations = new ArrayList<>();
Collection<QueryEntity> entityToAdd = new ArrayList<>();
StringBuilder conflicts = new StringBuilder();
for (QueryEntity queryEntity : target) {
if (localEntities.containsKey(queryEntity.getTableName())) {
QueryEntity localEntity = localEntities.get(queryEntity.getTableName());
QueryEntityPatch entityPatch = localEntity.makePatch(queryEntity);
if (entityPatch.hasConflict()) {
if (conflicts.length() > 0)
conflicts.append("\n");
conflicts.append(entityPatch.getConflictsMessage());
}
if (!entityPatch.isEmpty())
patchOperations.addAll(entityPatch.getPatchOperations());
} else
entityToAdd.add(QueryUtils.copy(queryEntity));
}
return new QuerySchemaPatch(patchOperations, entityToAdd, conflicts.toString());
}
}
Aggregations