m1a2st commented on code in PR #19214:
URL: https://github.com/apache/kafka/pull/19214#discussion_r1999093157


##########
clients/src/main/java/org/apache/kafka/common/record/FileRecords.java:
##########
@@ -298,11 +298,28 @@ public int writeTo(TransferableChannel destChannel, int 
offset, int length) thro
      * @return the batch's base offset, its physical position, and its size 
(including log overhead)
      */
     public LogOffsetPosition searchForOffsetWithSize(long targetOffset, int 
startingPosition) {
+        FileChannelRecordBatch prevBatch = null;
+
         for (FileChannelRecordBatch batch : batchesFrom(startingPosition)) {
-            long offset = batch.lastOffset();
-            if (offset >= targetOffset)
-                return new LogOffsetPosition(batch.baseOffset(), 
batch.position(), batch.sizeInBytes());
+            // This indicates that either the current batch or the previous 
batch 
+            // contains the target we are looking for.
+            if (batch.baseOffset() >= targetOffset) {

Review Comment:
   In my first version, I split the logic into separate parts. However, later I 
merged the logic to reduce the number of early return branches.
   ```
           for (FileChannelRecordBatch batch : batchesFrom(startingPosition)) {
               // if baseOffset exactly equals targetOffset, return immediately
               if (batch.baseOffset() == targetOffset) {
                   return new LogOffsetPosition(batch);
               }
   
               // If we find the first batch with baseOffset greater than 
targetOffset
               if (batch.baseOffset() > targetOffset) {
                   // If there's no previous batch, return the current batch
                   if (prevBatch == null) {
                       return new LogOffsetPosition(batch);
                   }
   
                   // Check if the previous batch contains the target
                   if (prevBatch.lastOffset() >= targetOffset) {
                       return new LogOffsetPosition(prevBatch);
                   } else {
                       // If the previous batch doesn't contain the target, 
return the current batch
                       return new LogOffsetPosition(batch);
                   }
               }
               prevBatch = batch;
           }
    ```
    If having the condition if (batch.baseOffset() == targetOffset) makes it 
clearer. I can add it back.



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to