|
44 | 44 | import java.util.concurrent.ExecutorService;
|
45 | 45 | import java.util.concurrent.Executors;
|
46 | 46 | import java.util.concurrent.Future;
|
| 47 | +import java.util.stream.IntStream; |
47 | 48 |
|
48 | 49 | import static java.util.Arrays.asList;
|
49 | 50 | import static org.apache.kafka.common.utils.Utils.utf8;
|
@@ -433,6 +434,38 @@ public void testSearchForTimestamp() throws IOException {
|
433 | 434 | }
|
434 | 435 | }
|
435 | 436 |
|
| 437 | + /** |
| 438 | + * Test slice when already sliced file records have start position greater than available bytes |
| 439 | + * in the file records. |
| 440 | + */ |
| 441 | + @Test |
| 442 | + public void testSliceForAlreadySlicedFileRecords() throws IOException { |
| 443 | + byte[][] values = new byte[][] { |
| 444 | + "abcd".getBytes(), |
| 445 | + "efgh".getBytes(), |
| 446 | + "ijkl".getBytes(), |
| 447 | + "mnop".getBytes(), |
| 448 | + "qrst".getBytes() |
| 449 | + }; |
| 450 | + try (FileRecords fileRecords = createFileRecords(values)) { |
| 451 | + List<RecordBatch> items = batches(fileRecords.slice(0, fileRecords.sizeInBytes())); |
| 452 | + |
| 453 | + // Slice from fourth message until the end. |
| 454 | + int position = IntStream.range(0, 3).map(i -> items.get(i).sizeInBytes()).sum(); |
| 455 | + FileRecords sliced = fileRecords.slice(position, fileRecords.sizeInBytes() - position); |
| 456 | + assertEquals(fileRecords.sizeInBytes() - position, sliced.sizeInBytes()); |
| 457 | + assertEquals(items.subList(3, items.size()), batches(sliced), "Read starting from the fourth message"); |
| 458 | + |
| 459 | + // Further slice the already sliced file records, from fifth message until the end. Now the |
| 460 | + // bytes available in the sliced file records are less than the start position. However, the |
| 461 | + // position to slice is relative hence reset position to first batch in the sliced file records. |
| 462 | + position = items.get(4).sizeInBytes(); |
| 463 | + FileRecords finalSliced = sliced.slice(position, sliced.sizeInBytes() - position); |
| 464 | + assertEquals(sliced.sizeInBytes() - position, finalSliced.sizeInBytes()); |
| 465 | + assertEquals(items.subList(4, items.size()), batches(finalSliced), "Read starting from the fifth message"); |
| 466 | + } |
| 467 | + } |
| 468 | + |
436 | 469 | private void testSearchForTimestamp(RecordVersion version) throws IOException {
|
437 | 470 | File temp = tempFile();
|
438 | 471 | FileRecords fileRecords = FileRecords.open(temp, false, 1024 * 1024, true);
|
|
0 commit comments