Refine search
Equivalence<Object> getValueEquivalence() { return MoreObjects.firstNonNull(valueEquivalence, getValueStrength().defaultEquivalence()); }
/** * This method is a convenience for testing. Code should call {@link Segment#setValue} instead. */ // Guarded By Segment.this @VisibleForTesting ValueReference<K, V> newValueReference(ReferenceEntry<K, V> entry, V value, int weight) { int hash = entry.getHash(); return valueStrength.referenceValue(segmentFor(hash), entry, checkNotNull(value), weight); }
Equivalence<Object> getKeyEquivalence() { return MoreObjects.firstNonNull(keyEquivalence, getKeyStrength().defaultEquivalence()); }
public void testSetValueEquivalence() { Equivalence<Object> testEquivalence = new Equivalence<Object>() { @Override protected boolean doEquivalent(Object a, Object b) { return false; } @Override protected int doHash(Object t) { return 0; } }; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().valueEquivalence(testEquivalence)); assertSame(testEquivalence, map.valueEquivalence); assertSame(map.keyStrength.defaultEquivalence(), map.keyEquivalence); }
public void testSetKeyEquivalence() { Equivalence<Object> testEquivalence = new Equivalence<Object>() { @Override protected boolean doEquivalent(Object a, Object b) { return false; } @Override protected int doHash(Object t) { return 0; } }; LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().keyEquivalence(testEquivalence)); assertSame(testEquivalence, map.keyEquivalence); assertSame(map.valueStrength.defaultEquivalence(), map.valueEquivalence); }
/** Sets a new value of an entry. Adds newly created entries at the end of the access queue. */ @GuardedBy("this") void setValue(ReferenceEntry<K, V> entry, K key, V value, long now) { ValueReference<K, V> previous = entry.getValueReference(); int weight = map.weigher.weigh(key, value); checkState(weight >= 0, "Weights must be non-negative"); ValueReference<K, V> valueReference = map.valueStrength.referenceValue(this, entry, value, weight); entry.setValueReference(valueReference); recordWrite(entry, weight, now); previous.notifyNewValue(value); }
private static void checkStrength( LocalCache<Object, Object> map, Strength keyStrength, Strength valueStrength) { assertSame(keyStrength, map.keyStrength); assertSame(valueStrength, map.valueStrength); assertSame(keyStrength.defaultEquivalence(), map.keyEquivalence); assertSame(valueStrength.defaultEquivalence(), map.valueEquivalence); }
private CacheBuilderFactory cacheFactory() { return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(0, 1, 10, 100, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites( ImmutableSet.of( DurationSpec.of(0, SECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses( ImmutableSet.of( DurationSpec.of(0, SECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes(ImmutableSet.of(DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
public void testDefaults() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); assertSame(Strength.STRONG, map.keyStrength); assertSame(Strength.STRONG, map.valueStrength); assertSame(map.keyStrength.defaultEquivalence(), map.keyEquivalence); assertSame(map.valueStrength.defaultEquivalence(), map.valueEquivalence); assertEquals(0, map.expireAfterAccessNanos); assertEquals(0, map.expireAfterWriteNanos); assertEquals(0, map.refreshNanos); assertEquals(CacheBuilder.UNSET_INT, map.maxWeight); assertSame(EntryFactory.STRONG, map.entryFactory); assertSame(CacheBuilder.NullListener.INSTANCE, map.removalListener); assertSame(DISCARDING_QUEUE, map.removalNotificationQueue); assertSame(NULL_TICKER, map.ticker); assertEquals(4, map.concurrencyLevel); // concurrency level assertThat(map.segments).hasLength(4); // initial capacity / concurrency level assertEquals(16 / map.segments.length, map.segments[0].table.length()); assertFalse(map.evictsBySize()); assertFalse(map.expires()); assertFalse(map.expiresAfterWrite()); assertFalse(map.expiresAfterAccess()); assertFalse(map.refreshes()); }
private CacheBuilderFactory cacheFactory() { // This is trickier than expected. We plan to put 15 values in each of these (WARMUP_MIN to // WARMUP_MAX), but the tests assume no values get evicted. Even with a maximumSize of 100, one // of the values gets evicted. With weak keys, we use identity equality, which means using // System.identityHashCode, which means the assignment of keys to segments is nondeterministic, // so more than (maximumSize / #segments) keys could get assigned to the same segment, which // would cause one to be evicted. return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(400, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites( ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses( ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes( ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
/** * This method is a convenience for testing. Code should call {@link Segment#setValue} instead. */ // Guarded By Segment.this @VisibleForTesting ValueReference<K, V> newValueReference(ReferenceEntry<K, V> entry, V value, int weight) { int hash = entry.getHash(); return valueStrength.referenceValue(segmentFor(hash), entry, checkNotNull(value), weight); }
/** Sets a new value of an entry. Adds newly created entries at the end of the access queue. */ @GuardedBy("this") void setValue(ReferenceEntry<K, V> entry, K key, V value, long now) { ValueReference<K, V> previous = entry.getValueReference(); int weight = map.weigher.weigh(key, value); checkState(weight >= 0, "Weights must be non-negative"); ValueReference<K, V> valueReference = map.valueStrength.referenceValue(this, entry, value, weight); entry.setValueReference(valueReference); recordWrite(entry, weight, now); previous.notifyNewValue(value); }
/** Sets a new value of an entry. Adds newly created entries at the end of the access queue. */ @GuardedBy("this") void setValue(ReferenceEntry<K, V> entry, K key, V value, long now) { ValueReference<K, V> previous = entry.getValueReference(); int weight = map.weigher.weigh(key, value); checkState(weight >= 0, "Weights must be non-negative"); ValueReference<K, V> valueReference = map.valueStrength.referenceValue(this, entry, value, weight); entry.setValueReference(valueReference); recordWrite(entry, weight, now); previous.notifyNewValue(value); }
/** * This method is a convenience for testing. Code should call {@link Segment#setValue} instead. */ // Guarded By Segment.this @VisibleForTesting ValueReference<K, V> newValueReference(ReferenceEntry<K, V> entry, V value, int weight) { int hash = entry.getHash(); return valueStrength.referenceValue(segmentFor(hash), entry, checkNotNull(value), weight); }
private CacheBuilderFactory cacheFactory() { return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(0, 1, 10, 100, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites(ImmutableSet.of( DurationSpec.of(0, SECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses(ImmutableSet.of( DurationSpec.of(0, SECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes(ImmutableSet.of( DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
private CacheBuilderFactory cacheFactory() { // This is trickier than expected. We plan to put 15 values in each of these (WARMUP_MIN to // WARMUP_MAX), but the tests assume no values get evicted. Even with a maximumSize of 100, one // of the values gets evicted. With weak keys, we use identity equality, which means using // System.identityHashCode, which means the assignment of keys to segments is nondeterministic, // so more than (maximumSize / #segments) keys could get assigned to the same segment, which // would cause one to be evicted. return new CacheBuilderFactory() .withKeyStrengths(ImmutableSet.of(Strength.STRONG, Strength.WEAK)) .withValueStrengths(ImmutableSet.copyOf(Strength.values())) .withConcurrencyLevels(ImmutableSet.of(1, 4, 16, 64)) .withMaximumSizes(ImmutableSet.of(400, 1000)) .withInitialCapacities(ImmutableSet.of(0, 1, 10, 100, 1000)) .withExpireAfterWrites(ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withExpireAfterAccesses(ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))) .withRefreshes(ImmutableSet.of( // DurationSpec.of(500, MILLISECONDS), DurationSpec.of(1, SECONDS), DurationSpec.of(1, DAYS))); }
/** * This method is a convenience for testing. Code should call {@link Segment#setValue} instead. */ @GuardedBy("Segment.this") @VisibleForTesting ValueReference<K, V> newValueReference(ReferenceEntry<K, V> entry, V value, int weight) { int hash = entry.getHash(); return valueStrength.referenceValue(segmentFor(hash), entry, checkNotNull(value), weight); }
/** * This method is a convenience for testing. Code should call {@link Segment#setValue} instead. */ @GuardedBy("Segment.this") @VisibleForTesting ValueReference<K, V> newValueReference(ReferenceEntry<K, V> entry, V value, int weight) { int hash = entry.getHash(); return valueStrength.referenceValue(segmentFor(hash), entry, checkNotNull(value), weight); }