Add JournalSegmentFile.map() 33/111633/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 6 May 2024 00:52:44 +0000 (02:52 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 6 May 2024 00:53:56 +0000 (02:53 +0200)
We already provide a hub for I/O operations. Mapping a file is one such
operation.

JIRA: CONTROLLER-2099
Change-Id: I4fba610eac00739691849454bb99f99796c24e9c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentFile.java
atomix-storage/src/main/java/io/atomix/storage/journal/MappedFileWriter.java

index d096b33fd88846eb3d4bd57f461cf963182ec66c..134b5233c05b586e4bb5089465e988fd1307f157 100644 (file)
@@ -22,7 +22,9 @@ import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
 import java.nio.file.Path;
 import org.eclipse.jdt.annotation.NonNull;
 
@@ -127,6 +129,16 @@ final class JournalSegmentFile {
         return file.getChannel();
     }
 
+    /**
+     * Map the contents of the file into memory.
+     *
+     * @return A {@link MappedByteBuffer}
+     * @throws IOException if an I/O error occurs
+     */
+    @NonNull MappedByteBuffer map() throws IOException {
+        return channel().map(MapMode.READ_WRITE, 0, maxSize());
+    }
+
     void close() throws IOException {
         file.close();
     }
index f91cdc827ae47dd4a27d5dc74989974df600435f..a8877fabf7dfe30abf530da71bc9003f0146aa24 100644 (file)
@@ -19,7 +19,6 @@ import io.netty.util.internal.PlatformDependent;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
 import org.eclipse.jdt.annotation.NonNull;
 
 /**
@@ -33,17 +32,13 @@ final class MappedFileWriter extends FileWriter {
     MappedFileWriter(final JournalSegmentFile file, final int maxEntrySize) {
         super(file, maxEntrySize);
 
-        mappedBuffer = mapBuffer(file.channel(), file.maxSize());
-        buffer = mappedBuffer.slice();
-        reader = new MappedFileReader(file, mappedBuffer);
-    }
-
-    private static @NonNull MappedByteBuffer mapBuffer(final FileChannel channel, final int maxSegmentSize) {
         try {
-            return channel.map(FileChannel.MapMode.READ_WRITE, 0, maxSegmentSize);
+            mappedBuffer = file.map();
         } catch (IOException e) {
             throw new StorageException(e);
         }
+        buffer = mappedBuffer.slice();
+        reader = new MappedFileReader(file, mappedBuffer);
     }
 
     @Override