From b004038a282786fc29b4ba7fc9f8874debe21afd Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 9 Mar 2024 15:49:37 +0100 Subject: [PATCH] Invert checksum check 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 --- .../FileChannelJournalSegmentWriter.java | 20 +++++++++---------- .../journal/MappedJournalSegmentWriter.java | 16 +++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java index b399b5f9c8..5bd5212c2e 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java @@ -99,19 +99,19 @@ class FileChannelJournalSegmentWriter implements JournalWriter { 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(); diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java index a851447d86..7ff58590d9 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java @@ -103,17 +103,17 @@ class MappedJournalSegmentWriter implements JournalWriter { 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); -- 2.36.6