@Override public int getActiveCount() { try (Stream<Map.Entry<BeanKey<I>, BeanEntry<I>>> entries = this.cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).entrySet().stream()) { return (int) entries.filter(this.filter).count(); } }
void schedule(Locality oldLocality, Locality newLocality) { if (this.dispatcher != null) { // Iterate over beans in memory try (Stream<Map.Entry<BeanKey<I>, BeanEntry<I>>> stream = this.cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).entrySet().stream().filter(this.filter)) { Iterator<Map.Entry<BeanKey<I>, BeanEntry<I>>> entries = stream.iterator(); while (entries.hasNext()) { if (Thread.currentThread().isInterrupted()) break; Map.Entry<BeanKey<I>, BeanEntry<I>> entry = entries.next(); BeanKey<I> key = entry.getKey(); // If we are the new primary owner of this bean then schedule expiration of this bean locally if (this.filter.test(entry) && !oldLocality.isLocal(key) && newLocality.isLocal(key)) { this.scheduler.schedule(key.getId()); } } } } }
/** * {@inheritDoc} */ public Set<java.util.Map.Entry<CacheKey, Object>> entrySet() { return parentCache.entrySet(); }
@Override public int getActiveCount() { try (Stream<Map.Entry<BeanKey<I>, BeanEntry<I>>> entries = this.cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).entrySet().stream()) { return (int) entries.filter(this.filter).count(); } }
private void schedule(Locality oldLocality, Locality newLocality) { // Iterate over beans in memory try (Stream<Map.Entry<BeanKey<I>, BeanEntry<I>>> stream = this.cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD).entrySet().stream().filter(this.filter)) { Iterator<Map.Entry<BeanKey<I>, BeanEntry<I>>> entries = stream.iterator(); while (entries.hasNext()) { if (Thread.currentThread().isInterrupted()) break; Map.Entry<BeanKey<I>, BeanEntry<I>> entry = entries.next(); BeanKey<I> key = entry.getKey(); // If we are the new primary owner of this bean then schedule expiration of this bean locally if (this.filter.test(entry) && !oldLocality.isLocal(key) && newLocality.isLocal(key)) { this.scheduler.schedule(key.getId()); } } } }
protected void assertNoInvalidators(AdvancedCache<Object, Object> pendingPutsCache) throws Exception { Method getInvalidators = null; for (Map.Entry<Object, Object> entry : pendingPutsCache.entrySet()) { if (getInvalidators == null) { getInvalidators = entry.getValue().getClass().getMethod("getInvalidators"); getInvalidators.setAccessible(true); } Collection invalidators = (Collection) getInvalidators.invoke(entry.getValue()); if (invalidators != null) { assertTrue("Invalidators on key " + entry.getKey() + ": " + invalidators, invalidators.isEmpty()); } } }
protected void onRealmRemovedEvent(String realmId) { Iterator<Map.Entry<String, RootAuthenticationSessionEntity>> itr = CacheDecorators.localCache(cache) .entrySet() .stream() .filter(RootAuthenticationSessionPredicate.create(realmId)) .iterator(); while (itr.hasNext()) { CacheDecorators.localCache(cache) .remove(itr.next().getKey()); } }
@Override public Map toMap() { if ( checkValid() ) { if (filter == null) { return cache; } else { return cache.withFlags(Flag.CACHE_MODE_LOCAL).entrySet().stream() .filter(filter).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } } return Collections.EMPTY_MAP; }
@Override public long getElementCountInMemory() { if ( checkValid() ) { if (filter == null) { return localAndSkipLoadCache.size(); } else { return localAndSkipLoadCache.entrySet().stream().filter(filter).count(); } } return 0; }
@Override protected CloseableIterator<ObjectFilter.FilterResult> getIterator() { IckleFilterAndConverter<Object, Object> ickleFilter = (IckleFilterAndConverter<Object, Object>) createFilter(); CacheStream<Map.Entry<Object, Object>> entryStream = ((AdvancedCache<Object, Object>) cache).entrySet().stream(); CacheStream<ObjectFilter.FilterResult> resultStream = entryStream.map(ickleFilter).filter(NON_NULL_PREDICATE); return Closeables.iterator(resultStream); }
public static void main(String[] args) throws Exception { // Setup up a clustered cache manager GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); // Make the default cache a distributed synchronous one ConfigurationBuilder builder = new ConfigurationBuilder(); builder.clustering().cacheMode(CacheMode.DIST_SYNC); // Initialize the cache manager DefaultCacheManager cacheManager = new DefaultCacheManager(global.build(), builder.build()); // Obtain the default cache Cache<String, String> cache = cacheManager.getCache(); // Store the current node address in some random keys for(int i=0; i < 10; i++) { cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress()); } // Display the current cache contents for the whole cluster cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue())); // Display the current cache contents for this node cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP) .entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue())); // Stop the cache manager and release all resources cacheManager.stop(); }
@Test public void testCacheLoaderIgnored() throws InterruptedException, ExecutionException, TimeoutException { Map<Object, String> originalValues = insertDefaultValues(false); Cache<Object, String> cache = cache(0, cacheName); Iterator<Map.Entry<Object, String>> iterator = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD). entrySet().stream().iterator(); // we need this count since the map will replace same key'd value int count = 0; Map<Object, String> results = new HashMap<>(); while (iterator.hasNext()) { Map.Entry<Object, String> entry = iterator.next(); results.put(entry.getKey(), entry.getValue()); count++; } assertEquals(count, 3); assertEquals(originalValues, results); } }
private void bulkGetInternal(HotRodHeader header, AdvancedCache<byte[], byte[]> cache, int size) { try { if (trace) { log.tracef("About to create bulk response count = %d", size); } writeResponse(header, header.encoder().bulkGetResponse(header, server, channel.alloc(), size, cache.entrySet())); } catch (Throwable t) { writeException(header, t); } }
@Override public void removeExpired(RealmModel realm) { log.debugf("Removing expired sessions"); int expired = Time.currentTime() - RealmInfoUtil.getDettachedClientSessionLifespan(realm); // Each cluster node cleanups just local sessions, which are those owned by himself (+ few more taking l1 cache into account) Iterator<Map.Entry<String, RootAuthenticationSessionEntity>> itr = CacheDecorators.localCache(cache) .entrySet() .stream() .filter(RootAuthenticationSessionPredicate.create(realm.getId()).expired(expired)) .iterator(); int counter = 0; while (itr.hasNext()) { counter++; RootAuthenticationSessionEntity entity = itr.next().getValue(); tx.remove(cache, entity.getId()); } log.debugf("Removed %d expired authentication sessions for realm '%s'", counter, realm.getName()); }
@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()); }
public void testEntrySetWithEvictedEntriesAndFlags() { final int numKeys = 300; for (int i = 0; i < numKeys; i++) { cache.put(i, i); } AdvancedCache<Object, Object> flagCache = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD); DataContainer dc = flagCache.getDataContainer(); assertFalse("Data Container should not have all keys", numKeys == dc.size()); Set<Map.Entry<Object, Object>> entrySet = flagCache.entrySet(); assertEquals(dc.size(), entrySet.size()); Set<InternalCacheEntry> entries = dc.entrySet(); Map<Object, Object> map = new HashMap<>(entrySet.size()); for (Map.Entry<Object, Object> entry : entrySet) { map.put(entry.getKey(), entry.getValue()); } for (InternalCacheEntry entry : entries) { assertEquals("Key/Value mismatch!", entry.getValue(), map.get(entry.getKey())); } }
@Test public void testEntrySetStreamRemove() { 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); Iterator<Map.Entry<Object, Object>> it = cache(0, CACHE_NAME).getAdvancedCache().withFlags( Flag.SKIP_CACHE_STORE) .entrySet() .stream() .iterator(); assertTrue(it.hasNext()); Map.Entry<Object, Object> entry = it.next(); Object key = entry.getKey(); assertEquals(values.get(key), entry.getValue()); // We don't support remove on stream iterator Exceptions.expectException(UnsupportedOperationException.class, it::remove); } }
@Test public void testIndexUsage() throws IOException { cache = cacheManager.getCache(); TestSegmentReadLocker testSegmentReadLocker = new TestSegmentReadLocker(cache, cache, cache, INDEX_NAME); Directory dir = DirectoryBuilder.newDirectoryInstance(cache, cache, cache, INDEX_NAME) .overrideSegmentReadLocker(testSegmentReadLocker).create(); writeTextToIndex(dir, 0, "hello database"); assertTextIsFoundInIds(dir, "hello", 0); writeTextToIndex(dir, 1, "you have to store my index segments"); writeTextToIndex(dir, 2, "so that I can shut down all nodes"); writeTextToIndex(dir, 3, "and restart later keeping the index around"); assertTextIsFoundInIds(dir, "index", 1, 3); removeByTerm(dir, "and"); assertTextIsFoundInIds(dir, "index", 1); dir.close(); for (Map.Entry me : cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).entrySet()) { cacheCopy.put(me.getKey(), me.getValue()); } cache.stop(); cacheManager.stop(); }
public void testRetrievalWhenPartitionIsDegradedButLocal() { Cache<MagicKey, String> cache0 = cache(0); cache0.put(new MagicKey(cache(1), cache(2)), "not-local"); cache0.put(new MagicKey(cache(0), cache(1)), "local"); splitCluster(new int[]{0, 1}, new int[]{2, 3}); partition(0).assertDegradedMode(); try (CloseableIterator<Map.Entry<MagicKey, String>> iterator = Closeables.iterator(cache0.getAdvancedCache() .withFlags(Flag.CACHE_MODE_LOCAL).entrySet().stream())) { assertEquals("local", iterator.next().getValue()); assertFalse(iterator.hasNext()); } }
@Override public void testRetrievalWhenPartitionIsDegradedButLocal() { Cache<MagicKey, String> cache0 = cache(0); cache0.put(new MagicKey(cache(1), cache(2)), "not-local"); cache0.put(new MagicKey(cache(0), cache(1)), "local"); splitCluster(new int[]{0, 1}, new int[]{2, 3}); partition(0).assertDegradedMode(); try (CloseableIterator<Map.Entry<MagicKey, String>> iterator = Closeables.iterator(cache0.getAdvancedCache() .withFlags(Flag.CACHE_MODE_LOCAL).entrySet().stream())) { assertNotNull(iterator.next()); assertNotNull(iterator.next()); assertFalse(iterator.hasNext()); } } }