Skip to content

Commit 4dab0af

Browse files
committed
Remove CacheMapAdapter and use Cache directly in AbstractSession
- Remove CacheMapAdapter.java as it's no longer needed - Update DefaultRequestCache to use Cache<K,V> directly instead of Map adapter - Update AbstractSession to use Cache for all internal caching: * services cache: Map -> Cache * allNodes cache: WeakHashMap -> Cache * allArtifacts cache: ConcurrentHashMap<Class, Map> -> Cache<Class, Cache> * allRepositories cache: WeakHashMap -> Cache * allDependencies cache: WeakHashMap -> Cache - This brings the implementation closer to the original PR apache#2506 - All 12 cache tests continue to pass - Maintains thread safety and memory efficiency with soft references
1 parent d1d04ba commit 4dab0af

File tree

3 files changed

+17
-128
lines changed

3 files changed

+17
-128
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/AbstractSession.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.util.NoSuchElementException;
2929
import java.util.Objects;
3030
import java.util.Optional;
31-
import java.util.WeakHashMap;
32-
import java.util.concurrent.ConcurrentHashMap;
3331
import java.util.concurrent.CopyOnWriteArrayList;
3432
import java.util.function.Function;
3533
import java.util.function.Supplier;
@@ -96,6 +94,7 @@
9694
import org.apache.maven.api.services.VersionRangeResolver;
9795
import org.apache.maven.api.services.VersionResolver;
9896
import org.apache.maven.api.services.VersionResolverException;
97+
import org.apache.maven.impl.cache.Cache;
9998
import org.eclipse.aether.DefaultRepositorySystemSession;
10099
import org.eclipse.aether.RepositorySystem;
101100
import org.eclipse.aether.RepositorySystemSession;
@@ -112,16 +111,14 @@ public abstract class AbstractSession implements InternalSession {
112111
protected final RepositorySystem repositorySystem;
113112
protected final List<RemoteRepository> repositories;
114113
protected final Lookup lookup;
115-
private final Map<Class<? extends Service>, Service> services = new ConcurrentHashMap<>();
114+
private final Cache<Class<? extends Service>, Service> services = Cache.newCache();
116115
private final List<Listener> listeners = new CopyOnWriteArrayList<>();
117-
private final Map<org.eclipse.aether.graph.DependencyNode, Node> allNodes =
118-
Collections.synchronizedMap(new WeakHashMap<>());
119-
private final Map<Class<? extends Artifact>, Map<org.eclipse.aether.artifact.Artifact, Artifact>> allArtifacts =
120-
new ConcurrentHashMap<>();
121-
private final Map<org.eclipse.aether.repository.RemoteRepository, RemoteRepository> allRepositories =
122-
Collections.synchronizedMap(new WeakHashMap<>());
123-
private final Map<org.eclipse.aether.graph.Dependency, Dependency> allDependencies =
124-
Collections.synchronizedMap(new WeakHashMap<>());
116+
private final Cache<org.eclipse.aether.graph.DependencyNode, Node> allNodes = Cache.newCache();
117+
private final Cache<Class<? extends Artifact>, Cache<org.eclipse.aether.artifact.Artifact, Artifact>> allArtifacts =
118+
Cache.newCache();
119+
private final Cache<org.eclipse.aether.repository.RemoteRepository, RemoteRepository> allRepositories =
120+
Cache.newCache();
121+
private final Cache<org.eclipse.aether.graph.Dependency, Dependency> allDependencies = Cache.newCache();
125122
private volatile RequestCache requestCache;
126123

127124
static {
@@ -225,18 +222,18 @@ public Artifact getArtifact(@Nonnull org.eclipse.aether.artifact.Artifact artifa
225222
@SuppressWarnings("unchecked")
226223
@Override
227224
public <T extends Artifact> T getArtifact(Class<T> clazz, org.eclipse.aether.artifact.Artifact artifact) {
228-
Map<org.eclipse.aether.artifact.Artifact, Artifact> map =
229-
allArtifacts.computeIfAbsent(clazz, c -> Collections.synchronizedMap(new WeakHashMap<>()));
225+
Cache<org.eclipse.aether.artifact.Artifact, Artifact> cache =
226+
allArtifacts.computeIfAbsent(clazz, c -> Cache.newCache());
230227
if (clazz == Artifact.class) {
231-
return (T) map.computeIfAbsent(artifact, a -> new DefaultArtifact(this, a));
228+
return (T) cache.computeIfAbsent(artifact, a -> new DefaultArtifact(this, a));
232229
} else if (clazz == DownloadedArtifact.class) {
233230
if (artifact.getPath() == null) {
234231
throw new IllegalArgumentException("The given artifact is not resolved");
235232
} else {
236-
return (T) map.computeIfAbsent(artifact, a -> new DefaultDownloadedArtifact(this, a));
233+
return (T) cache.computeIfAbsent(artifact, a -> new DefaultDownloadedArtifact(this, a));
237234
}
238235
} else if (clazz == ProducedArtifact.class) {
239-
return (T) map.computeIfAbsent(artifact, a -> new DefaultProducedArtifact(this, a));
236+
return (T) cache.computeIfAbsent(artifact, a -> new DefaultProducedArtifact(this, a));
240237
} else {
241238
throw new IllegalArgumentException("Unsupported Artifact class: " + clazz);
242239
}

impl/maven-impl/src/main/java/org/apache/maven/impl/cache/CacheMapAdapter.java

Lines changed: 0 additions & 108 deletions
This file was deleted.

impl/maven-impl/src/main/java/org/apache/maven/impl/cache/DefaultRequestCache.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class DefaultRequestCache extends AbstractRequestCache {
3838
SessionData.key(ConcurrentMap.class, CacheMetadata.class);
3939
protected static final Object ROOT = new Object();
4040

41-
protected final Map<Object, CachingSupplier<?, ?>> forever = new ConcurrentHashMap<>();
41+
protected final Cache<Object, CachingSupplier<?, ?>> forever = Cache.newCache();
4242

4343
@Override
4444
@SuppressWarnings("unchecked")
@@ -48,13 +48,13 @@ protected <REQ extends Request<?>, REP extends Result<REQ>> CachingSupplier<REQ,
4848
req instanceof CacheMetadata metadata ? metadata.getCacheRetention() : null,
4949
CacheRetention.SESSION_SCOPED);
5050

51-
Map<Object, CachingSupplier<?, ?>> cache = null;
51+
Cache<Object, CachingSupplier<?, ?>> cache = null;
5252
if ((retention == CacheRetention.REQUEST_SCOPED || retention == CacheRetention.SESSION_SCOPED)
5353
&& req.getSession() instanceof Session session) {
5454
Object key = retention == CacheRetention.REQUEST_SCOPED ? doGetOuterRequest(req) : ROOT;
55-
Map<Object, Map<Object, CachingSupplier<?, ?>>> caches =
55+
Map<Object, Cache<Object, CachingSupplier<?, ?>>> caches =
5656
session.getData().computeIfAbsent(KEY, ConcurrentHashMap::new);
57-
cache = caches.computeIfAbsent(key, k -> new CacheMapAdapter<>());
57+
cache = caches.computeIfAbsent(key, k -> Cache.newCache());
5858
} else if (retention == CacheRetention.PERSISTENT) {
5959
cache = forever;
6060
}

0 commit comments

Comments
 (0)