6996906c7b424efed9485d5d94c6bf9c7e7a442f
[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  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.storage.journal;
17
18 import static java.util.Objects.requireNonNull;
19
20 import io.atomix.storage.journal.index.JournalIndex;
21 import java.nio.MappedByteBuffer;
22 import java.nio.channels.FileChannel;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25
26 abstract sealed class JournalSegmentWriter<E> permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
27     final @NonNull FileChannel channel;
28     final @NonNull JournalIndex index;
29     final @NonNull JournalSerdes namespace;
30     final int maxSegmentSize;
31     final int maxEntrySize;
32     final long firstIndex;
33
34     JournalSegmentWriter(final FileChannel channel, final JournalSegment<E> segment, final int maxEntrySize,
35             final JournalIndex index, final JournalSerdes namespace) {
36         this.channel = requireNonNull(channel);
37         this.index = requireNonNull(index);
38         this.namespace = requireNonNull(namespace);
39         this.maxSegmentSize = segment.descriptor().maxSegmentSize();
40         this.maxEntrySize = maxEntrySize;
41         this.firstIndex = segment.index();
42     }
43
44     JournalSegmentWriter(JournalSegmentWriter<E> previous) {
45         this.channel = previous.channel;
46         this.index = previous.index;
47         this.namespace = previous.namespace;
48         this.maxSegmentSize = previous.maxSegmentSize;
49         this.maxEntrySize = previous.maxEntrySize;
50         this.firstIndex = previous.firstIndex;
51     }
52
53     /**
54      * Returns the last written index.
55      *
56      * @return The last written index.
57      */
58     abstract long getLastIndex();
59
60     /**
61      * Returns the last entry written.
62      *
63      * @return The last entry written.
64      */
65     abstract Indexed<E> getLastEntry();
66
67     /**
68      * Returns the next index to be written.
69      *
70      * @return The next index to be written.
71      */
72     abstract long getNextIndex();
73
74     /**
75      * Appends an entry to the journal.
76      *
77      * @param entry The entry to append.
78      * @return The appended indexed entry.
79      */
80     abstract <T extends E> Indexed<T> append(T entry);
81
82     /**
83      * Resets the head of the segment to the given index.
84      *
85      * @param index the index to which to reset the head of the segment
86      */
87     abstract void reset(long index);
88
89     /**
90      * Truncates the log to the given index.
91      *
92      * @param index The index to which to truncate the log.
93      */
94     abstract void truncate(long index);
95
96     /**
97      * Flushes written entries to disk.
98      */
99     abstract void flush();
100
101     /**
102      * Closes this writer.
103      */
104     abstract void close();
105
106     /**
107      * Returns the mapped buffer underlying the segment writer, or {@code null} if the writer does not have such a
108      * buffer.
109      *
110      * @return the mapped buffer underlying the segment writer, or {@code null}.
111      */
112     abstract @Nullable MappedByteBuffer buffer();
113
114     abstract @NonNull MappedJournalSegmentWriter<E> toMapped();
115
116     abstract @NonNull FileChannelJournalSegmentWriter<E> toFileChannel();
117 }