Add PANTHEON.tech copyright headers
[controller.git] / atomix-storage / src / main / java / io / atomix / storage / journal / FileChannelJournalSegmentWriter.java
index 7e9b81035d1420242bf6da21de68a05d3881b937..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;
   }
@@ -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();
         }
 
@@ -148,41 +147,11 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     }
   }
 
-  @Override
-  long getLastIndex() {
-    return lastEntry != null ? lastEntry.index() : firstIndex - 1;
-  }
-
   @Override
   Indexed<E> getLastEntry() {
     return lastEntry;
   }
 
-  @Override
-  long getNextIndex() {
-    if (lastEntry != null) {
-      return lastEntry.index() + 1;
-    } else {
-      return firstIndex;
-    }
-  }
-
-  @Override
-  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")
   <T extends E> Indexed<T> append(T entry) {
@@ -190,19 +159,17 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     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 (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,7 +196,7 @@ 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;
   }