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

Make ClientFileCollector parallel #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ subprojects {
group = 'com.skcraft'
version = '4.3-SNAPSHOT'

sourceCompatibility = 1.6
targetCompatibility = 1.6
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
import com.skcraft.launcher.model.modpack.FileInstall;
import com.skcraft.launcher.model.modpack.Manifest;
import lombok.NonNull;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.extern.java.Log;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;

/**
* Walks a path and adds hashed path versions to the given
Expand Down Expand Up @@ -51,13 +57,41 @@ public ClientFileCollector(@NonNull Manifest manifest, @NonNull PropertiesApplic
protected DirectoryBehavior getBehavior(@NonNull String name) {
return getDirectoryBehavior(name);
}

@AllArgsConstructor
@EqualsAndHashCode
private static class FileEntry {
final File file;
final String relPath;
}

private final ArrayList<FileEntry> fileEntries = new ArrayList<>();

@Override
protected void onFile(File file, String relPath) throws IOException {
if (file.getName().endsWith(FileInfoScanner.FILE_SUFFIX) || file.getName().endsWith(URL_FILE_SUFFIX)) {
return;
}

fileEntries.add(new FileEntry(file, relPath));
}

@Override
protected void onWalkComplete() {
long start = System.currentTimeMillis();
fileEntries.parallelStream().forEach(fileEntry -> {
try {
addFile(fileEntry.file, fileEntry.relPath);
} catch (IOException e) {
log.log(Level.SEVERE, "Error processing file.", e);
throw new RuntimeException(e);
}
});
long stop = System.currentTimeMillis();
log.info("Finished processing " + fileEntries.size() + " files in " + (stop - start) + "ms.");
fileEntries.clear();
}

private void addFile(File file, String relPath) throws IOException {
FileInstall entry = new FileInstall();
String hash = Files.hash(file, hf).toString();
String to = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(relPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.skcraft.launcher.builder;

import lombok.NonNull;
import lombok.extern.java.Log;

import java.io.File;
import java.io.IOException;
Expand All @@ -16,6 +17,7 @@
* path (which may be modified by dropping certain directory entries),
* and call {@link #onFile(java.io.File, String)} with each file.
*/
@Log
public abstract class DirectoryWalker {

public enum DirectoryBehavior {
Expand All @@ -40,7 +42,11 @@ public enum DirectoryBehavior {
* @throws IOException thrown on I/O error
*/
public final void walk(@NonNull File dir) throws IOException {
walk(dir, "");
long start = System.currentTimeMillis();
walk(dir, "");
long stop = System.currentTimeMillis();
log.info("Directory walk complete in " + (stop - start) + "ms.");
this.onWalkComplete();
}

/**
Expand Down Expand Up @@ -80,7 +86,7 @@ private void walk(@NonNull File dir, @NonNull String basePath) throws IOExceptio
* Return the behavior for the given directory name.
*
* @param name the directory name
* @return the behavor
* @return the behavior
*/
protected DirectoryBehavior getBehavior(String name) {
return DirectoryBehavior.CONTINUE;
Expand All @@ -96,4 +102,8 @@ protected DirectoryBehavior getBehavior(String name) {
protected abstract void onFile(File file, String relPath) throws IOException;


/**
* Called after the walk is completed.
*/
protected void onWalkComplete() {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
Expand All @@ -39,7 +40,7 @@ public class Manifest extends BaseManifest {
private LaunchModifier launchModifier;
private List<Feature> features = new ArrayList<Feature>();
@JsonManagedReference("manifest")
private List<ManifestEntry> tasks = new ArrayList<ManifestEntry>();
private List<ManifestEntry> tasks = Collections.synchronizedList(new ArrayList<ManifestEntry>());
@Getter @Setter @JsonIgnore
private Installer installer;
private VersionManifest versionManifest;
Expand Down