Skip to content

Commit 439c5b8

Browse files
committed
[refactor] Use NIO for opening the Journal File
1 parent fda6553 commit 439c5b8

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/org/exist/storage/journal/Journal.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.nio.BufferOverflowException;
2424
import java.nio.ByteBuffer;
2525
import java.nio.channels.FileChannel;
26+
import java.nio.channels.SeekableByteChannel;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;
2829
import java.nio.file.StandardCopyOption;
@@ -42,6 +43,8 @@
4243
import org.exist.util.ReadOnlyException;
4344
import org.exist.util.sanity.SanityCheck;
4445

46+
import static java.nio.file.StandardOpenOption.CREATE_NEW;
47+
import static java.nio.file.StandardOpenOption.WRITE;
4548
import static org.exist.util.ThreadUtils.newInstanceThread;
4649

4750
/**
@@ -135,8 +138,7 @@ public final class Journal {
135138
* the current output channel
136139
* Only valid after switchFiles() was called at least once!
137140
*/
138-
private FileOutputStream os;
139-
private FileChannel channel;
141+
private SeekableByteChannel channel;
140142

141143
/**
142144
* Syncing the journal is done by a background thread
@@ -474,12 +476,9 @@ public void switchFiles() throws LogException {
474476
synchronized (latch) {
475477
close();
476478
try {
477-
//RandomAccessFile raf = new RandomAccessFile(file, "rw");
478-
os = new FileOutputStream(file.toFile(), true);
479-
channel = os.getChannel();
480-
481-
fileSyncRunnable.setChannel(channel);
482-
} catch (final FileNotFoundException e) {
479+
channel = Files.newByteChannel(file, CREATE_NEW, WRITE);
480+
fileSyncRunnable.setChannel((FileChannel) channel);
481+
} catch (final IOException e) {
483482
throw new LogException("Failed to open new journal: " + file.toAbsolutePath().toString(), e);
484483
}
485484
}
@@ -496,13 +495,6 @@ public void close() {
496495
LOG.warn("Failed to close journal channel", e);
497496
}
498497
}
499-
if (os != null) {
500-
try {
501-
os.close();
502-
} catch (final IOException e) {
503-
LOG.warn("Failed to close journal output stream", e);
504-
}
505-
}
506498
}
507499

508500
private static int journalFileNum(final Path path) {
@@ -609,10 +601,10 @@ static String getFileName(final int fileNum) {
609601
}
610602

611603
private static class RemoveRunnable implements Runnable {
612-
private final FileChannel channel;
604+
private final SeekableByteChannel channel;
613605
private final Path path;
614606

615-
RemoveRunnable(final FileChannel channel, final Path path) {
607+
RemoveRunnable(final SeekableByteChannel channel, final Path path) {
616608
this.channel = channel;
617609
this.path = path;
618610
}

0 commit comments

Comments
 (0)