Separate out {From,To}ByteBufMapper
[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 import org.eclipse.jdt.annotation.NonNull;
23
24 /**
25  * A {@link Journal} implementation based on a {@link ByteBufJournal}.
26  */
27 public final class SegmentedJournal<E> implements Journal<E> {
28     private final @NonNull SegmentedJournalWriter<E> writer;
29     private final @NonNull FromByteBufMapper<E> readMapper;
30     private final @NonNull ByteBufJournal journal;
31
32     public SegmentedJournal(final ByteBufJournal journal, final FromByteBufMapper<E> readMapper,
33             final ToByteBufMapper<E> writeMapper) {
34         this.journal = requireNonNull(journal, "journal is required");
35         this.readMapper = requireNonNull(readMapper, "readMapper cannot be null");
36         writer = new SegmentedJournalWriter<>(journal.writer(),
37             requireNonNull(writeMapper, "writeMapper cannot be null"));
38     }
39
40     @Override
41     public long firstIndex() {
42         return journal.firstIndex();
43     }
44
45     @Override
46     public long lastIndex() {
47         return journal.lastIndex();
48     }
49
50     @Override
51     public JournalWriter<E> writer() {
52         return writer;
53     }
54
55     @Override
56     public JournalReader<E> openReader(final long index) {
57         return openReader(index, JournalReader.Mode.ALL);
58     }
59
60     /**
61      * Opens a new journal reader with the given reader mode.
62      *
63      * @param index The index from which to begin reading entries.
64      * @param mode The mode in which to read entries.
65      * @return The journal reader.
66      */
67     @Override
68     public JournalReader<E> openReader(final long index, final JournalReader.Mode mode) {
69         final var byteReader = switch (mode) {
70             case ALL -> journal.openReader(index);
71             case COMMITS -> journal.openCommitsReader(index);
72         };
73         return new SegmentedJournalReader<>(byteReader, readMapper);
74     }
75
76     @Override
77     public void compact(final long index) {
78         journal.compact(index);
79     }
80
81     @Override
82     public void close() {
83         journal.close();
84     }
85
86     @Override
87     public String toString() {
88         return MoreObjects.toStringHelper(this).add("journal", journal).toString();
89     }
90 }