use of org.apache.ignite.internal.util.typedef.T2 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
err = new SchemaOperationException("Unsupported operation: " + op);
return new T2<>(nop, err);
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class IgniteCachePeekModesAbstractTest method offheapKeysCount.
/**
* @param nodeIdx Node index.
* @param part Cache partition.
* @return Tuple with number of primary and backup keys (one or both will be zero).
*/
private T2<Integer, Integer> offheapKeysCount(int nodeIdx, int part) throws IgniteCheckedException {
GridCacheContext ctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context();
// Swap and offheap are disabled for near cache.
IgniteCacheOffheapManager offheapManager = ctx.isNear() ? ctx.near().dht().context().offheap() : ctx.offheap();
//First count entries...
int cnt = (int) offheapManager.entriesCount(part);
GridCacheAffinityManager affinity = ctx.affinity();
AffinityTopologyVersion topVer = affinity.affinityTopologyVersion();
//And then find out whether they are primary or backup ones.
int primaryCnt = 0;
int backupCnt = 0;
if (affinity.primaryByPartition(ctx.localNode(), part, topVer))
primaryCnt = cnt;
else if (affinity.backupByPartition(ctx.localNode(), part, topVer))
backupCnt = cnt;
return new T2<>(primaryCnt, backupCnt);
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class IgniteCachePeekModesAbstractTest method swapKeys.
/**
* @param nodeIdx Node index.
* @return Tuple with primary and backup keys.
*/
private T2<List<Integer>, List<Integer>> swapKeys(int nodeIdx) {
// TODO: GG-11148.
// SwapSpaceSpi swap = ignite(nodeIdx).configuration().getSwapSpaceSpi();
//
// IgniteSpiCloseableIterator<KeyCacheObject> it = swap.keyIterator(SPACE_NAME, null);
IgniteSpiCloseableIterator<KeyCacheObject> it = new GridEmptyCloseableIterator<>();
assertNotNull(it);
Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
ClusterNode node = ignite(nodeIdx).cluster().localNode();
List<Integer> primary = new ArrayList<>();
List<Integer> backups = new ArrayList<>();
CacheObjectContext coctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context().cacheObjectContext();
while (it.hasNext()) {
Integer key = it.next().value(coctx, false);
if (aff.isPrimary(node, key))
primary.add(key);
else {
assertTrue(aff.isBackup(node, key));
backups.add(key);
}
}
return new T2<>(primary, backups);
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class GridCacheNearMultiNodeSelfTest method mapKeys.
/** @param cnt Count. */
private Map<UUID, T2<Set<Integer>, Set<Integer>>> mapKeys(int cnt) {
Affinity<Object> aff = affinity(0);
//Mapping primary and backup keys on node
Map<UUID, T2<Set<Integer>, Set<Integer>>> map = new HashMap<>();
for (int i = 0; i < GRID_CNT; i++) {
IgniteEx grid = grid(i);
map.put(grid.cluster().localNode().id(), new T2<Set<Integer>, Set<Integer>>(new HashSet<Integer>(), new HashSet<Integer>()));
}
for (int key = 1; key <= cnt; key++) {
Integer part = aff.partition(key);
assert part != null;
Collection<ClusterNode> nodes = aff.mapPartitionToPrimaryAndBackups(part);
ClusterNode primary = F.first(nodes);
map.get(primary.id()).get1().add(key);
if (mapDebug)
info("Mapped key to primary node [key=" + key + ", node=" + U.toShortString(primary));
for (ClusterNode n : nodes) {
if (n != primary) {
map.get(n.id()).get2().add(key);
if (mapDebug)
info("Mapped key to backup node [key=" + key + ", node=" + U.toShortString(n));
}
}
}
return map;
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class GridCacheNearMultiNodeSelfTest method testReadThrough.
/** @throws Exception If failed. */
public void testReadThrough() throws Exception {
ClusterNode loc = grid(0).localNode();
info("Local node: " + U.toShortString(loc));
IgniteCache<Integer, String> near = jcache(0);
int cnt = 10;
Map<UUID, T2<Set<Integer>, Set<Integer>>> mapKeys = mapKeys(cnt);
for (int key = 1; key <= cnt; key++) {
String s = near.get(key);
info("Read key [key=" + key + ", val=" + s + ']');
assert s != null;
}
info("Read all keys.");
for (int key = 1; key <= cnt; key++) {
ClusterNode n = primaryNode(key);
info("Primary node for key [key=" + key + ", node=" + U.toShortString(n) + ']');
assert n != null;
assert mapKeys.get(n.id()).get1().contains(key);
GridCacheAdapter<Integer, String> dhtCache = dht(G.ignite(n.id()));
String s = dhtCache.localPeek(key, null, null);
assert s != null : "Value is null for key: " + key;
assertEquals(s, Integer.toString(key));
}
}
Aggregations