Make JournalSegmentWriter.getLastIndex() final
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedJournalSegmentWriter.java
index a851447d86122fd881a5c0fa8d03a745d92b85aa..ffc45dcfe75c3d5d4969b81b910b53f0de00014d 100644 (file)
@@ -23,7 +23,9 @@ import java.nio.BufferOverflowException;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
 import java.util.zip.CRC32;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * Segment writer.
@@ -40,43 +42,59 @@ import java.util.zip.CRC32;
  *
  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
  */
-class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
-  private final MappedByteBuffer mappedBuffer;
+final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
+  private final @NonNull MappedByteBuffer mappedBuffer;
   private final ByteBuffer buffer;
-  private final JournalSegment<E> segment;
-  private final int maxEntrySize;
-  private final JournalIndex index;
-  private final JournalSerdes namespace;
-  private final long firstIndex;
+
   private Indexed<E> lastEntry;
 
   MappedJournalSegmentWriter(
-      MappedByteBuffer buffer,
+      FileChannel channel,
       JournalSegment<E> segment,
       int maxEntrySize,
       JournalIndex index,
       JournalSerdes namespace) {
-    this.mappedBuffer = buffer;
-    this.buffer = buffer.slice();
-    this.segment = segment;
-    this.maxEntrySize = maxEntrySize;
-    this.index = index;
-    this.namespace = namespace;
-    this.firstIndex = segment.index();
+    super(channel, segment, maxEntrySize, index, namespace);
+    mappedBuffer = mapBuffer(channel, maxSegmentSize);
+    buffer = mappedBuffer.slice();
     reset(0);
   }
 
-  /**
-   * Returns the mapped buffer underlying the segment writer.
-   *
-   * @return the mapped buffer underlying the segment writer
-   */
-  MappedByteBuffer buffer() {
+  MappedJournalSegmentWriter(JournalSegmentWriter<E> previous, int position) {
+    super(previous);
+    mappedBuffer = mapBuffer(channel, maxSegmentSize);
+    buffer = mappedBuffer.slice();
+    lastEntry = previous.getLastEntry();
+    buffer.position(position);
+  }
+
+  private static @NonNull MappedByteBuffer mapBuffer(FileChannel channel, int maxSegmentSize) {
+    try {
+      return channel.map(FileChannel.MapMode.READ_WRITE, 0, maxSegmentSize);
+    } catch (IOException e) {
+      throw new StorageException(e);
+    }
+  }
+
+  @Override
+  @NonNull MappedByteBuffer buffer() {
     return mappedBuffer;
   }
 
   @Override
-  public void reset(long index) {
+  MappedJournalSegmentWriter<E> toMapped() {
+    return this;
+  }
+
+  @Override
+  FileChannelJournalSegmentWriter<E> toFileChannel() {
+    final int position = buffer.position();
+    close();
+    return new FileChannelJournalSegmentWriter<>(this, position);
+  }
+
+  @Override
+  void reset(long index) {
     long nextIndex = firstIndex;
 
     // Clear the buffer indexes.
@@ -97,23 +115,25 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
         // Read the checksum of the entry.
         final long checksum = buffer.getInt() & 0xFFFFFFFFL;
 
+        // Slice off the entry's bytes
+        final ByteBuffer entryBytes = buffer.slice();
+        entryBytes.limit(length);
+
         // Compute the checksum for the entry bytes.
         final CRC32 crc32 = new CRC32();
-        ByteBuffer slice = buffer.slice();
-        slice.limit(length);
-        crc32.update(slice);
-
-        // If the stored checksum equals the computed checksum, return the entry.
-        if (checksum == crc32.getValue()) {
-          slice.rewind();
-          final E entry = namespace.deserialize(slice);
-          lastEntry = new Indexed<>(nextIndex, entry, length);
-          this.index.index(nextIndex, position);
-          nextIndex++;
-        } else {
+        crc32.update(entryBytes);
+
+        // If the stored checksum does not equal the computed checksum, do not proceed further
+        if (checksum != crc32.getValue()) {
           break;
         }
 
+        entryBytes.rewind();
+        final E entry = namespace.deserialize(entryBytes);
+        lastEntry = new Indexed<>(nextIndex, entry, length);
+        this.index.index(nextIndex, position);
+        nextIndex++;
+
         // Update the current position for indexing.
         position = buffer.position() + length;
         buffer.position(position);
@@ -130,17 +150,12 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
   }
 
   @Override
-  public long getLastIndex() {
-    return lastEntry != null ? lastEntry.index() : segment.index() - 1;
-  }
-
-  @Override
-  public Indexed<E> getLastEntry() {
+  Indexed<E> getLastEntry() {
     return lastEntry;
   }
 
   @Override
-  public long getNextIndex() {
+  long getNextIndex() {
     if (lastEntry != null) {
       return lastEntry.index() + 1;
     } else {
@@ -148,25 +163,9 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
     }
   }
 
-  @Override
-  public void append(Indexed<E> entry) {
-    final long nextIndex = getNextIndex();
-
-    // If the entry's index is greater than the next index in the segment, skip some entries.
-    if (entry.index() > nextIndex) {
-      throw new IndexOutOfBoundsException("Entry index is not sequential");
-    }
-
-    // If the entry's index is less than the next index, truncate the segment.
-    if (entry.index() < nextIndex) {
-      truncate(entry.index() - 1);
-    }
-    append(entry.entry());
-  }
-
   @Override
   @SuppressWarnings("unchecked")
-  public <T extends E> Indexed<T> append(T entry) {
+  <T extends E> Indexed<T> append(T entry) {
     // Store the entry index.
     final long index = getNextIndex();
 
@@ -215,12 +214,7 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
   }
 
   @Override
-  public void commit(long index) {
-
-  }
-
-  @Override
-  public void truncate(long index) {
+  void truncate(long index) {
     // If the index is greater than or equal to the last index, skip the truncate.
     if (index >= getLastIndex()) {
       return;
@@ -232,7 +226,7 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
     // Truncate the index.
     this.index.truncate(index);
 
-    if (index < segment.index()) {
+    if (index < firstIndex) {
       // Reset the writer to the first entry.
       buffer.position(JournalSegmentDescriptor.BYTES);
     } else {
@@ -248,12 +242,12 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
   }
 
   @Override
-  public void flush() {
+  void flush() {
     mappedBuffer.force();
   }
 
   @Override
-  public void close() {
+  void close() {
     flush();
     try {
       BufferCleaner.freeBuffer(mappedBuffer);