Search in sources :

Example 1 with SetItemKey

use of org.apache.ignite.internal.processors.datastructures.SetItemKey in project ignite by apache.

the class GridCacheQueryManager method sharedCacheSetIterator.

/**
 * @param qry Query.
 * @return Cache set items iterator.
 */
private GridCloseableIterator<IgniteBiTuple<K, V>> sharedCacheSetIterator(GridCacheQueryAdapter<?> qry) throws IgniteCheckedException {
    final GridSetQueryPredicate filter = (GridSetQueryPredicate) qry.scanFilter();
    IgniteUuid id = filter.setId();
    GridCacheQueryAdapter<CacheEntry<K, ?>> qry0 = new GridCacheQueryAdapter<>(cctx, SCAN, new IgniteBiPredicate<Object, Object>() {

        @Override
        public boolean apply(Object k, Object v) {
            return k instanceof SetItemKey && id.equals(((SetItemKey) k).setId());
        }
    }, new IgniteClosure<Map.Entry, Object>() {

        @Override
        public Object apply(Map.Entry entry) {
            return new IgniteBiTuple<K, V>((K) ((SetItemKey) entry.getKey()).item(), (V) Boolean.TRUE);
        }
    }, qry.partition(), false, true, qry.isDataPageScanEnabled());
    return scanQueryLocal(qry0, false);
}
Also used : CacheEntry(org.apache.ignite.cache.CacheEntry) CacheEntry(org.apache.ignite.cache.CacheEntry) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridSetQueryPredicate(org.apache.ignite.internal.processors.datastructures.GridSetQueryPredicate) SetItemKey(org.apache.ignite.internal.processors.datastructures.SetItemKey) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap)

Example 2 with SetItemKey

use of org.apache.ignite.internal.processors.datastructures.SetItemKey in project ignite by apache.

