try (Batch batch = this.batcher.createBatch()) { try (CloseableIterator<Map.Entry<T, Set<Address>>> entries = this.cache.entrySet().iterator()) { while (entries.hasNext()) { Map.Entry<T, Set<Address>> entry = entries.next(); Set<Address> addresses = entry.getValue(); if (addresses.removeAll(leftMembers)) {
@Override public void close() { if (iterator instanceof CloseableIterator) { ((CloseableIterator) iterator).close(); } } }
public void testKeySetIteratorRemoveInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { for (CloseableIterator<Object> entryIterator = cache.keySet().iterator(); entryIterator.hasNext(); ) { entryIterator.next(); entryIterator.remove(); assertEquals(0, cache.size()); } tm.setRollbackOnly(); return null; }); }
public Object next() { return decode(iterator.next()); } public void remove() {
@Override public boolean hasNext() { return iterator.hasNext(); }
public void testEntrySetIteratorRemoveInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { try (CloseableIterator<Entry<Object, Object>> entryIterator = cache.entrySet().iterator()) { entryIterator.next(); entryIterator.remove(); assertEquals(0, cache.size()); } tm.setRollbackOnly(); return null; }); }
@Test public void testIterationWithPrimitiveValues() { remoteCache.clear(); IntStream.range(0, CACHE_SIZE).forEach(k -> remoteCache.put(k, "value" + k)); Set<Object> keys = new HashSet<>(); try (CloseableIterator<Entry<Object, Object>> iter = remoteCache.retrieveEntries(null, 10)) { iter.forEachRemaining(e -> keys.add(e.getKey())); } assertEquals(CACHE_SIZE, keys.size()); } }
public void testEntrySetIteratorRemoveContextEntryInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { // This should be removed by iterator as well as the k1 entry cache.put("k2", "v2"); assertEquals(2, cache.size()); for (CloseableIterator<Entry<Object, Object>> entryIterator = cache.entrySet().iterator(); entryIterator.hasNext(); ) { entryIterator.next(); entryIterator.remove(); } assertEquals(0, cache.size()); tm.setRollbackOnly(); return null; }); assertEquals(1, cache.size()); }
@Override public Tuple next() { Entry<Object, MetadataValue<Object>> next = iterator.next(); VersionedTuple tuple = createTuple( next ); return tuple; }
@Override public boolean hasNext() { return entryIterator.hasNext(); }
@Test public void testIterationWithCustomClasses() { remoteCache.put("1", new SampleEntity("value1,value2")); remoteCache.put("2", new SampleEntity("value3,value2")); remoteCache.put("ignore", new SampleEntity("whatever")); remoteCache.put("3", new SampleEntity("value7,value8")); final Map<Object, Object> entryMap = new HashMap<>(); try(CloseableIterator<Entry<Object, Object>> closeableIterator = remoteCache.retrieveEntries("csv-key-value-filter-converter-factory", null, 10)) { closeableIterator.forEachRemaining(new Consumer<Entry<Object, Object>>() { @Override public void accept(Entry<Object, Object> e) { entryMap.put(e.getKey(), e.getValue()); } }); } assertEquals(3, entryMap.size()); assertEquals(Arrays.asList("value1","value2"), ((Summary) entryMap.get("1")).getAttributes()); assertEquals(Arrays.asList("value3","value2"), ((Summary) entryMap.get("2")).getAttributes()); assertEquals(Arrays.asList("value7","value8"), ((Summary) entryMap.get("3")).getAttributes()); }
private void schedule(Locality oldLocality, Locality newLocality) { SessionMetaDataFactory<InfinispanSessionMetaData<L>, L> metaDataFactory = this.factory.getMetaDataFactory(); // Iterate over sessions in memory try (CloseableIterator<Key<String>> keys = this.cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).keySet().iterator()) { while (keys.hasNext()) { if (Thread.currentThread().isInterrupted()) break; Key<String> key = keys.next(); // If we are the new primary owner of this session then schedule expiration of this session locally if (this.filter.test(key) && !oldLocality.isLocal(key) && newLocality.isLocal(key)) { String id = key.getValue(); try (Batch batch = this.batcher.createBatch()) { try { // We need to lookup the session to obtain its meta data InfinispanSessionMetaData<L> value = metaDataFactory.tryValue(id); if (value != null) { this.scheduler.schedule(id, metaDataFactory.createImmutableSessionMetaData(id, value)); } return; } catch (CacheException e) { batch.discard(); } } } } } } }
public void testKeySetIteratorRemoveContextEntryInExplicitTx() throws Exception { assertEquals(0, cache.size()); cache.put("k1", "v1"); TransactionManager tm = TestingUtil.getTransactionManager(cache); withTx(tm, () -> { // This should be removed by iterator as well as the k1 entry cache.put("k2", "v2"); assertEquals(2, cache.size()); for (CloseableIterator<Object> keyIterator = cache.keySet().iterator(); keyIterator.hasNext(); ) { keyIterator.next(); keyIterator.remove(); } assertEquals(0, cache.size()); tm.setRollbackOnly(); return null; }); assertEquals(1, cache.size()); }
@Override public T next() { return selector.apply(entryIterator.next()); }
public boolean hasNext() { return iterator.hasNext(); } public Object next() {
@Override public void close() { this.iterator.close(); } }