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 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.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 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.QueryIndex in project ignite by apache.
the class QueryUtils method validateDropColumn.
/**
* Checks if given column can be removed from table using its {@link QueryEntity}.
*
* @param entity Query entity.
* @param fieldName Name of the field of the key or value object.
* @param colName Name of the column.
* @return {@code null} if it's OK to remove the column and exception otherwise.
*/
public static SchemaOperationException validateDropColumn(QueryEntity entity, String fieldName, String colName) {
if (F.eq(fieldName, entity.getKeyFieldName()) || KEY_FIELD_NAME.equalsIgnoreCase(fieldName))
return new SchemaOperationException("Cannot drop column \"" + colName + "\" because it represents an entire cache key");
if (F.eq(fieldName, entity.getValueFieldName()) || VAL_FIELD_NAME.equalsIgnoreCase(fieldName))
return new SchemaOperationException("Cannot drop column \"" + colName + "\" because it represents an entire cache value");
Set<String> keyFields = entity.getKeyFields();
if (keyFields != null && keyFields.contains(fieldName))
return new SchemaOperationException("Cannot drop column \"" + colName + "\" because it is a part of a cache key");
Collection<QueryIndex> indexes = entity.getIndexes();
if (indexes != null) {
for (QueryIndex idxDesc : indexes) {
if (idxDesc.getFields().containsKey(fieldName))
return new SchemaOperationException("Cannot drop column \"" + colName + "\" because an index exists (\"" + idxDesc.getName() + "\") that uses the column.");
}
}
return null;
}
use of org.apache.ignite.cache.QueryIndex 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.QueryIndex in project ignite by apache.
the class AbstractSchemaSelfTest method assertNoIndexDescriptor.
/**
* Assert index doesn't exist in particular node's cache descriptor.
*
* @param node Node.
* @param cacheName Cache name.
* @param idxName Index name.
*/
protected static void assertNoIndexDescriptor(IgniteEx node, String cacheName, String idxName) {
awaitCompletion();
DynamicCacheDescriptor desc = node.context().cache().cacheDescriptor(cacheName);
if (desc == null)
return;
for (QueryEntity entity : desc.schema().entities()) {
for (QueryIndex idx : entity.getIndexes()) {
if (F.eq(idxName, QueryUtils.indexName(entity, idx)))
fail("Index exists: " + idxName);
}
}
}
Aggregations