Bump versions 9.0.4-SNAPSHOT
[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 io.netty.buffer.ByteBuf;
22 import org.eclipse.jdt.annotation.NonNull;
23
24 /**
25  * An abstraction over how to read a {@link JournalSegmentFile}.
26  */
27 abstract sealed class FileReader permits DiskFileReader, MappedFileReader {
28     private final @NonNull JournalSegmentFile file;
29
30     FileReader(final JournalSegmentFile file) {
31         this.file = requireNonNull(file);
32     }
33
34     /**
35      * Invalidate any cache that is present, so that the next read is coherent with the backing file.
36      */
37     abstract void invalidateCache();
38
39     /**
40      * Read the some bytes as specified position. The sum of position and size is guaranteed not to exceed the maximum
41      * segment size nor maximum entry size.
42      *
43      * @param position position to the entry header
44      * @param size to read
45      * @return resulting buffer
46      */
47     abstract @NonNull ByteBuf read(int position, int size);
48
49     /**
50      * Releases resources associated with this writer.
51      */
52     abstract void release();
53
54     @Override
55     public final String toString() {
56         return MoreObjects.toStringHelper(this).add("path", file.path()).toString();
57     }
58 }