summary |
shortlog |
log |
commit | commitdiff |
review |
tree
raw |
patch |
inline | side by side (from parent 1:
2a6738b)
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>
import io.atomix.storage.journal.index.JournalIndex;
import io.atomix.storage.journal.index.Position;
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 {
import org.eclipse.jdt.annotation.Nullable;
abstract sealed class JournalSegmentReader<E> permits DiskJournalSegmentReader, MappedJournalSegmentReader {
return currentEntry != null ? currentEntry.index() + 1 : firstIndex;
}
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.
*
/**
* 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.
}
// Set the current entry to the next entry.
setPosition(position.position());
nextEntry = readNext();
}
setPosition(position.position());
nextEntry = readNext();
}
- while (getNextIndex() < index && hasNext()) {
- next();
+ while (getNextIndex() < index && tryNext() != null) {
+ // Nothing else
@Override
public Indexed<E> tryNext() {
@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());
}
final var nextSegment = journal.getNextSegment(currentSegment.index());
currentSegment = nextSegment;
currentReader = currentSegment.createReader();
currentSegment = nextSegment;
currentReader = currentSegment.createReader();
- return currentReader.hasNext() ? currentReader.next() : null;
+ return currentReader.tryNext();