Introduce JournalSegmentWriter
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / JournalSegmentWriter.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package io.atomix.storage.journal;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.atomix.storage.journal.index.JournalIndex;
13 import java.nio.MappedByteBuffer;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 abstract sealed class JournalSegmentWriter<E> implements JournalWriter<E>
18         permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
19     final @NonNull JournalSegment<E> segment;
20     final int maxEntrySize;
21     final @NonNull JournalIndex index;
22     final @NonNull JournalSerdes namespace;
23     final long firstIndex;
24
25     JournalSegmentWriter(final JournalSegment<E> segment, final int maxEntrySize, final JournalIndex index,
26             final JournalSerdes namespace) {
27         this.segment = requireNonNull(segment);
28         this.maxEntrySize = maxEntrySize;
29         this.index = requireNonNull(index);
30         this.namespace = requireNonNull(namespace);
31         this.firstIndex = segment.index();
32     }
33
34     @Override
35     public final void commit(final long index) {
36         // FIXME: CONTROLLER-2098: eliminate the need for this method
37     }
38
39     /**
40      * Returns the mapped buffer underlying the segment writer, or {@code null} if the writer does not have such a
41      * buffer.
42      *
43      * @return the mapped buffer underlying the segment writer, or {@code null}.
44      */
45     abstract @Nullable MappedByteBuffer buffer();
46 }