From: Robert Varga Date: Mon, 6 May 2024 00:52:44 +0000 (+0200) Subject: Add JournalSegmentFile.map() X-Git-Tag: v9.0.3~37 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=27c1f8ccaf729f7a53d49f2afa519adff62f6f76;p=controller.git Add JournalSegmentFile.map() 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 --- diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentFile.java b/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentFile.java index d096b33fd8..134b5233c0 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentFile.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentFile.java @@ -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(); } diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedFileWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedFileWriter.java index f91cdc827a..a8877fabf7 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedFileWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedFileWriter.java @@ -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