-
Notifications
You must be signed in to change notification settings - Fork 0
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
Joshua J. A. Harwood
committed
Feb 12, 2024
1 parent
1d60834
commit b83cd8e
Showing
5 changed files
with
103 additions
and
34 deletions.
There are no files selected for viewing
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
21 changes: 0 additions & 21 deletions
21
src/main/java/com/joshuaharwood/cifparser/processing/CifFileParser.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
src/main/java/com/joshuaharwood/cifparser/processing/CifMultithreadedParser.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,63 @@ | ||
package com.joshuaharwood.cifparser.processing; | ||
|
||
import static com.pivovarit.collectors.ParallelCollectors.parallel; | ||
|
||
import com.joshuaharwood.cifparser.parsing.model.CifRecord; | ||
import com.joshuaharwood.cifparser.parsing.parser.CifLineParser; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executor; | ||
import java.util.stream.Stream; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class CifMultithreadedParser implements | ||
CifProcessor<Path, CompletableFuture<Stream<CifRecord>>> { | ||
|
||
private static final CifLineParser CIF_LINE_PARSER = new CifLineParser(); | ||
private final CifMultithreadedParserConfig config; | ||
|
||
public CifMultithreadedParser() { | ||
this(null); | ||
} | ||
|
||
public CifMultithreadedParser(@Nullable CifMultithreadedParserConfig config) { | ||
this.config = config; | ||
} | ||
|
||
private static CompletableFuture<Stream<CifRecord>> usingDefaultExecutor(BufferedReader b) { | ||
return b.lines().collect(parallel(CIF_LINE_PARSER::parseLine)); | ||
} | ||
|
||
private static CompletableFuture<Stream<CifRecord>> usingCustomExecutor(BufferedReader b, | ||
CifMultithreadedParserConfig config) { | ||
return b.lines() | ||
.collect(parallel(CIF_LINE_PARSER::parseLine, config.executor(), config.parallelism())); | ||
} | ||
|
||
public CompletableFuture<Stream<CifRecord>> parseCifRecords(Path path) throws IOException { | ||
try (BufferedReader b = Files.newBufferedReader(path, StandardCharsets.US_ASCII)) { | ||
|
||
if (this.config != null) { | ||
return usingCustomExecutor(b, config); | ||
} else { | ||
return usingDefaultExecutor(b); | ||
} | ||
} | ||
} | ||
|
||
public record CifMultithreadedParserConfig(@NotNull Executor executor, | ||
@NotNull Integer parallelism) { | ||
|
||
public CifMultithreadedParserConfig { | ||
Objects.requireNonNull(executor); | ||
Objects.requireNonNull(parallelism); | ||
} | ||
} | ||
} | ||
|
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