Skip to content

Commit 2d3b26b

Browse files
committed
try a thing
1 parent 5fac740 commit 2d3b26b

3 files changed

Lines changed: 45 additions & 64 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/ChannelLock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void close() throws IOException {
2929
channel.close();
3030
}
3131

32+
FileChannel getChannel() {
33+
return channel;
34+
}
35+
3236
/**
3337
* Create a {@link FileChannel} on the given {@code path} and lock it with a
3438
* system-level {@link FileLock}. If there is an existing overlapping file

src/main/java/org/janelia/saalfeldlab/n5/KeyLockState.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ LockedFileChannel acquireRead() throws IOException {
7676
// We have a FileChannel.
7777
// Create a LockedFileChannel that will releaseRead() when it is closed.
7878
++numReaders;
79-
return new LockedFileChannel(channel, this::releaseRead);
79+
return new LockedFileChannel(channel, () -> {
80+
channel.close();
81+
releaseRead();
82+
});
8083
} finally {
8184
readerMutex.release();
8285
}
@@ -177,23 +180,8 @@ LockedFileChannel acquireWrite() throws IOException {
177180
}
178181

179182
// We have a WRITE ChannelLock.
180-
// Try to open a FileChannel.
181-
final FileChannel channel;
182-
try {
183-
channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
184-
} catch (IOException e) {
185-
// Something went wrong. Back off.
186-
try {
187-
channelLock.close();
188-
} finally {
189-
channelLockMutex.release();
190-
}
191-
throw e;
192-
}
193-
194-
// We have a FileChannel.
195183
// Create a LockedFileChannel that will releaseWrite() when it is closed.
196-
return new LockedFileChannel(channel, this::releaseWrite);
184+
return new LockedFileChannel(channelLock.getChannel(), this::releaseWrite);
197185
} catch (InterruptedException e) {
198186
throw new IOException(e);
199187
}
@@ -214,22 +202,8 @@ LockedFileChannel tryAcquireWrite() {
214202
}
215203

216204
// We have a WRITE ChannelLock.
217-
// Try to open a FileChannel.
218-
final FileChannel channel;
219-
try {
220-
channel = FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
221-
} catch (IOException e) {
222-
// Something went wrong. Back off.
223-
try {
224-
releaseChannelLock();
225-
} catch (IOException ignored) {
226-
}
227-
return null;
228-
}
229-
230-
// We have a FileChannel.
231205
// Create a LockedFileChannel that will releaseWrite() when it is closed.
232-
return new LockedFileChannel(channel, this::releaseWrite);
206+
return new LockedFileChannel(channelLock.getChannel(), this::releaseWrite);
233207
}
234208

235209
void releaseWrite() throws IOException {

src/main/java/org/janelia/saalfeldlab/n5/LockedFileChannel.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.janelia.saalfeldlab.n5;
22

3-
import java.io.*;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.io.Reader;
7+
import java.io.Writer;
48
import java.nio.channels.Channels;
59
import java.nio.channels.FileChannel;
610
import java.nio.charset.StandardCharsets;
@@ -11,7 +15,7 @@
1115
// TODO: This only has to be public because of a test in another package. Fix that
1216
public class LockedFileChannel implements LockedChannel {
1317

14-
private final FileChannel channel;
18+
private final FileChannel channel;
1519
private ReleaseLock releaseLock;
1620

1721
@FunctionalInterface
@@ -27,50 +31,49 @@ public interface ReleaseLock {
2731
}
2832

2933
@Override
30-
public Reader newReader() throws N5Exception.N5IOException {
34+
public Reader newReader() throws N5Exception.N5IOException {
3135

32-
return Channels.newReader(channel, StandardCharsets.UTF_8.name());
33-
}
36+
return Channels.newReader(channel, StandardCharsets.UTF_8.name());
37+
}
3438

35-
@Override
36-
public InputStream newInputStream() throws N5Exception.N5IOException {
39+
@Override
40+
public InputStream newInputStream() throws N5Exception.N5IOException {
3741

38-
return Channels.newInputStream(channel);
39-
}
42+
return Channels.newInputStream(channel);
43+
}
4044

41-
@Override
42-
public Writer newWriter() throws N5Exception.N5IOException {
45+
@Override
46+
public Writer newWriter() throws N5Exception.N5IOException {
4347

44-
truncateChannel();
45-
return Channels.newWriter(channel, StandardCharsets.UTF_8.name());
46-
}
48+
truncateChannel();
49+
return Channels.newWriter(channel, StandardCharsets.UTF_8.name());
50+
}
4751

48-
@Override
49-
public OutputStream newOutputStream() throws N5Exception.N5IOException {
52+
@Override
53+
public OutputStream newOutputStream() throws N5Exception.N5IOException {
5054

51-
truncateChannel();
52-
return Channels.newOutputStream(channel);
53-
}
55+
truncateChannel();
56+
return Channels.newOutputStream(channel);
57+
}
5458

5559
// TODO: This only has to be public because of a test in another package. Fix that
56-
public FileChannel getFileChannel() {
60+
public FileChannel getFileChannel() {
5761

58-
return channel;
59-
}
62+
return channel;
63+
}
6064

61-
private void truncateChannel() throws N5Exception.N5IOException {
65+
private void truncateChannel() throws N5Exception.N5IOException {
6266

63-
try {
64-
channel.truncate(0);
65-
} catch (final IOException e) {
66-
throw new N5Exception.N5IOException("Failed to truncate channel", e);
67-
}
68-
}
67+
try {
68+
channel.truncate(0);
69+
} catch (final IOException e) {
70+
throw new N5Exception.N5IOException("Failed to truncate channel", e);
71+
}
72+
}
6973

70-
@Override
71-
public void close() throws IOException {
74+
@Override
75+
public void close() throws IOException {
7276

73-
channel.close();
7477
if (releaseLock != null) {
7578
releaseLock.release();
7679
releaseLock = null;

0 commit comments

Comments
 (0)