@Override public Map getAllValues(Collection keys) { return cache.getAllPresent(keys); }
@Override public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys) { // The assumption here is that every value is accessed at least once. Materializing here ensures deserialize is only // called *once* per value. return ImmutableMap.copyOf(Maps.transformValues(cache.getAllPresent(keys), this::deserialize)); }
@Override public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) { return ImmutableMap.copyOf(cache.getAllPresent(keys)); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAllPresent_iterable_empty(Cache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAllPresent(ImmutableList.of()); assertThat(result.size(), is(0)); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAllPresent_ordered(Cache<Integer, Integer> cache, CacheContext context) { List<Integer> keys = new ArrayList<>(context.original().keySet()); Collections.shuffle(keys); List<Integer> result = new ArrayList<>(cache.getAllPresent(keys).keySet()); assertThat(result, is(equalTo(keys))); }
Map<K, Expirable<V>> result = new HashMap<>(cache.getAllPresent(keys));
@Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, keys = ReferenceType.STRONG, writer = Writer.DISABLED) public void getAllPresent_jdk8186171(Cache<Object, Integer> cache, CacheContext context) { class Key { @Override public int hashCode() { return 0; // to put keys in one bucket } } List<Key> keys = new ArrayList<>(); for (int i = 0; i < Population.FULL.size(); i++) { keys.add(new Key()); } Key key = Iterables.getLast(keys); Integer value = context.absentValue(); cache.put(key, value); Map<Object, Integer> result = cache.getAllPresent(keys); assertThat(result.values(), not(hasItem(nullValue()))); assertThat(result, is(equalTo(ImmutableMap.of(key, value)))); }
@CheckNoWriter @CheckNoStats @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void getAllPresent_iterable_nullKey(Cache<Integer, Integer> cache, CacheContext context) { cache.getAllPresent(Collections.singletonList(null)); }
@CacheSpec @CheckNoWriter @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void getAllPresent_immutable(Cache<Integer, Integer> cache, CacheContext context) { cache.getAllPresent(context.absentKeys()).clear(); }
@CheckNoWriter @CheckNoStats @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void getAllPresent_iterable_null(Cache<Integer, Integer> cache, CacheContext context) { cache.getAllPresent(null); }
@Test(dataProvider = "caches") @CacheSpec(keys = ReferenceType.STRONG, values = {ReferenceType.WEAK, ReferenceType.SOFT}, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED, removalListener = Listener.CONSUMING) public void getAllPresent(Cache<Integer, Integer> cache, CacheContext context) { Set<Integer> keys = context.firstMiddleLastKeys(); context.clear(); GcFinalization.awaitFullGc(); assertThat(cache.getAllPresent(keys), is(emptyMap())); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAllPresent_present_full(Cache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAllPresent(context.original().keySet()); assertThat(result, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(result.size()))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(population = { Population.PARTIAL, Population.FULL }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAllPresent_present_partial(Cache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> expect = new HashMap<>(); expect.put(context.firstKey(), context.original().get(context.firstKey())); expect.put(context.middleKey(), context.original().get(context.middleKey())); expect.put(context.lastKey(), context.original().get(context.lastKey())); Map<Integer, Integer> result = cache.getAllPresent(expect.keySet()); assertThat(result, is(equalTo(expect))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(expect.size()))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@Test(dataProvider = "caches") @CacheSpec(population = { Population.PARTIAL, Population.FULL }, mustExpireWithAnyOf = { AFTER_WRITE, VARIABLE }, expireAfterWrite = Expire.ONE_MINUTE, expiry = { CacheExpiry.DISABLED, CacheExpiry.WRITE }, expiryTime = Expire.ONE_MINUTE) public void getAllPresent(Cache<Integer, Integer> cache, CacheContext context) { context.ticker().advance(30, TimeUnit.SECONDS); cache.getAllPresent(context.firstMiddleLastKeys()); context.ticker().advance(45, TimeUnit.SECONDS); assertThat(cache.getAllPresent(context.firstMiddleLastKeys()).size(), is(0)); cache.cleanUp(); assertThat(cache.estimatedSize(), is(0L)); long count = context.initialSize(); assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED)); }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAllPresent_absent(Cache<Integer, Integer> cache, CacheContext context) { Map<Integer, Integer> result = cache.getAllPresent(context.absentKeys()); assertThat(result.size(), is(0)); int count = context.absentKeys().size(); assertThat(context, both(hasMissCount(count)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@Test(dataProvider = "caches") @CacheSpec(mustExpireWithAnyOf = { AFTER_ACCESS, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.ACCESS }, expiryTime = Expire.ONE_MINUTE, expireAfterAccess = Expire.ONE_MINUTE, population = { Population.PARTIAL, Population.FULL }) public void getAllPresent(Cache<Integer, Integer> cache, CacheContext context) { context.ticker().advance(30, TimeUnit.SECONDS); cache.getAllPresent(context.firstMiddleLastKeys()); context.ticker().advance(45, TimeUnit.SECONDS); assertThat(cache.getAllPresent(context.firstMiddleLastKeys()).size(), is(3)); cache.cleanUp(); assertThat(cache.estimatedSize(), is(3L)); long count = context.initialSize() - 3; assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED)); verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED)); }
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expiry = CacheExpiry.MOCKITO) @Test(dataProvider = "caches", expectedExceptions = ExpirationException.class) public void getAllPresent_expiryFails(Cache<Integer, Integer> cache, CacheContext context) { try { context.ticker().advance(1, TimeUnit.HOURS); when(context.expiry().expireAfterRead(any(), any(), anyLong(), anyLong())) .thenThrow(ExpirationException.class); cache.getAllPresent(context.firstMiddleLastKeys()); } finally { context.ticker().advance(-1, TimeUnit.HOURS); assertThat(cache.asMap(), equalTo(context.original())); } }
@CheckNoWriter @Test(dataProvider = "caches") @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void getAllPresent_duplicates(Cache<Integer, Integer> cache, CacheContext context) { Iterable<Integer> keys = Iterables.concat( context.absentKeys(), context.absentKeys(), context.original().keySet(), context.original().keySet()); Map<Integer, Integer> result = cache.getAllPresent(keys); int misses = context.absentKeys().size(); int hits = context.original().keySet().size(); if (context.isGuava()) { // does not filter duplicate queries misses += misses; hits += hits; } assertThat(result, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(misses)).and(hasHitCount(hits))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); }
@Override public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys) { // The assumption here is that every value is accessed at least once. Materializing here ensures deserialize is only // called *once* per value. return ImmutableMap.copyOf(Maps.transformValues(cache.getAllPresent(keys), this::deserialize)); }
@InvokeOnHeader(CaffeineConstants.ACTION_GET_ALL) public void onGetAll(Message message) throws Exception { Object result = cache.getAllPresent(message.getHeader(CaffeineConstants.KEYS, Collections::emptySet, Set.class)); setResult(message, true, result, null); }