From eb20034763e3eb181a4de75a1fbb49ce62dee709 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 20 Sep 2023 21:11:57 +0200 Subject: [PATCH] Simplify ReactorModelPool and use a concurrent map --- .../maven/project/DefaultProjectBuilder.java | 35 ++++---------- .../maven/project/ReactorModelPool.java | 46 ++++++++----------- 2 files changed, 27 insertions(+), 54 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 8615ac4b8d..341dee9261 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -345,7 +345,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { buffer.append("").append(artifact.getType()).append(""); buffer.append(""); - return new StringModelSource(buffer, artifact.getId()); + return new StringModelSource(buffer.toString(), artifact.getId()); } @Override @@ -355,24 +355,15 @@ public class DefaultProjectBuilder implements ProjectBuilder { List interimResults = new ArrayList<>(); - ReactorModelPool.Builder poolBuilder = new ReactorModelPool.Builder(); - final ReactorModelPool modelPool = poolBuilder.build(); + ReactorModelPool pool = new ReactorModelPool(); - InternalConfig config = new InternalConfig(request, modelPool, modelBuilder.newTransformerContextBuilder()); + InternalConfig config = new InternalConfig(request, pool, modelBuilder.newTransformerContextBuilder()); Map projectIndex = new HashMap<>(256); // phase 1: get file Models from the reactor. boolean noErrors = build( - results, - interimResults, - projectIndex, - pomFiles, - new LinkedHashSet<>(), - true, - recursive, - config, - poolBuilder); + results, interimResults, projectIndex, pomFiles, new LinkedHashSet<>(), true, recursive, config, pool); ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader(); @@ -414,22 +405,14 @@ public class DefaultProjectBuilder implements ProjectBuilder { boolean root, boolean recursive, InternalConfig config, - ReactorModelPool.Builder poolBuilder) { + ReactorModelPool pool) { boolean noErrors = true; for (File pomFile : pomFiles) { aggregatorFiles.add(pomFile); if (!build( - results, - interimResults, - projectIndex, - pomFile, - aggregatorFiles, - root, - recursive, - config, - poolBuilder)) { + results, interimResults, projectIndex, pomFile, aggregatorFiles, root, recursive, config, pool)) { noErrors = false; } @@ -449,7 +432,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { boolean isRoot, boolean recursive, InternalConfig config, - ReactorModelPool.Builder poolBuilder) { + ReactorModelPool pool) { boolean noErrors = true; MavenProject project = new MavenProject(); @@ -483,7 +466,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { 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); interimResults.add(interimResult); @@ -563,7 +546,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { false, recursive, config, - poolBuilder)) { + pool)) { noErrors = false; } } diff --git a/maven-core/src/main/java/org/apache/maven/project/ReactorModelPool.java b/maven-core/src/main/java/org/apache/maven/project/ReactorModelPool.java index 43f9eb0871..1a17572371 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ReactorModelPool.java +++ b/maven-core/src/main/java/org/apache/maven/project/ReactorModelPool.java @@ -20,11 +20,11 @@ package org.apache.maven.project; import java.nio.file.Path; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.apache.maven.model.Model; @@ -34,9 +34,9 @@ import org.apache.maven.model.Model; * */ class ReactorModelPool { - private final Map> modelsByGa = new HashMap<>(); + private final Map> modelsByGa = new ConcurrentHashMap<>(); - private final Map modelsByPath = new HashMap<>(); + private final Map modelsByPath = new ConcurrentHashMap<>(); /** * 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; } - static class Builder { - private ReactorModelPool pool = new ReactorModelPool(); + void put(Path pomFile, Model model) { + modelsByPath.put(pomFile, model); + modelsByGa + .computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<>()) + .add(model); + } - Builder put(Path pomFile, Model model) { - pool.modelsByPath.put(pomFile, model); - pool.modelsByGa - .computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet()) - .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; + private static String getGroupId(Model model) { + String groupId = model.getGroupId(); + if (groupId == null && model.getParent() != null) { + groupId = model.getParent().getGroupId(); } + return groupId; } private static final class GAKey { @@ -109,9 +100,10 @@ class ReactorModelPool { if (this == obj) { return true; } - + if (!(obj instanceof GAKey)) { + return false; + } GAKey that = (GAKey) obj; - return artifactId.equals(that.artifactId) && groupId.equals(that.groupId); } @@ -122,9 +114,7 @@ class ReactorModelPool { @Override public String toString() { - StringBuilder buffer = new StringBuilder(128); - buffer.append(groupId).append(':').append(artifactId); - return buffer.toString(); + return groupId + ':' + artifactId; } } }