Search in sources :

Example 1 with UpdateSourceIterator

use of org.apache.ignite.internal.processors.query.UpdateSourceIterator in project ignite by apache.

the class IgniteH2Indexing method lockSelectedRows.

/**
 * Locks rows from query cursor and returns the select result.
 *
 * @param cur Query cursor.
 * @param cctx Cache context.
 * @param pageSize Page size.
 * @param timeout Timeout.
 * @return Query results cursor.
 */
private Iterable<List<?>> lockSelectedRows(Iterable<List<?>> cur, GridCacheContext cctx, int pageSize, long timeout) {
    assert cctx != null && cctx.mvccEnabled();
    GridNearTxLocal tx = tx(ctx);
    if (tx == null)
        throw new IgniteSQLException("Failed to perform SELECT FOR UPDATE operation: transaction has already finished.");
    Collection<List<?>> rowsCache = new ArrayList<>();
    UpdateSourceIterator srcIt = new UpdateSourceIterator<KeyCacheObject>() {

        private Iterator<List<?>> it = cur.iterator();

        @Override
        public EnlistOperation operation() {
            return EnlistOperation.LOCK;
        }

        @Override
        public boolean hasNextX() throws IgniteCheckedException {
            return it.hasNext();
        }

        @Override
        public KeyCacheObject nextX() throws IgniteCheckedException {
            List<?> res = it.next();
            // nextX() can be called from the different threads.
            synchronized (rowsCache) {
                rowsCache.add(res.subList(0, res.size() - 1));
                if (rowsCache.size() > TX_SIZE_THRESHOLD) {
                    throw new IgniteCheckedException("Too many rows are locked by SELECT FOR UPDATE statement. " + "Consider locking fewer keys or increase the limit by setting a " + IGNITE_MVCC_TX_SIZE_CACHING_THRESHOLD + " system property. Current value is " + TX_SIZE_THRESHOLD + " rows.");
                }
            }
            // The last column is expected to be a _key.
            return cctx.toCacheKeyObject(res.get(res.size() - 1));
        }
    };
    IgniteInternalFuture<Long> fut = tx.updateAsync(cctx, srcIt, pageSize, timeout, true);
    try {
        fut.get();
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
    return rowsCache;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) DmlUpdateSingleEntryIterator(org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateSingleEntryIterator) DmlUpdateResultsIterator(org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateResultsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)1 GridQueryCacheObjectsIterator (org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator)1 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)1 UpdateSourceIterator (org.apache.ignite.internal.processors.query.UpdateSourceIterator)1 DmlUpdateResultsIterator (org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateResultsIterator)1 DmlUpdateSingleEntryIterator (org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateSingleEntryIterator)1 GridEmptyCloseableIterator (org.apache.ignite.internal.util.GridEmptyCloseableIterator)1 GridCloseableIterator (org.apache.ignite.internal.util.lang.GridCloseableIterator)1 IgniteSingletonIterator (org.apache.ignite.internal.util.lang.IgniteSingletonIterator)1