Search in sources :

Example 1 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridDhtPartitionSupplier method clearContext.

/**
     * Clear context.
     *
     * @param sc Supply context.
     * @param log Logger.
     */
private static void clearContext(final SupplyContext sc, final IgniteLogger log) {
    if (sc != null) {
        final Iterator it = sc.entryIt;
        if (it != null && it instanceof GridCloseableIterator && !((GridCloseableIterator) it).isClosed()) {
            try {
                ((GridCloseableIterator) it).close();
            } catch (IgniteCheckedException e) {
                U.error(log, "Iterator close failed.", e);
            }
        }
        final GridDhtLocalPartition loc = sc.loc;
        if (loc != null) {
            assert loc.reservations() > 0;
            loc.release();
        }
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) IgniteRebalanceIterator(org.apache.ignite.internal.processors.cache.IgniteRebalanceIterator) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition)

Example 2 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridUnsafeMap method iterator.

/**
 * {@inheritDoc}
 */
@Override
public <T> GridCloseableIterator<T> iterator(final CX2<T2<Long, Integer>, T2<Long, Integer>, T> c) {
    return new GridCloseableIteratorAdapter<T>() {

        private GridCloseableIterator<T> curIt;

        private int idx;

        {
            try {
                advance();
            } catch (IgniteCheckedException e) {
                // Should never happen.
                e.printStackTrace();
            }
        }

        private void advance() throws IgniteCheckedException {
            curIt = null;
            while (idx < segs.length) {
                curIt = segs[idx++].iterator(c);
                if (curIt.hasNext())
                    return;
                else
                    curIt.close();
            }
            curIt = null;
        }

        @Override
        protected T onNext() throws IgniteCheckedException {
            if (curIt == null)
                throw new NoSuchElementException();
            T t = curIt.next();
            if (!curIt.hasNext()) {
                curIt.close();
                advance();
            }
            return t;
        }

        @Override
        protected boolean onHasNext() {
            return curIt != null;
        }

        @Override
        protected void onRemove() {
            throw new UnsupportedOperationException();
        }

        @Override
        protected void onClose() throws IgniteCheckedException {
            if (curIt != null)
                curIt.close();
        }
    };
}
Also used : GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridUnsafeMap method iterator.

/**
 * {@inheritDoc}
 */
@Override
public GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> iterator() {
    return new GridCloseableIteratorAdapter<IgniteBiTuple<byte[], byte[]>>() {

        private GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> curIt;

        private int idx;

        {
            try {
                advance();
            } catch (IgniteCheckedException e) {
                // Should never happen.
                e.printStackTrace();
            }
        }

        private void advance() throws IgniteCheckedException {
            curIt = null;
            while (idx < segs.length) {
                curIt = segs[idx++].iterator();
                if (curIt.hasNext())
                    return;
                else
                    curIt.close();
            }
            curIt = null;
        }

        @Override
        protected IgniteBiTuple<byte[], byte[]> onNext() throws IgniteCheckedException {
            if (curIt == null)
                throw new NoSuchElementException();
            IgniteBiTuple<byte[], byte[]> t = curIt.next();
            if (!curIt.hasNext()) {
                curIt.close();
                advance();
            }
            return t;
        }

        @Override
        protected boolean onHasNext() {
            return curIt != null;
        }

        @Override
        protected void onRemove() {
            throw new UnsupportedOperationException();
        }

        @Override
        protected void onClose() throws IgniteCheckedException {
            if (curIt != null)
                curIt.close();
        }
    };
}
Also used : GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class GridOffHeapPartitionedMapAbstractSelfTest method testPartitionIteratorMultithreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testPartitionIteratorMultithreaded() throws Exception {
    initCap = 10;
    map = newMap();
    final AtomicInteger rehashes = new AtomicInteger();
    final AtomicInteger releases = new AtomicInteger();
    map.eventListener(new GridOffHeapEventListener() {

        @Override
        public void onEvent(GridOffHeapEvent evt) {
            switch(evt) {
                case REHASH:
                    rehashes.incrementAndGet();
                    break;
                case RELEASE:
                    releases.incrementAndGet();
                    break;
                // No-op.
                default:
            }
        }
    });
    final int max = 64;
    int threads = 5;
    final Map<String, String> m = new ConcurrentHashMap<>(max);
    multithreaded(new Callable() {

        @Override
        public Object call() throws Exception {
            for (int p = 0; p < parts; p++) {
                for (int i = 0; i < max; i++) {
                    String key = string();
                    String val = string();
                    // info("Storing [i=" + i + ", key=" + key + ", val=" + val + ']');
                    m.put(key, val);
                    assertTrue(map.put(p, hash(key), key.getBytes(), val.getBytes()));
                    assertTrue(map.contains(p, hash(key), key.getBytes()));
                    assertNotNull(map.get(p, hash(key), key.getBytes()));
                    assertEquals(new String(map.get(p, hash(key), key.getBytes())), val);
                    try (GridCloseableIterator<IgniteBiTuple<byte[], byte[]>> it = map.iterator(p)) {
                        while (it.hasNext()) {
                            IgniteBiTuple<byte[], byte[]> t = it.next();
                            String k = new String(t.get1());
                            String v = new String(t.get2());
                            // info("Entry [k=" + k + ", v=" + v + ']');
                            assertEquals(m.get(k), v);
                        }
                    }
                }
            }
            return null;
        }
    }, threads);
    assertEquals(max * threads * parts, map.size());
    info("Stats [size=" + map.size() + ", rehashes=" + rehashes + ", releases=" + releases + ']');
    assertTrue(rehashes.get() > 0);
    assertEquals(rehashes.get(), releases.get());
}
Also used : GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Callable(java.util.concurrent.Callable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with GridCloseableIterator

use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.

the class IgniteH2Indexing method queryLocalSql.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(String schemaName, String cacheName, final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException {
    String type = qry.getType();
    String sqlQry = qry.getSql();
    String alias = qry.getAlias();
    Object[] params = qry.getArgs();
    GridQueryCancel cancel = new GridQueryCancel();
    final GridCloseableIterator<IgniteBiTuple<K, V>> i = queryLocalSql(schemaName, cacheName, sqlQry, alias, F.asList(params), type, filter, cancel);
    return new QueryCursorImpl<>(new Iterable<Cache.Entry<K, V>>() {

        @Override
        public Iterator<Cache.Entry<K, V>> iterator() {
            return new ClIter<Cache.Entry<K, V>>() {

                @Override
                public void close() throws Exception {
                    i.close();
                }

                @Override
                public boolean hasNext() {
                    return i.hasNext();
                }

                @Override
                public Cache.Entry<K, V> next() {
                    IgniteBiTuple<K, V> t = i.next();
                    K key = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get1(), keepBinary, false);
                    V val = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get2(), keepBinary, false);
                    return new CacheEntryImpl<>(key, val);
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, cancel);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) Cache(javax.cache.Cache)

Aggregations

GridCloseableIterator (org.apache.ignite.internal.util.lang.GridCloseableIterator)16 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)14 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)6 Test (org.junit.Test)6 NoSuchElementException (java.util.NoSuchElementException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 ArrayList (java.util.ArrayList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Callable (java.util.concurrent.Callable)3 CacheException (javax.cache.CacheException)3 IgniteException (org.apache.ignite.IgniteException)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Collection (java.util.Collection)2