Skip to content

Commit 8a8975d

Browse files
committed
try a thing
1 parent 5fac740 commit 8a8975d

3 files changed

Lines changed: 42 additions & 63 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ private ChannelLock(final FileChannel channel, final FileLock lock) {
2525
}
2626

2727
public void close() throws IOException {
28-
lock.release();
28+
// lock.release();
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: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,8 @@ LockedFileChannel acquireWrite() throws IOException {
177177
}
178178

179179
// 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.
195180
// Create a LockedFileChannel that will releaseWrite() when it is closed.
196-
return new LockedFileChannel(channel, this::releaseWrite);
181+
return new LockedFileChannel(channelLock.getChannel(), this::releaseWrite);
197182
} catch (InterruptedException e) {
198183
throw new IOException(e);
199184
}
@@ -214,22 +199,8 @@ LockedFileChannel tryAcquireWrite() {
214199
}
215200

216201
// 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.
231202
// Create a LockedFileChannel that will releaseWrite() when it is closed.
232-
return new LockedFileChannel(channel, this::releaseWrite);
203+
return new LockedFileChannel(channelLock.getChannel(), this::releaseWrite);
233204
}
234205

235206
void releaseWrite() throws IOException {

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

Lines changed: 35 additions & 31 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,48 +31,48 @@ 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

7377
channel.close();
7478
if (releaseLock != null) {

0 commit comments

Comments
 (0)