Move JournalSegmentWriter switchover logic
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedJournalSegmentWriter.java
index 7ff58590d996729c51e7b8c2612f7a23e82deed5..f5d4ed9d04a9554de4ec935c6b54b85720158973 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,41 +42,44 @@ 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);
+    try {
+      mappedBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, segment.descriptor().maxSegmentSize());
+    } catch (IOException e) {
+      throw new StorageException(e);
+    }
+    this.buffer = mappedBuffer.slice();
     reset(0);
   }
 
-  /**
-   * Returns the mapped buffer underlying the segment writer.
-   *
-   * @return the mapped buffer underlying the segment writer
-   */
-  MappedByteBuffer buffer() {
+  @Override
+  @NonNull MappedByteBuffer buffer() {
     return mappedBuffer;
   }
 
+  @Override
+  MappedJournalSegmentWriter<E> toMapped() {
+    return this;
+  }
+
+  @Override
+  FileChannelJournalSegmentWriter<E> toFileChannel() {
+    close();
+    return new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
+  }
+
   @Override
   public void reset(long index) {
     long nextIndex = firstIndex;
@@ -97,19 +102,21 @@ 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);
+        crc32.update(entryBytes);
 
         // If the stored checksum does not equal the computed checksum, do not proceed further
         if (checksum != crc32.getValue()) {
           break;
         }
 
-        slice.rewind();
-        final E entry = namespace.deserialize(slice);
+        entryBytes.rewind();
+        final E entry = namespace.deserialize(entryBytes);
         lastEntry = new Indexed<>(nextIndex, entry, length);
         this.index.index(nextIndex, position);
         nextIndex++;
@@ -214,10 +221,6 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
     return (Indexed<T>) indexedEntry;
   }
 
-  @Override
-  public void commit(long index) {
-
-  }
 
   @Override
   public void truncate(long index) {