2 * Copyright 2015-2022 Open Networking Foundation 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;
22 import java.io.IOException;
23 import java.io.RandomAccessFile;
24 import java.nio.ByteBuffer;
25 import java.nio.channels.FileChannel;
26 import java.nio.file.Path;
27 import org.eclipse.jdt.annotation.NonNull;
30 * Segment file utility.
32 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
34 final class JournalSegmentFile {
35 private static final char PART_SEPARATOR = '-';
36 private static final char EXTENSION_SEPARATOR = '.';
37 private static final String EXTENSION = "log";
39 * Just do not bother with IO smaller than this many bytes.
41 private static final int MIN_IO_SIZE = 8192;
43 private final @NonNull JournalSegmentDescriptor descriptor;
44 private final @NonNull Path path;
46 private final RandomAccessFile file;
48 private JournalSegmentFile(final Path path, final JournalSegmentDescriptor descriptor,
49 final RandomAccessFile file) {
50 this.path = requireNonNull(path);
51 this.descriptor = requireNonNull(descriptor);
52 this.file = requireNonNull(file);
55 static @NonNull JournalSegmentFile createNew(final String name, final File directory,
56 final JournalSegmentDescriptor descriptor) throws IOException {
57 final var file = createSegmentFile(name, directory, descriptor.id());
58 final var raf = new RandomAccessFile(file, "rw");
60 raf.setLength(descriptor.maxSegmentSize());
61 raf.write(descriptor.toArray());
62 } catch (IOException e) {
66 return new JournalSegmentFile(file.toPath(), descriptor, raf);
69 static @NonNull JournalSegmentFile openExisting(final Path path) throws IOException {
70 final var raf = new RandomAccessFile(path.toFile(), "rw");
71 final JournalSegmentDescriptor descriptor;
73 // read the descriptor
74 descriptor = JournalSegmentDescriptor.readFrom(raf.getChannel());
75 } catch (IOException e) {
79 return new JournalSegmentFile(path, descriptor, raf);
83 * Returns the segment file.
85 * @return The segment file.
87 @NonNull Path path() {
92 * Returns the segment version.
94 * @return the segment version
97 return descriptor.version();
101 * Returns the segment identifier.
103 * @return the segment identifier
106 return descriptor.id();
110 * Returns the index of first entry stored in this file.
112 * @return the index of first entry stored in this file
115 return descriptor.index();
119 return descriptor.maxSegmentSize();
122 int size() throws IOException {
123 return (int) file.length();
126 FileChannel channel() {
127 return file.getChannel();
130 void close() throws IOException {
134 ByteBuffer allocateBuffer(final int maxEntrySize) {
135 return ByteBuffer.allocate(chooseBufferSize(maxEntrySize));
139 public String toString() {
140 return MoreObjects.toStringHelper(this).add("path", path).add("descriptor", descriptor).toString();
143 private int chooseBufferSize(final int maxEntrySize) {
144 final int maxSegmentSize = maxSize();
145 if (maxSegmentSize <= MIN_IO_SIZE) {
146 // just buffer the entire segment
147 return maxSegmentSize;
150 // one full entry plus its header, or MIN_IO_SIZE, which benefits the read of many small entries
151 final int minBufferSize = maxEntrySize + SegmentEntry.HEADER_BYTES;
152 return minBufferSize <= MIN_IO_SIZE ? MIN_IO_SIZE : minBufferSize;
156 * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
158 * @throws NullPointerException if {@code file} is null
160 public static boolean isSegmentFile(final String name, final File file) {
161 return isSegmentFile(name, file.getName());
165 * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
167 * @param journalName the name of the journal
168 * @param fileName the name of the file to check
169 * @throws NullPointerException if {@code file} is null
171 public static boolean isSegmentFile(final String journalName, final String fileName) {
172 requireNonNull(journalName, "journalName cannot be null");
173 requireNonNull(fileName, "fileName cannot be null");
175 int partSeparator = fileName.lastIndexOf(PART_SEPARATOR);
176 int extensionSeparator = fileName.lastIndexOf(EXTENSION_SEPARATOR);
178 if (extensionSeparator == -1 || partSeparator == -1 || extensionSeparator < partSeparator
179 || !fileName.endsWith(EXTENSION)) {
183 for (int i = partSeparator + 1; i < extensionSeparator; i++) {
184 if (!Character.isDigit(fileName.charAt(i))) {
189 return fileName.startsWith(journalName);
193 * Creates a segment file for the given directory, log name, segment ID, and segment version.
195 static File createSegmentFile(final String name, final File directory, final long id) {
196 return new File(directory, String.format("%s-%d.log", requireNonNull(name, "name cannot be null"), id));