Search in sources :

Example 81 with QueryIndex

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

the class QueryUtils method validateQueryEntity.

/**
 * Validate query entity.
 *
 * @param entity Entity.
 */
private static void validateQueryEntity(QueryEntity entity) {
    if (F.isEmpty(entity.findValueType()))
        throw new IgniteException("Value type cannot be null or empty [queryEntity=" + entity + ']');
    String keyFieldName = entity.getKeyFieldName();
    if (keyFieldName != null && !entity.getFields().containsKey(keyFieldName)) {
        throw new IgniteException("Key field is not in the field list [queryEntity=" + entity + ", keyFieldName=" + keyFieldName + "]");
    }
    String valFieldName = entity.getValueFieldName();
    if (valFieldName != null && !entity.getFields().containsKey(valFieldName)) {
        throw new IgniteException("Value field is not in the field list [queryEntity=" + entity + ", valFieldName=" + valFieldName + "]");
    }
    Collection<QueryIndex> idxs = entity.getIndexes();
    if (!F.isEmpty(idxs)) {
        Set<String> idxNames = new HashSet<>();
        for (QueryIndex idx : idxs) {
            String idxName = idx.getName();
            if (idxName == null)
                idxName = indexName(entity, idx);
            assert !F.isEmpty(idxName);
            if (!idxNames.add(idxName))
                throw new IgniteException("Duplicate index name [queryEntity=" + entity + ", queryIdx=" + idx + ']');
            if (idx.getIndexType() == null)
                throw new IgniteException("Index type is not set [queryEntity=" + entity + ", queryIdx=" + idx + ']');
        }
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) QueryIndex(org.apache.ignite.cache.QueryIndex) HashSet(java.util.HashSet)

Example 82 with QueryIndex

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

the class QueryUtils method normalizeQueryEntity.

/**
 * Normalize query entity. If "escape" flag is set, nothing changes. Otherwise we convert all object names to
 * upper case and replace inner class separator characters ('$' for Java and '.' for .NET) with underscore.
 *
 * @param entity Query entity.
 * @param escape Escape flag taken form configuration.
 * @return Normalized query entity.
 */
public static QueryEntity normalizeQueryEntity(QueryEntity entity, boolean escape) {
    if (escape) {
        String tblName = tableName(entity);
        entity.setTableName(tblName);
        Map<String, String> aliases = new HashMap<>(entity.getAliases());
        for (String fieldName : entity.getFields().keySet()) {
            String fieldAlias = entity.getAliases().get(fieldName);
            if (fieldAlias == null) {
                fieldAlias = aliasForFieldName(fieldName);
                aliases.put(fieldName, fieldAlias);
            }
        }
        entity.setAliases(aliases);
        for (QueryIndex idx : entity.getIndexes()) idx.setName(indexName(tblName, idx));
        validateQueryEntity(entity);
        return entity;
    }
    QueryEntity normalEntity = entity instanceof QueryEntityEx ? new QueryEntityEx() : new QueryEntity();
    // Propagate plain properties.
    normalEntity.setKeyType(entity.getKeyType());
    normalEntity.setValueType(entity.getValueType());
    normalEntity.setFields(entity.getFields());
    normalEntity.setKeyFields(entity.getKeyFields());
    normalEntity.setKeyFieldName(entity.getKeyFieldName());
    normalEntity.setValueFieldName(entity.getValueFieldName());
    normalEntity.setNotNullFields(entity.getNotNullFields());
    normalEntity.setDefaultFieldValues(entity.getDefaultFieldValues());
    // Normalize table name.
    String normalTblName = entity.getTableName();
    if (normalTblName == null)
        // Replace special characters for auto-generated table name.
        normalTblName = normalizeObjectName(tableName(entity), true);
    else
        // No replaces for manually defined table.
        normalTblName = normalizeObjectName(normalTblName, false);
    normalEntity.setTableName(normalTblName);
    // Normalize field names through aliases.
    Map<String, String> normalAliases = new HashMap<>(normalEntity.getAliases());
    for (String fieldName : normalEntity.getFields().keySet()) {
        String fieldAlias = entity.getAliases().get(fieldName);
        if (fieldAlias == null)
            fieldAlias = aliasForFieldName(fieldName);
        assert fieldAlias != null;
        normalAliases.put(fieldName, normalizeObjectName(fieldAlias, false));
    }
    normalEntity.setAliases(normalAliases);
    // Normalize indexes.
    Collection<QueryIndex> normalIdxs = new LinkedList<>();
    for (QueryIndex idx : entity.getIndexes()) {
        QueryIndex normalIdx = new QueryIndex();
        normalIdx.setFields(idx.getFields());
        normalIdx.setIndexType(idx.getIndexType());
        normalIdx.setInlineSize(idx.getInlineSize());
        normalIdx.setName(normalizeObjectName(indexName(normalTblName, idx), false));
        normalIdxs.add(normalIdx);
    }
    normalEntity.setIndexes(normalIdxs);
    validateQueryEntity(normalEntity);
    return normalEntity;
}
Also used : HashMap(java.util.HashMap) QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedList(java.util.LinkedList)

Example 83 with QueryIndex

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

the class CacheBinaryKeyConcurrentQueryTest method cacheConfiguration.

/**
 * @param name Cache name.
 * @param atomicityMode Cache atomicity mode.
 * @return Cache configuration.
 */
private CacheConfiguration cacheConfiguration(String name, CacheAtomicityMode atomicityMode) {
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setName(name);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    ccfg.setBackups(1);
    QueryEntity qryEntity = new QueryEntity();
    qryEntity.setKeyType(TestKey.class.getName());
    qryEntity.setValueType(TestValue.class.getName());
    qryEntity.addQueryField("id", Integer.class.getName(), null);
    qryEntity.addQueryField("val", Integer.class.getName(), null);
    qryEntity.setIndexes(F.asList(new QueryIndex("id"), new QueryIndex("val")));
    ccfg.setQueryEntities(F.asList(qryEntity));
    return ccfg;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 84 with QueryIndex

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

the class BinarySerializationQuerySelfTest method entityForClass.

/**
 * Create type metadata for class.
 *
 * @param cls Class.
 * @return Type metadata.
 */
private static QueryEntity entityForClass(Class cls) {
    QueryEntity entity = new QueryEntity(Integer.class.getName(), cls.getName());
    entity.addQueryField("val", Integer.class.getName(), null);
    entity.setIndexes(Collections.singletonList(new QueryIndex("val", true)));
    return entity;
}
Also used : QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity)

Example 85 with QueryIndex

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

the class IgniteCacheGroupsSqlTest method accountCacheConfiguration.

/**
 * @param grpName Group name.
 * @param cacheName Cache name.
 * @return Account cache configuration.
 */
private CacheConfiguration accountCacheConfiguration(String grpName, String cacheName) {
    QueryEntity entity = new QueryEntity();
    entity.setKeyType(AffinityKey.class.getName());
    entity.setValueType(Account.class.getName());
    entity.addQueryField("personId", Integer.class.getName(), null);
    entity.addQueryField("attr", String.class.getName(), null);
    entity.setIndexes(F.asList(new QueryIndex("personId")));
    return cacheConfiguration(grpName, cacheName, entity);
}
Also used : QueryIndex(org.apache.ignite.cache.QueryIndex) QueryEntity(org.apache.ignite.cache.QueryEntity) AffinityKey(org.apache.ignite.internal.binary.AffinityKey)

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