use of org.apache.pulsar.client.impl.BatchMessageIdImpl in project incubator-pulsar by apache.
the class MessageIdTest method messageIdTest.
@Test
public void messageIdTest() {
MessageId mId = new MessageIdImpl(1, 2, 3);
assertEquals(mId.toString(), "1:2:3");
mId = new BatchMessageIdImpl(0, 2, 3, 4);
assertEquals(mId.toString(), "0:2:3:4");
mId = new BatchMessageIdImpl(-1, 2, -3, 4);
assertEquals(mId.toString(), "-1:2:-3:4");
mId = new MessageIdImpl(0, -23, 3);
assertEquals(mId.toString(), "0:-23:3");
}
use of org.apache.pulsar.client.impl.BatchMessageIdImpl in project incubator-pulsar by apache.
the class BatchMessageIdImplTest method compareToTest.
@Test
public void compareToTest() {
BatchMessageIdImpl batchMsgId1 = new BatchMessageIdImpl(0, 0, 0, 0);
BatchMessageIdImpl batchMsgId2 = new BatchMessageIdImpl(1, 1, 1, 1);
Assert.assertEquals(batchMsgId1.compareTo(batchMsgId2), -1);
Assert.assertEquals(batchMsgId2.compareTo(batchMsgId1), 1);
Assert.assertEquals(batchMsgId2.compareTo(batchMsgId2), 0);
}
use of org.apache.pulsar.client.impl.BatchMessageIdImpl in project incubator-pulsar by apache.
the class BatchMessageIdImplSerializationTest method testSerialization1.
@Test
void testSerialization1() throws Exception {
BatchMessageIdImpl id = new BatchMessageIdImpl(1, 2, 3, 4);
byte[] serializedId = id.toByteArray();
assertEquals(BatchMessageIdImpl.fromByteArray(serializedId), id);
}
use of org.apache.pulsar.client.impl.BatchMessageIdImpl in project incubator-pulsar by apache.
the class BatchMessageIdImplSerializationTest method testSerialization2.
@Test
void testSerialization2() throws Exception {
BatchMessageIdImpl id = new BatchMessageIdImpl(1, 2, -1, 3);
byte[] serializedId = id.toByteArray();
assertEquals(BatchMessageIdImpl.fromByteArray(serializedId), id);
}
use of org.apache.pulsar.client.impl.BatchMessageIdImpl in project incubator-pulsar by apache.
the class RawBatchConverter method rebatchMessage.
/**
* Take a batched message and a filter, and returns a message with the only the submessages
* which match the filter. Returns an empty optional if no messages match.
*
* This takes ownership of the passes in message, and if the returned optional is not empty,
* the ownership of that message is returned also.
*/
public static Optional<RawMessage> rebatchMessage(RawMessage msg, BiPredicate<String, MessageId> filter) throws IOException {
checkArgument(msg.getMessageIdData().getBatchIndex() == -1);
ByteBuf payload = msg.getHeadersAndPayload();
MessageMetadata metadata = Commands.parseMessageMetadata(payload);
ByteBuf batchBuffer = PooledByteBufAllocator.DEFAULT.buffer(payload.capacity());
try {
int batchSize = metadata.getNumMessagesInBatch();
int messagesRetained = 0;
SingleMessageMetadata.Builder emptyMetadataBuilder = SingleMessageMetadata.newBuilder().setCompactedOut(true);
for (int i = 0; i < batchSize; i++) {
SingleMessageMetadata.Builder singleMessageMetadataBuilder = SingleMessageMetadata.newBuilder();
ByteBuf singleMessagePayload = Commands.deSerializeSingleMessageInBatch(payload, singleMessageMetadataBuilder, 0, batchSize);
String key = singleMessageMetadataBuilder.getPartitionKey();
MessageId id = new BatchMessageIdImpl(msg.getMessageIdData().getLedgerId(), msg.getMessageIdData().getEntryId(), msg.getMessageIdData().getPartition(), i);
if (filter.test(key, id)) {
messagesRetained++;
Commands.serializeSingleMessageInBatchWithPayload(singleMessageMetadataBuilder, singleMessagePayload, batchBuffer);
} else {
Commands.serializeSingleMessageInBatchWithPayload(emptyMetadataBuilder, Unpooled.EMPTY_BUFFER, batchBuffer);
}
singleMessageMetadataBuilder.recycle();
singleMessagePayload.release();
}
emptyMetadataBuilder.recycle();
if (messagesRetained > 0) {
ByteBuf metadataAndPayload = Commands.serializeMetadataAndPayload(Commands.ChecksumType.Crc32c, metadata, batchBuffer);
return Optional.of(new RawMessageImpl(msg.getMessageIdData(), metadataAndPayload));
} else {
return Optional.empty();
}
} finally {
batchBuffer.release();
metadata.recycle();
msg.close();
}
}
Aggregations