Expand JournalSegmentFile semantics
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedFileWriter.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 io.netty.util.internal.PlatformDependent;
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.nio.MappedByteBuffer;
22 import java.nio.channels.FileChannel;
23 import org.eclipse.jdt.annotation.NonNull;
24
25 /**
26  * A {@link StorageLevel#MAPPED} {@link FileWriter}.
27  */
28 final class MappedFileWriter extends FileWriter {
29     private final @NonNull MappedByteBuffer mappedBuffer;
30     private final MappedFileReader reader;
31     private final ByteBuffer buffer;
32
33     MappedFileWriter(final JournalSegmentFile file, final FileChannel channel, final int maxEntrySize) {
34         super(file, channel, maxEntrySize);
35
36         mappedBuffer = mapBuffer(channel, file.maxSize());
37         buffer = mappedBuffer.slice();
38         reader = new MappedFileReader(file, mappedBuffer);
39     }
40
41     private static @NonNull MappedByteBuffer mapBuffer(final FileChannel channel, final int maxSegmentSize) {
42         try {
43             return channel.map(FileChannel.MapMode.READ_WRITE, 0, maxSegmentSize);
44         } catch (IOException e) {
45             throw new StorageException(e);
46         }
47     }
48
49     @Override
50     MappedFileReader reader() {
51         return reader;
52     }
53
54     @Override
55     MappedByteBuffer buffer() {
56         return mappedBuffer;
57     }
58
59     @Override
60     MappedFileWriter toMapped() {
61         return null;
62     }
63
64     @Override
65     DiskFileWriter toDisk() {
66         close();
67         return new DiskFileWriter(file, channel, maxEntrySize);
68     }
69
70     @Override
71     void writeEmptyHeader(final int position) {
72         // Note: we issue a single putLong() instead of two putInt()s.
73         buffer.putLong(position, 0L);
74     }
75
76     @Override
77     ByteBuffer startWrite(final int position, final int size) {
78         return buffer.slice(position, size);
79     }
80
81     @Override
82     void commitWrite(final int position, final ByteBuffer entry) {
83         // No-op, buffer is write-through
84     }
85
86     @Override
87     void flush() {
88         mappedBuffer.force();
89     }
90
91     @Override
92     void close() {
93         flush();
94         PlatformDependent.freeDirectBuffer(mappedBuffer);
95     }
96 }