the class GridCacheSetFailoverAbstractSelfTest method testNodeRestart.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("WhileLoopReplaceableByForEach")
@Test
public void testNodeRestart() throws Exception {
    IgniteSet<Integer> set = grid(0).set(SET_NAME, config(false));
    final int ITEMS = 10_000;
    Collection<Integer> items = new ArrayList<>(ITEMS);
    for (int i = 0; i < ITEMS; i++) items.add(i);
    set.addAll(items);
    assertEquals(ITEMS, set.size());
    AtomicBoolean stop = new AtomicBoolean();
    IgniteInternalFuture<?> killFut = startNodeKiller(stop);
    long stopTime = System.currentTimeMillis() + TEST_DURATION;
    try {
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        while (System.currentTimeMillis() < stopTime) {
            for (int i = 0; i < 10; i++) {
                try {
                    int size = set.size();
                    assertEquals(ITEMS, size);
                } catch (IgniteException ignore) {
                // No-op.
                }
                try {
                    Iterator<Integer> iter = set.iterator();
                    int cnt = 0;
                    while (iter.hasNext()) {
                        assertNotNull(iter.next());
                        cnt++;
                    }
                    assertEquals(ITEMS, cnt);
                } catch (IgniteException ignore) {
                // No-op.
                }
                int val = rnd.nextInt(ITEMS);
                assertTrue("Not contains: " + val, set.contains(val));
                val = ITEMS + rnd.nextInt(ITEMS);
                assertFalse("Contains: " + val, set.contains(val));
            }
            log.info("Remove set.");
            set.close();
            log.info("Create new set.");
            set = grid(0).set(SET_NAME, config(false));
            set.addAll(items);
        }
    } finally {
        stop.set(true);
    }
    killFut.get();
    set.close();
    if (false) {
        // TODO IGNITE-600: enable check when fixed.
        int cnt = 0;
        Set<IgniteUuid> setIds = new HashSet<>();
        for (int i = 0; i < gridCount(); i++) {
            GridCacheAdapter cache = grid(i).context().cache().internalCache(DEFAULT_CACHE_NAME);
            Iterator<GridCacheMapEntry> entries = cache.map().entries(cache.context().cacheId()).iterator();
            while (entries.hasNext()) {
                GridCacheEntryEx entry = entries.next();
                if (entry.hasValue()) {
                    cnt++;
                    if (entry.key() instanceof SetItemKey) {
                        SetItemKey setItem = (SetItemKey) entry.key();
                        if (setIds.add(setItem.setId()))
                            log.info("Unexpected set item [setId=" + setItem.setId() + ", grid: " + grid(i).name() + ", entry=" + entry + ']');
                    }
                }
            }
        }
        assertEquals("Found unexpected cache entries", 0, cnt);
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteException(org.apache.ignite.IgniteException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) SetItemKey(org.apache.ignite.internal.processors.datastructures.SetItemKey) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GridCacheMapEntry(org.apache.ignite.internal.processors.cache.GridCacheMapEntry) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with SetItemKey

use of org.apache.ignite.internal.processors.datastructures.SetItemKey in project ignite by apache.

the class CacheDataStructuresManager method removeSetData.

/**
 * @param setId Set ID.
 * @param topVer Topology version.
 * @throws IgniteCheckedException If failed.
 */
private void removeSetData(IgniteUuid setId, AffinityTopologyVersion topVer) throws IgniteCheckedException {
    boolean loc = cctx.isLocal();
    GridCacheAffinityManager aff = cctx.affinity();
    if (!loc) {
        aff.affinityReadyFuture(topVer).get();
        cctx.preloader().syncFuture().get();
    }
    IgniteInternalCache<?, ?> cache = cctx.cache();
    final int BATCH_SIZE = 100;
    Collection<SetItemKey> keys = new ArrayList<>(BATCH_SIZE);
    for (Cache.Entry entry : cache.localEntries(new CachePeekMode[] { CachePeekMode.PRIMARY })) {
        Object obj = entry.getKey();
        if (!(obj instanceof SetItemKey && setId.equals(((SetItemKey) obj).setId())))
            continue;
        keys.add((SetItemKey) obj);
        if (keys.size() == BATCH_SIZE) {
            retryRemoveAll(cache, keys);
            keys.clear();
        }
    }
    if (!keys.isEmpty())
        retryRemoveAll(cache, keys);
}
Also used : GridCacheAffinityManager(org.apache.ignite.internal.processors.cache.GridCacheAffinityManager) SetItemKey(org.apache.ignite.internal.processors.datastructures.SetItemKey) ArrayList(java.util.ArrayList) BinaryObject(org.apache.ignite.binary.BinaryObject) Cache(javax.cache.Cache) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache)

Example 4 with SetItemKey

use of org.apache.ignite.internal.processors.datastructures.SetItemKey in project ignite by apache.

the class GridCacheQueryManager method setIterator.

/**
 * @param qry Query.
 * @return Cache set items iterator.
 */
private GridCloseableIterator<IgniteBiTuple<K, V>> setIterator(GridCacheQueryAdapter<?> qry) {
    final GridSetQueryPredicate filter = (GridSetQueryPredicate) qry.scanFilter();
    filter.init(cctx);
    IgniteUuid id = filter.setId();
    Collection<SetItemKey> data = cctx.dataStructures().setData(id);
    if (data == null)
        data = Collections.emptyList();
    final GridIterator<IgniteBiTuple<K, V>> it = F.iterator(data, new C1<SetItemKey, IgniteBiTuple<K, V>>() {

        @Override
        public IgniteBiTuple<K, V> apply(SetItemKey e) {
            return new IgniteBiTuple<>((K) e.item(), (V) Boolean.TRUE);
        }
    }, true, new P1<SetItemKey>() {

        @Override
        public boolean apply(SetItemKey e) {
            return filter.apply(e, null);
        }
    });
    return new GridCloseableIteratorAdapter<IgniteBiTuple<K, V>>() {

        @Override
        protected boolean onHasNext() {
            return it.hasNext();
        }

        @Override
        protected IgniteBiTuple<K, V> onNext() {
            return it.next();
        }

        @Override
        protected void onRemove() {
            it.remove();
        }

        @Override
        protected void onClose() {
        // No-op.
        }
    };
}
Also used : GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridSetQueryPredicate(org.apache.ignite.internal.processors.datastructures.GridSetQueryPredicate) SetItemKey(org.apache.ignite.internal.processors.datastructures.SetItemKey)

Aggregations

SetItemKey (org.apache.ignite.internal.processors.datastructures.SetItemKey)4 IgniteUuid (org.apache.ignite.lang.IgniteUuid)3 ArrayList (java.util.ArrayList)2 GridSetQueryPredicate (org.apache.ignite.internal.processors.datastructures.GridSetQueryPredicate)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Cache (javax.cache.Cache)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 CacheEntry (org.apache.ignite.cache.CacheEntry)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 GridCacheAdapter (org.apache.ignite.internal.processors.cache.GridCacheAdapter)1 GridCacheAffinityManager (org.apache.ignite.internal.processors.cache.GridCacheAffinityManager)1 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)1