Clean up initial FileChannel writer reset's read 67/110567/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 9 Mar 2024 14:13:20 +0000 (15:13 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 11 Mar 2024 12:51:11 +0000 (12:51 +0000)
The code here bears marks of 'copy, paste and cudgel to compliance'
programming and is strikingly similar to what MappedJournalSegmentWriter
does -- except it needs to deal with reading as well.

This patch takes the first step towards sanity by eliminating a useless
if() and uses FileChannel.read(ByteBuffer, long) to reduce position
adjustments.

JIRA: CONTROLLER-2095
Change-Id: I528cb14608402a406e20d168a7e8520c9eb66edf
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java

index 025e1a841b1fe7bd3ee211b0adbc73f43b3801bd..d40d42ade50b2445fd21f39e283432163f745f8c 100644 (file)
@@ -77,18 +77,14 @@ class FileChannelJournalSegmentWriter<E> implements JournalWriter<E> {
     // Clear the buffer indexes.
     try {
       channel.position(JournalSegmentDescriptor.BYTES);
-      memory.clear().flip();
 
       // Record the current buffer position.
       long position = channel.position();
 
-      // Read more bytes from the segment if necessary.
-      if (memory.remaining() < maxEntrySize) {
-        memory.clear();
-        channel.read(memory);
-        channel.position(position);
-        memory.flip();
-      }
+      // Clear memory buffer and read fist chunk
+      memory.clear();
+      channel.read(memory, position);
+      memory.flip();
 
       // Read the entry length.
       memory.mark();