From 9f1702771cc777690b18ecffd0e7059dcf574475 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 7 May 2024 23:02:42 +0200 Subject: [PATCH] Use getCompactableIndex() to unmask firstIndex We have duplicated code which can easily use the result of getCompactableIndex() and work on top of that. JIRA: CONTROLLER-2100 Change-Id: Ib580853424d445d82c448a86d82a706f4bba50d2 Signed-off-by: Robert Varga --- .../storage/journal/SegmentedByteBufJournal.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedByteBufJournal.java b/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedByteBufJournal.java index 3ae64ea82e..c3e4b2bad1 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedByteBufJournal.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedByteBufJournal.java @@ -370,8 +370,8 @@ public final class SegmentedByteBufJournal implements ByteBufJournal { * @return indicates whether a segment can be removed from the journal */ public boolean isCompactable(final long index) { - final var segmentEntry = segments.floorEntry(index); - return segmentEntry != null && segments.headMap(segmentEntry.getValue().firstIndex()).size() > 0; + final var firstIndex = getCompactableIndex(index); + return firstIndex != 0 && !segments.headMap(firstIndex).isEmpty(); } /** @@ -393,14 +393,14 @@ public final class SegmentedByteBufJournal implements ByteBufJournal { * @param index The index up to which to compact the journal. */ public void compact(final long index) { - final var segmentEntry = segments.floorEntry(index); - if (segmentEntry != null) { - final var compactSegments = segments.headMap(segmentEntry.getValue().firstIndex()); + final var firstIndex = getCompactableIndex(index); + if (firstIndex != 0) { + final var compactSegments = segments.headMap(firstIndex); if (!compactSegments.isEmpty()) { LOG.debug("{} - Compacting {} segment(s)", name, compactSegments.size()); compactSegments.values().forEach(JournalSegment::delete); compactSegments.clear(); - resetHead(segmentEntry.getValue().firstIndex()); + resetHead(firstIndex); } } } -- 2.36.6