Skip to content

Commit

Permalink
Remove version number from segment file name format.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Aug 29, 2017
1 parent 4bb2604 commit f0c2b10
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
Expand Up @@ -36,35 +36,44 @@ public final class JournalSegmentFile {
* @throws NullPointerException if {@code file} is null * @throws NullPointerException if {@code file} is null
*/ */
public static boolean isSegmentFile(String name, File file) { public static boolean isSegmentFile(String name, File file) {
checkNotNull(name, "name cannot be null"); return isSegmentFile(name, file.getName());
checkNotNull(file, "file cannot be null"); }
String fileName = file.getName();
if (fileName.lastIndexOf(EXTENSION_SEPARATOR) == -1 || fileName.lastIndexOf(PART_SEPARATOR) == -1 || fileName.lastIndexOf(EXTENSION_SEPARATOR) < fileName.lastIndexOf(PART_SEPARATOR) || !fileName.endsWith(EXTENSION))
return false;


for (int i = fileName.lastIndexOf(PART_SEPARATOR) + 1; i < fileName.lastIndexOf(EXTENSION_SEPARATOR); i++) { /**
if (!Character.isDigit(fileName.charAt(i))) { * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
return false; *
} * @param journalName the name of the journal
} * @param fileName the name of the file to check
* @throws NullPointerException if {@code file} is null
*/
public static boolean isSegmentFile(String journalName, String fileName) {
checkNotNull(journalName, "journalName cannot be null");
checkNotNull(fileName, "fileName cannot be null");


if (fileName.lastIndexOf(PART_SEPARATOR, fileName.lastIndexOf(PART_SEPARATOR) - 1) == -1) int partSeparator = fileName.lastIndexOf(PART_SEPARATOR);
int extensionSeparator = fileName.lastIndexOf(EXTENSION_SEPARATOR);

if (extensionSeparator == -1
|| partSeparator == -1
|| extensionSeparator < partSeparator
|| !fileName.endsWith(EXTENSION)) {
return false; return false;
}


for (int i = fileName.lastIndexOf(PART_SEPARATOR, fileName.lastIndexOf(PART_SEPARATOR) - 1) + 1; i < fileName.lastIndexOf(PART_SEPARATOR); i++) { for (int i = partSeparator + 1; i < extensionSeparator; i++) {
if (!Character.isDigit(fileName.charAt(i))) { if (!Character.isDigit(fileName.charAt(i))) {
return false; return false;
} }
} }


return fileName.substring(0, fileName.lastIndexOf(PART_SEPARATOR, fileName.lastIndexOf(PART_SEPARATOR) - 1)).equals(name); return fileName.substring(0, partSeparator).equals(journalName);
} }


/** /**
* Creates a segment file for the given directory, log name, segment ID, and segment version. * Creates a segment file for the given directory, log name, segment ID, and segment version.
*/ */
static File createSegmentFile(String name, File directory, long id, long version) { static File createSegmentFile(String name, File directory, long id) {
return new File(directory, String.format("%s-%d-%d.log", checkNotNull(name, "name cannot be null"), id, version)); return new File(directory, String.format("%s-%d.log", checkNotNull(name, "name cannot be null"), id));
} }


/** /**
Expand All @@ -87,13 +96,6 @@ public File file() {
* Returns the segment identifier. * Returns the segment identifier.
*/ */
public long id() { public long id() {
return Long.valueOf(file.getName().substring(file.getName().lastIndexOf(PART_SEPARATOR, file.getName().lastIndexOf(PART_SEPARATOR) - 1) + 1, file.getName().lastIndexOf(PART_SEPARATOR)));
}

/**
* Returns the segment version.
*/
public long version() {
return Long.valueOf(file.getName().substring(file.getName().lastIndexOf(PART_SEPARATOR) + 1, file.getName().lastIndexOf(EXTENSION_SEPARATOR))); return Long.valueOf(file.getName().substring(file.getName().lastIndexOf(PART_SEPARATOR) + 1, file.getName().lastIndexOf(EXTENSION_SEPARATOR)));
} }


Expand Down
Expand Up @@ -370,7 +370,7 @@ protected JournalSegment<E> newSegment(JournalSegmentFile segmentFile, JournalSe
* Creates a new segment. * Creates a new segment.
*/ */
private JournalSegment<E> createDiskSegment(JournalSegmentDescriptor descriptor) { private JournalSegment<E> createDiskSegment(JournalSegmentDescriptor descriptor) {
File segmentFile = JournalSegmentFile.createSegmentFile(name, directory, descriptor.id(), descriptor.version()); File segmentFile = JournalSegmentFile.createSegmentFile(name, directory, descriptor.id());
Buffer buffer = MappedBuffer.allocate(segmentFile, Math.min(DEFAULT_BUFFER_SIZE, descriptor.maxSegmentSize()), Integer.MAX_VALUE); Buffer buffer = MappedBuffer.allocate(segmentFile, Math.min(DEFAULT_BUFFER_SIZE, descriptor.maxSegmentSize()), Integer.MAX_VALUE);
descriptor.copyTo(buffer); descriptor.copyTo(buffer);
JournalSegment<E> segment = newSegment(new JournalSegmentFile(segmentFile), descriptor); JournalSegment<E> segment = newSegment(new JournalSegmentFile(segmentFile), descriptor);
Expand All @@ -382,7 +382,7 @@ private JournalSegment<E> createDiskSegment(JournalSegmentDescriptor descriptor)
* Creates a new segment. * Creates a new segment.
*/ */
private JournalSegment<E> createMemorySegment(JournalSegmentDescriptor descriptor) { private JournalSegment<E> createMemorySegment(JournalSegmentDescriptor descriptor) {
File segmentFile = JournalSegmentFile.createSegmentFile(name, directory, descriptor.id(), descriptor.version()); File segmentFile = JournalSegmentFile.createSegmentFile(name, directory, descriptor.id());
Buffer buffer = HeapBuffer.allocate(Math.min(DEFAULT_BUFFER_SIZE, descriptor.maxSegmentSize()), Integer.MAX_VALUE); Buffer buffer = HeapBuffer.allocate(Math.min(DEFAULT_BUFFER_SIZE, descriptor.maxSegmentSize()), Integer.MAX_VALUE);
descriptor.copyTo(buffer); descriptor.copyTo(buffer);
JournalSegment<E> segment = newSegment(new JournalSegmentFile(segmentFile), descriptor); JournalSegment<E> segment = newSegment(new JournalSegmentFile(segmentFile), descriptor);
Expand All @@ -393,12 +393,12 @@ private JournalSegment<E> createMemorySegment(JournalSegmentDescriptor descripto
/** /**
* Loads a segment. * Loads a segment.
*/ */
private JournalSegment<E> loadSegment(long segmentId, long segmentVersion) { private JournalSegment<E> loadSegment(long segmentId) {
switch (storageLevel) { switch (storageLevel) {
case MEMORY: case MEMORY:
return loadMemorySegment(segmentId, segmentVersion); return loadMemorySegment(segmentId);
case DISK: case DISK:
return loadDiskSegment(segmentId, segmentVersion); return loadDiskSegment(segmentId);
default: default:
throw new AssertionError(); throw new AssertionError();
} }
Expand All @@ -407,8 +407,8 @@ private JournalSegment<E> loadSegment(long segmentId, long segmentVersion) {
/** /**
* Loads a segment. * Loads a segment.
*/ */
private JournalSegment<E> loadDiskSegment(long segmentId, long segmentVersion) { private JournalSegment<E> loadDiskSegment(long segmentId) {
File file = JournalSegmentFile.createSegmentFile(name, directory, segmentId, segmentVersion); File file = JournalSegmentFile.createSegmentFile(name, directory, segmentId);
Buffer buffer = MappedBuffer.allocate(file, Math.min(DEFAULT_BUFFER_SIZE, maxSegmentSize), Integer.MAX_VALUE); Buffer buffer = MappedBuffer.allocate(file, Math.min(DEFAULT_BUFFER_SIZE, maxSegmentSize), Integer.MAX_VALUE);
JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(buffer); JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(buffer);
JournalSegment<E> segment = newSegment(new JournalSegmentFile(file), descriptor); JournalSegment<E> segment = newSegment(new JournalSegmentFile(file), descriptor);
Expand All @@ -419,8 +419,8 @@ private JournalSegment<E> loadDiskSegment(long segmentId, long segmentVersion) {
/** /**
* Loads a segment. * Loads a segment.
*/ */
private JournalSegment<E> loadMemorySegment(long segmentId, long segmentVersion) { private JournalSegment<E> loadMemorySegment(long segmentId) {
File file = JournalSegmentFile.createSegmentFile(name, directory, segmentId, segmentVersion); File file = JournalSegmentFile.createSegmentFile(name, directory, segmentId);
Buffer buffer = HeapBuffer.allocate(Math.min(DEFAULT_BUFFER_SIZE, maxSegmentSize), Integer.MAX_VALUE); Buffer buffer = HeapBuffer.allocate(Math.min(DEFAULT_BUFFER_SIZE, maxSegmentSize), Integer.MAX_VALUE);
JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(buffer); JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(buffer);
JournalSegment<E> segment = newSegment(new JournalSegmentFile(file), descriptor); JournalSegment<E> segment = newSegment(new JournalSegmentFile(file), descriptor);
Expand Down Expand Up @@ -448,7 +448,7 @@ protected Collection<JournalSegment<E>> loadSegments() {
JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(FileBuffer.allocate(file, JournalSegmentDescriptor.BYTES)); JournalSegmentDescriptor descriptor = new JournalSegmentDescriptor(FileBuffer.allocate(file, JournalSegmentDescriptor.BYTES));


// Load the segment. // Load the segment.
JournalSegment<E> segment = loadSegment(descriptor.id(), descriptor.version()); JournalSegment<E> segment = loadSegment(descriptor.id());


// If a segment with an equal or lower index has already been loaded, ensure this segment is not superseded // If a segment with an equal or lower index has already been loaded, ensure this segment is not superseded
// by the earlier segment. This can occur due to segments being combined during log compaction. // by the earlier segment. This can occur due to segments being combined during log compaction.
Expand Down
@@ -0,0 +1,47 @@
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.storage.journal;

import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Journal segment file test.
*/
public class JournalSegmentFileTest {

@Test
public void testIsSegmentFile() throws Exception {
assertTrue(JournalSegmentFile.isSegmentFile("foo", "foo-1.log"));
assertFalse(JournalSegmentFile.isSegmentFile("foo", "bar-1.log"));
assertFalse(JournalSegmentFile.isSegmentFile("foo", "foo-1-1.log"));
}

@Test
public void testCreateSegmentFile() throws Exception {
File file = JournalSegmentFile.createSegmentFile("foo", new File(System.getProperty("user.dir")), 1);
assertTrue(JournalSegmentFile.isSegmentFile("foo", file));

JournalSegmentFile segmentFile = new JournalSegmentFile(file);
assertEquals(1, segmentFile.id());
}

}

0 comments on commit f0c2b10

Please sign in to comment.