Centralize entry header size 47/110747/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 15 Mar 2024 11:09:13 +0000 (12:09 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 15 Mar 2024 11:09:55 +0000 (12:09 +0100)
We jave 'Integer.BYTES + Integer.BYTES' strewn all across the code,
centralize this constant into a single place.

JIRA: CONTROLLER-2098
Change-Id: If15f944db20b01767ca387f652ddf11fed64e2fa
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentReader.java
atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java

index e4409ad25727a55bbf96c2b8892b57be0e2428b2..b776b3b167bff655ee3c2600821eefd3ea9ce0dc 100644 (file)
@@ -40,7 +40,7 @@ final class FileChannelJournalSegmentReader<E> extends JournalSegmentReader<E> {
       JournalSerdes namespace) {
     super(segment, maxEntrySize, index, namespace);
     this.channel = channel;
-    this.memory = ByteBuffer.allocate((maxEntrySize + Integer.BYTES + Integer.BYTES) * 2);
+    this.memory = ByteBuffer.allocate((maxEntrySize + JournalSegmentWriter.ENTRY_HEADER_BYTES) * 2);
     reset();
   }
 
index c4423d3c92951fba652f138ae230e40dfcd86eaf..9593064ef35370b6d392d05fddd648e99f7f7c53 100644 (file)
@@ -42,7 +42,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 +67,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;
   }
@@ -128,7 +128,7 @@ 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.
@@ -159,16 +159,16 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
     // Serialize the entry.
     try {
-      namespace.serialize(entry, memory.clear().position(Integer.BYTES + Integer.BYTES));
+      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();
     }
 
@@ -179,7 +179,7 @@ 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.
@@ -195,7 +195,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;
   }
 
index ae3c0b5058ec74dce39c9edde962d232bf06a6fa..309ce39be96c251b2b4acf858063918238398c2c 100644 (file)
@@ -24,6 +24,15 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 
 abstract sealed class JournalSegmentWriter<E> permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
+    /**
+     * The size of the header, comprising of:
+     * <ul>
+     *   <li>32-bit signed entry length</li>
+     *   <li>32-bit unsigned CRC32 checksum</li>
+     * </li>
+     */
+    static final int ENTRY_HEADER_BYTES = Integer.BYTES + Integer.BYTES;
+
     final @NonNull FileChannel channel;
     final @NonNull JournalIndex index;
     final @NonNull JournalSerdes namespace;
index 76080c583d43d2fe34013647fba43aeb95ef8ad6..f78e31541bffabda7ded4e9b5bab53093c3d1b3c 100644 (file)
@@ -162,11 +162,11 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
     // Serialize the entry.
     int position = buffer.position();
-    if (position + Integer.BYTES + Integer.BYTES > buffer.limit()) {
+    if (position + ENTRY_HEADER_BYTES > buffer.limit()) {
       throw new BufferOverflowException();
     }
 
-    buffer.position(position + Integer.BYTES + Integer.BYTES);
+    buffer.position(position + ENTRY_HEADER_BYTES);
 
     try {
       namespace.serialize(entry, buffer);
@@ -174,7 +174,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
       throw new BufferOverflowException();
     }
 
-    final int length = buffer.position() - (position + Integer.BYTES + Integer.BYTES);
+    final int length = buffer.position() - (position + ENTRY_HEADER_BYTES);
 
     // If the entry length exceeds the maximum entry size then throw an exception.
     if (length > maxEntrySize) {
@@ -185,7 +185,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
     // Compute the checksum for the entry.
     final CRC32 crc32 = new CRC32();
-    buffer.position(position + Integer.BYTES + Integer.BYTES);
+    buffer.position(position + ENTRY_HEADER_BYTES);
     ByteBuffer slice = buffer.slice();
     slice.limit(length);
     crc32.update(slice);
@@ -195,7 +195,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     buffer.position(position);
     buffer.putInt(length);
     buffer.putInt((int) checksum);
-    buffer.position(position + Integer.BYTES + Integer.BYTES + length);
+    buffer.position(position + ENTRY_HEADER_BYTES + length);
 
     // Update the last entry with the correct index/term/length.
     Indexed<E> indexedEntry = new Indexed<>(index, entry, length);