Simplify ReactorModelPool and use a concurrent map

This commit is contained in:
Guillaume Nodet 2023-09-20 21:11:57 +02:00
parent 59a8d90397
commit eb20034763
2 changed files with 27 additions and 54 deletions

View File

@ -345,7 +345,7 @@ public class DefaultProjectBuilder implements ProjectBuilder {
buffer.append("<packaging>").append(artifact.getType()).append("</packaging>"); buffer.append("<packaging>").append(artifact.getType()).append("</packaging>");
buffer.append("</project>"); buffer.append("</project>");
return new StringModelSource(buffer, artifact.getId()); return new StringModelSource(buffer.toString(), artifact.getId());
} }
@Override @Override
@ -355,24 +355,15 @@ public class DefaultProjectBuilder implements ProjectBuilder {
List<InterimResult> interimResults = new ArrayList<>(); List<InterimResult> interimResults = new ArrayList<>();
ReactorModelPool.Builder poolBuilder = new ReactorModelPool.Builder(); ReactorModelPool pool = new ReactorModelPool();
final ReactorModelPool modelPool = poolBuilder.build();
InternalConfig config = new InternalConfig(request, modelPool, modelBuilder.newTransformerContextBuilder()); InternalConfig config = new InternalConfig(request, pool, modelBuilder.newTransformerContextBuilder());
Map<File, MavenProject> projectIndex = new HashMap<>(256); Map<File, MavenProject> projectIndex = new HashMap<>(256);
// phase 1: get file Models from the reactor. // phase 1: get file Models from the reactor.
boolean noErrors = build( boolean noErrors = build(
results, results, interimResults, projectIndex, pomFiles, new LinkedHashSet<>(), true, recursive, config, pool);
interimResults,
projectIndex,
pomFiles,
new LinkedHashSet<>(),
true,
recursive,
config,
poolBuilder);
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
@ -414,22 +405,14 @@ public class DefaultProjectBuilder implements ProjectBuilder {
boolean root, boolean root,
boolean recursive, boolean recursive,
InternalConfig config, InternalConfig config,
ReactorModelPool.Builder poolBuilder) { ReactorModelPool pool) {
boolean noErrors = true; boolean noErrors = true;
for (File pomFile : pomFiles) { for (File pomFile : pomFiles) {
aggregatorFiles.add(pomFile); aggregatorFiles.add(pomFile);
if (!build( if (!build(
results, results, interimResults, projectIndex, pomFile, aggregatorFiles, root, recursive, config, pool)) {
interimResults,
projectIndex,
pomFile,
aggregatorFiles,
root,
recursive,
config,
poolBuilder)) {
noErrors = false; noErrors = false;
} }
@ -449,7 +432,7 @@ public class DefaultProjectBuilder implements ProjectBuilder {
boolean isRoot, boolean isRoot,
boolean recursive, boolean recursive,
InternalConfig config, InternalConfig config,
ReactorModelPool.Builder poolBuilder) { ReactorModelPool pool) {
boolean noErrors = true; boolean noErrors = true;
MavenProject project = new MavenProject(); MavenProject project = new MavenProject();
@ -483,7 +466,7 @@ public class DefaultProjectBuilder implements ProjectBuilder {
Model model = request.getFileModel(); Model model = request.getFileModel();
poolBuilder.put(model.getPomFile().toPath(), model); pool.put(model.getPomFile().toPath(), model);
InterimResult interimResult = new InterimResult(pomFile, request, result, listener, isRoot); InterimResult interimResult = new InterimResult(pomFile, request, result, listener, isRoot);
interimResults.add(interimResult); interimResults.add(interimResult);
@ -563,7 +546,7 @@ public class DefaultProjectBuilder implements ProjectBuilder {
false, false,
recursive, recursive,
config, config,
poolBuilder)) { pool)) {
noErrors = false; noErrors = false;
} }
} }

View File

@ -20,11 +20,11 @@ package org.apache.maven.project;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.maven.model.Model; import org.apache.maven.model.Model;
@ -34,9 +34,9 @@ import org.apache.maven.model.Model;
* *
*/ */
class ReactorModelPool { class ReactorModelPool {
private final Map<GAKey, Set<Model>> modelsByGa = new HashMap<>(); private final Map<GAKey, Set<Model>> modelsByGa = new ConcurrentHashMap<>();
private final Map<Path, Model> modelsByPath = new HashMap<>(); private final Map<Path, Model> modelsByPath = new ConcurrentHashMap<>();
/** /**
* Get the model by its GAV or (since 4.0.0) by its GA if there is only one. * Get the model by its GAV or (since 4.0.0) by its GA if there is only one.
@ -65,28 +65,19 @@ class ReactorModelPool {
return version; return version;
} }
static class Builder { void put(Path pomFile, Model model) {
private ReactorModelPool pool = new ReactorModelPool(); modelsByPath.put(pomFile, model);
modelsByGa
.computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<>())
.add(model);
}
Builder put(Path pomFile, Model model) { private static String getGroupId(Model model) {
pool.modelsByPath.put(pomFile, model); String groupId = model.getGroupId();
pool.modelsByGa if (groupId == null && model.getParent() != null) {
.computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<Model>()) groupId = model.getParent().getGroupId();
.add(model);
return this;
}
ReactorModelPool build() {
return pool;
}
private static String getGroupId(Model model) {
String groupId = model.getGroupId();
if (groupId == null && model.getParent() != null) {
groupId = model.getParent().getGroupId();
}
return groupId;
} }
return groupId;
} }
private static final class GAKey { private static final class GAKey {
@ -109,9 +100,10 @@ class ReactorModelPool {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (!(obj instanceof GAKey)) {
return false;
}
GAKey that = (GAKey) obj; GAKey that = (GAKey) obj;
return artifactId.equals(that.artifactId) && groupId.equals(that.groupId); return artifactId.equals(that.artifactId) && groupId.equals(that.groupId);
} }
@ -122,9 +114,7 @@ class ReactorModelPool {
@Override @Override
public String toString() { public String toString() {
StringBuilder buffer = new StringBuilder(128); return groupId + ':' + artifactId;
buffer.append(groupId).append(':').append(artifactId);
return buffer.toString();
} }
} }
} }