Make JournalSegmentWriter.getLastIndex() final
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / MappedJournalSegmentWriter.java
index f5d4ed9d04a9554de4ec935c6b54b85720158973..ffc45dcfe75c3d5d4969b81b910b53f0de00014d 100644 (file)
@@ -55,13 +55,25 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
       JournalIndex index,
       JournalSerdes namespace) {
     super(channel, segment, maxEntrySize, index, namespace);
+    mappedBuffer = mapBuffer(channel, maxSegmentSize);
+    buffer = mappedBuffer.slice();
+    reset(0);
+  }
+
+  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 {
-      mappedBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, segment.descriptor().maxSegmentSize());
+      return channel.map(FileChannel.MapMode.READ_WRITE, 0, maxSegmentSize);
     } catch (IOException e) {
       throw new StorageException(e);
     }
-    this.buffer = mappedBuffer.slice();
-    reset(0);
   }
 
   @Override
@@ -76,12 +88,13 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
   @Override
   FileChannelJournalSegmentWriter<E> toFileChannel() {
+    final int position = buffer.position();
     close();
-    return new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
+    return new FileChannelJournalSegmentWriter<>(this, position);
   }
 
   @Override
-  public void reset(long index) {
+  void reset(long index) {
     long nextIndex = firstIndex;
 
     // Clear the buffer indexes.
@@ -137,17 +150,12 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<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 {
@@ -155,25 +163,9 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<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();
 
@@ -221,9 +213,8 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     return (Indexed<T>) indexedEntry;
   }
 
-
   @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;
@@ -235,7 +226,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<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 {
@@ -251,12 +242,12 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void flush() {
+  void flush() {
     mappedBuffer.force();
   }
 
   @Override
-  public void close() {
+  void close() {
     flush();
     try {
       BufferCleaner.freeBuffer(mappedBuffer);