2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.storage.journal;
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;
26 * A {@link StorageLevel#MAPPED} {@link FileWriter}.
28 final class MappedFileWriter extends FileWriter {
29 private final @NonNull MappedByteBuffer mappedBuffer;
30 private final MappedFileReader reader;
31 private final ByteBuffer buffer;
33 MappedFileWriter(final JournalSegmentFile file, final FileChannel channel, final int maxEntrySize) {
34 super(file, channel, maxEntrySize);
36 mappedBuffer = mapBuffer(channel, file.maxSize());
37 buffer = mappedBuffer.slice();
38 reader = new MappedFileReader(file, mappedBuffer);
41 private static @NonNull MappedByteBuffer mapBuffer(final FileChannel channel, final int maxSegmentSize) {
43 return channel.map(FileChannel.MapMode.READ_WRITE, 0, maxSegmentSize);
44 } catch (IOException e) {
45 throw new StorageException(e);
50 MappedFileReader reader() {
55 MappedByteBuffer buffer() {
60 MappedFileWriter toMapped() {
65 DiskFileWriter toDisk() {
67 return new DiskFileWriter(file, channel, maxEntrySize);
71 void writeEmptyHeader(final int position) {
72 // Note: we issue a single putLong() instead of two putInt()s.
73 buffer.putLong(position, 0L);
77 ByteBuffer startWrite(final int position, final int size) {
78 return buffer.slice(position, size);
82 void commitWrite(final int position, final ByteBuffer entry) {
83 // No-op, buffer is write-through
94 PlatformDependent.freeDirectBuffer(mappedBuffer);