/*
- * Copyright 2017-present Open Networking Foundation
+ * Copyright 2017-2022 Open Networking Foundation and others. All rights reserved.
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static java.util.Objects.requireNonNull;
-import java.util.NoSuchElementException;
+import org.eclipse.jdt.annotation.Nullable;
/**
- * A {@link JournalReader} traversing all entries.
+ * A {@link JournalReader} backed by a {@link ByteBufReader}.
*/
-sealed class SegmentedJournalReader<E> implements JournalReader<E> permits CommitsSegmentJournalReader {
- final SegmentedJournal<E> journal;
- private JournalSegment<E> currentSegment;
- private Indexed<E> previousEntry;
- private JournalSegmentReader<E> currentReader;
+final class SegmentedJournalReader<E> implements JournalReader<E> {
+ private final ByteBufMapper<E> mapper;
+ private final ByteBufReader reader;
- SegmentedJournalReader(SegmentedJournal<E> journal, JournalSegment<E> segment) {
- this.journal = requireNonNull(journal);
- currentSegment = requireNonNull(segment);
- currentReader = segment.createReader();
- }
-
- @Override
- public final long getFirstIndex() {
- return journal.getFirstSegment().index();
- }
-
- @Override
- public final long getCurrentIndex() {
- final var currentEntry = currentReader.getCurrentEntry();
- if (currentEntry != null) {
- final long currentIndex = currentEntry.index();
- if (currentIndex != 0) {
- return currentIndex;
- }
+ SegmentedJournalReader(final ByteBufReader reader, final ByteBufMapper<E> mapper) {
+ this.reader = requireNonNull(reader);
+ this.mapper = requireNonNull(mapper);
}
- return previousEntry != null ? previousEntry.index() : 0;
- }
-
- @Override
- public final Indexed<E> getCurrentEntry() {
- // If previousEntry was the last in the previous segment, we may have moved currentReader to the next segment.
- // That segment may be empty, though, in which case we need to report the previousEntry.
- final Indexed<E> currentEntry;
- return (currentEntry = currentReader.getCurrentEntry()) != null ? currentEntry : previousEntry;
- }
-
- @Override
- public final long getNextIndex() {
- return currentReader.getNextIndex();
- }
- @Override
- public final void reset() {
- previousEntry = null;
- currentReader.close();
-
- currentSegment = journal.getFirstSegment();
- currentReader = currentSegment.createReader();
- }
-
- @Override
- public final void reset(long index) {
- // If the current segment is not open, it has been replaced. Reset the segments.
- if (!currentSegment.isOpen()) {
- reset();
+ @Override
+ public long getFirstIndex() {
+ return reader.firstIndex();
}
- if (index < currentReader.getNextIndex()) {
- rewind(index);
- } else if (index > currentReader.getNextIndex()) {
- forward(index);
- } else {
- currentReader.reset(index);
+ @Override
+ public long getNextIndex() {
+ return reader.nextIndex();
}
- }
-
- /**
- * Rewinds the journal to the given index.
- */
- private void rewind(long index) {
- if (currentSegment.index() >= index) {
- JournalSegment<E> segment = journal.getSegment(index - 1);
- if (segment != null) {
- currentReader.close();
- currentSegment = segment;
- currentReader = currentSegment.createReader();
- }
+ @Override
+ public void reset() {
+ reader.reset();
}
- currentReader.reset(index);
- previousEntry = currentReader.getCurrentEntry();
- }
-
- /**
- * Fast forwards the journal to the given index.
- */
- private void forward(long index) {
- while (getNextIndex() < index && hasNext()) {
- next();
+ @Override
+ public void reset(final long index) {
+ reader.reset(index);
}
- }
-
- @Override
- public boolean hasNext() {
- return currentReader.hasNext() || moveToNextSegment() && currentReader.hasNext();
- }
- @Override
- public final Indexed<E> next() {
- if (currentReader.hasNext()) {
- previousEntry = currentReader.getCurrentEntry();
- return currentReader.next();
- }
- if (moveToNextSegment()) {
- return currentReader.next();
+ @Override
+ public <T> @Nullable T tryNext(final EntryMapper<E, T> entryMapper) {
+ return reader.tryNext(
+ (index, buf) -> requireNonNull(entryMapper.mapEntry(index, mapper.bytesToObject(buf), buf.readableBytes()))
+ );
}
- throw new NoSuchElementException();
- }
- @Override
- public final void close() {
- currentReader.close();
- journal.closeReader(this);
- }
-
- private boolean moveToNextSegment() {
- final var nextSegment = journal.getNextSegment(currentSegment.index());
- if (nextSegment == null || nextSegment.index() != getNextIndex()) {
- return false;
+ @Override
+ public void close() {
+ reader.close();
}
-
- previousEntry = currentReader.getCurrentEntry();
- currentReader.close();
-
- currentSegment = nextSegment;
- currentReader = currentSegment.createReader();
- return true;
- }
}