8ddaaae3c62acc57d517adb5d9b43cab97d89efa
[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     final long getLastIndex() {
59         final Indexed<?> lastEntry;
60         return (lastEntry = getLastEntry()) != null ? lastEntry.index() : firstIndex - 1;
61     }
62
63     /**
64      * Returns the last entry written.
65      *
66      * @return The last entry written.
67      */
68     abstract Indexed<E> getLastEntry();
69
70     /**
71      * Returns the next index to be written.
72      *
73      * @return The next index to be written.
74      */
75     abstract long getNextIndex();
76
77     /**
78      * Appends an entry to the journal.
79      *
80      * @param entry The entry to append.
81      * @return The appended indexed entry.
82      */
83     abstract <T extends E> Indexed<T> append(T entry);
84
85     /**
86      * Resets the head of the segment to the given index.
87      *
88      * @param index the index to which to reset the head of the segment
89      */
90     abstract void reset(long index);
91
92     /**
93      * Truncates the log to the given index.
94      *
95      * @param index The index to which to truncate the log.
96      */
97     abstract void truncate(long index);
98
99     /**
100      * Flushes written entries to disk.
101      */
102     abstract void flush();
103
104     /**
105      * Closes this writer.
106      */
107     abstract void close();
108
109     /**
110      * Returns the mapped buffer underlying the segment writer, or {@code null} if the writer does not have such a
111      * buffer.
112      *
113      * @return the mapped buffer underlying the segment writer, or {@code null}.
114      */
115     abstract @Nullable MappedByteBuffer buffer();
116
117     abstract @NonNull MappedJournalSegmentWriter<E> toMapped();
118
119     abstract @NonNull FileChannelJournalSegmentWriter<E> toFileChannel();
120 }