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 + ']');
}
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations