-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathParallelBlockInputStream.java
More file actions
157 lines (140 loc) · 5.73 KB
/
Copy pathParallelBlockInputStream.java
File metadata and controls
157 lines (140 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/** Copyright (c) 2026 Leonard Ehrenfried. <mail@leonard.io>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package crosby.binary.file;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* A drop-in, parallel replacement for {@link BlockInputStream}.
*
* Reading a fileblock's header and raw bytes must stay strictly sequential,
* since it consumes an {@link InputStream}, but decompressing and parsing a
* block's contents (the CPU-heavy step, dominated by zlib inflate for large
* files) is independent per block. This class overlaps that work across a
* thread pool while still delivering completed blocks to the adaptor one at
* a time, in file order -- exactly the contract {@link BlockReaderAdapter}
* documents -- so an existing {@link BlockReaderAdapter} (such as a
* {@code crosby.binary.BinaryParser} subclass) needs no changes to benefit:
* its {@code handleBlock}/{@code skipBlock}/{@code complete} calls all still
* happen on a single thread, one at a time, in order.
*
* Memory use is bounded by keeping at most {@code pipelineDepth} decompressed
* blocks in flight at once (queued or in progress); tune it down for memory
* constrained environments, or up to keep more threads fed on files with many
* small blocks.
*/
public class ParallelBlockInputStream implements Closeable {
public ParallelBlockInputStream(InputStream input, BlockReaderAdapter adaptor) {
this(input, adaptor, Runtime.getRuntime().availableProcessors());
}
public ParallelBlockInputStream(InputStream input, BlockReaderAdapter adaptor, int numThreads) {
this(input, adaptor, Executors.newFixedThreadPool(numThreads), true, numThreads * 2);
}
/**
* Use a caller-supplied executor, e.g. to share a thread pool across
* several files. The executor is not shut down by {@link #close()}.
*/
public ParallelBlockInputStream(InputStream input, BlockReaderAdapter adaptor,
ExecutorService executor, int pipelineDepth) {
this(input, adaptor, executor, false, pipelineDepth);
}
private ParallelBlockInputStream(InputStream input, BlockReaderAdapter adaptor,
ExecutorService executor, boolean ownsExecutor, int pipelineDepth) {
this.input = input;
this.adaptor = adaptor;
this.executor = executor;
this.ownsExecutor = ownsExecutor;
this.pipelineDepth = Math.max(1, pipelineDepth);
}
public void process() throws IOException {
Queue<Future<FileBlock>> inflight = new ArrayDeque<>();
try {
while (true) {
FileBlockHead head;
try {
head = FileBlockHead.readHead(input);
} catch (EOFException e) {
break;
}
if (adaptor.skipBlock(head)) {
head.skipContents(input);
continue;
}
byte[] buf = new byte[head.getDatasize()];
new DataInputStream(input).readFully(buf);
inflight.add(submit(head, buf));
if (inflight.size() >= pipelineDepth) {
adaptor.handleBlock(take(inflight));
}
}
while (!inflight.isEmpty()) {
adaptor.handleBlock(take(inflight));
}
adaptor.complete();
} finally {
if (ownsExecutor) {
executor.shutdown();
}
}
}
private Future<FileBlock> submit(final FileBlockHead head, final byte[] buf) {
return executor.submit(new Callable<FileBlock>() {
@Override
public FileBlock call() throws IOException {
return head.parseData(buf);
}
});
}
private static FileBlock take(Queue<Future<FileBlock>> inflight) throws IOException {
Future<FileBlock> future = inflight.poll();
try {
return future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException) {
throw (IOException) cause;
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new IOException(cause);
}
}
@Override
public void close() throws IOException {
try {
input.close();
} finally {
if (ownsExecutor) {
executor.shutdownNow();
}
}
}
private final InputStream input;
private final BlockReaderAdapter adaptor;
private final ExecutorService executor;
private final boolean ownsExecutor;
private final int pipelineDepth;
}