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 io.netty.buffer.ByteBuf;
22 import org.eclipse.jdt.annotation.NonNull;
25 * An abstraction over how to read a {@link JournalSegmentFile}.
27 abstract sealed class FileReader permits DiskFileReader, MappedFileReader {
28 private final @NonNull JournalSegmentFile file;
30 FileReader(final JournalSegmentFile file) {
31 this.file = requireNonNull(file);
35 * Invalidate any cache that is present, so that the next read is coherent with the backing file.
37 abstract void invalidateCache();
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.
43 * @param position position to the entry header
45 * @return resulting buffer
47 abstract @NonNull ByteBuf read(int position, int size);
50 * Releases resources associated with this writer.
52 abstract void release();
55 public final String toString() {
56 return MoreObjects.toStringHelper(this).add("path", file.path()).toString();