try (CloseableIterator<Map.Entry<T, Set<Address>>> entries = this.cache.entrySet().iterator()) { while (entries.hasNext()) { Map.Entry<T, Set<Address>> entry = entries.next();
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(); } } } } } } }
@SuppressWarnings({ "rawtypes", "unchecked" }) public Map getAllElements() { CloseableIterator<Map.Entry<Object, Object>> values = getJBossCache().entrySet().iterator(); Map output = new HashMap(); while (values.hasNext()) { Map.Entry<Object, Object> curr = values.next(); output.put(curr.getKey(), curr.getValue()); } return output; }
@SuppressWarnings({ "rawtypes", "unchecked" }) public Set getAllKeys() { CloseableIterator<Object> values = getJBossCache().keySet().iterator(); Set output = new HashSet(); while (values.hasNext()) { output.add(values.next()); } return output; }
@Override public void removeJobExecutions(final JobExecutionSelector jobExecutionSelector) { final Collection<Long> allJobExecutionIds = jobExecutionCache.keySet(); for (final Iterator<Map.Entry<Long, JobExecutionImpl>> it = jobExecutionCache.entrySet().iterator(); it.hasNext(); ) { final JobExecutionImpl je = it.next().getValue(); if (je != null && (jobExecutionSelector == null || jobExecutionSelector.select(je, allJobExecutionIds))) { if (je.getJobParameters() != null) { je.getJobParameters().clear(); } BatchLogger.LOGGER.removing(JobExecution.class.getName(), String.valueOf(je.getExecutionId())); it.remove(); } } }
@Override public ByteBuf bulkGetResponse(HotRodHeader header, HotRodServer server, ByteBufAllocator alloc, int size, CacheSet<Map.Entry<byte[], byte[]>> entries) { ByteBuf buf = writeHeader(header, server, alloc, OperationStatus.Success); try (CloseableIterator<Map.Entry<byte[], byte[]>> iterator = entries.iterator()) { int max = Integer.MAX_VALUE; if (size != 0) { if (trace) log.tracef("About to write (max) %d messages to the client", size); max = size; } int count = 0; while (iterator.hasNext() && count < max) { Map.Entry<byte[], byte[]> entry = iterator.next(); buf.writeByte(1); // Not done ExtendedByteBuf.writeRangedBytes(entry.getKey(), buf); ExtendedByteBuf.writeRangedBytes(entry.getValue(), buf); count++; } buf.writeByte(0); // Done } return buf; }
public static void removeAll(AdvancedCache cache) { CloseableIterator it = cache.keySet().iterator(); try { while (it.hasNext()) { // Cannot use it.next(); it.remove() due to ISPN-5653 cache.remove(it.next()); } } finally { it.close(); } }
@Override void perform(MagicKey key, Cache<MagicKey, String> cache) { Iterator iterator = cache.keySet().iterator(); assertTrue(iterator.hasNext()); assertEquals(key, iterator.next()); assertFalse(iterator.hasNext()); } },
/** * Brings all data from the distributed cache into our local cache. */ private void populateLocalCache() { CloseableIterator iterator = cache.keySet().iterator(); try { while (iterator.hasNext()) { get(null, iterator.next()); } } finally { iterator.close(); } }
@Override @SuppressWarnings("unchecked") public Object call() throws IOException, ClassNotFoundException { Cache<Object, Object> cache = (Cache<Object, Object>) taskContext.getCache().get(); Map.Entry<Object, Object> entry = cache.entrySet().iterator().next(); cache.getCacheManager().getCache(CACHE_NAME).put(entry.getKey(), MODIFIED_PREFIX + entry.getValue()); return null; }
private void bulkGetKeysInternal(HotRodHeader header, AdvancedCache<byte[], byte[]> cache, int scope) { try { if (trace) { log.tracef("About to create bulk get keys response scope = %d", scope); } writeResponse(header, header.encoder().bulkGetKeysResponse(header, server, channel.alloc(), cache.keySet().iterator())); } catch (Throwable t) { writeException(header, t); } }
@Test public void simpleTest() { Map<Object, String> values = putValuesInCache(); Cache<MagicKey, String> cache = cache(0, CACHE_NAME); Iterator<Map.Entry<MagicKey, String>> iterator = cache.entrySet().iterator(); Map<MagicKey, String> results = mapFromIterator(iterator); assertEquals(values, results); }
@Override @SuppressWarnings("unchecked") public Object call() throws Exception { Cache<Object, Object> cache = (Cache<Object, Object>) taskContext.getCache().get() .getAdvancedCache() .withEncoding(IdentityEncoder.class); Map<String, String> parameters = (Map<String, String>) taskContext.getParameters().get(); assert taskContext.getMarshaller().isPresent(); Map.Entry<Object, Object> entry = cache.entrySet().iterator().next(); TransactionManager transactionManager = cache.getAdvancedCache().getTransactionManager(); transactionManager.begin(); cache.getAdvancedCache().lock(entry.getKey()); cache.put(entry.getKey(), VALUE_PREFIX + entry.getValue() + ":" + parameters.get(PARAM_KEY)); transactionManager.commit(); return null; }
@Test public void testEntrySetRemove() { Map<Object, String> values = putValuesInCache(); final Cache<Object, Object> cache = cache(0, CACHE_NAME); cache.getAdvancedCache().addInterceptor(new BaseCustomInterceptor() { @Override public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable { assertTrue(command.hasAnyFlag(FlagBitSets.SKIP_CACHE_STORE)); return super.visitRemoveCommand(ctx, command); } }, 0); for (Iterator<Map.Entry<Object, Object>> it = cache(0, CACHE_NAME).getAdvancedCache().withFlags( Flag.SKIP_CACHE_STORE).entrySet().iterator(); it.hasNext();) { Map.Entry<Object, Object> entry = it.next(); Object key = entry.getKey(); assertEquals(values.get(key), entry.getValue()); it.remove(); } assertEquals(0, cache.size()); }
@Test public void testKeySetRemove() { Map<Object, String> values = putValuesInCache(); final Cache<Object, Object> cache = cache(0, CACHE_NAME); cache.getAdvancedCache().addInterceptor(new BaseCustomInterceptor() { @Override public Object visitRemoveCommand(InvocationContext ctx, RemoveCommand command) throws Throwable { assertTrue(command.hasAnyFlag(FlagBitSets.SKIP_CACHE_STORE)); return super.visitRemoveCommand(ctx, command); } }, 0); for (Iterator<Object> it = cache(0, CACHE_NAME).getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).keySet().iterator(); it.hasNext();) { assertTrue(values.containsKey(it.next())); it.remove(); } assertEquals(0, cache.size()); }
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 testNoTenancy() throws Exception { final Item item = new Item("my item", "description" ); withTxSession(s -> s.persist(item)); for (int i = 0; i < 5; ++i) { // make sure we get something cached withTxSession(s -> { Item item2 = s.get(Item.class, item.getId()); assertNotNull(item2); assertEquals(item.getName(), item2.getName()); }); } InfinispanBaseRegion region = TEST_SESSION_ACCESS.getRegion(sessionFactory(), Item.class.getName()); AdvancedCache localCache = region.getCache().withFlags(Flag.CACHE_MODE_LOCAL); assertEquals(1, localCache.size()); try (CloseableIterator iterator = localCache.keySet().iterator()) { assertEquals(sessionFactory().getClassMetadata(Item.class).getIdentifierType().getReturnedClass(), iterator.next().getClass()); } } }
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 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()); }
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()); }