-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
517 additions
and
343 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...net/neoforged/jst/cli/io/SourceEntry.java → ...java/net/neoforged/jst/api/FileEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.neoforged.jst.api; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public interface FileSink extends AutoCloseable { | ||
@Override | ||
default void close() throws IOException { | ||
} | ||
|
||
boolean isOrdered(); | ||
|
||
void put(FileEntry entry, byte[] content) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package net.neoforged.jst.api; | ||
|
||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.VirtualFileManager; | ||
|
||
import java.io.IOException; | ||
import java.util.stream.Stream; | ||
|
||
public interface FileSource extends AutoCloseable { | ||
VirtualFile createSourceRoot(VirtualFileManager vfsManager); | ||
|
||
Stream<FileEntry> streamEntries() throws IOException; | ||
|
||
boolean isOrdered(); | ||
|
||
@Override | ||
default void close() throws IOException { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
api/src/main/java/net/neoforged/jst/api/IntelliJEnvironment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.neoforged.jst.api; | ||
|
||
import com.intellij.core.CoreApplicationEnvironment; | ||
import com.intellij.core.JavaCoreProjectEnvironment; | ||
import com.intellij.psi.PsiManager; | ||
|
||
public interface IntelliJEnvironment { | ||
CoreApplicationEnvironment getAppEnv(); | ||
|
||
JavaCoreProjectEnvironment getProjectEnv(); | ||
|
||
PsiManager getPsiManager(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
api/src/main/java/net/neoforged/jst/api/TransformContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package net.neoforged.jst.api; | ||
|
||
public record TransformContext(IntelliJEnvironment environment, FileSource source, FileSink sink) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.neoforged.jst.cli; | ||
|
||
import java.io.IOException; | ||
|
||
@FunctionalInterface | ||
public interface IoSuppplier { | ||
byte[] getContent() throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
cli/src/main/java/net/neoforged/jst/cli/OrderedParallelWorkQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package net.neoforged.jst.cli; | ||
|
||
import net.neoforged.jst.api.FileEntry; | ||
import net.neoforged.jst.api.FileSink; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Deque; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.function.Consumer; | ||
|
||
class OrderedParallelWorkQueue implements AutoCloseable { | ||
private final Deque<Future<List<WorkResult>>> pending; | ||
private final FileSink sink; | ||
private final int maxQueueDepth; | ||
|
||
public OrderedParallelWorkQueue(FileSink sink, int maxQueueDepth) { | ||
this.sink = sink; | ||
this.maxQueueDepth = maxQueueDepth; | ||
if (maxQueueDepth < 0) { | ||
throw new IllegalArgumentException("Max queue depth must not be negative"); | ||
} | ||
this.pending = new ArrayDeque<>(maxQueueDepth); | ||
} | ||
|
||
public void submit(Consumer<FileSink> producer) throws IOException { | ||
if (pending.isEmpty()) { | ||
// Can write directly if nothing else is pending | ||
producer.accept(sink); | ||
} else { | ||
// Needs to be queued behind currently queued async work | ||
submitAsync(producer); | ||
} | ||
} | ||
|
||
public void submitAsync(Consumer<FileSink> producer) { | ||
try { | ||
if (maxQueueDepth <= 0) { | ||
// Forced into synchronous mode | ||
submit(producer); | ||
return; | ||
} | ||
drainTo(maxQueueDepth - 1); | ||
pending.add(CompletableFuture.supplyAsync(() -> { | ||
try (var parallelSink = new ParallelSink()) { | ||
producer.accept(parallelSink); | ||
return parallelSink.workResults; | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
})); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
private static final class ParallelSink implements FileSink { | ||
private final List<WorkResult> workResults = new ArrayList<>(); | ||
|
||
@Override | ||
public boolean isOrdered() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void put(FileEntry entry, byte[] content) { | ||
workResults.add(new WorkResult(entry, content)); | ||
} | ||
} | ||
|
||
private void drainTo(int drainTo) throws InterruptedException, IOException { | ||
while (pending.size() > drainTo) { | ||
List<WorkResult> workResults; | ||
try { | ||
workResults = pending.removeFirst().get(); | ||
} catch (ExecutionException e) { | ||
if (e.getCause() instanceof IOException ioe) { | ||
throw ioe; | ||
} | ||
throw new RuntimeException(e.getCause()); | ||
} | ||
for (var workResult : workResults) { | ||
sink.put(workResult.entry, workResult.content); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try { | ||
drainTo(0); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} finally { | ||
sink.close(); | ||
} | ||
} | ||
|
||
private record WorkResult(FileEntry entry, byte[] content) { | ||
} | ||
} |
80 changes: 0 additions & 80 deletions
80
cli/src/main/java/net/neoforged/jst/cli/OrderedWorkQueue.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.