Enable checkstyle
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedFileAccess.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.netty.util.internal.PlatformDependent;
21 import java.io.UncheckedIOException;
22 import java.nio.MappedByteBuffer;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24
25 /**
26  * {@link FileAccess} for {@link StorageLevel#MAPPED}.
27  */
28 @NonNullByDefault
29 final class MappedFileAccess extends FileAccess {
30     private final MappedByteBuffer mappedBuffer;
31
32     MappedFileAccess(final JournalSegmentFile file, final int maxEntrySize, final MappedByteBuffer mappedBuffer) {
33         super(file, maxEntrySize);
34         this.mappedBuffer = requireNonNull(mappedBuffer);
35     }
36
37     @Override
38     MappedFileReader newFileReader() {
39         return new MappedFileReader(file, mappedBuffer.slice());
40     }
41
42     @Override
43     @SuppressWarnings("checkstyle:avoidHidingCauseException")
44     MappedFileWriter newFileWriter() {
45         return new MappedFileWriter(file, maxEntrySize, mappedBuffer.slice(), () -> {
46             try {
47                 mappedBuffer.force();
48             } catch (UncheckedIOException e) {
49                 throw e.getCause();
50             }
51         });
52     }
53
54     @Override
55     public void close() {
56         PlatformDependent.freeDirectBuffer(mappedBuffer);
57     }
58 }