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.channels.FileChannel;
25 import java.nio.file.Path;
26 import org.eclipse.jdt.annotation.NonNull;
29 * Segment file utility.
31 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
33 final class JournalSegmentFile {
34 private static final char PART_SEPARATOR = '-';
35 private static final char EXTENSION_SEPARATOR = '.';
36 private static final String EXTENSION = "log";
38 private final @NonNull JournalSegmentDescriptor descriptor;
39 private final @NonNull Path path;
41 private final RandomAccessFile file;
43 private JournalSegmentFile(final Path path, final JournalSegmentDescriptor descriptor,
44 final RandomAccessFile file) {
45 this.path = requireNonNull(path);
46 this.descriptor = requireNonNull(descriptor);
47 this.file = requireNonNull(file);
50 static @NonNull JournalSegmentFile createNew(final String name, final File directory,
51 final JournalSegmentDescriptor descriptor) throws IOException {
52 final var file = createSegmentFile(name, directory, descriptor.id());
53 final var raf = new RandomAccessFile(file, "rw");
55 raf.setLength(descriptor.maxSegmentSize());
56 raf.write(descriptor.toArray());
57 } catch (IOException e) {
61 return new JournalSegmentFile(file.toPath(), descriptor, raf);
64 static @NonNull JournalSegmentFile openExisting(final Path path) throws IOException {
65 final var raf = new RandomAccessFile(path.toFile(), "rw");
66 final JournalSegmentDescriptor descriptor;
68 // read the descriptor
69 descriptor = JournalSegmentDescriptor.readFrom(raf.getChannel());
70 } catch (IOException e) {
74 return new JournalSegmentFile(path, descriptor, raf);
78 * Returns the segment file.
80 * @return The segment file.
82 @NonNull Path path() {
87 * Returns the segment version.
89 * @return the segment version
92 return descriptor.version();
96 * Returns the segment identifier.
98 * @return the segment identifier
101 return descriptor.id();
105 * Returns the index of first entry stored in this file.
107 * @return the index of first entry stored in this file
110 return descriptor.index();
114 return descriptor.maxSegmentSize();
117 int size() throws IOException {
118 return (int) file.length();
121 FileChannel channel() {
122 return file.getChannel();
125 void close() throws IOException {
130 public String toString() {
131 return MoreObjects.toStringHelper(this).add("path", path).add("descriptor", descriptor).toString();
135 * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
137 * @throws NullPointerException if {@code file} is null
139 public static boolean isSegmentFile(final String name, final File file) {
140 return isSegmentFile(name, file.getName());
144 * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
146 * @param journalName the name of the journal
147 * @param fileName the name of the file to check
148 * @throws NullPointerException if {@code file} is null
150 public static boolean isSegmentFile(final String journalName, final String fileName) {
151 requireNonNull(journalName, "journalName cannot be null");
152 requireNonNull(fileName, "fileName cannot be null");
154 int partSeparator = fileName.lastIndexOf(PART_SEPARATOR);
155 int extensionSeparator = fileName.lastIndexOf(EXTENSION_SEPARATOR);
157 if (extensionSeparator == -1 || partSeparator == -1 || extensionSeparator < partSeparator
158 || !fileName.endsWith(EXTENSION)) {
162 for (int i = partSeparator + 1; i < extensionSeparator; i++) {
163 if (!Character.isDigit(fileName.charAt(i))) {
168 return fileName.startsWith(journalName);
172 * Creates a segment file for the given directory, log name, segment ID, and segment version.
174 static File createSegmentFile(final String name, final File directory, final long id) {
175 return new File(directory, String.format("%s-%d.log", requireNonNull(name, "name cannot be null"), id));