Move {Journal,ByteBuf}Reader.firstIndex()
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / SegmentedJournal.java
1 /*
2  * Copyright 2017-2022 Open Networking Foundation and others.  All rights reserved.
3  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package io.atomix.storage.journal;
18
19 import static java.util.Objects.requireNonNull;
20
21 import com.google.common.base.MoreObjects;
22
23 /**
24  * A {@link Journal} implementation based on a {@link ByteBufJournal}.
25  */
26 public final class SegmentedJournal<E> implements Journal<E> {
27     private final SegmentedJournalWriter<E> writer;
28     private final ByteBufMapper<E> mapper;
29     private final ByteBufJournal journal;
30
31     public SegmentedJournal(final ByteBufJournal journal, final ByteBufMapper<E> mapper) {
32         this.journal = requireNonNull(journal, "journal is required");
33         this.mapper = requireNonNull(mapper, "mapper cannot be null");
34         writer = new SegmentedJournalWriter<>(journal.writer(), mapper);
35     }
36
37     @Override
38     public long firstIndex() {
39         return journal.firstIndex();
40     }
41
42     @Override
43     public long lastIndex() {
44         return journal.lastIndex();
45     }
46
47     @Override
48     public JournalWriter<E> writer() {
49         return writer;
50     }
51
52     @Override
53     public JournalReader<E> openReader(final long index) {
54         return openReader(index, JournalReader.Mode.ALL);
55     }
56
57     /**
58      * Opens a new journal reader with the given reader mode.
59      *
60      * @param index The index from which to begin reading entries.
61      * @param mode The mode in which to read entries.
62      * @return The journal reader.
63      */
64     @Override
65     public JournalReader<E> openReader(final long index, final JournalReader.Mode mode) {
66         final var byteReader = switch (mode) {
67             case ALL -> journal.openReader(index);
68             case COMMITS -> journal.openCommitsReader(index);
69         };
70         return new SegmentedJournalReader<>(byteReader, mapper);
71     }
72
73     @Override
74     public void compact(final long index) {
75         journal.compact(index);
76     }
77
78     @Override
79     public void close() {
80         journal.close();
81     }
82
83     @Override
84     public String toString() {
85         return MoreObjects.toStringHelper(this).add("journal", journal).toString();
86     }
87 }