Search in sources :

Example 1 with SQL_SUM

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);
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCompoundIdentityFuture(org.apache.ignite.internal.util.future.GridCompoundIdentityFuture) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Transaction(org.apache.ignite.transactions.Transaction) TRANSACTIONAL_SNAPSHOT(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) SqlQuery(org.apache.ignite.cache.query.SqlQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) X(org.apache.ignite.internal.util.typedef.X) Cache(javax.cache.Cache) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) WALMode(org.apache.ignite.configuration.WALMode) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) StopNodeFailureHandler(org.apache.ignite.failure.StopNodeFailureHandler) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) SQL_SUM(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL_SUM) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) DML(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.WriteMode.DML) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) SQL(org.apache.ignite.internal.processors.cache.mvcc.CacheMvccAbstractTest.ReadMode.SQL) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) GridKernalContext(org.apache.ignite.internal.GridKernalContext) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) LinkedHashSet(java.util.LinkedHashSet) G(org.apache.ignite.internal.util.typedef.G) F(org.apache.ignite.internal.util.typedef.F) Iterator(java.util.Iterator) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) Ignite(org.apache.ignite.Ignite) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) REPLICATED(org.apache.ignite.cache.CacheMode.REPLICATED) SF(org.apache.ignite.testframework.GridTestUtils.SF) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) TreeMap(java.util.TreeMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Collections(java.util.Collections) IgniteTransactions(org.apache.ignite.IgniteTransactions) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) CacheMode(org.apache.ignite.cache.CacheMode) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Callable (java.util.concurrent.Callable)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1