Simplify JournalSegmentReader entry access 99/110999/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 23 Mar 2024 12:27:22 +0000 (13:27 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 23 Mar 2024 12:29:05 +0000 (13:29 +0100)
The only user is SegmentedJournalReader, which is using these methods to
implement its tryNext() operation.

Mirror this with JournalSegmentReader.tryNext(), which simplifies some
of the logic.

JIRA: CONTROLLER-2109
Change-Id: Id104743b16199b4734b322d1397b85280f03346b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentReader.java
atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalReader.java

index c9f8c1b05d5624128e7202375f501a464d75b5d7..36cd6ef5c3dad6c14f6db3da68296f43bd3754a4 100644 (file)
@@ -19,7 +19,6 @@ import static java.util.Objects.requireNonNull;
 
 import io.atomix.storage.journal.index.JournalIndex;
 import io.atomix.storage.journal.index.Position;
-import java.util.NoSuchElementException;
 import org.eclipse.jdt.annotation.Nullable;
 
 abstract sealed class JournalSegmentReader<E> permits DiskJournalSegmentReader, MappedJournalSegmentReader {
@@ -59,24 +58,17 @@ abstract sealed class JournalSegmentReader<E> permits DiskJournalSegmentReader,
         return currentEntry != null ? currentEntry.index() + 1 : firstIndex;
     }
 
-    /**
-     * Returns whether the reader has a next entry to read.
-     *
-     * @return Whether the reader has a next entry to read.
-     */
-    final boolean hasNext() {
-        return nextEntry != null || (nextEntry = readNext()) != null;
-    }
-
     /**
      * Returns the next entry in the reader.
      *
-     * @return The next entry in the reader.
-     * @throws UnsupportedOperationException if there is no such entry
+     * @return The next entry in the reader, or {@code null}
      */
-    final Indexed<E> next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+    final @Nullable Indexed<E> tryNext() {
+        if (nextEntry == null) {
+            nextEntry = readNext();
+        }
+        if (nextEntry == null) {
+            return null;
         }
 
         // Set the current entry to the next entry.
@@ -116,8 +108,8 @@ abstract sealed class JournalSegmentReader<E> permits DiskJournalSegmentReader,
             setPosition(position.position());
             nextEntry = readNext();
         }
-        while (getNextIndex() < index && hasNext()) {
-            next();
+        while (getNextIndex() < index && tryNext() != null) {
+            // Nothing else
         }
     }
 
index 3f5d3f9e4fbe15ab476d9c952ea74da9f1504b65..50270442e79226e46334c1abcff9e335efe7384f 100644 (file)
@@ -118,9 +118,11 @@ sealed class SegmentedJournalReader<E> implements JournalReader<E> permits Commi
 
   @Override
   public Indexed<E> tryNext() {
-    if (currentReader.hasNext()) {
-      previousEntry = currentReader.getCurrentEntry();
-      return currentReader.next();
+    final var current = currentReader.getCurrentEntry();
+    final var next = currentReader.tryNext();
+    if (next != null) {
+      previousEntry = current;
+      return next;
     }
 
     final var nextSegment = journal.getNextSegment(currentSegment.index());
@@ -133,7 +135,7 @@ sealed class SegmentedJournalReader<E> implements JournalReader<E> permits Commi
 
     currentSegment = nextSegment;
     currentReader = currentSegment.createReader();
-    return currentReader.hasNext() ? currentReader.next() : null;
+    return currentReader.tryNext();
   }
 
   @Override