Improve segmented journal actor metrics
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / FileReader.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 com.google.common.base.MoreObjects;
21 import java.nio.ByteBuffer;
22 import java.nio.file.Path;
23 import org.eclipse.jdt.annotation.NonNull;
24
25 /**
26  * An abstraction over how to read a {@link JournalSegmentFile}.
27  */
28 abstract sealed class FileReader permits DiskFileReader, MappedFileReader {
29     private final Path path;
30
31     FileReader(final Path path) {
32         this.path = requireNonNull(path);
33     }
34
35     /**
36      * Invalidate any cache that is present, so that the next read is coherent with the backing file.
37      */
38     abstract void invalidateCache();
39
40     /**
41      * Read the some bytes as specified position. The sum of position and size is guaranteed not to exceed the maximum
42      * segment size nor maximum entry size.
43      *
44      * @param position position to the entry header
45      * @param size to read
46      * @return resulting buffer
47      */
48     abstract @NonNull ByteBuffer read(int position, int size);
49
50     @Override
51     public final String toString() {
52         return MoreObjects.toStringHelper(this).add("path", path).toString();
53     }
54 }