use of org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation 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.internal.processors.query.schema.operation.SchemaIndexCreateOperation in project ignite by apache.
the class GridQueryProcessor method onCacheStart0.
/**
* Create type descriptors from schema and initialize indexing for given cache.<p>
* Use with {@link #busyLock} where appropriate.
* @param cacheInfo Cache context info.
* @param schema Initial schema.
* @param isSql {@code true} in case create cache initialized from SQL.
* @throws IgniteCheckedException If failed.
*/
public void onCacheStart0(GridCacheContextInfo<?, ?> cacheInfo, QuerySchema schema, boolean isSql) throws IgniteCheckedException {
if (!cacheSupportSql(cacheInfo.config())) {
synchronized (stateMux) {
boolean proceed = false;
for (SchemaAbstractDiscoveryMessage msg : activeProposals.values()) {
if (msg.operation() instanceof SchemaAddQueryEntityOperation) {
SchemaAddQueryEntityOperation op = (SchemaAddQueryEntityOperation) msg.operation();
if (op.cacheName().equals(cacheInfo.name())) {
proceed = true;
break;
}
}
}
if (!proceed)
return;
}
}
ctx.cache().context().database().checkpointReadLock();
try {
if (cacheInfo.isClientCache() && cacheInfo.isCacheContextInited() && idx.initCacheContext(cacheInfo.cacheContext()))
return;
synchronized (stateMux) {
boolean escape = cacheInfo.config().isSqlEscapeAll();
String cacheName = cacheInfo.name();
String schemaName = QueryUtils.normalizeSchemaName(cacheName, cacheInfo.config().getSqlSchema());
T3<Collection<QueryTypeCandidate>, Map<String, QueryTypeDescriptorImpl>, Map<String, QueryTypeDescriptorImpl>> candRes = createQueryCandidates(cacheName, schemaName, cacheInfo, schema.entities(), escape);
// Ensure that candidates has unique index names.
// Otherwise we will not be able to apply pending operations.
Collection<QueryTypeCandidate> cands = candRes.get1();
Map<String, QueryTypeDescriptorImpl> tblTypMap = candRes.get2();
Map<String, QueryTypeDescriptorImpl> idxTypMap = candRes.get3();
// There could be only one in-flight operation for a cache.
for (SchemaOperation op : schemaOps.values()) {
if (F.eq(op.proposeMessage().deploymentId(), cacheInfo.dynamicDeploymentId())) {
if (op.started()) {
SchemaOperationWorker worker = op.manager().worker();
assert !worker.cacheRegistered();
if (!worker.nop()) {
IgniteInternalFuture fut = worker.future();
assert fut.isDone();
if (fut.error() == null) {
SchemaAbstractOperation op0 = op.proposeMessage().operation();
if (op0 instanceof SchemaIndexCreateOperation) {
SchemaIndexCreateOperation opCreate = (SchemaIndexCreateOperation) op0;
QueryTypeDescriptorImpl typeDesc = tblTypMap.get(opCreate.tableName());
assert typeDesc != null;
QueryUtils.processDynamicIndexChange(opCreate.indexName(), opCreate.index(), typeDesc);
} else if (op0 instanceof SchemaIndexDropOperation) {
SchemaIndexDropOperation opDrop = (SchemaIndexDropOperation) op0;
QueryTypeDescriptorImpl typeDesc = idxTypMap.get(opDrop.indexName());
assert typeDesc != null;
QueryUtils.processDynamicIndexChange(opDrop.indexName(), null, typeDesc);
} else if (op0 instanceof SchemaAlterTableAddColumnOperation) {
SchemaAlterTableAddColumnOperation opAddCol = (SchemaAlterTableAddColumnOperation) op0;
QueryTypeDescriptorImpl typeDesc = tblTypMap.get(opAddCol.tableName());
assert typeDesc != null;
processDynamicAddColumn(typeDesc, opAddCol.columns());
} else if (op0 instanceof SchemaAlterTableDropColumnOperation) {
SchemaAlterTableDropColumnOperation opDropCol = (SchemaAlterTableDropColumnOperation) op0;
QueryTypeDescriptorImpl typeDesc = tblTypMap.get(opDropCol.tableName());
assert typeDesc != null;
processDynamicDropColumn(typeDesc, opDropCol.columns());
} else if (op0 instanceof SchemaAddQueryEntityOperation) {
SchemaAddQueryEntityOperation opEnableIdx = (SchemaAddQueryEntityOperation) op0;
cacheInfo.onSchemaAddQueryEntity(opEnableIdx);
cands = createQueryCandidates(opEnableIdx.cacheName(), opEnableIdx.schemaName(), cacheInfo, opEnableIdx.entities(), opEnableIdx.isSqlEscape()).get1();
schemaName = opEnableIdx.schemaName();
} else
assert false : "Unsupported operation: " + op0;
}
}
}
break;
}
}
// Ready to register at this point.
registerCache0(cacheName, schemaName, cacheInfo, cands, isSql);
}
} finally {
ctx.cache().context().database().checkpointReadUnlock();
}
}
use of org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation in project ignite by apache.
the class GridQueryProcessor method onLocalOperationFinished.
/**
* Apply positive index operation result.
*
* @param op Operation.
* @param type Type descriptor (if available),
*/
public void onLocalOperationFinished(SchemaAbstractOperation op, @Nullable QueryTypeDescriptorImpl type) {
synchronized (stateMux) {
if (disconnected)
return;
// No need to apply anything to obsolete type.
if (type == null || type.obsolete()) {
if (log.isDebugEnabled())
log.debug("Local operation finished, but type descriptor is either missing or obsolete " + "(will ignore) [opId=" + op.id() + ']');
return;
}
if (log.isDebugEnabled())
log.debug("Local operation finished successfully [opId=" + op.id() + ']');
String schemaName = op.schemaName();
try {
if (op instanceof SchemaIndexCreateOperation) {
SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation) op;
QueryUtils.processDynamicIndexChange(op0.indexName(), op0.index(), type);
QueryIndexDescriptorImpl idxDesc = type.index(op0.indexName());
QueryIndexKey idxKey = new QueryIndexKey(schemaName, op0.indexName());
idxs.put(idxKey, idxDesc);
} else if (op instanceof SchemaIndexDropOperation) {
SchemaIndexDropOperation op0 = (SchemaIndexDropOperation) op;
QueryUtils.processDynamicIndexChange(op0.indexName(), null, type);
QueryIndexKey idxKey = new QueryIndexKey(schemaName, op0.indexName());
idxs.remove(idxKey);
} else {
assert (op instanceof SchemaAddQueryEntityOperation || op instanceof SchemaAlterTableAddColumnOperation || op instanceof SchemaAlterTableDropColumnOperation);
// No-op - all processing is done at "local" stage
// as we must update both table and type descriptor atomically.
}
} catch (IgniteCheckedException e) {
U.warn(log, "Failed to finish index operation [opId=" + op.id() + " op=" + op + ']', e);
}
}
}
use of org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation 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.SchemaIndexCreateOperation in project ignite by apache.
the class QueryEntity method makePatch.
/**
* Make query entity patch. This patch can only add properties to entity and can't remove them.
* Other words, the patch will contain only add operations(e.g. add column, create index) and not remove ones.
*
* @param target Query entity to which this entity should be expanded.
* @return Patch which contains operations for expanding this entity.
*/
@NotNull
public QueryEntityPatch makePatch(QueryEntity target) {
if (target == null)
return QueryEntityPatch.empty();
StringBuilder conflicts = new StringBuilder();
checkEquals(conflicts, "keyType", keyType, target.keyType);
checkEquals(conflicts, "valType", valType, target.valType);
checkEquals(conflicts, "keyFieldName", keyFieldName, target.keyFieldName);
checkEquals(conflicts, "valueFieldName", valueFieldName, target.valueFieldName);
checkEquals(conflicts, "tableName", tableName, target.tableName);
List<QueryField> queryFieldsToAdd = checkFields(target, conflicts);
Collection<QueryIndex> indexesToAdd = checkIndexes(target, conflicts);
if (conflicts.length() != 0)
return QueryEntityPatch.conflict(tableName + " conflict: \n" + conflicts.toString());
Collection<SchemaAbstractOperation> patchOperations = new ArrayList<>();
if (!queryFieldsToAdd.isEmpty())
patchOperations.add(new SchemaAlterTableAddColumnOperation(UUID.randomUUID(), null, null, tableName, queryFieldsToAdd, true, true));
if (!indexesToAdd.isEmpty()) {
for (QueryIndex index : indexesToAdd) {
patchOperations.add(new SchemaIndexCreateOperation(UUID.randomUUID(), null, null, tableName, index, true, 0));
}
}
return QueryEntityPatch.patch(patchOperations);
}
Aggregations