2 * Copyright 2017-2022 Open Networking Foundation and others. All rights reserved.
3 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.atomix.storage.journal;
19 import static java.util.Objects.requireNonNull;
21 import com.google.common.base.MoreObjects;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.controller.raft.journal.FromByteBufMapper;
24 import org.opendaylight.controller.raft.journal.RaftJournal;
25 import org.opendaylight.controller.raft.journal.ToByteBufMapper;
28 * A {@link Journal} implementation based on a {@link RaftJournal}.
30 public final class SegmentedJournal<E> implements Journal<E> {
31 private final @NonNull SegmentedJournalWriter<E> writer;
32 private final @NonNull FromByteBufMapper<E> readMapper;
33 private final @NonNull RaftJournal journal;
35 public SegmentedJournal(final RaftJournal journal, final FromByteBufMapper<E> readMapper,
36 final ToByteBufMapper<E> writeMapper) {
37 this.journal = requireNonNull(journal, "journal is required");
38 this.readMapper = requireNonNull(readMapper, "readMapper cannot be null");
39 writer = new SegmentedJournalWriter<>(journal.writer(),
40 requireNonNull(writeMapper, "writeMapper cannot be null"));
44 public long firstIndex() {
45 return journal.firstIndex();
49 public long lastIndex() {
50 return journal.lastIndex();
54 public JournalWriter<E> writer() {
59 public JournalReader<E> openReader(final long index) {
60 return openReader(index, JournalReader.Mode.ALL);
64 * Opens a new journal reader with the given reader mode.
66 * @param index The index from which to begin reading entries.
67 * @param mode The mode in which to read entries.
68 * @return The journal reader.
71 public JournalReader<E> openReader(final long index, final JournalReader.Mode mode) {
72 final var byteReader = switch (mode) {
73 case ALL -> journal.openReader(index);
74 case COMMITS -> journal.openCommitsReader(index);
76 return new SegmentedJournalReader<>(byteReader, readMapper);
80 public void compact(final long index) {
81 journal.compact(index);
90 public String toString() {
91 return MoreObjects.toStringHelper(this).add("journal", journal).toString();