mirror of https://github.com/apache/maven.git
[MNG-8396] Add a cache layer to the filtered dependency graph (#1944)
This commit is contained in:
parent
a58551d9ab
commit
62f85a4233
|
@ -24,6 +24,7 @@ import java.util.IdentityHashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.maven.execution.ProjectDependencyGraph;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
|
@ -40,6 +41,10 @@ class FilteredProjectDependencyGraph implements ProjectDependencyGraph {
|
|||
|
||||
private final List<MavenProject> sortedProjects;
|
||||
|
||||
private final Map<Key, List<MavenProject>> cache = new ConcurrentHashMap<>();
|
||||
|
||||
private record Key(MavenProject project, boolean transitive, boolean upstream) {}
|
||||
|
||||
/**
|
||||
* Creates a new project dependency graph from the specified graph.
|
||||
*
|
||||
|
@ -74,12 +79,28 @@ class FilteredProjectDependencyGraph implements ProjectDependencyGraph {
|
|||
|
||||
@Override
|
||||
public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) {
|
||||
return applyFilter(projectDependencyGraph.getDownstreamProjects(project, transitive), transitive, false);
|
||||
Key key = new Key(project, transitive, false);
|
||||
// Do not use computeIfAbsent here, as the computation is recursive
|
||||
// and this is not supported by computeIfAbsent.
|
||||
List<MavenProject> list = cache.get(key);
|
||||
if (list == null) {
|
||||
list = applyFilter(projectDependencyGraph.getDownstreamProjects(project, transitive), transitive, false);
|
||||
cache.put(key, list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
|
||||
return applyFilter(projectDependencyGraph.getUpstreamProjects(project, transitive), transitive, true);
|
||||
Key key = new Key(project, transitive, true);
|
||||
// Do not use computeIfAbsent here, as the computation is recursive
|
||||
// and this is not supported by computeIfAbsent.
|
||||
List<MavenProject> list = cache.get(key);
|
||||
if (list == null) {
|
||||
list = applyFilter(projectDependencyGraph.getUpstreamProjects(project, transitive), transitive, true);
|
||||
cache.put(key, list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue