635f6248c44f5d585e6dd52a118618672a1458ab
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / JournalReader.java
1 /*
2  * Copyright 2017-2022 Open Networking Foundation and others.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.storage.journal;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Log reader.
23  *
24  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
25  */
26 @NonNullByDefault
27 public interface JournalReader<E> extends AutoCloseable {
28     /**
29      * Raft log reader mode.
30      */
31     enum Mode {
32         /**
33          * Reads all entries from the log.
34          */
35         ALL,
36         /**
37          * Reads committed entries from the log.
38          */
39         COMMITS,
40     }
41
42     /**
43      * A journal entry processor. Responsible for transforming entries into their internal representation.
44      *
45      * @param <E> Entry type
46      * @param <T> Internal representation type
47      */
48     @FunctionalInterface
49     interface EntryMapper<E, T> {
50         /**
51          * Process an entry.
52          *
53          * @param index entry index
54          * @param entry entry itself
55          * @param size entry size
56          * @return resulting internal representation
57          */
58         T mapEntry(long index, E entry, int size);
59     }
60
61     /**
62      * Returns the first index in the journal.
63      *
64      * @return the first index in the journal
65      */
66     long getFirstIndex();
67
68     /**
69      * Returns the next reader index.
70      *
71      * @return The next reader index.
72      */
73     long getNextIndex();
74
75     /**
76      * Try to move to the next entry.
77      *
78      * @param entryMapper callback to be invoked for the entry
79      * @return processed entry, or {@code null}
80      */
81     <T> @Nullable T tryNext(EntryMapper<E, T> entryMapper);
82
83     /**
84      * Resets the reader to the start.
85      */
86     void reset();
87
88     /**
89      * Resets the reader to the given index.
90      *
91      * @param index The index to which to reset the reader.
92      */
93     void reset(long index);
94
95     @Override
96     void close();
97 }