Search in sources :

Example 1 with GridCursor

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

the class IgniteCacheOffheapManagerImpl method rowsIterator.

/**
     * @param primary Primary entries flag.
     * @param backups Backup entries flag.
     * @param topVer Topology version.
     * @return Iterator.
     */
private GridIterator<CacheDataRow> rowsIterator(boolean primary, boolean backups, AffinityTopologyVersion topVer) {
    final Iterator<CacheDataStore> dataIt = cacheData(primary, backups, topVer);
    return new GridCloseableIteratorAdapter<CacheDataRow>() {

        /** */
        private GridCursor<? extends CacheDataRow> cur;

        /** */
        private int curPart;

        /** */
        private CacheDataRow next;

        @Override
        protected CacheDataRow onNext() {
            CacheDataRow res = next;
            next = null;
            return res;
        }

        @Override
        protected boolean onHasNext() throws IgniteCheckedException {
            if (next != null)
                return true;
            while (true) {
                if (cur == null) {
                    if (dataIt.hasNext()) {
                        CacheDataStore ds = dataIt.next();
                        curPart = ds.partId();
                        cur = ds.cursor();
                    } else
                        break;
                }
                if (cur.next()) {
                    next = cur.get();
                    next.key().partition(curPart);
                    break;
                } else
                    cur = null;
            }
            return next != null;
        }
    };
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter)

Example 2 with GridCursor

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

the class BPlusTreeSelfTest method doTestRandomPutRemoveMultithreaded.

/**
     * @param canGetRow Can get row from inner page.
     * @throws Exception If failed.
     */
private void doTestRandomPutRemoveMultithreaded(boolean canGetRow) throws Exception {
    final TestTree tree = createTestTree(canGetRow);
    final Map<Long, Long> map = new ConcurrentHashMap8<>();
    final int loops = reuseList == null ? 20_000 : 60_000;
    final GridStripedLock lock = new GridStripedLock(256);
    final String[] ops = { "put", "rmv", "inv_put", "inv_rmv" };
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < loops; i++) {
                final Long x = (long) DataStructure.randomInt(CNT);
                final int op = DataStructure.randomInt(4);
                if (i % 10000 == 0)
                    X.println(" --> " + ops[op] + "_" + i + "  " + x);
                Lock l = lock.getLock(x.longValue());
                l.lock();
                try {
                    if (op == 0) {
                        // Put.
                        assertEquals(map.put(x, x), tree.put(x));
                        assertNoLocks();
                    } else if (op == 1) {
                        // Remove.
                        if (map.remove(x) != null) {
                            assertEquals(x, tree.remove(x));
                            assertNoLocks();
                        }
                        assertNull(tree.remove(x));
                        assertNoLocks();
                    } else if (op == 2) {
                        tree.invoke(x, null, new IgniteTree.InvokeClosure<Long>() {

                            IgniteTree.OperationType opType;

                            @Override
                            public void call(@Nullable Long row) {
                                opType = PUT;
                                if (row != null)
                                    assertEquals(x, row);
                            }

                            @Override
                            public Long newRow() {
                                return x;
                            }

                            @Override
                            public IgniteTree.OperationType operationType() {
                                return opType;
                            }
                        });
                        map.put(x, x);
                    } else if (op == 3) {
                        tree.invoke(x, null, new IgniteTree.InvokeClosure<Long>() {

                            IgniteTree.OperationType opType;

                            @Override
                            public void call(@Nullable Long row) {
                                if (row != null) {
                                    assertEquals(x, row);
                                    opType = REMOVE;
                                } else
                                    opType = NOOP;
                            }

                            @Override
                            public Long newRow() {
                                return null;
                            }

                            @Override
                            public IgniteTree.OperationType operationType() {
                                return opType;
                            }
                        });
                        map.remove(x);
                    } else
                        fail();
                } finally {
                    l.unlock();
                }
            }
            return null;
        }
    }, Runtime.getRuntime().availableProcessors(), "put-remove");
    final AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stop.get()) {
                Thread.sleep(5000);
                X.println(TestTree.printLocks());
            }
            return null;
        }
    }, 1, "printLocks");
    IgniteInternalFuture<?> fut3 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stop.get()) {
                int low = DataStructure.randomInt(CNT);
                int high = low + DataStructure.randomInt(CNT - low);
                GridCursor<Long> c = tree.find((long) low, (long) high);
                Long last = null;
                while (c.next()) {
                    // Correct bounds.
                    assertTrue(low + " <= " + c.get() + " <= " + high, c.get() >= low);
                    assertTrue(low + " <= " + c.get() + " <= " + high, c.get() <= high);
                    if (// No duplicates.
                    last != null)
                        assertTrue(low + " <= " + last + " < " + c.get() + " <= " + high, c.get() > last);
                    last = c.get();
                }
            }
            return null;
        }
    }, 4, "find");
    try {
        fut.get(getTestTimeout(), TimeUnit.MILLISECONDS);
    } finally {
        stop.set(true);
        fut2.get();
        fut3.get();
    }
    GridCursor<Long> cursor = tree.find(null, null);
    while (cursor.next()) {
        Long x = cursor.get();
        assert x != null;
        assertEquals(map.get(x), x);
    }
    info("size: " + map.size());
    assertEquals(map.size(), tree.size());
    tree.validateTree();
    assertNoLocks();
}
Also used : GridCursor(org.apache.ignite.internal.util.lang.GridCursor) GridStripedLock(org.apache.ignite.internal.util.GridStripedLock) ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridStripedLock(org.apache.ignite.internal.util.GridStripedLock) Lock(java.util.concurrent.locks.Lock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteTree(org.apache.ignite.internal.util.IgniteTree) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GridCursor

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

the class H2PkHashIndex method find.

/** {@inheritDoc} */
@Override
public Cursor find(Session ses, final SearchRow lower, final SearchRow upper) {
    IndexingQueryFilter f = threadLocalFilter();
    IgniteBiPredicate<Object, Object> p = null;
    if (f != null) {
        String cacheName = getTable().cacheName();
        p = f.forCache(cacheName);
    }
    KeyCacheObject lowerObj = null;
    KeyCacheObject upperObj = null;
    if (lower != null)
        lowerObj = cctx.toCacheKeyObject(lower.getValue(0).getObject());
    if (upper != null)
        upperObj = cctx.toCacheKeyObject(upper.getValue(0).getObject());
    try {
        List<GridCursor<? extends CacheDataRow>> cursors = new ArrayList<>();
        for (IgniteCacheOffheapManager.CacheDataStore store : cctx.offheap().cacheDataStores()) cursors.add(store.cursor(lowerObj, upperObj));
        return new H2Cursor(new CompositeGridCursor<>(cursors.iterator()), p);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

GridCursor (org.apache.ignite.internal.util.lang.GridCursor)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 CacheDataRow (org.apache.ignite.internal.processors.cache.database.CacheDataRow)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Lock (java.util.concurrent.locks.Lock)1 IgniteCacheOffheapManager (org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 GridCloseableIteratorAdapter (org.apache.ignite.internal.util.GridCloseableIteratorAdapter)1 GridStripedLock (org.apache.ignite.internal.util.GridStripedLock)1 IgniteTree (org.apache.ignite.internal.util.IgniteTree)1 IndexingQueryFilter (org.apache.ignite.spi.indexing.IndexingQueryFilter)1 Nullable (org.jetbrains.annotations.Nullable)1 ConcurrentHashMap8 (org.jsr166.ConcurrentHashMap8)1