Search in sources :

Example 41 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class GridQueryProcessor method prepareChangeOnNotStartedCache.

/**
 * Prepare operation on non-started cache.
 *
 * @param op Operation.
 * @param schema Known cache schema.
 * @return Result: nop flag, error.
 */
private T2<Boolean, SchemaOperationException> prepareChangeOnNotStartedCache(SchemaAbstractOperation op, QuerySchema schema) {
    boolean nop = false;
    SchemaOperationException err = null;
    // Build table and index maps.
    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(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 {
            Map<String, String> aliases = e.getAliases();
            for (String colName : op0.columns()) {
                if (err != null)
                    break;
                String fldName = colName;
                if (!F.isEmpty(aliases)) {
                    for (Map.Entry<String, String> a : aliases.entrySet()) {
                        if (colName.equals(a.getValue())) {
                            fldName = a.getKey();
                            break;
                        }
                    }
                }
                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);
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) 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) SchemaAlterTableAddColumnOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAlterTableAddColumnOperation) QueryIndex(org.apache.ignite.cache.QueryIndex) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) T2(org.apache.ignite.internal.util.typedef.T2) HashSet(java.util.HashSet) GridBoundedConcurrentLinkedHashSet(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet) SchemaAlterTableDropColumnOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAlterTableDropColumnOperation)

Example 42 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class QuerySchema method finish.

/**
 * Process finish message.
 *
 * @param msg Message.
 */
public void finish(SchemaFinishDiscoveryMessage msg) {
    synchronized (mux) {
        SchemaAbstractOperation op = msg.operation();
        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 {
            assert 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()) entity.getFields().remove(field);
        }
    }
}
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) SchemaAbstractOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAbstractOperation) 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 43 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class QueryUtils method validateDropColumn.

/**
 * Checks if given column can be removed from table using its {@link QueryEntity}.
 *
 * @param entity Query entity.
 * @param fieldName Name of the field of the key or value object.
 * @param colName Name of the column.
 * @return {@code null} if it's OK to remove the column and exception otherwise.
 */
public static SchemaOperationException validateDropColumn(QueryEntity entity, String fieldName, String colName) {
    if (F.eq(fieldName, entity.getKeyFieldName()) || KEY_FIELD_NAME.equalsIgnoreCase(fieldName))
        return new SchemaOperationException("Cannot drop column \"" + colName + "\" because it represents an entire cache key");
    if (F.eq(fieldName, entity.getValueFieldName()) || VAL_FIELD_NAME.equalsIgnoreCase(fieldName))
        return new SchemaOperationException("Cannot drop column \"" + colName + "\" because it represents an entire cache value");
    Set<String> keyFields = entity.getKeyFields();
    if (keyFields != null && keyFields.contains(fieldName))
        return new SchemaOperationException("Cannot drop column \"" + colName + "\" because it is a part of a cache key");
    Collection<QueryIndex> indexes = entity.getIndexes();
    if (indexes != null) {
        for (QueryIndex idxDesc : indexes) {
            if (idxDesc.getFields().containsKey(fieldName))
                return new SchemaOperationException("Cannot drop column \"" + colName + "\" because an index exists (\"" + idxDesc.getName() + "\") that uses the column.");
        }
    }
    return null;
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) QueryIndex(org.apache.ignite.cache.QueryIndex)

Example 44 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class PlatformConfigurationUtils method readQueryEntity.

/**
 * Reads the query entity.
 *
 * @param in Stream.
 * @return QueryEntity.
 */
public static QueryEntity readQueryEntity(BinaryRawReader in) {
    QueryEntity res = new QueryEntity();
    res.setKeyType(in.readString());
    res.setValueType(in.readString());
    res.setTableName(in.readString());
    res.setKeyFieldName(in.readString());
    res.setValueFieldName(in.readString());
    // Fields
    int cnt = in.readInt();
    Set<String> keyFields = new HashSet<>(cnt);
    Set<String> notNullFields = new HashSet<>(cnt);
    Map<String, Object> defVals = new HashMap<>(cnt);
    if (cnt > 0) {
        LinkedHashMap<String, String> fields = new LinkedHashMap<>(cnt);
        for (int i = 0; i < cnt; i++) {
            String fieldName = in.readString();
            String fieldType = in.readString();
            fields.put(fieldName, fieldType);
            if (in.readBoolean())
                keyFields.add(fieldName);
            if (in.readBoolean())
                notNullFields.add(fieldName);
            Object defVal = in.readObject();
            if (defVal != null)
                defVals.put(fieldName, defVal);
        }
        res.setFields(fields);
        if (!keyFields.isEmpty())
            res.setKeyFields(keyFields);
        if (!notNullFields.isEmpty())
            res.setNotNullFields(notNullFields);
        if (!defVals.isEmpty())
            res.setDefaultFieldValues(defVals);
    }
    // Aliases
    cnt = in.readInt();
    if (cnt > 0) {
        Map<String, String> aliases = new HashMap<>(cnt);
        for (int i = 0; i < cnt; i++) aliases.put(in.readString(), in.readString());
        res.setAliases(aliases);
    }
    // Indexes
    cnt = in.readInt();
    if (cnt > 0) {
        Collection<QueryIndex> indexes = new ArrayList<>(cnt);
        for (int i = 0; i < cnt; i++) indexes.add(readQueryIndex(in));
        res.setIndexes(indexes);
    }
    return res;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedHashMap(java.util.LinkedHashMap) QueryIndex(org.apache.ignite.cache.QueryIndex) HashSet(java.util.HashSet)

Example 45 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class AbstractSchemaSelfTest method assertNoIndexDescriptor.

/**
     * Assert index doesn't exist in particular node's cache descriptor.
     *
     * @param node Node.
     * @param cacheName Cache name.
     * @param idxName Index name.
     */
protected static void assertNoIndexDescriptor(IgniteEx node, String cacheName, String idxName) {
    awaitCompletion();
    DynamicCacheDescriptor desc = node.context().cache().cacheDescriptor(cacheName);
    if (desc == null)
        return;
    for (QueryEntity entity : desc.schema().entities()) {
        for (QueryIndex idx : entity.getIndexes()) {
            if (F.eq(idxName, QueryUtils.indexName(entity, idx)))
                fail("Index exists: " + idxName);
        }
    }
}
Also used : DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity)

Aggregations

QueryIndex (org.apache.ignite.cache.QueryIndex)89 QueryEntity (org.apache.ignite.cache.QueryEntity)46 LinkedHashMap (java.util.LinkedHashMap)35 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)26 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)24 ArrayList (java.util.ArrayList)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)14 Ignite (org.apache.ignite.Ignite)13 CacheException (javax.cache.CacheException)10 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)10 IgniteException (org.apache.ignite.IgniteException)9 HashMap (java.util.HashMap)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)8 Map (java.util.Map)7 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)7 HashSet (java.util.HashSet)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 BinaryObject (org.apache.ignite.binary.BinaryObject)5 CountDownLatch (java.util.concurrent.CountDownLatch)4