|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT GmbH) - initial implementation. |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.lsp4e.internal; |
| 13 | + |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.WeakHashMap; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | +import java.util.concurrent.ConcurrentMap; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.function.Supplier; |
| 23 | + |
| 24 | +import org.eclipse.jdt.annotation.Nullable; |
| 25 | +import org.eclipse.jface.text.IDocument; |
| 26 | +import org.eclipse.jface.text.IDocumentExtension4; |
| 27 | + |
| 28 | +/** |
| 29 | + * Generic, per-document+offset cache for asynchronous results that avoids |
| 30 | + * starting the same work twice by sharing a single running task. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * Features: |
| 34 | + * <li>Weakly keys by {@link IDocument} to avoid memory leaks. |
| 35 | + * <li>Per-document concurrent maps for thread-safe access from UI and |
| 36 | + * background. |
| 37 | + * <li>Eviction: TTL-based using {@link System#nanoTime()} and document-change |
| 38 | + * invalidation when a stable modification stamp is available. |
| 39 | + * <li>In-flight de-duplication: only one running task per document+offset. |
| 40 | + * <li>Stale-result protection: if the document changes while a value is being |
| 41 | + * computed, the result is delivered to callers but is not cached. |
| 42 | + */ |
| 43 | +public final class DocumentOffsetAsyncCache<V> { |
| 44 | + |
| 45 | + private record Entry<V>(V value, long createdNanos, long docModStamp) { |
| 46 | + boolean stale(final long ttlNanos, final long currentDocStamp) { |
| 47 | + return System.nanoTime() - createdNanos > ttlNanos // |
| 48 | + // Invalidate when we can confidently detect a document change |
| 49 | + || (docModStamp != IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP |
| 50 | + && currentDocStamp != IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP |
| 51 | + && docModStamp != currentDocStamp); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private final Map<IDocument, ConcurrentMap<Integer, Entry<V>>> cache = Collections |
| 56 | + .synchronizedMap(new WeakHashMap<>()); |
| 57 | + private final Map<IDocument, ConcurrentMap<Integer, CompletableFuture<V>>> inFlight = Collections |
| 58 | + .synchronizedMap(new WeakHashMap<>()); |
| 59 | + |
| 60 | + private final long ttlNanos; |
| 61 | + |
| 62 | + public DocumentOffsetAsyncCache(final Duration ttl) { |
| 63 | + this.ttlNanos = TimeUnit.MILLISECONDS.toNanos(ttl.toMillis()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns cached value if present and valid; otherwise returns the single |
| 68 | + * running task or starts one via {@code supplier}. A value is valid if it has |
| 69 | + * not expired by TTL and (when stamps are available) matches the current |
| 70 | + * document stamp. Results computed for an older stamp are not cached. |
| 71 | + */ |
| 72 | + public CompletableFuture<V> computeIfAbsent(final IDocument doc, final int offset, |
| 73 | + final Supplier<CompletableFuture<V>> supplier) { |
| 74 | + // Fast path: return a completed future if a fresh value is already cached |
| 75 | + final @Nullable V cachedNow = getNow(doc, offset); |
| 76 | + if (cachedNow != null) |
| 77 | + return CompletableFuture.completedFuture(cachedNow); |
| 78 | + |
| 79 | + final ConcurrentMap<Integer, CompletableFuture<V>> byOffset = inFlight.computeIfAbsent(doc, |
| 80 | + d -> new ConcurrentHashMap<>()); |
| 81 | + return byOffset.computeIfAbsent(offset, k -> { |
| 82 | + final long startStamp = DocumentUtil.getDocumentModificationStamp(doc); |
| 83 | + final CompletableFuture<V> cf = supplier.get(); |
| 84 | + cf.whenComplete((v, t) -> { |
| 85 | + // Always clean up the in-flight entry by key. Only one future exists |
| 86 | + // per offset due to computeIfAbsent, so this is safe and avoids capturing |
| 87 | + // a specific future instance. |
| 88 | + byOffset.remove(offset); |
| 89 | + if (t == null && v != null) { |
| 90 | + final long nowStamp = DocumentUtil.getDocumentModificationStamp(doc); |
| 91 | + if (startStamp == IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP |
| 92 | + || nowStamp == IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP || nowStamp == startStamp) { |
| 93 | + put(doc, offset, v); |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | + return cf; |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return the cached value if present and valid; removes and returns null if |
| 103 | + * TTL expired or the document stamp changed. |
| 104 | + */ |
| 105 | + public @Nullable V getNow(final IDocument doc, final int offset) { |
| 106 | + final ConcurrentMap<Integer, Entry<V>> byOffset = cache.get(doc); |
| 107 | + if (byOffset == null) |
| 108 | + return null; |
| 109 | + |
| 110 | + final Entry<V> e = byOffset.get(offset); |
| 111 | + if (e == null) |
| 112 | + return null; |
| 113 | + |
| 114 | + final long nowStamp = DocumentUtil.getDocumentModificationStamp(doc); |
| 115 | + if (e.stale(ttlNanos, nowStamp)) { |
| 116 | + byOffset.remove(offset, e); |
| 117 | + return null; |
| 118 | + } |
| 119 | + return e.value; |
| 120 | + } |
| 121 | + |
| 122 | + public void invalidate(final IDocument doc) { |
| 123 | + cache.remove(doc); // synchronizedMap handles its own locking |
| 124 | + final var map = inFlight.remove(doc); // remove returns the per-doc map, if any |
| 125 | + if (map != null) { |
| 126 | + map.values().forEach(f -> f.cancel(true)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Stores a value tagged with the current document modification stamp. |
| 132 | + */ |
| 133 | + public void put(final IDocument doc, final int offset, final V value) { |
| 134 | + cache.compute(doc, (d, byOffset) -> { |
| 135 | + final ConcurrentMap<Integer, Entry<V>> map = byOffset != null ? byOffset : new ConcurrentHashMap<>(); |
| 136 | + final long stamp = DocumentUtil.getDocumentModificationStamp(doc); |
| 137 | + map.put(offset, new Entry<>(value, System.nanoTime(), stamp)); |
| 138 | + return map; |
| 139 | + }); |
| 140 | + } |
| 141 | +} |
0 commit comments