@Override public Map.Entry<String, Map<D, S>> findEntryContaining(S session) { SessionFilter<D, S> filter = new SessionFilter<>(session); // Erase type to handle compilation issues with generics // Our filter will handle type safety and casting @SuppressWarnings("rawtypes") Cache cache = this.cache; try (Stream<Map.Entry<?, ?>> stream = cache.entrySet().stream()) { Map.Entry<CoarseSessionsKey, Map<D, S>> entry = stream.filter(this.filter).map(this.filter).filter(filter).findAny().orElse(null); return (entry != null) ? new AbstractMap.SimpleImmutableEntry<>(entry.getKey().getValue(), entry.getValue()) : null; } }
@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 Set<String> getSessions(Flag... flags) { Locality locality = new CacheLocality(this.cache); try (Stream<Key<String>> keys = this.cache.getAdvancedCache().withFlags(flags).keySet().stream()) { return keys.filter(this.filter.and(key -> locality.isLocal(key))).map(key -> key.getValue()).collect(Collectors.toSet()); } }
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()); } } } } }
private Void removeInternal(Predicate<? super V> p) { cache.keySet().stream().forEach((c, key) -> c.computeIfPresent(key, (o, o1) -> { Collection<V> values = (Collection<V>) o1; Collection<V> newValues = new HashSet<>(); for (V v : values) { if (!p.test(v)) newValues.add(v); } return newValues.isEmpty() ? null : newValues; })); return null; }
protected List<UserSessionModel> getUserSessions(RealmModel realm, Predicate<Map.Entry<String, SessionEntity>> predicate, boolean offline) { CacheStream<Map.Entry<String, SessionEntity>> cacheStream = getCache(offline).entrySet().stream(); Iterator<Map.Entry<String, SessionEntity>> itr = cacheStream.filter(predicate).iterator(); List<UserSessionModel> sessions = new LinkedList<>(); while (itr.hasNext()) { UserSessionEntity e = (UserSessionEntity) itr.next().getValue(); sessions.add(wrap(realm, e, offline)); } return sessions; }
@Override public Integer call() throws Exception { Cache<String, String> cache = getCache(); return cache.keySet() .stream() .map(e -> Integer.valueOf(e.substring(0, e.indexOf("-")))) .collect(() -> Collectors.summingInt(Integer::intValue)); }
private void removeExpiredClientInitialAccess(RealmModel realm) { Iterator<String> itr = sessionCache.entrySet().stream().filter(ClientInitialAccessPredicate.create(realm.getId()).expired(Time.currentTime())).map(Mappers.sessionId()).iterator(); while (itr.hasNext()) { tx.remove(sessionCache, itr.next()); } }
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 void perform(MagicKey key, Cache<MagicKey, String> cache) { List<Map.Entry<MagicKey, String>> list = cache.entrySet().stream().collect(() -> Collectors.toList()); assertEquals(1, list.size()); assertEquals(key, list.get(0).getKey()); } }
@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); }
private List<CacheXid> getKeys(XidImpl xid) { return storage.keySet().stream() .filter(new XidPredicate(xid)) .collect(CacheCollectors.serializableCollector(Collectors::toList)); }
@Override protected CloseableIterator<ObjectFilter.FilterResult> getIterator() { CacheStream<CacheEntry<?, ObjectFilter.FilterResult>> stream = (CacheStream<CacheEntry<?, ObjectFilter.FilterResult>>) CacheFilters.filterAndConvert(cache.cacheEntrySet().stream(), createFilter()); return Closeables.iterator(stream.map(CacheEntry::getValue)); }
@Override public Object call() throws Exception { Cache<String, String> cache = (Cache<String, String>) taskContext.getCache().get(); return cache.entrySet().stream() .map((Serializable & Function<Map.Entry<String, String>, String[]>) e -> e.getValue().split("\\s+")) .flatMap((Serializable & Function<String[], Stream<String>>) Arrays::stream) .collect(() -> Collectors.groupingBy(Function.identity(), Collectors.counting())); }
public void testStressNodesLeavingWhileMultipleCount() throws Throwable { testStressNodesLeavingWhilePerformingCallable(((cache, masterValues, iteration) -> { long size; assertEquals(CACHE_ENTRY_COUNT, (size = cache.entrySet().stream().count()), "We didn't get a matching size! Expected " + CACHE_ENTRY_COUNT + " but was " + size); })); }
@Override public CacheStream stream() { return new LocalCacheStream(new EntryStreamSupplier(cache, getConsistentHash(cache), () -> delegate.stream().map(mapper)), false, cache.getAdvancedCache().getComponentRegistry()); }
@Test public void simpleTestIteratorFromOtherNode() { Map<Object, String> values = putValuesInCache(); Cache<MagicKey, String> cache = cache(1, CACHE_NAME); Iterator<CacheEntry<MagicKey, String>> iterator = cache.getAdvancedCache().cacheEntrySet().stream().iterator(); Map<MagicKey, String> results = mapFromIterator(iterator); assertEquals(values, results); }
public static <K, V, T> MapCollectableCloseableIterable<K, T> entrySet(AdvancedCache<K, V> cache, KeyValueFilter<K, V> filter, Converter<K, V, T> converter) { if (cache.getCacheConfiguration().transaction().transactionMode().isTransactional()) { // Dummy read to enlist the LocalTransaction as workaround for ISPN-5676 cache.containsKey(false); } // HHH-10023: we can't use values() CloseableIterator<CacheEntry<K, T>> it = Closeables.iterator(cache.cacheEntrySet().stream() .filter(CacheFilters.predicate(filter)) .map(CacheFilters.function(converter))); return new MapCollectableCloseableIterableImpl<K, T>(it); }
@Test( expectedExceptions = AvailabilityException.class) public void testRetrievalWhenPartitionIsDegraded() { 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 iterator = Closeables.iterator(cache(0).entrySet().stream())) { iterator.next(); } }
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()); } }