*/
package io.atomix.storage.journal;
+import java.util.NoSuchElementException;
+
/**
* A {@link JournalReader} traversing only committed entries.
*/
@Override
public boolean hasNext() {
- return getNextIndex() <= journal.getCommitIndex() && super.hasNext();
+ return isNextCommited() && super.hasNext();
+ }
+
+ @Override
+ public Indexed<E> next() {
+ if (isNextCommited()) {
+ return super.next();
+ }
+ throw new NoSuchElementException();
+ }
+
+ private boolean isNextCommited() {
+ return getNextIndex() <= journal.getCommitIndex();
}
}
package io.atomix.storage.journal;
import java.util.Iterator;
+import java.util.NoSuchElementException;
/**
* Log reader.
* Returns the next entry in the reader.
*
* @return The next entry in the reader.
+ * @throws NoSuchElementException there is no next entry
*/
@Override
Indexed<E> next();
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
+import java.util.NoSuchElementException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
}
}
+ // Same as testWriteReadCommittedEntries(), but does not use hasNext() but checks whether an exception is thrown
+ @Test
+ public void testWriteReadCommittedEntriesException() throws Exception {
+ try (Journal<TestEntry> journal = createJournal()) {
+ JournalWriter<TestEntry> writer = journal.writer();
+ JournalReader<TestEntry> reader = journal.openReader(1, JournalReader.Mode.COMMITS);
+
+ for (int i = 1; i <= entriesPerSegment * 5; i++) {
+ writer.append(ENTRY);
+ assertThrows(NoSuchElementException.class, reader::next);
+ writer.commit(i);
+ Indexed<TestEntry> entry = reader.next();
+ assertEquals(i, entry.index());
+ assertEquals(32, entry.entry().bytes().length);
+ reader.reset(i);
+ entry = reader.next();
+ assertEquals(i, entry.index());
+ assertEquals(32, entry.entry().bytes().length);
+ }
+ }
+ }
+
@Test
public void testReadAfterCompact() throws Exception {
try (SegmentedJournal<TestEntry> journal = createJournal()) {