Search in sources :

Example 1 with BatchMessageIdImpl

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");
}
Also used : MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) Test(org.testng.annotations.Test)

Example 2 with BatchMessageIdImpl

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);
}
Also used : BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) Test(org.testng.annotations.Test)

Example 3 with BatchMessageIdImpl

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);
}
Also used : BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) Test(org.testng.annotations.Test)

Example 4 with BatchMessageIdImpl

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);
}
Also used : BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) Test(org.testng.annotations.Test)

Example 5 with BatchMessageIdImpl

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();
    }
}
Also used : SingleMessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.SingleMessageMetadata) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) SingleMessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.SingleMessageMetadata) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) ByteBuf(io.netty.buffer.ByteBuf) MessageId(org.apache.pulsar.client.api.MessageId)

Aggregations

BatchMessageIdImpl (org.apache.pulsar.client.impl.BatchMessageIdImpl)9 Test (org.testng.annotations.Test)4 ByteBuf (io.netty.buffer.ByteBuf)3 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)3 MessageMetadata (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 Position (org.apache.bookkeeper.mledger.Position)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)2 InitialPosition (org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 GeneratedMessageLite (com.google.protobuf.GeneratedMessageLite)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelOption (io.netty.channel.ChannelOption)1 SslHandler (io.netty.handler.ssl.SslHandler)1 SocketAddress (java.net.SocketAddress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1