Invert checksum check 70/110570/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 9 Mar 2024 14:49:37 +0000 (15:49 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 11 Mar 2024 12:51:11 +0000 (12:51 +0000)
We have a hardly-visible else branch which terminates our loop. Increase
its visibility by making it a dedicated if.

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

index b399b5f9c83872daeaafd138e417689e8e83ccad..5bd5212c2eb83675327cb529c1ea18e5e0817c3d 100644 (file)
@@ -99,19 +99,19 @@ class FileChannelJournalSegmentWriter<E> implements JournalWriter<E> {
         final CRC32 crc32 = new CRC32();
         crc32.update(memory.array(), memory.position(), length);
 
-        // If the stored checksum equals the computed checksum, return the entry.
-        if (checksum == crc32.getValue()) {
-          int limit = memory.limit();
-          memory.limit(memory.position() + length);
-          final E entry = namespace.deserialize(memory);
-          memory.limit(limit);
-          lastEntry = new Indexed<>(nextIndex, entry, length);
-          this.index.index(nextIndex, (int) position);
-          nextIndex++;
-        } else {
+        // If the stored checksum does not equal the computed checksum, do not proceed further
+        if (checksum != crc32.getValue()) {
           break;
         }
 
+        int limit = memory.limit();
+        memory.limit(memory.position() + length);
+        final E entry = namespace.deserialize(memory);
+        memory.limit(limit);
+        lastEntry = new Indexed<>(nextIndex, entry, length);
+        this.index.index(nextIndex, (int) position);
+        nextIndex++;
+
         // Update the current position for indexing.
         position = channel.position() + memory.position();
 
index a851447d86122fd881a5c0fa8d03a745d92b85aa..7ff58590d996729c51e7b8c2612f7a23e82deed5 100644 (file)
@@ -103,17 +103,17 @@ class MappedJournalSegmentWriter<E> implements JournalWriter<E> {
         slice.limit(length);
         crc32.update(slice);
 
-        // If the stored checksum equals the computed checksum, return the entry.
-        if (checksum == crc32.getValue()) {
-          slice.rewind();
-          final E entry = namespace.deserialize(slice);
-          lastEntry = new Indexed<>(nextIndex, entry, length);
-          this.index.index(nextIndex, position);
-          nextIndex++;
-        } else {
+        // 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);
+        lastEntry = new Indexed<>(nextIndex, entry, length);
+        this.index.index(nextIndex, position);
+        nextIndex++;
+
         // Update the current position for indexing.
         position = buffer.position() + length;
         buffer.position(position);