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


##########
clients/src/main/java/org/apache/kafka/common/record/FileRecords.java:
##########
@@ -297,12 +297,33 @@ public int writeTo(TransferableChannel destChannel, int 
offset, int length) thro
      * @param startingPosition The starting position in the file to begin 
searching from.
      * @return the batch's base offset, its physical position, and its size 
(including log overhead)
      */
-    public LogOffsetPosition searchForOffsetWithSize(long targetOffset, int 
startingPosition) {
+    public LogOffsetPosition searchForOffsetFromPosition(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());
+            // If baseOffset exactly equals targetOffset, return immediately
+            if (batch.baseOffset() == targetOffset) {
+                return LogOffsetPosition.fromBatch(batch);
+            }
+            
+            // If we find the first batch with baseOffset greater than 
targetOffset
+            if (batch.baseOffset() > targetOffset) {
+                // If the previous batch contains the target
+                if (prevBatch != null && prevBatch.lastOffset() >= 
targetOffset)
+                    return LogOffsetPosition.fromBatch(prevBatch);
+                else {
+                    // If there's no previous batch or the previous batch 
doesn't contain the 
+                    // target, return the current batch
+                    return LogOffsetPosition.fromBatch(batch);
+                }
+            }
+            prevBatch = batch;
         }
+        // Only one case would reach here: all batches have baseOffset less 
than or equal to targetOffset

Review Comment:
   less than or equal to => less than



-- 
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