sashapolo commented on code in PR #7101:
URL: https://github.com/apache/ignite-3/pull/7101#discussion_r2597896442


##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/storage/segstore/SegmentInfo.java:
##########
@@ -168,29 +201,74 @@ int size() {
     void saveOffsetsTo(ByteBuffer buffer) {
         ArrayWithSize offsets = segmentFileOffsets;
 
+        assert offsets.size() > 0 : "Offsets array must not be empty";
+
         buffer.asIntBuffer().put(offsets.array, 0, offsets.size);
     }
 
     /**
      * Removes all data which log indices are strictly greater than {@code 
lastLogIndexKept}.
      */
-    void truncateSuffix(long lastLogIndexKept) {
+    SegmentInfo truncateSuffix(long lastLogIndexKept) {
         assert lastLogIndexKept >= logIndexBase : 
String.format("logIndexBase=%d, lastLogIndexKept=%d", logIndexBase, 
lastLogIndexKept);
 
         ArrayWithSize segmentFileOffsets = this.segmentFileOffsets;
 
-        long newSize = lastLogIndexKept - logIndexBase + 1;
+        long lastLogIndexExclusive = logIndexBase + segmentFileOffsets.size();
 
-        // Not using an assertion here, because this value comes doesn't come 
from the storage code.
-        if (newSize > segmentFileOffsets.size()) {
+        // Not using an assertion here, because this value doesn't come from 
the storage code.
+        if (lastLogIndexKept >= lastLogIndexExclusive) {
             throw new IllegalArgumentException(String.format(
                     "lastLogIndexKept is too large. Last index in memtable: 
%d, lastLogIndexKept: %d",
-                    logIndexBase + segmentFileOffsets.size() - 1, 
lastLogIndexKept
+                    lastLogIndexExclusive - 1, lastLogIndexKept
             ));
         }
 
-        ArrayWithSize newSegmentFileOffsets = 
segmentFileOffsets.truncate((int) newSize);
+        int newSize = toIntExact(lastLogIndexKept - logIndexBase + 1);
+
+        setSegmentFileOffsets(segmentFileOffsets, 
segmentFileOffsets.truncateSuffix(newSize));
+
+        // This could have been a "void" method, but this way it looks 
consistent with "truncatePrefix".
+        return this;
+    }
+
+    /**
+     * Removes all data which log indices are strictly smaller than {@code 
firstIndexKept}.
+     */
+    SegmentInfo truncatePrefix(long firstIndexKept) {
+        if (isPrefixTombstone()) {
+            if (this.firstIndexKept >= firstIndexKept) {
+                throw new IllegalStateException(String.format(
+                        "Trying to truncate an already truncated prefix 
[curFirstIndexKept=%d, newFirstIndexKept=%d]",
+                        this.firstIndexKept, firstIndexKept
+                ));
+            }
+
+            return prefixTombstone(firstIndexKept);
+        }
+
+        ArrayWithSize segmentFileOffsets = this.segmentFileOffsets;
+
+        if (firstIndexKept < logIndexBase) {
+            return new SegmentInfo(logIndexBase, firstIndexKept, 
segmentFileOffsets);
+        }

Review Comment:
   It converts the current SegmentInfo into a prefix tombstone. Added a comment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to