Search in sources :

Example 1 with QueryEntityPatch

use of org.apache.ignite.cache.QueryEntityPatch 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

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 QueryEntity (org.apache.ignite.cache.QueryEntity)1 QueryEntityPatch (org.apache.ignite.cache.QueryEntityPatch)1 SchemaAbstractOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAbstractOperation)1 SchemaAddQueryEntityOperation (org.apache.ignite.internal.processors.query.schema.operation.SchemaAddQueryEntityOperation)1