use of org.apache.ignite.internal.util.typedef.CI2 in project ignite by apache.
the class GridCacheAdapter method getAllAsync0.
/**
* @param keys Keys.
* @param readerArgs Near cache reader will be added if not null.
* @param readThrough Read-through flag.
* @param checkTx Check local transaction flag.
* @param subjId Subject ID.
* @param taskName Task name/
* @param deserializeBinary Deserialize binary flag.
* @param expiry Expiry policy.
* @param skipVals Skip values flag.
* @param keepCacheObjects Keep cache objects.
* @param needVer If {@code true} returns values as tuples containing value and version.
* @return Future.
*/
protected final <K1, V1> IgniteInternalFuture<Map<K1, V1>> getAllAsync0(@Nullable final Collection<KeyCacheObject> keys, @Nullable final ReaderArguments readerArgs, final boolean readThrough, boolean checkTx, @Nullable final UUID subjId, final String taskName, final boolean deserializeBinary, @Nullable final IgniteCacheExpiryPolicy expiry, final boolean skipVals, final boolean keepCacheObjects, final boolean recovery, final boolean needVer) {
if (F.isEmpty(keys))
return new GridFinishedFuture<>(Collections.<K1, V1>emptyMap());
GridNearTxLocal tx = null;
if (checkTx) {
try {
checkJta();
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
tx = ctx.tm().threadLocalTx(ctx);
}
if (tx == null || tx.implicit()) {
Map<KeyCacheObject, EntryGetResult> misses = null;
Set<GridCacheEntryEx> newLocalEntries = null;
final AffinityTopologyVersion topVer = tx == null ? ctx.affinity().affinityTopologyVersion() : tx.topologyVersion();
try {
int keysSize = keys.size();
GridDhtTopologyFuture topFut = ctx.shared().exchange().lastFinishedFuture();
Throwable ex = topFut != null ? topFut.validateCache(ctx, recovery, /*read*/
true, null, keys) : null;
if (ex != null)
return new GridFinishedFuture<>(ex);
final Map<K1, V1> map = keysSize == 1 ? (Map<K1, V1>) new IgniteBiTuple<>() : U.<K1, V1>newHashMap(keysSize);
final boolean storeEnabled = !skipVals && readThrough && ctx.readThrough();
boolean readNoEntry = ctx.readNoEntry(expiry, readerArgs != null);
for (KeyCacheObject key : keys) {
while (true) {
try {
EntryGetResult res = null;
boolean evt = !skipVals;
boolean updateMetrics = !skipVals;
GridCacheEntryEx entry = null;
boolean skipEntry = readNoEntry;
if (readNoEntry) {
CacheDataRow row = ctx.offheap().read(ctx, key);
if (row != null) {
long expireTime = row.expireTime();
if (expireTime != 0) {
if (expireTime > U.currentTimeMillis()) {
res = new EntryGetWithTtlResult(row.value(), row.version(), false, expireTime, 0);
} else
skipEntry = false;
} else
res = new EntryGetResult(row.value(), row.version(), false);
}
if (res != null) {
if (evt) {
ctx.events().readEvent(key, null, row.value(), subjId, taskName, !deserializeBinary);
}
if (updateMetrics && ctx.statisticsEnabled())
ctx.cache().metrics0().onRead(true);
} else if (storeEnabled)
skipEntry = false;
}
if (!skipEntry) {
boolean isNewLocalEntry = this.map.getEntry(ctx, key) == null;
entry = entryEx(key);
if (entry == null) {
if (!skipVals && ctx.statisticsEnabled())
ctx.cache().metrics0().onRead(false);
break;
}
if (isNewLocalEntry) {
if (newLocalEntries == null)
newLocalEntries = new HashSet<>();
newLocalEntries.add(entry);
}
if (storeEnabled) {
res = entry.innerGetAndReserveForLoad(updateMetrics, evt, subjId, taskName, expiry, !deserializeBinary, readerArgs);
assert res != null;
if (res.value() == null) {
if (misses == null)
misses = new HashMap<>();
misses.put(key, res);
res = null;
}
} else {
res = entry.innerGetVersioned(null, null, updateMetrics, evt, subjId, null, taskName, expiry, !deserializeBinary, readerArgs);
if (res == null)
ctx.evicts().touch(entry, topVer);
}
}
if (res != null) {
ctx.addResult(map, key, res, skipVals, keepCacheObjects, deserializeBinary, true, needVer);
if (entry != null && (tx == null || (!tx.implicit() && tx.isolation() == READ_COMMITTED)))
ctx.evicts().touch(entry, topVer);
if (keysSize == 1)
// Safe to return because no locks are required in READ_COMMITTED mode.
return new GridFinishedFuture<>(map);
}
break;
} catch (GridCacheEntryRemovedException ignored) {
if (log.isDebugEnabled())
log.debug("Got removed entry in getAllAsync(..) method (will retry): " + key);
}
}
}
if (storeEnabled && misses != null) {
final Map<KeyCacheObject, EntryGetResult> loadKeys = misses;
final IgniteTxLocalAdapter tx0 = tx;
final Collection<KeyCacheObject> loaded = new HashSet<>();
return new GridEmbeddedFuture(ctx.closures().callLocalSafe(ctx.projectSafe(new GPC<Map<K1, V1>>() {
@Override
public Map<K1, V1> call() throws Exception {
ctx.store().loadAll(null, /*tx*/
loadKeys.keySet(), new CI2<KeyCacheObject, Object>() {
@Override
public void apply(KeyCacheObject key, Object val) {
EntryGetResult res = loadKeys.get(key);
if (res == null || val == null)
return;
loaded.add(key);
CacheObject cacheVal = ctx.toCacheObject(val);
while (true) {
GridCacheEntryEx entry = null;
try {
ctx.shared().database().ensureFreeSpace(ctx.dataRegion());
} catch (IgniteCheckedException e) {
// Wrap errors (will be unwrapped).
throw new GridClosureException(e);
}
ctx.shared().database().checkpointReadLock();
try {
entry = entryEx(key);
entry.unswap();
EntryGetResult verVal = entry.versionedValue(cacheVal, res.version(), null, expiry, readerArgs);
if (log.isDebugEnabled())
log.debug("Set value loaded from store into entry [" + "oldVer=" + res.version() + ", newVer=" + verVal.version() + ", " + "entry=" + entry + ']');
// Don't put key-value pair into result map if value is null.
if (verVal.value() != null) {
ctx.addResult(map, key, verVal, skipVals, keepCacheObjects, deserializeBinary, true, needVer);
}
if (tx0 == null || (!tx0.implicit() && tx0.isolation() == READ_COMMITTED))
ctx.evicts().touch(entry, topVer);
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry during getAllAsync (will retry): " + entry);
} catch (IgniteCheckedException e) {
// Wrap errors (will be unwrapped).
throw new GridClosureException(e);
} finally {
ctx.shared().database().checkpointReadUnlock();
}
}
}
});
clearReservationsIfNeeded(topVer, loadKeys, loaded, tx0);
return map;
}
}), true), new C2<Map<K, V>, Exception, IgniteInternalFuture<Map<K, V>>>() {
@Override
public IgniteInternalFuture<Map<K, V>> apply(Map<K, V> map, Exception e) {
if (e != null) {
clearReservationsIfNeeded(topVer, loadKeys, loaded, tx0);
return new GridFinishedFuture<>(e);
}
if (tx0 == null || (!tx0.implicit() && tx0.isolation() == READ_COMMITTED)) {
Collection<KeyCacheObject> notFound = new HashSet<>(loadKeys.keySet());
notFound.removeAll(loaded);
// Touch entries that were not found in store.
for (KeyCacheObject key : notFound) {
GridCacheEntryEx entry = peekEx(key);
if (entry != null)
ctx.evicts().touch(entry, topVer);
}
}
// There were no misses.
return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
}
}, new C2<Map<K1, V1>, Exception, Map<K1, V1>>() {
@Override
public Map<K1, V1> apply(Map<K1, V1> loaded, Exception e) {
if (e == null)
map.putAll(loaded);
return map;
}
});
} else
// Misses can be non-zero only if store is enabled.
assert misses == null;
return new GridFinishedFuture<>(map);
} catch (RuntimeException | AssertionError e) {
if (misses != null) {
for (KeyCacheObject key0 : misses.keySet()) ctx.evicts().touch(peekEx(key0), topVer);
}
if (newLocalEntries != null) {
for (GridCacheEntryEx entry : newLocalEntries) removeEntry(entry);
}
return new GridFinishedFuture<>(e);
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
} else {
return asyncOp(tx, new AsyncOp<Map<K1, V1>>(keys) {
@Override
public IgniteInternalFuture<Map<K1, V1>> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
return tx.getAllAsync(ctx, readyTopVer, keys, deserializeBinary, skipVals, false, !readThrough, recovery, needVer);
}
}, ctx.operationContextPerCall(), /*retry*/
false);
}
}
use of org.apache.ignite.internal.util.typedef.CI2 in project ignite by apache.
the class GridCacheStoreManagerAdapter method loadAllFromStore.
/**
* @param tx Cache transaction.
* @param keys Keys to load.
* @param vis Key/value closure (only one of vis or verVis can be specified).
* @param verVis Key/value/version closure (only one of vis or verVis can be specified).
* @throws IgniteCheckedException If failed.
*/
private void loadAllFromStore(@Nullable IgniteInternalTx tx, Collection<? extends KeyCacheObject> keys, @Nullable final IgniteBiInClosure<KeyCacheObject, Object> vis, @Nullable final GridInClosure3<KeyCacheObject, Object, GridCacheVersion> verVis) throws IgniteCheckedException {
assert vis != null ^ verVis != null;
assert verVis == null || locStore;
final boolean convert = verVis == null;
if (!keys.isEmpty()) {
if (keys.size() == 1) {
KeyCacheObject key = F.first(keys);
if (convert)
vis.apply(key, load(tx, key));
else {
IgniteBiTuple<Object, GridCacheVersion> t = (IgniteBiTuple<Object, GridCacheVersion>) loadFromStore(tx, key, false);
if (t != null)
verVis.apply(key, t.get1(), t.get2());
}
return;
}
Collection<Object> keys0 = F.viewReadOnly(keys, new C1<KeyCacheObject, Object>() {
@Override
public Object apply(KeyCacheObject key) {
return cctx.unwrapBinaryIfNeeded(key, !convertBinary(), null);
}
});
if (log.isDebugEnabled())
log.debug("Loading values from store for keys: " + keys0);
sessionInit0(tx, StoreOperation.READ, false);
boolean threwEx = true;
try {
IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {
@Override
public void apply(Object k, Object val) {
if (convert) {
Object v = convert(val);
vis.apply(cctx.toCacheKeyObject(k), v);
} else {
IgniteBiTuple<Object, GridCacheVersion> v = (IgniteBiTuple<Object, GridCacheVersion>) val;
if (v != null)
verVis.apply(cctx.toCacheKeyObject(k), v.get1(), v.get2());
}
}
};
if (keys.size() > singleThreadGate.loadAllThreshold()) {
Map<Object, Object> map = store.loadAll(keys0);
if (map != null) {
for (Map.Entry<Object, Object> e : map.entrySet()) c.apply(cctx.toCacheKeyObject(e.getKey()), e.getValue());
}
} else
singleThreadGate.loadAll(keys0, c);
threwEx = false;
} catch (ClassCastException e) {
handleClassCastException(e);
} catch (CacheLoaderException e) {
throw new IgniteCheckedException(e);
} catch (Exception e) {
throw new IgniteCheckedException(new CacheLoaderException(e));
} finally {
sessionEnd0(tx, threwEx);
}
if (log.isDebugEnabled())
log.debug("Loaded values from store for keys: " + keys0);
}
}
use of org.apache.ignite.internal.util.typedef.CI2 in project ignite by apache.
the class CacheJdbcPojoStoreTest method testParallelLoad.
/**
* @throws Exception If failed.
*/
@Test
public void testParallelLoad() throws Exception {
Connection conn = store.openConnection(false);
PreparedStatement prnComplexStmt = conn.prepareStatement("INSERT INTO Person_Complex(id, org_id, city_id, name, salary) VALUES (?, ?, ?, ?, ?)");
for (int i = 0; i < 8; i++) {
prnComplexStmt.setInt(1, (i >> 2) & 1);
prnComplexStmt.setInt(2, (i >> 1) & 1);
prnComplexStmt.setInt(3, i % 2);
prnComplexStmt.setString(4, "name");
prnComplexStmt.setInt(5, 1000 + i * 500);
prnComplexStmt.addBatch();
}
prnComplexStmt.executeBatch();
U.closeQuiet(prnComplexStmt);
conn.commit();
U.closeQuiet(conn);
final Collection<Object> prnComplexKeys = new ConcurrentLinkedQueue<>();
IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {
@Override
public void apply(Object k, Object v) {
if (binaryEnable) {
if (k instanceof BinaryObject && v instanceof BinaryObject) {
BinaryObject key = (BinaryObject) k;
BinaryObject val = (BinaryObject) v;
String keyType = key.type().typeName();
String valType = val.type().typeName();
if (PersonComplexKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
prnComplexKeys.add(key);
}
} else {
if (k instanceof PersonComplexKey && v instanceof Person)
prnComplexKeys.add(k);
else
fail("Unexpected entry [key=" + k + ", value=" + v + "]");
}
}
};
store.setParallelLoadCacheMinimumThreshold(2);
store.loadCache(c);
assertEquals(8, prnComplexKeys.size());
}
use of org.apache.ignite.internal.util.typedef.CI2 in project ignite by apache.
the class GridCacheAdapter method getAllAsync0.
/**
* @param keys Keys.
* @param readerArgs Near cache reader will be added if not null.
* @param readThrough Read-through flag.
* @param checkTx Check local transaction flag.
* @param taskName Task name/
* @param deserializeBinary Deserialize binary flag.
* @param expiry Expiry policy.
* @param skipVals Skip values flag.
* @param keepCacheObjects Keep cache objects.
* @param needVer If {@code true} returns values as tuples containing value and version.
* @param txLbl Transaction label.
* @param mvccSnapshot MVCC snapshot.
* @return Future.
*/
protected final <K1, V1> IgniteInternalFuture<Map<K1, V1>> getAllAsync0(@Nullable final Collection<KeyCacheObject> keys, @Nullable final ReaderArguments readerArgs, final boolean readThrough, boolean checkTx, final String taskName, final boolean deserializeBinary, @Nullable final IgniteCacheExpiryPolicy expiry, final boolean skipVals, final boolean keepCacheObjects, final boolean recovery, final ReadRepairStrategy readRepairStrategy, final boolean needVer, @Nullable String txLbl, MvccSnapshot mvccSnapshot) {
if (F.isEmpty(keys))
return new GridFinishedFuture<>(Collections.<K1, V1>emptyMap());
GridNearTxLocal tx = null;
if (checkTx) {
try {
checkJta();
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
}
tx = checkCurrentTx();
}
if (ctx.mvccEnabled() || tx == null || tx.implicit()) {
assert (mvccSnapshot == null) == !ctx.mvccEnabled();
Map<KeyCacheObject, EntryGetResult> misses = null;
Set<GridCacheEntryEx> newLocalEntries = null;
final AffinityTopologyVersion topVer = tx == null ? ctx.affinity().affinityTopologyVersion() : tx.topologyVersion();
ctx.shared().database().checkpointReadLock();
try {
int keysSize = keys.size();
GridDhtTopologyFuture topFut = ctx.shared().exchange().lastFinishedFuture();
Throwable ex = topFut != null ? topFut.validateCache(ctx, recovery, /*read*/
true, null, keys) : null;
if (ex != null)
return new GridFinishedFuture<>(ex);
final Map<K1, V1> map = keysSize == 1 ? (Map<K1, V1>) new IgniteBiTuple<>() : U.<K1, V1>newHashMap(keysSize);
final boolean storeEnabled = !skipVals && readThrough && ctx.readThrough();
boolean readNoEntry = ctx.readNoEntry(expiry, readerArgs != null);
for (KeyCacheObject key : keys) {
while (true) {
try {
EntryGetResult res = null;
boolean evt = !skipVals;
boolean updateMetrics = !skipVals;
GridCacheEntryEx entry = null;
boolean skipEntry = readNoEntry;
if (readNoEntry) {
CacheDataRow row = mvccSnapshot != null ? ctx.offheap().mvccRead(ctx, key, mvccSnapshot) : ctx.offheap().read(ctx, key);
if (row != null) {
long expireTime = row.expireTime();
if (expireTime != 0) {
if (expireTime > U.currentTimeMillis()) {
res = new EntryGetWithTtlResult(row.value(), row.version(), false, expireTime, 0);
} else
skipEntry = false;
} else
res = new EntryGetResult(row.value(), row.version(), false);
}
if (res != null) {
if (evt) {
ctx.events().readEvent(key, null, txLbl, row.value(), taskName, !deserializeBinary);
}
if (updateMetrics && ctx.statisticsEnabled())
ctx.cache().metrics0().onRead(true);
} else if (storeEnabled)
skipEntry = false;
}
if (!skipEntry) {
boolean isNewLocalEntry = this.map.getEntry(ctx, key) == null;
entry = entryEx(key);
if (entry == null) {
if (!skipVals && ctx.statisticsEnabled())
ctx.cache().metrics0().onRead(false);
break;
}
if (isNewLocalEntry) {
if (newLocalEntries == null)
newLocalEntries = new HashSet<>();
newLocalEntries.add(entry);
}
if (storeEnabled) {
res = entry.innerGetAndReserveForLoad(updateMetrics, evt, taskName, expiry, !deserializeBinary, readerArgs);
assert res != null;
if (res.value() == null) {
if (misses == null)
misses = new HashMap<>();
misses.put(key, res);
res = null;
}
} else {
res = entry.innerGetVersioned(null, null, updateMetrics, evt, null, taskName, expiry, !deserializeBinary, readerArgs);
if (res == null)
entry.touch();
}
}
if (res != null) {
ctx.addResult(map, key, res, skipVals, keepCacheObjects, deserializeBinary, true, needVer);
if (entry != null && (tx == null || (!tx.implicit() && tx.isolation() == READ_COMMITTED)))
entry.touch();
if (keysSize == 1)
// Safe to return because no locks are required in READ_COMMITTED mode.
return new GridFinishedFuture<>(map);
}
break;
} catch (GridCacheEntryRemovedException ignored) {
if (log.isDebugEnabled())
log.debug("Got removed entry in getAllAsync(..) method (will retry): " + key);
}
}
}
if (storeEnabled && misses != null) {
final Map<KeyCacheObject, EntryGetResult> loadKeys = misses;
final IgniteTxLocalAdapter tx0 = tx;
final Collection<KeyCacheObject> loaded = new HashSet<>();
return new GridEmbeddedFuture(ctx.closures().callLocalSafe(ctx.projectSafe(new GPC<Map<K1, V1>>() {
@Override
public Map<K1, V1> call() throws Exception {
ctx.store().loadAll(null, /*tx*/
loadKeys.keySet(), new CI2<KeyCacheObject, Object>() {
@Override
public void apply(KeyCacheObject key, Object val) {
EntryGetResult res = loadKeys.get(key);
if (res == null || val == null)
return;
loaded.add(key);
CacheObject cacheVal = ctx.toCacheObject(val);
while (true) {
GridCacheEntryEx entry = null;
try {
ctx.shared().database().ensureFreeSpace(ctx.dataRegion());
} catch (IgniteCheckedException e) {
// Wrap errors (will be unwrapped).
throw new GridClosureException(e);
}
ctx.shared().database().checkpointReadLock();
try {
entry = entryEx(key);
entry.unswap();
GridCacheVersion newVer = nextVersion();
EntryGetResult verVal = entry.versionedValue(cacheVal, res.version(), newVer, expiry, readerArgs);
if (log.isDebugEnabled())
log.debug("Set value loaded from store into entry [" + "oldVer=" + res.version() + ", newVer=" + verVal.version() + ", " + "entry=" + entry + ']');
// Don't put key-value pair into result map if value is null.
if (verVal.value() != null) {
ctx.addResult(map, key, verVal, skipVals, keepCacheObjects, deserializeBinary, true, needVer);
} else {
ctx.addResult(map, key, new EntryGetResult(cacheVal, res.version()), skipVals, keepCacheObjects, deserializeBinary, false, needVer);
}
if (tx0 == null || (!tx0.implicit() && tx0.isolation() == READ_COMMITTED))
entry.touch();
break;
} catch (GridCacheEntryRemovedException ignore) {
if (log.isDebugEnabled())
log.debug("Got removed entry during getAllAsync (will retry): " + entry);
} catch (IgniteCheckedException e) {
// Wrap errors (will be unwrapped).
throw new GridClosureException(e);
} finally {
ctx.shared().database().checkpointReadUnlock();
}
}
}
});
clearReservationsIfNeeded(topVer, loadKeys, loaded, tx0);
return map;
}
}), true), new C2<Map<K, V>, Exception, IgniteInternalFuture<Map<K, V>>>() {
@Override
public IgniteInternalFuture<Map<K, V>> apply(Map<K, V> map, Exception e) {
if (e != null) {
clearReservationsIfNeeded(topVer, loadKeys, loaded, tx0);
return new GridFinishedFuture<>(e);
}
if (tx0 == null || (!tx0.implicit() && tx0.isolation() == READ_COMMITTED)) {
Collection<KeyCacheObject> notFound = new HashSet<>(loadKeys.keySet());
notFound.removeAll(loaded);
// Touch entries that were not found in store.
for (KeyCacheObject key : notFound) {
GridCacheEntryEx entry = peekEx(key);
if (entry != null)
entry.touch();
}
}
// There were no misses.
return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
}
}, new C2<Map<K1, V1>, Exception, Map<K1, V1>>() {
@Override
public Map<K1, V1> apply(Map<K1, V1> loaded, Exception e) {
if (e == null)
map.putAll(loaded);
return map;
}
});
} else
// Misses can be non-zero only if store is enabled.
assert misses == null;
return new GridFinishedFuture<>(map);
} catch (RuntimeException | AssertionError e) {
if (misses != null) {
for (KeyCacheObject key0 : misses.keySet()) {
GridCacheEntryEx entry = peekEx(key0);
if (entry != null)
entry.touch();
}
}
if (newLocalEntries != null) {
for (GridCacheEntryEx entry : newLocalEntries) removeEntry(entry);
}
return new GridFinishedFuture<>(e);
} catch (IgniteCheckedException e) {
return new GridFinishedFuture<>(e);
} finally {
ctx.shared().database().checkpointReadUnlock();
}
} else {
return asyncOp(tx, new AsyncOp<Map<K1, V1>>(keys) {
@Override
public IgniteInternalFuture<Map<K1, V1>> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
return tx.getAllAsync(ctx, readyTopVer, keys, deserializeBinary, skipVals, false, !readThrough, recovery, readRepairStrategy, needVer);
}
}, ctx.operationContextPerCall(), /*retry*/
false);
}
}
use of org.apache.ignite.internal.util.typedef.CI2 in project ignite by apache.
the class CacheJdbcPojoStoreTest method testLoadCache.
/**
* @throws Exception If failed.
*/
@Test
public void testLoadCache() throws Exception {
Connection conn = store.openConnection(false);
PreparedStatement orgStmt = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)");
for (int i = 0; i < ORGANIZATION_CNT; i++) {
orgStmt.setInt(1, i);
orgStmt.setString(2, "name" + i);
orgStmt.setString(3, "city" + i % 10);
orgStmt.addBatch();
}
orgStmt.executeBatch();
U.closeQuiet(orgStmt);
conn.commit();
PreparedStatement prnStmt = conn.prepareStatement("INSERT INTO Person(id, org_id, name) VALUES (?, ?, ?)");
for (int i = 0; i < PERSON_CNT; i++) {
prnStmt.setInt(1, i);
prnStmt.setInt(2, i % 100);
prnStmt.setString(3, "name" + i);
prnStmt.addBatch();
}
prnStmt.executeBatch();
conn.commit();
U.closeQuiet(prnStmt);
PreparedStatement prnComplexStmt = conn.prepareStatement("INSERT INTO Person_Complex(id, org_id, city_id, name, salary) VALUES (?, ?, ?, ?, ?)");
for (int i = 0; i < PERSON_CNT; i++) {
prnComplexStmt.setInt(1, i);
prnComplexStmt.setInt(2, i % 500);
prnComplexStmt.setInt(3, i % 100);
prnComplexStmt.setString(4, "name" + i);
if (i > 0)
prnComplexStmt.setInt(5, 1000 + i * 500);
else
// Add person with null salary
prnComplexStmt.setNull(5, Types.INTEGER);
prnComplexStmt.addBatch();
}
prnComplexStmt.executeBatch();
U.closeQuiet(prnComplexStmt);
conn.commit();
U.closeQuiet(prnStmt);
PreparedStatement binaryStmt = conn.prepareStatement("INSERT INTO Binary_Entries(key, val) VALUES (?, ?)");
byte[] bytes = new byte[16];
for (byte i = 0; i < 16; i++) bytes[i] = i;
binaryStmt.setInt(1, 1);
binaryStmt.setBinaryStream(2, new ByteArrayInputStream(bytes));
binaryStmt.addBatch();
binaryStmt.executeBatch();
U.closeQuiet(binaryStmt);
conn.commit();
U.closeQuiet(conn);
final Collection<Object> orgKeys = new ConcurrentLinkedQueue<>();
final Collection<Object> prnKeys = new ConcurrentLinkedQueue<>();
final Collection<Object> prnComplexKeys = new ConcurrentLinkedQueue<>();
final Collection<Object> binaryTestVals = new ConcurrentLinkedQueue<>();
IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {
@Override
public void apply(Object k, Object v) {
if (binaryEnable) {
if (k instanceof BinaryObject && v instanceof BinaryObject) {
BinaryObject key = (BinaryObject) k;
BinaryObject val = (BinaryObject) v;
String keyType = key.type().typeName();
String valType = val.type().typeName();
if (OrganizationKey.class.getName().equals(keyType) && Organization.class.getName().equals(valType))
orgKeys.add(key);
if (PersonKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
prnKeys.add(key);
if (PersonComplexKey.class.getName().equals(keyType) && Person.class.getName().equals(valType))
prnComplexKeys.add(key);
if (BinaryTestKey.class.getName().equals(keyType) && BinaryTest.class.getName().equals(valType))
binaryTestVals.add(val.field("bytes"));
}
} else {
if (k instanceof OrganizationKey && v instanceof Organization)
orgKeys.add(k);
else if (k instanceof PersonKey && v instanceof Person)
prnKeys.add(k);
else if (k instanceof BinaryTestKey && v instanceof BinaryTest)
binaryTestVals.add(((BinaryTest) v).getBytes());
else if (k instanceof PersonComplexKey && v instanceof Person) {
PersonComplexKey key = (PersonComplexKey) k;
Person val = (Person) v;
assertTrue("Key ID should be the same as value ID", key.getId() == val.getId());
assertTrue("Key orgID should be the same as value orgID", key.getOrgId() == val.getOrgId());
assertEquals("name" + key.getId(), val.getName());
prnComplexKeys.add(k);
}
}
}
};
store.loadCache(c);
assertEquals(ORGANIZATION_CNT, orgKeys.size());
assertEquals(PERSON_CNT, prnKeys.size());
assertEquals(PERSON_CNT, prnComplexKeys.size());
assertEquals(1, binaryTestVals.size());
assertTrue(Arrays.equals(bytes, (byte[]) binaryTestVals.iterator().next()));
Collection<Object> tmpOrgKeys = new ArrayList<>(orgKeys);
Collection<Object> tmpPrnKeys = new ArrayList<>(prnKeys);
Collection<Object> tmpPrnComplexKeys = new ArrayList<>(prnComplexKeys);
orgKeys.clear();
prnKeys.clear();
prnComplexKeys.clear();
store.loadCache(c, OrganizationKey.class.getName(), "SELECT name, city, id FROM ORGANIZATION", PersonKey.class.getName(), "SELECT org_id, id, name FROM Person WHERE id < 1000");
assertEquals(ORGANIZATION_CNT, orgKeys.size());
assertEquals(1000, prnKeys.size());
assertEquals(0, prnComplexKeys.size());
store.deleteAll(tmpOrgKeys);
store.deleteAll(tmpPrnKeys);
store.deleteAll(tmpPrnComplexKeys);
orgKeys.clear();
prnKeys.clear();
prnComplexKeys.clear();
store.loadCache(c);
assertTrue(orgKeys.isEmpty());
assertTrue(prnKeys.isEmpty());
assertTrue(prnComplexKeys.isEmpty());
}
Aggregations