Skip to content

Commit 30304ce

Browse files
authored
✨ feat(checksum calculator): Add cache to remote checksum calculator (#1481)
1 parent bd08070 commit 30304ce

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/checksum/RemoteChecksumCalculator.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import java.net.http.HttpResponse;
1111
import java.security.MessageDigest;
1212
import java.util.Locale;
13+
import java.util.Map;
1314
import java.util.Optional;
15+
import java.util.concurrent.ConcurrentHashMap;
1416
import org.apache.maven.artifact.Artifact;
1517
import org.apache.maven.artifact.repository.ArtifactRepository;
1618
import org.apache.maven.project.ProjectBuildingRequest;
@@ -19,6 +21,8 @@ public class RemoteChecksumCalculator extends AbstractChecksumCalculator {
1921

2022
private final ProjectBuildingRequest artifactBuildingRequest;
2123
private final ProjectBuildingRequest pluginBuildingRequest;
24+
private final Map<String, String> checksumCache = new ConcurrentHashMap<>();
25+
private final Map<String, RepositoryInformation> resolvedCache = new ConcurrentHashMap<>();
2226

2327
public RemoteChecksumCalculator(
2428
String checksumAlgorithm,
@@ -37,7 +41,22 @@ public RemoteChecksumCalculator(
3741
this.pluginBuildingRequest = pluginBuildingRequest;
3842
}
3943

44+
private String getCacheKey(Artifact artifact) {
45+
String classifier = artifact.getClassifier();
46+
if (classifier == null) {
47+
classifier = "";
48+
}
49+
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + ":" + classifier
50+
+ ":" + artifact.getType();
51+
}
52+
4053
private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBuildingRequest buildingRequest) {
54+
String cacheKey = getCacheKey(artifact) + ":" + checksumAlgorithm;
55+
String cached = checksumCache.get(cacheKey);
56+
if (cached != null) {
57+
return cached.isEmpty() ? Optional.empty() : Optional.of(cached);
58+
}
59+
4160
try {
4261
String groupId = artifact.getGroupId().replace(".", "/");
4362
String artifactId = artifact.getArtifactId();
@@ -73,7 +92,9 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
7392
client.send(checksumRequest, HttpResponse.BodyHandlers.ofString());
7493

7594
if (checksumResponse.statusCode() >= 200 && checksumResponse.statusCode() < 300) {
76-
return Optional.of(checksumResponse.body().strip());
95+
String checksum = checksumResponse.body().strip();
96+
checksumCache.put(cacheKey, checksum);
97+
return Optional.of(checksum);
7798
}
7899

79100
if (checksumResponse.statusCode() == 404) {
@@ -133,6 +154,7 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
133154
String checksum = baseEncoding
134155
.encode(messageDigest.digest(artifactResponse.body()))
135156
.toLowerCase(Locale.ROOT);
157+
checksumCache.put(cacheKey, checksum);
136158
return Optional.of(checksum);
137159
}
138160
}
@@ -141,16 +163,24 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui
141163
.warn(String.format(
142164
"Artifact checksum `%s.%s` not found among remote repositories.",
143165
artifact, checksumAlgorithm));
166+
checksumCache.put(cacheKey, "");
144167
return Optional.empty();
145168
} catch (Exception e) {
146169
PluginLogManager.getLog()
147170
.warn(String.format("Could not resolve artifact: %s", artifact.getArtifactId()), e);
171+
checksumCache.put(cacheKey, "");
148172
return Optional.empty();
149173
}
150174
}
151175

152176
private Optional<RepositoryInformation> getResolvedFieldInternal(
153177
Artifact artifact, ProjectBuildingRequest buildingRequest) {
178+
String cacheKey = getCacheKey(artifact);
179+
RepositoryInformation cached = resolvedCache.get(cacheKey);
180+
if (cached != null) {
181+
return cached.equals(RepositoryInformation.Unresolved()) ? Optional.empty() : Optional.of(cached);
182+
}
183+
154184
try {
155185
String groupId = artifact.getGroupId().replace(".", "/");
156186
String artifactId = artifact.getArtifactId();
@@ -184,16 +214,20 @@ private Optional<RepositoryInformation> getResolvedFieldInternal(
184214
HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
185215

186216
if (response.statusCode() >= 200 && response.statusCode() < 300) {
187-
return Optional.of(
188-
new RepositoryInformation(ResolvedUrl.of(url), RepositoryId.of(repository.getId())));
217+
RepositoryInformation result =
218+
new RepositoryInformation(ResolvedUrl.of(url), RepositoryId.of(repository.getId()));
219+
resolvedCache.put(cacheKey, result);
220+
return Optional.of(result);
189221
}
190222
}
191223

192224
PluginLogManager.getLog().warn(String.format("Artifact resolved url `%s` not found.", artifact));
225+
resolvedCache.put(cacheKey, RepositoryInformation.Unresolved());
193226
return Optional.empty();
194227
} catch (Exception e) {
195228
PluginLogManager.getLog()
196229
.warn(String.format("Could not resolve url for artifact: %s", artifact.getArtifactId()), e);
230+
resolvedCache.put(cacheKey, RepositoryInformation.Unresolved());
197231
return Optional.empty();
198232
}
199233
}

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/checksum/RepositoryInformation.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.chains_project.maven_lockfile.data.RepositoryId;
44
import io.github.chains_project.maven_lockfile.data.ResolvedUrl;
5+
import java.util.Objects;
56

67
public class RepositoryInformation {
78
private final ResolvedUrl resolvedUrl;
@@ -29,4 +30,19 @@ public ResolvedUrl getResolvedUrl() {
2930
public RepositoryId getRepositoryId() {
3031
return repositoryId;
3132
}
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
if (o == this) return true;
37+
if (!(o instanceof RepositoryInformation)) {
38+
return false;
39+
}
40+
RepositoryInformation other = (RepositoryInformation) o;
41+
return Objects.equals(resolvedUrl, other.resolvedUrl) && Objects.equals(repositoryId, other.repositoryId);
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hash(resolvedUrl, repositoryId);
47+
}
3248
}

0 commit comments

Comments
 (0)