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 descriptor.
89 * @return The segment descriptor.
91 @NonNull JournalSegmentDescriptor descriptor() {
96 return descriptor.maxSegmentSize();
99 int size() throws IOException {
100 return (int) file.length();
103 FileChannel channel() {
104 return file.getChannel();
107 void close() throws IOException {
112 public String toString() {
113 return MoreObjects.toStringHelper(this).add("path", path).add("descriptor", descriptor).toString();
117 * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
119 * @throws NullPointerException if {@code file} is null
121 public static boolean isSegmentFile(final String name, final File file) {
122 return isSegmentFile(name, file.getName());
126 * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
128 * @param journalName the name of the journal
129 * @param fileName the name of the file to check
130 * @throws NullPointerException if {@code file} is null
132 public static boolean isSegmentFile(final String journalName, final String fileName) {
133 requireNonNull(journalName, "journalName cannot be null");
134 requireNonNull(fileName, "fileName cannot be null");
136 int partSeparator = fileName.lastIndexOf(PART_SEPARATOR);
137 int extensionSeparator = fileName.lastIndexOf(EXTENSION_SEPARATOR);
139 if (extensionSeparator == -1 || partSeparator == -1 || extensionSeparator < partSeparator
140 || !fileName.endsWith(EXTENSION)) {
144 for (int i = partSeparator + 1; i < extensionSeparator; i++) {
145 if (!Character.isDigit(fileName.charAt(i))) {
150 return fileName.startsWith(journalName);
154 * Creates a segment file for the given directory, log name, segment ID, and segment version.
156 static File createSegmentFile(final String name, final File directory, final long id) {
157 return new File(directory, String.format("%s-%d.log", requireNonNull(name, "name cannot be null"), id));