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;
26 public final class SegmentedJournal<E> implements Journal<E> {
27 private final SegmentedByteBufJournal journal;
28 private final SegmentedJournalWriter<E> writer;
29 private final ByteBufMapper<E> mapper;
31 public SegmentedJournal(final SegmentedByteBufJournal 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);
38 public long lastIndex() {
39 return journal.lastIndex();
43 public JournalWriter<E> writer() {
48 public JournalReader<E> openReader(final long index) {
49 return openReader(index, JournalReader.Mode.ALL);
53 * Opens a new journal reader with the given reader mode.
55 * @param index The index from which to begin reading entries.
56 * @param mode The mode in which to read entries.
57 * @return The journal reader.
60 public JournalReader<E> openReader(final long index, final JournalReader.Mode mode) {
61 final var byteReader = switch (mode) {
62 case ALL -> journal.openReader(index);
63 case COMMITS -> journal.openCommitsReader(index);
65 return new SegmentedJournalReader<>(byteReader, mapper);
74 * Compacts the journal up to the given index.
76 * The semantics of compaction are not specified by this interface.
78 * @param index The index up to which to compact the journal.
80 public void compact(final long index) {
81 journal.compact(index);
85 * Returns a new segmented journal builder.
87 * @return A new segmented journal builder.
89 public static <E> Builder<E> builder() {
90 return new Builder<>();
93 public static final class Builder<E> {
94 private final SegmentedByteBufJournal.Builder byteJournalBuilder = SegmentedByteBufJournal.builder();
95 private ByteBufMapper<E> mapper;
102 * Sets the journal name.
104 * @param name The journal name.
105 * @return The journal builder.
107 public Builder<E> withName(final String name) {
108 byteJournalBuilder.withName(name);
113 * Sets the journal storage level.
115 * The storage level indicates how individual entries will be persisted in the journal.
117 * @param storageLevel The log storage level.
118 * @return The journal builder.
120 public Builder<E> withStorageLevel(final StorageLevel storageLevel) {
121 byteJournalBuilder.withStorageLevel(storageLevel);
126 * Sets the journal storage directory.
128 * The journal will write segment files into the provided directory.
130 * @param directory The journal storage directory.
131 * @return The journal builder.
132 * @throws NullPointerException If the {@code directory} is {@code null}
134 public Builder<E> withDirectory(final String directory) {
135 byteJournalBuilder.withDirectory(directory);
140 * Sets the journal storage directory.
142 * The journal will write segment files into the provided directory.
144 * @param directory The journal storage directory.
145 * @return The journal builder.
146 * @throws NullPointerException If the {@code directory} is {@code null}
148 public Builder<E> withDirectory(final File directory) {
149 byteJournalBuilder.withDirectory(directory);
154 * Sets the journal namespace.
156 * @param namespace The journal serializer.
157 * @return The journal builder.
158 * @deprecated due to serialization refactoring, use {@link Builder#withMapper(ByteBufMapper)} instead
160 @Deprecated(forRemoval = true, since="9.0.3")
161 public Builder<E> withNamespace(final JournalSerdes namespace) {
162 return withMapper(requireNonNull(namespace, "namespace cannot be null").toMapper());
166 * Sets journal serializer.
168 * @param mapper Journal serializer
169 * @return The journal builder
171 public Builder<E> withMapper(final ByteBufMapper<E> mapper) {
172 this.mapper = requireNonNull(mapper);
177 * Sets the maximum segment size in bytes.
179 * The maximum segment size dictates when journal should roll over to new segments. As entries are written
180 * to a journal segment, once the size of the segment surpasses the configured maximum segment size, the
181 * journal will create a new segment and append new entries to that segment.
183 * By default, the maximum segment size is 32M.
185 * @param maxSegmentSize The maximum segment size in bytes.
186 * @return The storage builder.
187 * @throws IllegalArgumentException If the {@code maxSegmentSize} is not positive
189 public Builder<E> withMaxSegmentSize(final int maxSegmentSize) {
190 byteJournalBuilder.withMaxSegmentSize(maxSegmentSize);
195 * Sets the maximum entry size in bytes.
197 * @param maxEntrySize the maximum entry size in bytes
198 * @return the storage builder
199 * @throws IllegalArgumentException if the {@code maxEntrySize} is not positive
201 public Builder<E> withMaxEntrySize(final int maxEntrySize) {
202 byteJournalBuilder.withMaxEntrySize(maxEntrySize);
207 * Sets the maximum number of entries per segment.
209 * @param maxEntriesPerSegment The maximum number of entries allowed per segment.
210 * @return The journal builder.
211 * @deprecated since 3.0.2, no longer used
214 public Builder<E> withMaxEntriesPerSegment(final int maxEntriesPerSegment) {
220 * Sets the journal index density.
222 * The index density is the frequency at which the position of entries written to the journal will be recorded
223 * in an in-memory index for faster seeking.
225 * @param indexDensity the index density
226 * @return the journal builder
227 * @throws IllegalArgumentException if the density is not between 0 and 1
229 public Builder<E> withIndexDensity(final double indexDensity) {
230 byteJournalBuilder.withIndexDensity(indexDensity);
235 * Enables flushing buffers to disk when entries are committed to a segment.
237 * When flush-on-commit is enabled, log entry buffers will be automatically flushed to disk each time an
238 * entry is committed in a given segment.
240 * @return The journal builder.
242 public Builder<E> withFlushOnCommit() {
243 return withFlushOnCommit(true);
247 * Enables flushing buffers to disk when entries are committed to a segment.
249 * When flush-on-commit is enabled, log entry buffers will be automatically flushed to disk each time an
250 * entry is committed in a given segment.
252 * @param flushOnCommit Whether to flush buffers to disk when entries are committed to a segment.
253 * @return The journal builder.
255 public Builder<E> withFlushOnCommit(final boolean flushOnCommit) {
256 byteJournalBuilder.withFlushOnCommit(flushOnCommit);
261 * Build the {@link SegmentedJournal}.
263 * @return {@link SegmentedJournal} instance.
265 public SegmentedJournal<E> build() {
266 return new SegmentedJournal<>(byteJournalBuilder.build(), mapper);