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("</project>");
return new StringModelSource(buffer, artifact.getId());
return new StringModelSource(buffer.toString(), artifact.getId());
}
@Override
@ -355,24 +355,15 @@ public class DefaultProjectBuilder implements ProjectBuilder {
List<InterimResult> 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<File, MavenProject> 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;
}
}

View File

@ -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<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.
@ -65,19 +65,11 @@ class ReactorModelPool {
return version;
}
static class Builder {
private ReactorModelPool pool = new ReactorModelPool();
Builder put(Path pomFile, Model model) {
pool.modelsByPath.put(pomFile, model);
pool.modelsByGa
.computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<Model>())
void put(Path pomFile, Model model) {
modelsByPath.put(pomFile, model);
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) {
@ -87,7 +79,6 @@ class ReactorModelPool {
}
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;
}
}
}