Search in sources :

Example 11 with QueryEntity

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

the class JavaThinCompatibilityTest method testCacheConfiguration.

/**
 */
private void testCacheConfiguration(boolean checkFieldsPrecessionAndScale, boolean checkExpiryPlc) throws Exception {
    X.println(">>>> Testing cache configuration");
    try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
        String cacheName = "testCacheConfiguration";
        ClientCacheConfiguration ccfg = new ClientCacheConfiguration();
        ccfg.setName(cacheName);
        ccfg.setBackups(3);
        ccfg.setGroupName("cache");
        ccfg.setCacheMode(CacheMode.PARTITIONED);
        QueryEntity qryEntity = new QueryEntity(int.class.getName(), "Entity").setTableName("ENTITY").setFields(new LinkedHashMap<>(F.asMap("id", Integer.class.getName(), "rate", Double.class.getName())));
        if (checkFieldsPrecessionAndScale) {
            qryEntity.setFieldsPrecision(F.asMap("rate", 5));
            qryEntity.setFieldsScale(F.asMap("rate", 2));
        }
        ccfg.setQueryEntities(qryEntity);
        if (checkExpiryPlc)
            ccfg.setExpiryPolicy(new PlatformExpiryPolicy(10, 20, 30));
        client.createCache(ccfg);
        ClientCacheConfiguration ccfg1 = client.cache(cacheName).getConfiguration();
        assertEquals(ccfg.getName(), ccfg1.getName());
        assertEquals(ccfg.getBackups(), ccfg1.getBackups());
        assertEquals(ccfg.getGroupName(), ccfg1.getGroupName());
        assertEquals(ccfg.getCacheMode(), ccfg1.getCacheMode());
        assertEquals(ccfg.getQueryEntities().length, ccfg1.getQueryEntities().length);
        assertEquals(ccfg.getQueryEntities()[0].getTableName(), ccfg1.getQueryEntities()[0].getTableName());
        assertEquals(ccfg.getQueryEntities()[0].getFields(), ccfg1.getQueryEntities()[0].getFields());
        if (checkFieldsPrecessionAndScale) {
            assertEquals(ccfg.getQueryEntities()[0].getFieldsPrecision(), ccfg1.getQueryEntities()[0].getFieldsPrecision());
            assertEquals(ccfg.getQueryEntities()[0].getFieldsScale(), ccfg1.getQueryEntities()[0].getFieldsScale());
        }
        if (checkExpiryPlc) {
            assertEquals(ccfg.getExpiryPolicy().getExpiryForCreation(), ccfg1.getExpiryPolicy().getExpiryForCreation());
            assertEquals(ccfg.getExpiryPolicy().getExpiryForAccess(), ccfg1.getExpiryPolicy().getExpiryForAccess());
            assertEquals(ccfg.getExpiryPolicy().getExpiryForUpdate(), ccfg1.getExpiryPolicy().getExpiryForUpdate());
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) QueryEntity(org.apache.ignite.cache.QueryEntity) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration)

Example 12 with QueryEntity

use of org.apache.ignite.cache.QueryEntity 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);
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) 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) 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) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Collections.newSetFromMap(java.util.Collections.newSetFromMap) 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 13 with QueryEntity

use of org.apache.ignite.cache.QueryEntity 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 14 with QueryEntity

use of org.apache.ignite.cache.QueryEntity 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 15 with QueryEntity

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

the class ExternalStorage method cacheJdbcPojoStoreExample.

public static void cacheJdbcPojoStoreExample() {
    // tag::pojo[]
    IgniteConfiguration igniteCfg = new IgniteConfiguration();
    CacheConfiguration<Integer, Person> personCacheCfg = new CacheConfiguration<>();
    personCacheCfg.setName("PersonCache");
    personCacheCfg.setCacheMode(CacheMode.PARTITIONED);
    personCacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    personCacheCfg.setReadThrough(true);
    personCacheCfg.setWriteThrough(true);
    CacheJdbcPojoStoreFactory<Integer, Person> factory = new CacheJdbcPojoStoreFactory<>();
    factory.setDialect(new MySQLDialect());
    factory.setDataSourceFactory((Factory<DataSource>) () -> {
        MysqlDataSource mysqlDataSrc = new MysqlDataSource();
        mysqlDataSrc.setURL("jdbc:mysql://[host]:[port]/[database]");
        mysqlDataSrc.setUser("YOUR_USER_NAME");
        mysqlDataSrc.setPassword("YOUR_PASSWORD");
        return mysqlDataSrc;
    });
    JdbcType personType = new JdbcType();
    personType.setCacheName("PersonCache");
    personType.setKeyType(Integer.class);
    personType.setValueType(Person.class);
    // Specify the schema if applicable
    // personType.setDatabaseSchema("MY_DB_SCHEMA");
    personType.setDatabaseTable("PERSON");
    personType.setKeyFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
    personType.setValueFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
    personType.setValueFields(new JdbcTypeField(java.sql.Types.VARCHAR, "name", String.class, "name"));
    factory.setTypes(personType);
    personCacheCfg.setCacheStoreFactory(factory);
    QueryEntity qryEntity = new QueryEntity();
    qryEntity.setKeyType(Integer.class.getName());
    qryEntity.setValueType(Person.class.getName());
    qryEntity.setKeyFieldName("id");
    Set<String> keyFields = new HashSet<>();
    keyFields.add("id");
    qryEntity.setKeyFields(keyFields);
    LinkedHashMap<String, String> fields = new LinkedHashMap<>();
    fields.put("id", "java.lang.Integer");
    fields.put("name", "java.lang.String");
    qryEntity.setFields(fields);
    personCacheCfg.setQueryEntities(Collections.singletonList(qryEntity));
    igniteCfg.setCacheConfiguration(personCacheCfg);
// end::pojo[]
}
Also used : JdbcType(org.apache.ignite.cache.store.jdbc.JdbcType) QueryEntity(org.apache.ignite.cache.QueryEntity) MysqlDataSource(com.mysql.cj.jdbc.MysqlDataSource) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap) MySQLDialect(org.apache.ignite.cache.store.jdbc.dialect.MySQLDialect) CacheJdbcPojoStoreFactory(org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) MysqlDataSource(com.mysql.cj.jdbc.MysqlDataSource) JdbcTypeField(org.apache.ignite.cache.store.jdbc.JdbcTypeField) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet)

Aggregations

QueryEntity (org.apache.ignite.cache.QueryEntity)221 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)115 QueryIndex (org.apache.ignite.cache.QueryIndex)87 LinkedHashMap (java.util.LinkedHashMap)83 ArrayList (java.util.ArrayList)53 Test (org.junit.Test)42 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)41 HashMap (java.util.HashMap)27 HashSet (java.util.HashSet)22 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)21 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)21 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)19 List (java.util.List)18 Ignite (org.apache.ignite.Ignite)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)13 Map (java.util.Map)12 BinaryObject (org.apache.ignite.binary.BinaryObject)12 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)12 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)12