Search in sources :

Example 6 with SchemaAddQueryEntityOperation

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);
}
Also used : SchemaAddQueryEntityOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation) QueryEntity(org.apache.ignite.cache.QueryEntity) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with SchemaAddQueryEntityOperation

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));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SchemaIndexCreateOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation) SchemaIndexDropOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexDropOperation) QueryEntity(org.apache.ignite.cache.QueryEntity) SchemaAddQueryEntityOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation) SchemaAlterTableAddColumnOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAlterTableAddColumnOperation) QueryIndex(org.apache.ignite.cache.QueryIndex) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) SchemaAlterTableDropColumnOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAlterTableDropColumnOperation)

Example 8 with SchemaAddQueryEntityOperation

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());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryEntity(org.apache.ignite.cache.QueryEntity) QueryEntityPatch(org.apache.ignite.cache.QueryEntityPatch) SchemaAddQueryEntityOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation) SchemaAbstractOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAbstractOperation)

Aggregations

SchemaAddQueryEntityOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation)8 SchemaAlterTableAddColumnOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAlterTableAddColumnOperation)6 SchemaAlterTableDropColumnOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAlterTableDropColumnOperation)6 SchemaIndexCreateOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation)6 SchemaIndexDropOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexDropOperation)6 QueryEntity (org.apache.ignite.cache.QueryEntity)5 HashMap (java.util.HashMap)4 QueryIndex (org.apache.ignite.cache.QueryIndex)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Collections.newSetFromMap (java.util.Collections.newSetFromMap)3 IdentityHashMap (java.util.IdentityHashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)3 SchemaAbstractOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAbstractOperation)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2