EntryIterator(BoundedLocalCache<K, V> cache) { this.iterator = cache.data.values().iterator(); this.now = cache.expirationTicker().read(); this.cache = cache; }
@Override public boolean containsValue(Object value) { requireNonNull(value); long now = expirationTicker().read(); for (Node<K, V> node : data.values()) { if (node.containsValue(value) && !hasExpired(node, now) && (node.getKey() != null)) { return true; } } return false; }
@Override public @Nullable V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { requireNonNull(key); requireNonNull(value); requireNonNull(remappingFunction); long[] now = { expirationTicker().read() }; Object keyRef = nodeFactory.newReferenceKey(key, keyReferenceQueue()); BiFunction<? super K, ? super V, ? extends V> mergeFunction = (k, oldValue) -> (oldValue == null) ? value : statsAware(remappingFunction).apply(oldValue, value); return remap(key, keyRef, mergeFunction, now, /* computeIfAbsent */ true); }
@Override public boolean containsKey(Object key) { Node<K, V> node = data.get(nodeFactory.newLookupKey(key)); return (node != null) && (node.getValue() != null) && !hasExpired(node, expirationTicker().read()); }
@Override public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { requireNonNull(function); BiFunction<K, V, V> remappingFunction = (key, oldValue) -> { V newValue = requireNonNull(function.apply(key, oldValue)); if (oldValue != newValue) { writer.write(key, newValue); } return newValue; }; for (K key : keySet()) { long[] now = { expirationTicker().read() }; Object lookupKey = nodeFactory.newLookupKey(key); remap(key, lookupKey, remappingFunction, now, /* computeIfAbsent */ false); } }
@Override public @Nullable V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction, boolean recordMiss, boolean recordLoad) { requireNonNull(key); requireNonNull(remappingFunction); long[] now = { expirationTicker().read() }; Object keyRef = nodeFactory.newReferenceKey(key, keyReferenceQueue()); BiFunction<? super K, ? super V, ? extends V> statsAwareRemappingFunction = statsAware(remappingFunction, recordMiss, recordLoad); return remap(key, keyRef, statsAwareRemappingFunction, now, /* computeIfAbsent */ true); }
@Override public void forEachRemaining(Consumer<? super K> action) { requireNonNull(action); Consumer<Node<K, V>> consumer = node -> { K key = node.getKey(); V value = node.getValue(); long now = cache.expirationTicker().read(); if ((key != null) && (value != null) && node.isAlive() && !cache.hasExpired(node, now)) { action.accept(key); } }; spliterator.forEachRemaining(consumer); }
@Override public void forEachRemaining(Consumer<? super V> action) { requireNonNull(action); Consumer<Node<K, V>> consumer = node -> { K key = node.getKey(); V value = node.getValue(); long now = cache.expirationTicker().read(); if ((key != null) && (value != null) && node.isAlive() && !cache.hasExpired(node, now)) { action.accept(value); } }; spliterator.forEachRemaining(consumer); }
@Override @SuppressWarnings("FutureReturnValueIgnored") public void clear() { evictionLock.lock(); try { long now = expirationTicker().read(); // Apply all pending writes Runnable task; while (buffersWrites() && (task = writeBuffer().poll()) != null) { task.run(); } // Discard all entries for (Node<K, V> node : data.values()) { removeNode(node, now); } // Discard all pending reads readBuffer.drainTo(e -> {}); } finally { evictionLock.unlock(); } }
@Override public void forEachRemaining(Consumer<? super Entry<K, V>> action) { requireNonNull(action); Consumer<Node<K, V>> consumer = node -> { K key = node.getKey(); V value = node.getValue(); long now = cache.expirationTicker().read(); if ((key != null) && (value != null) && node.isAlive() && !cache.hasExpired(node, now)) { action.accept(new WriteThroughEntry<>(cache, key, value)); } }; spliterator.forEachRemaining(consumer); }
@Override public @Nullable V getIfPresentQuietly(Object key, long[/* 1 */] writeTime) { V value; Node<K, V> node = data.get(nodeFactory.newLookupKey(key)); if ((node == null) || ((value = node.getValue()) == null) || hasExpired(node, expirationTicker().read())) { return null; } writeTime[0] = node.getWriteTime(); return value; }
@Override public boolean tryAdvance(Consumer<? super V> action) { requireNonNull(action); boolean[] advanced = { false }; long now = cache.expirationTicker().read(); Consumer<Node<K, V>> consumer = node -> { K key = node.getKey(); V value = node.getValue(); if ((key != null) && (value != null) && !cache.hasExpired(node, now) && node.isAlive()) { action.accept(value); advanced[0] = true; } }; for (;;) { if (spliterator.tryAdvance(consumer)) { if (advanced[0]) { return true; } continue; } return false; } }
@Override public boolean tryAdvance(Consumer<? super K> action) { requireNonNull(action); boolean[] advanced = { false }; Consumer<Node<K, V>> consumer = node -> { K key = node.getKey(); V value = node.getValue(); long now = cache.expirationTicker().read(); if ((key != null) && (value != null) && node.isAlive() && !cache.hasExpired(node, now)) { action.accept(key); advanced[0] = true; } }; for (;;) { if (spliterator.tryAdvance(consumer)) { if (advanced[0]) { return true; } continue; } return false; } }
/** Expires entries that have expired by access, write, or variable. */ @GuardedBy("evictionLock") void expireEntries() { long now = expirationTicker().read(); expireAfterAccessEntries(now); expireAfterWriteEntries(now); expireVariableEntries(now); }
@Override public @Nullable V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { requireNonNull(key); requireNonNull(remappingFunction); // A optimistic fast path to avoid unnecessary locking Object lookupKey = nodeFactory.newLookupKey(key); @Nullable Node<K, V> node = data.get(lookupKey); long now; if (node == null) { return null; } else if ((node.getValue() == null) || hasExpired(node, (now = expirationTicker().read()))) { scheduleDrainBuffers(); return null; } BiFunction<? super K, ? super V, ? extends V> statsAwareRemappingFunction = statsAware(remappingFunction, /* recordMiss */ false, /* recordLoad */ true); return remap(key, lookupKey, statsAwareRemappingFunction, new long[] { now }, /* computeIfAbsent */ false); }
@Override public boolean tryAdvance(Consumer<? super Entry<K, V>> action) { requireNonNull(action); boolean[] advanced = { false }; Consumer<Node<K, V>> consumer = node -> { K key = node.getKey(); V value = node.getValue(); long now = cache.expirationTicker().read(); if ((key != null) && (value != null) && node.isAlive() && !cache.hasExpired(node, now)) { action.accept(new WriteThroughEntry<>(cache, key, value)); advanced[0] = true; } }; for (;;) { if (spliterator.tryAdvance(consumer)) { if (advanced[0]) { return true; } continue; } return false; } }
long now = expirationTicker().read(); Map<Object, Object> result = new LinkedHashMap<>(uniqueKeys.size()); for (Object key : uniqueKeys) {
@Override public @Nullable V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction, boolean recordStats, boolean recordLoad) { requireNonNull(key); requireNonNull(mappingFunction); long now = expirationTicker().read(); // An optimistic fast path to avoid unnecessary locking Node<K, V> node = data.get(nodeFactory.newLookupKey(key)); if (node != null) { V value = node.getValue(); if ((value != null) && !hasExpired(node, now)) { if (!isComputingAsync(node)) { setVariableTime(node, expireAfterRead(node, key, value, expiry(), now)); setAccessTime(node, now); } afterRead(node, now, /* recordHit */ true); return value; } } if (recordStats) { mappingFunction = statsAware(mappingFunction, recordLoad); } Object keyRef = nodeFactory.newReferenceKey(key, keyReferenceQueue()); return doComputeIfAbsent(key, keyRef, mappingFunction, new long[] { now }); }
@Override public @Nullable V getIfPresent(Object key, boolean recordStats) { Node<K, V> node = data.get(nodeFactory.newLookupKey(key)); if (node == null) { if (recordStats) { statsCounter().recordMisses(1); } return null; } V value = node.getValue(); long now = expirationTicker().read(); if (hasExpired(node, now) || (collectValues() && (value == null))) { if (recordStats) { statsCounter().recordMisses(1); } scheduleDrainBuffers(); return null; } if (!isComputingAsync(node)) { @SuppressWarnings("unchecked") K castedKey = (K) key; setAccessTime(node, now); setVariableTime(node, expireAfterRead(node, castedKey, value, expiry(), now)); } afterRead(node, now, recordStats); return value; }
/** Creates a serialization proxy based on the common configuration shared by all cache types. */ static <K, V> SerializationProxy<K, V> makeSerializationProxy( BoundedLocalCache<?, ?> cache, boolean isWeighted) { SerializationProxy<K, V> proxy = new SerializationProxy<>(); proxy.weakKeys = cache.collectKeys(); proxy.weakValues = cache.nodeFactory.weakValues(); proxy.softValues = cache.nodeFactory.softValues(); proxy.isRecordingStats = cache.isRecordingStats(); proxy.removalListener = cache.removalListener(); proxy.ticker = cache.expirationTicker(); proxy.writer = cache.writer; if (cache.expiresAfterAccess()) { proxy.expiresAfterAccessNanos = cache.expiresAfterAccessNanos(); } if (cache.expiresAfterWrite()) { proxy.expiresAfterWriteNanos = cache.expiresAfterWriteNanos(); } if (cache.expiresVariable()) { proxy.expiry = cache.expiry(); } if (cache.evicts()) { if (isWeighted) { proxy.weigher = cache.weigher; proxy.maximumWeight = cache.maximum(); } else { proxy.maximumSize = cache.maximum(); } } return proxy; }