Skip to content

Commit dc6fc0e

Browse files
committed
Fix performance issue with image repository
1 parent 8170aad commit dc6fc0e

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

platform_api/src/main/java/net/modfest/platform/git/GitManagedPath.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jspecify.annotations.NonNull;
55

66
import java.nio.file.Path;
7+
import java.util.ArrayList;
78
import java.util.function.Consumer;
89
import java.util.function.Function;
910

@@ -35,6 +36,20 @@ public void write(Consumer<Path> runner) {
3536
});
3637
}
3738

39+
@Override
40+
public void writePerformant(PerformantWriter runner) {
41+
var editedPaths = new ArrayList<Path>();
42+
runner.doWrite(this.path, editedPaths::add);
43+
if (!editedPaths.isEmpty()) {
44+
this.gitScope.runWithScopedGit(git -> {
45+
for (var p : editedPaths) {
46+
System.out.println("EE "+this.subPath+"/"+this.path.relativize(p));
47+
git.add().addFilepattern(this.subPath+"/"+this.path.relativize(p)).call();
48+
}
49+
});
50+
}
51+
}
52+
3853
@Override
3954
public void read(Consumer<Path> runner) {
4055
runner.accept(this.path);

platform_api/src/main/java/net/modfest/platform/git/ManagedPath.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,25 @@
1010
*/
1111
public interface ManagedPath {
1212
void write(Consumer<Path> runner);
13+
/**
14+
* Serves the same function as {@link #write(Consumer)} but avoids having to diff the entire
15+
* folder. In order for this to work you MUST register/log all paths which you've edited
16+
*/
17+
void writePerformant(PerformantWriter runner);
1318
void read(Consumer<Path> runner);
1419
<R> R withRead(Function<Path, R> runner);
1520
GitScope getCurrentScope();
1621
void runWithScope(GitScope scope, Runnable r);
22+
23+
@FunctionalInterface
24+
interface PerformantWriter {
25+
void doWrite(Path pathToWriteTo, WriteLogger logger);
26+
}
27+
28+
interface WriteLogger {
29+
/**
30+
* @param path The path that was written to
31+
*/
32+
void logWrite(Path path);
33+
}
1734
}

platform_api/src/main/java/net/modfest/platform/repository/ImageRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ public void download(URI url, String id) {
9191
var originalSha = originalScope.commitSha();
9292
var gitScope = originalSha == null ? originalScope : new GitScope("Writing image for #"+originalSha);
9393
this.imageStore.runWithScope(gitScope, () -> {
94-
this.imageStore.write(path -> {
94+
this.imageStore.writePerformant((path, logger) -> {
9595
var writePath = path.resolve(id+"."+extension);
9696
try {
9797
Files.createDirectories(writePath.getParent());
9898
Files.write(writePath, response.body(), StandardOpenOption.CREATE);
99+
logger.logWrite(writePath);
99100
} catch (IOException e) {
100101
throw new RuntimeException(e);
101102
}

0 commit comments

Comments
 (0)