Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel processing of files #568

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -360,7 +361,7 @@ public abstract class AbstractFileHeaderMojo extends AbstractLicenseNameMojo {
/**
* Dictionary of treated files indexed by their state.
*/
EnumMap<FileState, Set<File>> result;
Map<FileState, Set<File>> result;

/**
* Dictionary of files to treat indexed by their CommentStyle.
Expand Down Expand Up @@ -533,7 +534,7 @@ public void doAction() throws Exception {

long t0 = System.nanoTime();

processedFiles = new HashSet<>();
processedFiles = ConcurrentHashMap.newKeySet();
result = new EnumMap<>(FileState.class);

try {
Expand Down Expand Up @@ -586,7 +587,7 @@ public void doAction() throws Exception {
* @param result processed files by their status
* @throws MojoFailureException if check is not ok (some file with no header or to update)
*/
private void checkResults(EnumMap<FileState, Set<File>> result) throws MojoFailureException {
private void checkResults(Map<FileState, Set<File>> result) throws MojoFailureException {
Set<FileState> states = result.keySet();

StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -633,10 +634,18 @@ private void processCommentStyle(String commentStyle, List<File> filesToTreat) t

// use header transformer according to comment style given in header
FileHeaderTransformer transformer = getTransformer(transformers, commentStyle);
FileHeaderProcessor processor = getFileHeaderProcessor(license, transformer);

for (File file : filesToTreat) {
processFile(processor, file);
try {
filesToTreat.parallelStream().forEach(file -> {
try {
FileHeaderProcessor processor = getFileHeaderProcessor(license, transformer);
processFile(processor, file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
} catch (UncheckedIOException e) {
throw e.getCause();
}
filesToTreat.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -388,7 +387,7 @@ FileHeaderTransformer getTransformer(Map<String, FileHeaderTransformer> transfor
* @param state state of file to report
* @param buffer where to report
*/
void reportType(EnumMap<FileState, Set<File>> result, FileState state, StringBuilder buffer) {
void reportType(Map<FileState, Set<File>> result, FileState state, StringBuilder buffer) {
String operation = state.name();

Set<File> set = getFiles(result, state);
Expand Down Expand Up @@ -499,7 +498,7 @@ Map<String, List<File>> obtainFilesToProcessByCommentStyle(
* @param state state of files to get
* @return all files of the given state
*/
private Set<File> getFiles(EnumMap<FileState, Set<File>> result, FileState state) {
private Set<File> getFiles(Map<FileState, Set<File>> result, FileState state) {
return result.get(state);
}

Expand Down
13 changes: 4 additions & 9 deletions src/main/java/org/codehaus/mojo/license/FileState.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/

import java.io.File;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* Defines state of a file after process.
Expand Down Expand Up @@ -70,12 +70,7 @@ public enum FileState {
* @param file file to add
* @param results dictionary to update
*/
public void addFile(File file, EnumMap<FileState, Set<File>> results) {
Set<File> fileSet = results.get(this);
if (fileSet == null) {
fileSet = new HashSet<>();
results.put(this, fileSet);
}
fileSet.add(file);
public void addFile(File file, Map<FileState, Set<File>> results) {
results.computeIfAbsent(this, k -> ConcurrentHashMap.newKeySet()).add(file);
}
}