Search in sources :

Example 1 with QueryIndex

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

the class CacheClientBinaryQueryExample method createEmployeeQueryEntity.

/**
     * Create cache type metadata for {@link Employee}.
     *
     * @return Cache type metadata.
     */
private static QueryEntity createEmployeeQueryEntity() {
    QueryEntity employeeEntity = new QueryEntity();
    employeeEntity.setValueType(Employee.class.getName());
    employeeEntity.setKeyType(EmployeeKey.class.getName());
    LinkedHashMap<String, String> fields = new LinkedHashMap<>();
    fields.put("name", String.class.getName());
    fields.put("salary", Long.class.getName());
    fields.put("addr.zip", Integer.class.getName());
    fields.put("organizationId", Integer.class.getName());
    fields.put("addr.street", Integer.class.getName());
    employeeEntity.setFields(fields);
    employeeEntity.setIndexes(Arrays.asList(new QueryIndex("name"), new QueryIndex("salary"), new QueryIndex("addr.zip"), new QueryIndex("organizationId"), new QueryIndex("addr.street", QueryIndexType.FULLTEXT)));
    return employeeEntity;
}
Also used : Employee(org.apache.ignite.examples.model.Employee) EmployeeKey(org.apache.ignite.examples.model.EmployeeKey) QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with QueryIndex

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

the class AbstractJdbcPojoQuerySelfTest method getConfiguration.

/** {@inheritDoc} */
@Override
protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
    GridTestProperties.setProperty(GridTestProperties.MARSH_CLASS_NAME, BinaryMarshaller.class.getName());
    IgniteConfiguration cfg = super.getConfiguration(gridName);
    CacheConfiguration<?, ?> cache = defaultCacheConfiguration();
    cache.setWriteSynchronizationMode(FULL_SYNC);
    cache.setAtomicityMode(TRANSACTIONAL);
    TcpDiscoverySpi disco = new TcpDiscoverySpi();
    disco.setIpFinder(IP_FINDER);
    cfg.setDiscoverySpi(disco);
    cfg.setConnectorConfiguration(new ConnectorConfiguration());
    QueryEntity queryEntity = new QueryEntity();
    queryEntity.setKeyType("java.lang.String");
    queryEntity.setValueType("org.apache.ignite.internal.JdbcTestObject");
    queryEntity.addQueryField("id", "java.lang.Integer", null);
    queryEntity.addQueryField("testObject", "org.apache.ignite.internal.JdbcTestObject2", null);
    queryEntity.setIndexes(Collections.singletonList(new QueryIndex("id")));
    cache.setQueryEntities(Collections.singletonList(queryEntity));
    cfg.setCacheConfiguration(cache);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) QueryIndex(org.apache.ignite.cache.QueryIndex) ConnectorConfiguration(org.apache.ignite.configuration.ConnectorConfiguration) QueryEntity(org.apache.ignite.cache.QueryEntity) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 3 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
        err = new SchemaOperationException("Unsupported operation: " + op);
    return new T2<>(nop, err);
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) QueryIndex(org.apache.ignite.cache.QueryIndex) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) T2(org.apache.ignite.internal.util.typedef.T2) HashSet(java.util.HashSet) GridBoundedConcurrentLinkedHashSet(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet)

Example 4 with QueryIndex

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

the class GridQueryProcessor method prepareChangeOnStartedCache.

/**
     * Prepare change on started cache.
     *
     * @param op Operation.
     * @return Result: affected type, nop flag, error.
     */
private T3<QueryTypeDescriptorImpl, Boolean, SchemaOperationException> prepareChangeOnStartedCache(SchemaAbstractOperation op) {
    QueryTypeDescriptorImpl type = null;
    boolean nop = false;
    SchemaOperationException err = null;
    String cacheName = op.cacheName();
    if (op instanceof SchemaIndexCreateOperation) {
        SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation) op;
        QueryIndex idx = op0.index();
        // Make sure table exists.
        String tblName = op0.tableName();
        type = type(cacheName, tblName);
        if (type == null)
            err = new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, tblName);
        else {
            // Make sure that index can be applied to the given table.
            for (String idxField : idx.getFieldNames()) {
                if (!type.fields().containsKey(idxField)) {
                    err = new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, idxField);
                    break;
                }
            }
        }
        // Check conflict with other indexes.
        if (err == null) {
            String idxName = op0.index().getName();
            QueryIndexKey idxKey = new QueryIndexKey(op.schemaName(), idxName);
            if (idxs.get(idxKey) != null) {
                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();
        QueryIndexDescriptorImpl oldIdx = idxs.get(new QueryIndexKey(op.schemaName(), idxName));
        if (oldIdx == null) {
            if (op0.ifExists())
                nop = true;
            else
                err = new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, idxName);
        } else
            type = oldIdx.typeDescriptor();
    } else
        err = new SchemaOperationException("Unsupported operation: " + op);
    return new T3<>(type, nop, err);
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) SchemaIndexCreateOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation) QueryIndex(org.apache.ignite.cache.QueryIndex) SchemaIndexDropOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexDropOperation) T3(org.apache.ignite.internal.util.typedef.T3)

Example 5 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 {
            assert 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;
                }
            }
        }
    }
}
Also used : SchemaAbstractOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaAbstractOperation) ArrayList(java.util.ArrayList) SchemaIndexCreateOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation) QueryIndex(org.apache.ignite.cache.QueryIndex) SchemaIndexDropOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexDropOperation) QueryEntity(org.apache.ignite.cache.QueryEntity)

Aggregations

QueryIndex (org.apache.ignite.cache.QueryIndex)69 QueryEntity (org.apache.ignite.cache.QueryEntity)35 LinkedHashMap (java.util.LinkedHashMap)25 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)21 ArrayList (java.util.ArrayList)18 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)16 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 Ignite (org.apache.ignite.Ignite)13 IgniteException (org.apache.ignite.IgniteException)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)8 HashMap (java.util.HashMap)7 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)7 CacheException (javax.cache.CacheException)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4