use of org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL_SUM in project ignite by apache.
the class CacheMvccAbstractTest method readAllByMode.
/**
* Reads value from cache for the given key using given read mode.
*
* @param cache Cache.
* @param keys Key.
* @param readMode Read mode.
* @param codec Value codec.
* @return Value.
*/
@SuppressWarnings("unchecked")
protected Map readAllByMode(IgniteCache cache, Set keys, ReadMode readMode, ObjectCodec codec) {
assert cache != null && keys != null && readMode != null;
assert readMode != SQL || codec != null;
boolean emulateLongQry = ThreadLocalRandom.current().nextBoolean();
switch(readMode) {
case GET:
return cache.getAll(keys);
case SCAN:
ScanQuery scanQry = new ScanQuery(new IgniteBiPredicate() {
@Override
public boolean apply(Object k, Object v) {
if (emulateLongQry)
doSleep(ThreadLocalRandom.current().nextInt(50));
return keys.contains(k);
}
});
Map res;
try (QueryCursor qry = cache.query(scanQry)) {
res = (Map) qry.getAll().stream().collect(Collectors.toMap(v -> ((IgniteBiTuple) v).getKey(), v -> ((IgniteBiTuple) v).getValue()));
assertTrue("res.size()=" + res.size() + ", keys.size()=" + keys.size(), res.size() <= keys.size());
}
return res;
case SQL:
StringBuilder b = new StringBuilder("SELECT " + codec.columnsNames() + " FROM " + codec.tableName() + " WHERE _key IN (");
boolean first = true;
for (Object key : keys) {
if (first)
first = false;
else
b.append(", ");
b.append(key);
}
b.append(')');
String qry = b.toString();
SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(qry);
if (emulateLongQry)
sqlFieldsQry.setLazy(true).setPageSize(1);
List<List> rows;
try (FieldsQueryCursor<List> cur = cache.query(sqlFieldsQry)) {
if (emulateLongQry) {
rows = new ArrayList<>();
for (List row : cur) {
rows.add(row);
doSleep(ThreadLocalRandom.current().nextInt(50));
}
} else
rows = cur.getAll();
}
if (rows.isEmpty())
return Collections.emptyMap();
res = new HashMap();
for (List row : rows) res.put(row.get(0), codec.decode(row));
return res;
case SQL_SUM:
b = new StringBuilder("SELECT SUM(" + codec.aggregateColumnName() + ") FROM " + codec.tableName() + " WHERE _key IN (");
first = true;
for (Object key : keys) {
if (first)
first = false;
else
b.append(", ");
b.append(key);
}
b.append(')');
qry = b.toString();
FieldsQueryCursor<List> cur = cache.query(new SqlFieldsQuery(qry));
rows = cur.getAll();
if (rows.isEmpty())
return Collections.emptyMap();
res = new HashMap();
for (List row : rows) res.put(row.get(0), row.get(0));
return res;
default:
throw new AssertionError("Unsupported read mode: " + readMode);
}
}
Aggregations