|
| 1 | +package org.apache.ratis.util; |
| 2 | + |
| 3 | +import org.apache.ratis.thirdparty.com.google.common.collect.MapMaker; |
| 4 | + |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | +import java.util.concurrent.ConcurrentMap; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | +import java.util.function.BiFunction; |
| 11 | +import java.util.function.Consumer; |
| 12 | + |
| 13 | +/** |
| 14 | + * Weak Value Cache: ({@link OUTER}, {@link INNER}) -> {@link T}. |
| 15 | + * <p> |
| 16 | + * Note that the cached values are weakly referenced. |
| 17 | + * A cached value could be garage-collected (i.e. evicted from the cache) |
| 18 | + * when there are no external (strong) references. |
| 19 | + * |
| 20 | + * @param <OUTER> the type of the outer keys. |
| 21 | + * @param <INNER> the type of the inner keys. |
| 22 | + * @param <T> the type to be cached. |
| 23 | + */ |
| 24 | +public final class BiWeakValueCache<OUTER, INNER, T> { |
| 25 | + private static <K, V> ConcurrentMap<K, V> newMap() { |
| 26 | + return new MapMaker().weakValues().makeMap(); |
| 27 | + } |
| 28 | + |
| 29 | + private final String outerName; |
| 30 | + private final String innerName; |
| 31 | + private final String name; |
| 32 | + |
| 33 | + /** For constructing {@link T} values from ({@link OUTER}, {@link INNER}) keys. */ |
| 34 | + private final BiFunction<OUTER, INNER, T> constructor; |
| 35 | + /** Count the number of {@link T} values constructed. */ |
| 36 | + private final AtomicInteger valueCount = new AtomicInteger(0); |
| 37 | + |
| 38 | + /** |
| 39 | + * Actual map {@link OUTER} -> ({@link INNER} -> {@link T}) |
| 40 | + * for the logical view ({@link OUTER}, {@link INNER}) -> {@link T}. |
| 41 | + */ |
| 42 | + private final ConcurrentMap<OUTER, ConcurrentMap<INNER, T>> map = new ConcurrentHashMap<>(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Create a cache for mapping ({@link OUTER}, {@link INNER}) keys to {@link T} values. |
| 46 | + * |
| 47 | + * @param outerName the name of the outer long. |
| 48 | + * @param innerName the name of the inner long. |
| 49 | + * @param constructor for constructing {@link T} values. |
| 50 | + */ |
| 51 | + public BiWeakValueCache(String outerName, String innerName, BiFunction<OUTER, INNER, T> constructor) { |
| 52 | + this.outerName = outerName; |
| 53 | + this.innerName = innerName; |
| 54 | + this.name = "(" + outerName + ", " + innerName + ")-cache"; |
| 55 | + this.constructor = constructor; |
| 56 | + } |
| 57 | + |
| 58 | + private T construct(OUTER outer, INNER inner) { |
| 59 | + final T constructed = constructor.apply(outer, inner); |
| 60 | + Objects.requireNonNull(constructed, "constructed == null"); |
| 61 | + valueCount.incrementAndGet(); |
| 62 | + return constructed; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * If the key ({@link OUTER}, {@link INNER}) is in the cache, return the cached values. |
| 67 | + * Otherwise, create a new value and then return it. |
| 68 | + */ |
| 69 | + public T getOrCreate(OUTER outer, INNER inner) { |
| 70 | + Objects.requireNonNull(outer, () -> outerName + " (outer) == null"); |
| 71 | + Objects.requireNonNull(inner, () -> innerName + " (inner) == null"); |
| 72 | + final ConcurrentMap<INNER, T> innerMap = map.computeIfAbsent(outer, k -> newMap()); |
| 73 | + final T computed = innerMap.computeIfAbsent(inner, i -> construct(outer, i)); |
| 74 | + if ((valueCount.get() & 0xFFF) == 0) { |
| 75 | + cleanupEmptyInnerMaps(); // cleanup empty maps once in a while |
| 76 | + } |
| 77 | + return computed; |
| 78 | + } |
| 79 | + |
| 80 | + /** @return the value count for the given outer key. */ |
| 81 | + int count(OUTER outer) { |
| 82 | + final ConcurrentMap<INNER, T> innerMap = map.get(outer); |
| 83 | + if (innerMap == null) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + |
| 87 | + // size() may return incorrect result; see Guava MapMaker javadoc |
| 88 | + int n = 0; |
| 89 | + for (INNER ignored : innerMap.keySet()) { |
| 90 | + n++; |
| 91 | + } |
| 92 | + return n; |
| 93 | + } |
| 94 | + |
| 95 | + void cleanupEmptyInnerMaps() { |
| 96 | + // isEmpty() may return incorrect result; see Guava MapMaker javadoc |
| 97 | + map.values().removeIf(e -> !e.entrySet().iterator().hasNext()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() { |
| 102 | + return name; |
| 103 | + } |
| 104 | + |
| 105 | + /** The cache content for debugging. */ |
| 106 | + int dump(Consumer<String> out) { |
| 107 | + out.accept(name + ":\n"); |
| 108 | + int emptyCount = 0; |
| 109 | + for (Map.Entry<OUTER, ConcurrentMap<INNER, T>> entry : map.entrySet()) { |
| 110 | + final OUTER outer = entry.getKey(); |
| 111 | + final ConcurrentMap<INNER, T> innerMap = entry.getValue(); |
| 112 | + final int count = count(outer); |
| 113 | + if (count == 0) { |
| 114 | + emptyCount++; |
| 115 | + } |
| 116 | + |
| 117 | + out.accept(" " + outerName + ":" + outer); |
| 118 | + out.accept(", " + innerName + ":" + innerMap.keySet()); |
| 119 | + out.accept(", count=" + count); |
| 120 | + out.accept(", size=" + innerMap.size()); |
| 121 | + out.accept("\n"); |
| 122 | + } |
| 123 | + out.accept(" emptyCount=" + emptyCount); |
| 124 | + out.accept("\n"); |
| 125 | + return emptyCount; |
| 126 | + } |
| 127 | +} |
0 commit comments