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 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);
}
use of org.apache.ignite.cache.QueryEntity 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);
}
}
}
use of org.apache.ignite.cache.QueryEntity 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;
}
use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class PlatformConfigurationUtils method writeCacheConfiguration.
/**
* Writes cache configuration.
*
* @param writer Writer.
* @param ccfg Configuration.
*/
public static void writeCacheConfiguration(BinaryRawWriter writer, CacheConfiguration ccfg) {
assert writer != null;
assert ccfg != null;
writeEnumInt(writer, ccfg.getAtomicityMode(), CacheConfiguration.DFLT_CACHE_ATOMICITY_MODE);
writer.writeInt(ccfg.getBackups());
writeEnumInt(writer, ccfg.getCacheMode(), CacheConfiguration.DFLT_CACHE_MODE);
writer.writeBoolean(ccfg.isCopyOnRead());
writer.writeBoolean(ccfg.isEagerTtl());
writer.writeBoolean(ccfg.isInvalidate());
writer.writeBoolean(ccfg.isStoreKeepBinary());
writer.writeBoolean(ccfg.isLoadPreviousValue());
writer.writeLong(ccfg.getDefaultLockTimeout());
// noinspection deprecation
writer.writeLong(ccfg.getLongQueryWarningTimeout());
writer.writeInt(ccfg.getMaxConcurrentAsyncOperations());
writer.writeString(ccfg.getName());
writer.writeBoolean(ccfg.isReadFromBackup());
writer.writeInt(ccfg.getRebalanceBatchSize());
writer.writeLong(ccfg.getRebalanceDelay());
writeEnumInt(writer, ccfg.getRebalanceMode(), CacheConfiguration.DFLT_REBALANCE_MODE);
writer.writeLong(ccfg.getRebalanceThrottle());
writer.writeLong(ccfg.getRebalanceTimeout());
writer.writeBoolean(ccfg.isSqlEscapeAll());
writer.writeInt(ccfg.getWriteBehindBatchSize());
writer.writeBoolean(ccfg.isWriteBehindEnabled());
writer.writeLong(ccfg.getWriteBehindFlushFrequency());
writer.writeInt(ccfg.getWriteBehindFlushSize());
writer.writeInt(ccfg.getWriteBehindFlushThreadCount());
writer.writeBoolean(ccfg.getWriteBehindCoalescing());
writeEnumInt(writer, ccfg.getWriteSynchronizationMode());
writer.writeBoolean(ccfg.isReadThrough());
writer.writeBoolean(ccfg.isWriteThrough());
writer.writeBoolean(ccfg.isStatisticsEnabled());
// noinspection deprecation
writer.writeString(ccfg.getMemoryPolicyName());
writer.writeInt(ccfg.getPartitionLossPolicy().ordinal());
writer.writeString(ccfg.getGroupName());
if (ccfg.getCacheStoreFactory() instanceof PlatformDotNetCacheStoreFactoryNative)
writer.writeObject(((PlatformDotNetCacheStoreFactoryNative) ccfg.getCacheStoreFactory()).getNativeFactory());
else
writer.writeObject(null);
writer.writeInt(ccfg.getSqlIndexMaxInlineSize());
writer.writeBoolean(ccfg.isOnheapCacheEnabled());
writer.writeInt(ccfg.getStoreConcurrentLoadAllThreshold());
writer.writeInt(ccfg.getRebalanceOrder());
writer.writeLong(ccfg.getRebalanceBatchesPrefetchCount());
writer.writeInt(ccfg.getMaxQueryIteratorsCount());
writer.writeInt(ccfg.getQueryDetailMetricsSize());
writer.writeInt(ccfg.getQueryParallelism());
writer.writeString(ccfg.getSqlSchema());
Collection<QueryEntity> qryEntities = ccfg.getQueryEntities();
if (qryEntities != null) {
writer.writeInt(qryEntities.size());
for (QueryEntity e : qryEntities) writeQueryEntity(writer, e);
} else
writer.writeInt(0);
NearCacheConfiguration nearCfg = ccfg.getNearConfiguration();
if (nearCfg != null) {
writer.writeBoolean(true);
writeNearConfiguration(writer, nearCfg);
} else
writer.writeBoolean(false);
writeEvictionPolicy(writer, ccfg.getEvictionPolicy());
writeAffinityFunction(writer, ccfg.getAffinity());
writeExpiryPolicyFactory(writer, ccfg.getExpiryPolicyFactory());
CacheKeyConfiguration[] keys = ccfg.getKeyConfiguration();
if (keys != null) {
writer.writeInt(keys.length);
for (CacheKeyConfiguration key : keys) {
writer.writeString(key.getTypeName());
writer.writeString(key.getAffinityKeyFieldName());
}
} else {
writer.writeInt(0);
}
CachePluginConfiguration[] plugins = ccfg.getPluginConfigurations();
if (plugins != null) {
int cnt = 0;
for (CachePluginConfiguration cfg : plugins) {
if (cfg instanceof PlatformCachePluginConfiguration)
cnt++;
}
writer.writeInt(cnt);
for (CachePluginConfiguration cfg : plugins) {
if (cfg instanceof PlatformCachePluginConfiguration) {
// Pure platform plugin.
writer.writeBoolean(false);
writer.writeObject(((PlatformCachePluginConfiguration) cfg).nativeCfg());
}
}
}
}
use of org.apache.ignite.cache.QueryEntity in project ignite by apache.
the class PlatformConfigurationUtils method readCacheConfiguration.
/**
* Reads cache configuration from a stream.
*
* @param in Stream.
* @return Cache configuration.
*/
public static CacheConfiguration readCacheConfiguration(BinaryRawReaderEx in) {
assert in != null;
CacheConfiguration ccfg = new CacheConfiguration();
ccfg.setAtomicityMode(CacheAtomicityMode.fromOrdinal(in.readInt()));
ccfg.setBackups(in.readInt());
ccfg.setCacheMode(CacheMode.fromOrdinal(in.readInt()));
ccfg.setCopyOnRead(in.readBoolean());
ccfg.setEagerTtl(in.readBoolean());
ccfg.setInvalidate(in.readBoolean());
ccfg.setStoreKeepBinary(in.readBoolean());
ccfg.setLoadPreviousValue(in.readBoolean());
ccfg.setDefaultLockTimeout(in.readLong());
// noinspection deprecation
ccfg.setLongQueryWarningTimeout(in.readLong());
ccfg.setMaxConcurrentAsyncOperations(in.readInt());
ccfg.setName(in.readString());
ccfg.setReadFromBackup(in.readBoolean());
ccfg.setRebalanceBatchSize(in.readInt());
ccfg.setRebalanceDelay(in.readLong());
ccfg.setRebalanceMode(CacheRebalanceMode.fromOrdinal(in.readInt()));
ccfg.setRebalanceThrottle(in.readLong());
ccfg.setRebalanceTimeout(in.readLong());
ccfg.setSqlEscapeAll(in.readBoolean());
ccfg.setWriteBehindBatchSize(in.readInt());
ccfg.setWriteBehindEnabled(in.readBoolean());
ccfg.setWriteBehindFlushFrequency(in.readLong());
ccfg.setWriteBehindFlushSize(in.readInt());
ccfg.setWriteBehindFlushThreadCount(in.readInt());
ccfg.setWriteBehindCoalescing(in.readBoolean());
ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.fromOrdinal(in.readInt()));
ccfg.setReadThrough(in.readBoolean());
ccfg.setWriteThrough(in.readBoolean());
ccfg.setStatisticsEnabled(in.readBoolean());
String dataRegionName = in.readString();
if (dataRegionName != null)
// noinspection deprecation
ccfg.setMemoryPolicyName(dataRegionName);
ccfg.setPartitionLossPolicy(PartitionLossPolicy.fromOrdinal((byte) in.readInt()));
ccfg.setGroupName(in.readString());
Object storeFactory = in.readObjectDetached();
if (storeFactory != null)
ccfg.setCacheStoreFactory(new PlatformDotNetCacheStoreFactoryNative(storeFactory));
ccfg.setSqlIndexMaxInlineSize(in.readInt());
ccfg.setOnheapCacheEnabled(in.readBoolean());
ccfg.setStoreConcurrentLoadAllThreshold(in.readInt());
ccfg.setRebalanceOrder(in.readInt());
ccfg.setRebalanceBatchesPrefetchCount(in.readLong());
ccfg.setMaxQueryIteratorsCount(in.readInt());
ccfg.setQueryDetailMetricsSize(in.readInt());
ccfg.setQueryParallelism(in.readInt());
ccfg.setSqlSchema(in.readString());
int qryEntCnt = in.readInt();
if (qryEntCnt > 0) {
Collection<QueryEntity> entities = new ArrayList<>(qryEntCnt);
for (int i = 0; i < qryEntCnt; i++) entities.add(readQueryEntity(in));
ccfg.setQueryEntities(entities);
}
if (in.readBoolean())
ccfg.setNearConfiguration(readNearConfiguration(in));
ccfg.setEvictionPolicy(readEvictionPolicy(in));
if (ccfg.getEvictionPolicy() != null)
ccfg.setOnheapCacheEnabled(true);
ccfg.setAffinity(readAffinityFunction(in));
ccfg.setExpiryPolicyFactory(readExpiryPolicyFactory(in));
int keyCnt = in.readInt();
if (keyCnt > 0) {
CacheKeyConfiguration[] keys = new CacheKeyConfiguration[keyCnt];
for (int i = 0; i < keyCnt; i++) {
keys[i] = new CacheKeyConfiguration(in.readString(), in.readString());
}
ccfg.setKeyConfiguration(keys);
}
int pluginCnt = in.readInt();
if (pluginCnt > 0) {
ArrayList<CachePluginConfiguration> plugins = new ArrayList<>();
for (int i = 0; i < pluginCnt; i++) {
if (in.readBoolean()) {
// Java cache plugin.
readCachePluginConfiguration(ccfg, in);
} else {
// Platform cache plugin.
plugins.add(new PlatformCachePluginConfiguration(in.readObjectDetached()));
}
}
if (ccfg.getPluginConfigurations() != null)
Collections.addAll(plugins, ccfg.getPluginConfigurations());
ccfg.setPluginConfigurations(plugins.toArray(new CachePluginConfiguration[plugins.size()]));
}
return ccfg;
}
Aggregations