Free disk buffers
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / JournalReader.java
index 40b700d070532f66bda2ea96015c46f138ed800e..8f49aa2ebcabd1c26e59bd091f74e9b540ad6cae 100644 (file)
@@ -15,6 +15,7 @@
  */
 package io.atomix.storage.journal;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
 /**
@@ -22,6 +23,7 @@ import org.eclipse.jdt.annotation.Nullable;
  *
  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
  */
+@NonNullByDefault
 public interface JournalReader<E> extends AutoCloseable {
     /**
      * Raft log reader mode.
@@ -37,6 +39,25 @@ public interface JournalReader<E> extends AutoCloseable {
         COMMITS,
     }
 
+    /**
+     * A journal entry processor. Responsible for transforming entries into their internal representation.
+     *
+     * @param <E> Entry type
+     * @param <T> Internal representation type
+     */
+    @FunctionalInterface
+    interface EntryMapper<E, T> {
+        /**
+         * Process an entry.
+         *
+         * @param index entry index
+         * @param entry entry itself
+         * @param size entry size
+         * @return resulting internal representation
+         */
+        T mapEntry(long index, E entry, int size);
+    }
+
     /**
      * Returns the first index in the journal.
      *
@@ -54,9 +75,10 @@ public interface JournalReader<E> extends AutoCloseable {
     /**
      * Try to move to the next entry.
      *
-     * @return The next entry in the reader, or {@code null} if there is no next entry.
+     * @param entryMapper callback to be invoked for the entry
+     * @return processed entry, or {@code null}
      */
-    @Nullable Indexed<E> tryNext();
+    <T> @Nullable T tryNext(EntryMapper<E, T> entryMapper);
 
     /**
      * Resets the reader to the start.
@@ -66,7 +88,7 @@ public interface JournalReader<E> extends AutoCloseable {
     /**
      * Resets the reader to the given index.
      *
-     * @param index The index to which to reset the reader.
+     * @param index the next index to read
      */
     void reset(long index);