Add JournalSegmentFile.map()
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / JournalSegmentFile.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();
     }