use of org.apache.kafka.common.record.UnalignedFileRecords in project kafka by apache.
the class FileRawSnapshotTest method testPartialWriteReadSnapshot.
@Test
public void testPartialWriteReadSnapshot() throws IOException {
Path tempDir = TestUtils.tempDirectory().toPath();
OffsetAndEpoch offsetAndEpoch = new OffsetAndEpoch(10L, 3);
ByteBuffer records = buildRecords(ByteBuffer.wrap(Utils.utf8("foo"))).buffer();
ByteBuffer expectedBuffer = ByteBuffer.wrap(records.array());
ByteBuffer buffer1 = expectedBuffer.duplicate();
buffer1.position(0);
buffer1.limit(expectedBuffer.limit() / 2);
ByteBuffer buffer2 = expectedBuffer.duplicate();
buffer2.position(expectedBuffer.limit() / 2);
buffer2.limit(expectedBuffer.limit());
try (FileRawSnapshotWriter snapshot = createSnapshotWriter(tempDir, offsetAndEpoch)) {
snapshot.append(new UnalignedMemoryRecords(buffer1));
snapshot.append(new UnalignedMemoryRecords(buffer2));
snapshot.freeze();
}
try (FileRawSnapshotReader snapshot = FileRawSnapshotReader.open(tempDir, offsetAndEpoch)) {
int totalSize = Math.toIntExact(snapshot.sizeInBytes());
assertEquals(expectedBuffer.remaining(), totalSize);
UnalignedFileRecords record1 = (UnalignedFileRecords) snapshot.slice(0, totalSize / 2);
UnalignedFileRecords record2 = (UnalignedFileRecords) snapshot.slice(totalSize / 2, totalSize - totalSize / 2);
assertEquals(buffer1, TestUtils.toBuffer(record1));
assertEquals(buffer2, TestUtils.toBuffer(record2));
ByteBuffer readBuffer = ByteBuffer.allocate(record1.sizeInBytes() + record2.sizeInBytes());
readBuffer.put(TestUtils.toBuffer(record1));
readBuffer.put(TestUtils.toBuffer(record2));
readBuffer.flip();
assertEquals(expectedBuffer, readBuffer);
}
}
Aggregations