|
| 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.util.Collections; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.WeakHashMap; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
| 19 | +import java.util.concurrent.ConcurrentMap; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.function.Supplier; |
| 22 | + |
| 23 | +import org.eclipse.jdt.annotation.Nullable; |
| 24 | +import org.eclipse.jface.text.IDocument; |
| 25 | + |
| 26 | +/** |
| 27 | + * Generic, per-document+offset cache for asynchronous results that avoids |
| 28 | + * starting the same work twice by sharing a single running task. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * Features: |
| 32 | + * <li>Weakly keys by {@link IDocument} to avoid memory leaks. |
| 33 | + * <li>Per-document concurrent maps for thread-safe access from UI and |
| 34 | + * background. |
| 35 | + * <li>TTL-based eviction using {@link System#nanoTime()} for monotonic timing. |
| 36 | + * <li>Atomic in-flight de-duplication using |
| 37 | + * {@link ConcurrentMap#computeIfAbsent(Object, java.util.function.Function)} to |
| 38 | + * ensure only one running task per document+offset. |
| 39 | + */ |
| 40 | +public final class DocumentOffsetAsyncCache<V> { |
| 41 | + |
| 42 | + private static final class Entry<V> { |
| 43 | + final long createdNanos; |
| 44 | + final V value; |
| 45 | + |
| 46 | + Entry(final V value) { |
| 47 | + this.value = value; |
| 48 | + this.createdNanos = System.nanoTime(); |
| 49 | + } |
| 50 | + |
| 51 | + boolean expired(final long ttlNanos) { |
| 52 | + return System.nanoTime() - createdNanos > ttlNanos; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private final Map<IDocument, ConcurrentMap<Integer, Entry<V>>> cache = Collections |
| 57 | + .synchronizedMap(new WeakHashMap<>()); |
| 58 | + private final Map<IDocument, ConcurrentMap<Integer, CompletableFuture<V>>> inFlight = Collections |
| 59 | + .synchronizedMap(new WeakHashMap<>()); |
| 60 | + |
| 61 | + private final long ttlNanos; |
| 62 | + |
| 63 | + public DocumentOffsetAsyncCache(final long ttlMillis) { |
| 64 | + this.ttlNanos = TimeUnit.MILLISECONDS.toNanos(ttlMillis); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns a completed future with a cached value when available; otherwise, |
| 69 | + * returns the single running task for this {@code doc+offset}, or starts a new |
| 70 | + * one using {@code supplier}. On successful completion, the result is 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 | + |
| 80 | + final ConcurrentMap<Integer, CompletableFuture<V>> byOffset; |
| 81 | + synchronized (inFlight) { |
| 82 | + byOffset = inFlight.computeIfAbsent(doc, d -> new ConcurrentHashMap<>()); |
| 83 | + } |
| 84 | + return byOffset.computeIfAbsent(offset, k -> { |
| 85 | + final CompletableFuture<V> cf = supplier.get(); |
| 86 | + cf.whenComplete((v, t) -> { |
| 87 | + // Always clean up the in-flight entry by key. Only one future exists |
| 88 | + // per offset due to computeIfAbsent, so this is safe and avoids capturing |
| 89 | + // a specific future instance. |
| 90 | + byOffset.remove(offset); |
| 91 | + if (t == null && v != null) { |
| 92 | + put(doc, offset, v); |
| 93 | + } |
| 94 | + }); |
| 95 | + return cf; |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + public @Nullable V getNow(final IDocument doc, final int offset) { |
| 100 | + final ConcurrentMap<Integer, Entry<V>> byOffset = cache.get(doc); |
| 101 | + if (byOffset == null) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + final Entry<V> e = byOffset.get(offset); |
| 105 | + if (e == null) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + if (e.expired(ttlNanos)) { |
| 109 | + byOffset.remove(offset, e); |
| 110 | + return null; |
| 111 | + } |
| 112 | + return e.value; |
| 113 | + } |
| 114 | + |
| 115 | + public void invalidate(final IDocument doc) { |
| 116 | + cache.remove(doc); // synchronizedMap handles its own locking |
| 117 | + final var map = inFlight.remove(doc); // remove returns the per-doc map, if any |
| 118 | + if (map != null) { |
| 119 | + map.values().forEach(f -> f.cancel(true)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public void put(final IDocument doc, final int offset, final V value) { |
| 124 | + final ConcurrentMap<Integer, Entry<V>> byOffset; |
| 125 | + synchronized (cache) { |
| 126 | + byOffset = cache.computeIfAbsent(doc, d -> new ConcurrentHashMap<>()); |
| 127 | + } |
| 128 | + byOffset.put(offset, new Entry<>(value)); |
| 129 | + } |
| 130 | +} |
0 commit comments