Skip to content

Commit

Permalink
list-files parallel fix (#1013)
Browse files Browse the repository at this point in the history
Avoid merging listed files, use a map to store the results of each iteration
  • Loading branch information
romain-grecourt authored Jan 9, 2024
1 parent 8cd38ed commit ecb5a54
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
17 changes: 14 additions & 3 deletions maven-plugins/stager-maven-plugin/src/it/projects/file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,26 @@
<file target="docs/4.0.2/index.html"/>
<file target="docs/4.0.2/apidocs/index.html"/>
<file target="docs/4.0.2/images/foo.svg"/>
<file target="docs/3.2.2/index.html"/>
<file target="docs/3.2.2/apidocs/index.html"/>
</files>
<symlinks join="true">
<symlink source="docs/4.0.2" target="docs/v4"/>
<symlink source="docs/3.2.2" target="docs/v3"/>
</symlinks>
<files join="true">
<file target="sitemap.txt">
<list-files dir=".">
<file target="docs/{version}/sitemap.txt">
<iterators>
<variables>
<variable name="version">
<value>v3</value>
<value>v4</value>
</variable>
</variables>
</iterators>
<list-files dir="docs/{version}">
<includes>
<include>**/docs/**/*.html</include>
<include>**/*.html</include>
</includes>
<excludes>
<exclude>**/images/**</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars)
} else {
try (BufferedWriter writer = Files.newBufferedWriter(targetFile)) {
for (TextAction task : tasks()) {
writer.write(task.text());
writer.write(task.text(vars));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;

import io.helidon.build.common.Lists;
Expand All @@ -43,7 +44,7 @@ final class ListFilesTask extends StagingTask implements TextAction {
private final List<Substitution> substitutions;
private final List<BiFunction<String, Map<String, String>, String>> chain;
private final String dirName;
private final StringBuilder buf = new StringBuilder();
private final Map<Map<String, String>, String> results = new ConcurrentHashMap<>();

ListFilesTask(ActionIterators iterators,
List<Include> includes,
Expand All @@ -60,8 +61,8 @@ final class ListFilesTask extends StagingTask implements TextAction {
}

@Override
public String text() {
return buf.toString();
public String text(Map<String, String> vars) {
return results.getOrDefault(vars, "");
}

List<String> includes() {
Expand All @@ -78,15 +79,17 @@ List<Substitution> substitutions() {

@Override
protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars) {
StringBuilder sb = new StringBuilder();
Path resolved = dir.resolve(resolveVar(dirName, vars));
List<Path> files = walk(resolved, FILE_VISIT_OPTIONS, this::filter);
for (Path file : files) {
String entry = normalizePath(dir.relativize(file));
for (var function : chain) {
entry = function.apply(entry, vars);
}
buf.append(entry).append("\n");
sb.append(entry).append("\n");
}
results.put(vars, sb.toString());
}

private boolean filter(Path p, BasicFileAttributes attrs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.helidon.build.maven.stager;

import java.util.Map;

/**
* A {@link StagingAction} that produces text.
*/
Expand All @@ -25,5 +27,5 @@ interface TextAction extends StagingAction {
*
* @return text
*/
String text();
String text(Map<String, String> vars);
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,21 @@ void testFile(String basedir) throws IOException {
assertThat(stageDir.resolve("docs/v4/apidocs/index.html"), fileExists());
assertThat(stageDir.resolve("docs/v4/images/foo.svg"), fileExists());

Path file3 = stageDir.resolve("sitemap.txt");
Path file3 = stageDir.resolve("docs/v4/sitemap.txt");
assertThat(file3, fileExists());
assertThat(Files.readAllLines(file3), containsInAnyOrder(
"docs/v4",
"docs/v4/apidocs",
"docs/4.0.2",
"docs/4.0.2/apidocs"
"docs/v4/apidocs"
));

assertThat(stageDir.resolve("docs/v3/index.html"), fileExists());
assertThat(stageDir.resolve("docs/v3/apidocs/index.html"), fileExists());

Path file4 = stageDir.resolve("docs/v3/sitemap.txt");
assertThat(file4, fileExists());
assertThat(Files.readAllLines(file4), containsInAnyOrder(
"docs/v3",
"docs/v3/apidocs"
));
}

Expand Down

0 comments on commit ecb5a54

Please sign in to comment.