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 static java.util.Objects.requireNonNull;
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;
26 * An abstraction over how to read a {@link JournalSegmentFile}.
28 abstract sealed class FileReader permits DiskFileReader, MappedFileReader {
29 private final Path path;
31 FileReader(final Path path) {
32 this.path = requireNonNull(path);
36 * Invalidate any cache that is present, so that the next read is coherent with the backing file.
38 abstract void invalidateCache();
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.
44 * @param position position to the entry header
46 * @return resulting buffer
48 abstract @NonNull ByteBuffer read(int position, int size);
51 public final String toString() {
52 return MoreObjects.toStringHelper(this).add("path", path).toString();