Add PANTHEON.tech copyright headers
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / FileChannelJournalSegmentWriter.java
index 037c20d3293e1c2a14bb2f530de9b4c6c4ff384d..f4c7ec52c3c53f7d739eff7fa30da1945010f8d1 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright 2017-present Open Networking Foundation
+ * Copyright 2017-2022 Open Networking Foundation and others.  All rights reserved.
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -42,7 +43,7 @@ import java.util.zip.CRC32;
  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
  */
 final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
-  private static final ByteBuffer ZERO_ENTRY_HEADER = ByteBuffer.wrap(new byte[Integer.BYTES + Integer.BYTES]);
+  private static final ByteBuffer ZERO_ENTRY_HEADER = ByteBuffer.wrap(new byte[ENTRY_HEADER_BYTES]);
 
   private final ByteBuffer memory;
   private Indexed<E> lastEntry;
@@ -67,7 +68,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   private static ByteBuffer allocMemory(int maxEntrySize) {
-    final var buf = ByteBuffer.allocate((maxEntrySize + Integer.BYTES + Integer.BYTES) * 2);
+    final var buf = ByteBuffer.allocate((maxEntrySize + ENTRY_HEADER_BYTES) * 2);
     buf.limit(0);
     return buf;
   }
@@ -88,7 +89,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void reset(long index) {
+  void reset(long index) {
     long nextIndex = firstIndex;
 
     // Clear the buffer indexes.
@@ -96,8 +97,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
     try {
       // Clear memory buffer and read fist chunk
-      memory.clear();
-      channel.read(memory, JournalSegmentDescriptor.BYTES);
+      channel.read(memory.clear(), JournalSegmentDescriptor.BYTES);
       memory.flip();
 
       // Read the entry length.
@@ -129,13 +129,12 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
         nextIndex++;
 
         // Update the current position for indexing.
-        currentPosition = currentPosition + Integer.BYTES + Integer.BYTES + length;
+        currentPosition = currentPosition + ENTRY_HEADER_BYTES + length;
         memory.position(memory.position() + length);
 
         // Read more bytes from the segment if necessary.
         if (memory.remaining() < maxEntrySize) {
-          memory.clear();
-          channel.read(memory, currentPosition);
+          channel.read(memory.compact());
           memory.flip();
         }
 
@@ -149,60 +148,28 @@ final class FileChannelJournalSegmentWriter<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() {
-    if (lastEntry != null) {
-      return lastEntry.index() + 1;
-    } else {
-      return firstIndex;
-    }
-  }
-
-  @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();
 
     // Serialize the entry.
-    memory.clear();
-    memory.position(Integer.BYTES + Integer.BYTES);
     try {
-      namespace.serialize(entry, memory);
+      namespace.serialize(entry, memory.clear().position(ENTRY_HEADER_BYTES));
     } catch (KryoException e) {
       throw new StorageException.TooLarge("Entry size exceeds maximum allowed bytes (" + maxEntrySize + ")");
     }
     memory.flip();
 
-    final int length = memory.limit() - (Integer.BYTES + Integer.BYTES);
+    final int length = memory.limit() - ENTRY_HEADER_BYTES;
 
     // Ensure there's enough space left in the buffer to store the entry.
-    if (segment.descriptor().maxSegmentSize() - currentPosition < length + Integer.BYTES + Integer.BYTES) {
+    if (maxSegmentSize - currentPosition < length + ENTRY_HEADER_BYTES) {
       throw new BufferOverflowException();
     }
 
@@ -213,12 +180,11 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
     // Compute the checksum for the entry.
     final CRC32 crc32 = new CRC32();
-    crc32.update(memory.array(), Integer.BYTES + Integer.BYTES, memory.limit() - (Integer.BYTES + Integer.BYTES));
+    crc32.update(memory.array(), ENTRY_HEADER_BYTES, memory.limit() - ENTRY_HEADER_BYTES);
     final long checksum = crc32.getValue();
 
     // Create a single byte[] in memory for the entire entry and write it as a batch to the underlying buffer.
-    memory.putInt(0, length);
-    memory.putInt(Integer.BYTES, (int) checksum);
+    memory.putInt(0, length).putInt(Integer.BYTES, (int) checksum);
     try {
       channel.write(memory, currentPosition);
     } catch (IOException e) {
@@ -230,12 +196,12 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     this.lastEntry = indexedEntry;
     this.index.index(index, (int) currentPosition);
 
-    currentPosition = currentPosition + Integer.BYTES + Integer.BYTES + length;
+    currentPosition = currentPosition + ENTRY_HEADER_BYTES + length;
     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;
@@ -248,7 +214,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     this.index.truncate(index);
 
     try {
-      if (index < segment.index()) {
+      if (index < firstIndex) {
         // Reset the writer to the first entry.
         currentPosition = JournalSegmentDescriptor.BYTES;
       } else {
@@ -264,7 +230,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void flush() {
+  void flush() {
     try {
       if (channel.isOpen()) {
         channel.force(true);
@@ -275,7 +241,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void close() {
+  void close() {
     flush();
   }
 }