Retain RandomAccessFile in JournalSegmentFile
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / JournalSegment.java
index a7f4c5aadc5c2845723ec362f60412b47b373c83..b73d942c036d7c12d760fe23c31c91a36e7f0e8e 100644 (file)
@@ -23,9 +23,7 @@ import io.atomix.storage.journal.index.JournalIndex;
 import io.atomix.storage.journal.index.Position;
 import io.atomix.storage.journal.index.SparseJournalIndex;
 import java.io.IOException;
-import java.nio.channels.FileChannel;
 import java.nio.file.Files;
-import java.nio.file.StandardOpenOption;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -47,7 +45,6 @@ final class JournalSegment {
   private final JournalIndex journalIndex;
   private final Set<JournalSegmentReader> readers = ConcurrentHashMap.newKeySet();
   private final AtomicInteger references = new AtomicInteger();
-  private final FileChannel channel;
 
   private JournalSegmentWriter writer;
   private boolean open = true;
@@ -61,16 +58,10 @@ final class JournalSegment {
     this.storageLevel = requireNonNull(storageLevel);
     this.maxEntrySize = maxEntrySize;
     journalIndex = new SparseJournalIndex(indexDensity);
-    try {
-      channel = FileChannel.open(file.path(),
-        StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
-    } catch (IOException e) {
-      throw new StorageException(e);
-    }
 
     final var fileWriter = switch (storageLevel) {
-        case DISK -> new DiskFileWriter(file, channel, maxEntrySize);
-        case MAPPED -> new MappedFileWriter(file, channel, maxEntrySize);
+        case DISK -> new DiskFileWriter(file, maxEntrySize);
+        case MAPPED -> new MappedFileWriter(file, maxEntrySize);
     };
     writer = new JournalSegmentWriter(fileWriter, this, maxEntrySize, journalIndex)
         // relinquish mapped memory
@@ -95,19 +86,6 @@ final class JournalSegment {
     return writer.getLastIndex();
   }
 
-  /**
-   * Returns the size of the segment.
-   *
-   * @return the size of the segment
-   */
-  int size() {
-    try {
-      return (int) channel.size();
-    } catch (IOException e) {
-      throw new StorageException(e);
-    }
-  }
-
   /**
    * Returns the segment file.
    *
@@ -179,8 +157,7 @@ final class JournalSegment {
     acquire();
 
     final var buffer = writer.buffer();
-    final var fileReader = buffer != null ? new MappedFileReader(file, buffer)
-        : new DiskFileReader(file, channel, maxEntrySize);
+    final var fileReader = buffer != null ? new MappedFileReader(file, buffer) : new DiskFileReader(file, maxEntrySize);
     final var reader = new JournalSegmentReader(this, fileReader, maxEntrySize);
     reader.setPosition(JournalSegmentDescriptor.BYTES);
     readers.add(reader);
@@ -235,7 +212,7 @@ final class JournalSegment {
   private void finishClose() {
     writer.close();
     try {
-      channel.close();
+      file.close();
     } catch (IOException e) {
       throw new StorageException(e);
     }