static int getTotalSegmentSize(Cache<?, ?> cache) { LocalCache<?, ?> map = toLocalCache(cache); int totalSize = 0; for (Segment<?, ?> segment : map.segments) { totalSize += segment.maxSegmentWeight; } return totalSize; }
static int accessQueueSize(Cache<?, ?> cache) { LocalCache<?, ?> cchm = toLocalCache(cache); int size = 0; for (Segment<?, ?> segment : cchm.segments) { size += accessQueueSize(segment); } return size; }
static <K, V> ReferenceEntry<K, V> getReferenceEntry(Cache<K, V> cache, K key) { checkNotNull(cache); checkNotNull(key); LocalCache<K, V> map = toLocalCache(cache); return map.getEntry(key); }
static int writeQueueSize(Cache<?, ?> cache) { LocalCache<?, ?> cchm = toLocalCache(cache); int size = 0; for (Segment<?, ?> segment : cchm.segments) { size += writeQueueSize(segment); } return size; }
static void expireEntries(Cache<?, ?> cache, long expiringTime, FakeTicker ticker) { checkNotNull(ticker); expireEntries(toLocalCache(cache), expiringTime, ticker); }
/** * Forces the segment containing the given {@code key} to expand (see {@link Segment#expand()}. */ static <K, V> void forceExpandSegment(Cache<K, V> cache, K key) { checkNotNull(cache); checkNotNull(key); LocalCache<K, V> map = toLocalCache(cache); int hash = map.hash(key); Segment<K, V> segment = map.segmentFor(hash); segment.expand(); }
static void drainRecencyQueues(Cache<?, ?> cache) { if (hasLocalCache(cache)) { LocalCache<?, ?> map = toLocalCache(cache); for (Segment<?, ?> segment : map.segments) { drainRecencyQueue(segment); } } }
static void drainReferenceQueues(Cache<?, ?> cache) { if (hasLocalCache(cache)) { drainReferenceQueues(toLocalCache(cache)); } }
/** * Peeks into the cache's internals to verify that its expiration queue is consistent. Verifies * that the next/prev links in the expiration queue are correct, and that the queue is ordered by * expiration time. */ static void checkExpiration(Cache<?, ?> cache) { if (hasLocalCache(cache)) { checkExpiration(toLocalCache(cache)); } }
static void processPendingNotifications(Cache<?, ?> cache) { if (hasLocalCache(cache)) { LocalCache<?, ?> cchm = toLocalCache(cache); cchm.processPendingNotifications(); } }
/** * Peeks into the cache's internals to verify that its eviction queue is consistent. Verifies that * the prev/next links are correct, and that all items in each segment are also in that segment's * eviction (recency) queue. */ static void checkEviction(Cache<?, ?> cache) { if (hasLocalCache(cache)) { checkEviction(toLocalCache(cache)); } }
/** * Peeks into the cache's internals to check its internal consistency. Verifies that each * segment's count matches its #elements (after cleanup), each segment is unlocked, each entry * contains a non-null key and value, and the eviction and expiration queues are consistent (see * {@link #checkEviction}, {@link #checkExpiration}). */ static void checkValidState(Cache<?, ?> cache) { if (hasLocalCache(cache)) { checkValidState(toLocalCache(cache)); } }
@GwtIncompatible // CacheTesting public void testSizingDefaults() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(4); // concurrency level assertEquals(4, map.segments[0].table.length()); // capacity / conc level }
@GwtIncompatible // CacheTesting public void testConcurrencyLevel_small() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().concurrencyLevel(1).build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(1); }
@GwtIncompatible // CacheTesting public void testInitialCapacity_small() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().initialCapacity(5).build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(4); assertEquals(2, map.segments[0].table.length()); assertEquals(2, map.segments[1].table.length()); assertEquals(2, map.segments[2].table.length()); assertEquals(2, map.segments[3].table.length()); }
@GwtIncompatible // CacheTesting public void testInitialCapacity_smallest() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().initialCapacity(0).build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(4); // 1 is as low as it goes, not 0. it feels dirty to know this/test this. assertEquals(1, map.segments[0].table.length()); assertEquals(1, map.segments[1].table.length()); assertEquals(1, map.segments[2].table.length()); assertEquals(1, map.segments[3].table.length()); }
/** * Assuming the given cache has maximum size {@code maxSize}, this method populates the cache (by * getting a bunch of different keys), then makes sure all the items in the cache are also in the * eviction queue. It will invoke the given {@code operation} on the first element in the eviction * queue, and then reverify that all items in the cache are in the eviction queue, and verify that * the head of the eviction queue has changed as a result of the operation. */ static void checkRecency( LoadingCache<Integer, Integer> cache, int maxSize, Receiver<ReferenceEntry<Integer, Integer>> operation) { checkNotNull(operation); if (hasLocalCache(cache)) { warmUp(cache, 0, 2 * maxSize); LocalCache<Integer, Integer> cchm = toLocalCache(cache); Segment<?, ?> segment = cchm.segments[0]; drainRecencyQueue(segment); assertEquals(maxSize, accessQueueSize(cache)); assertEquals(maxSize, cache.size()); ReferenceEntry<?, ?> originalHead = segment.accessQueue.peek(); @SuppressWarnings("unchecked") ReferenceEntry<Integer, Integer> entry = (ReferenceEntry) originalHead; operation.accept(entry); drainRecencyQueue(segment); assertNotSame(originalHead, segment.accessQueue.peek()); assertEquals(cache.size(), accessQueueSize(cache)); } }
static void expireEntries(Cache<?, ?> cache, long expiringTime, FakeTicker ticker) { checkNotNull(ticker); expireEntries(toLocalCache(cache), expiringTime, ticker); }
@GwtIncompatible // CacheTesting public void testSizingDefaults() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(4); // concurrency level assertEquals(4, map.segments[0].table.length()); // capacity / conc level }
static void drainRecencyQueues(Cache<?, ?> cache) { if (hasLocalCache(cache)) { LocalCache<?, ?> map = toLocalCache(cache); for (Segment<?, ?> segment : map.segments) { drainRecencyQueue(segment); } } }