Search in sources :

Example 6 with ByteBufCodedOutputStream

use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.

the class CommandsTest method computeChecksum.

private int computeChecksum(MessageMetadata msgMetadata, ByteBuf compressedPayload) throws IOException {
    int metadataSize = msgMetadata.getSerializedSize();
    int metadataFrameSize = 4 + metadataSize;
    ByteBuf metaPayloadFrame = PooledByteBufAllocator.DEFAULT.buffer(metadataFrameSize, metadataFrameSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(metaPayloadFrame);
    metaPayloadFrame.writeInt(metadataSize);
    msgMetadata.writeTo(outStream);
    ByteBuf payload = compressedPayload.copy();
    ByteBufPair metaPayloadBuf = ByteBufPair.get(metaPayloadFrame, payload);
    int computedChecksum = Crc32cIntChecksum.computeChecksum(ByteBufPair.coalesce(metaPayloadBuf));
    outStream.recycle();
    metaPayloadBuf.release();
    return computedChecksum;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair)

Example 7 with ByteBufCodedOutputStream

use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.

the class PersistentMessageFinderTest method createMessageWrittenToLedger.

public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
    PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
    messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
    messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
    messageMetadataBuilder.setSequenceId(1);
    PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
    ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());
    int msgMetadataSize = messageMetadata.getSerializedSize();
    int payloadSize = data.readableBytes();
    int totalSize = 4 + msgMetadataSize + payloadSize;
    ByteBuf headers = PooledByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
    headers.writeInt(msgMetadataSize);
    messageMetadata.writeTo(outStream);
    ByteBuf headersAndPayload = ByteBufPair.coalesce(ByteBufPair.get(headers, data));
    byte[] byteMessage = headersAndPayload.nioBuffer().array();
    headersAndPayload.release();
    return byteMessage;
}
Also used : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Example 8 with ByteBufCodedOutputStream

use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.

the class ByteBufCodedInputStreamTest method testWritingDouble.

@Test
public void testWritingDouble() throws IOException {
    ByteBuf buf = Unpooled.buffer();
    buf.clear();
    ByteBufCodedOutputStream outputStream = ByteBufCodedOutputStream.get(buf);
    outputStream.writeDouble(12, 23d);
    outputStream.writeDouble(15, 13.13d);
    outputStream.writeDouble(1, -0.003d);
    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(buf);
    assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 12);
    assertEquals(inputStream.readDouble(), 23d);
    assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 15);
    assertEquals(inputStream.readDouble(), 13.13d);
    assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 1);
    assertEquals(inputStream.readDouble(), -0.003d);
}
Also used : ByteBufCodedInputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream) Test(org.testng.annotations.Test)

Example 9 with ByteBufCodedOutputStream

use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.

the class MessageIdImpl method toByteArray.

// batchIndex is -1 if message is non-batched message and has the batchIndex for a batch message
protected byte[] toByteArray(int batchIndex) {
    MessageIdData.Builder builder = MessageIdData.newBuilder();
    builder.setLedgerId(ledgerId);
    builder.setEntryId(entryId);
    if (partitionIndex >= 0) {
        builder.setPartition(partitionIndex);
    }
    if (batchIndex != -1) {
        builder.setBatchIndex(batchIndex);
    }
    MessageIdData msgId = builder.build();
    int size = msgId.getSerializedSize();
    ByteBuf serialized = Unpooled.buffer(size, size);
    ByteBufCodedOutputStream stream = ByteBufCodedOutputStream.get(serialized);
    try {
        msgId.writeTo(stream);
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }
    msgId.recycle();
    builder.recycle();
    stream.recycle();
    return serialized.array();
}
Also used : MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Example 10 with ByteBufCodedOutputStream

use of org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project incubator-pulsar by apache.

the class Commands method serializeSingleMessageInBatchWithPayload.

public static ByteBuf serializeSingleMessageInBatchWithPayload(PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, ByteBuf payload, ByteBuf batchBuffer) {
    int payLoadSize = payload.readableBytes();
    PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.setPayloadSize(payLoadSize).build();
    // serialize meta-data size, meta-data and payload for single message in batch
    int singleMsgMetadataSize = singleMessageMetadata.getSerializedSize();
    try {
        batchBuffer.writeInt(singleMsgMetadataSize);
        ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(batchBuffer);
        singleMessageMetadata.writeTo(outStream);
        singleMessageMetadata.recycle();
        outStream.recycle();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return batchBuffer.writeBytes(payload);
}
Also used : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) IOException(java.io.IOException) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Aggregations

ByteBufCodedOutputStream (org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)11 ByteBuf (io.netty.buffer.ByteBuf)10 IOException (java.io.IOException)7 PulsarApi (org.apache.pulsar.common.api.proto.PulsarApi)2 BaseCommand (org.apache.pulsar.common.api.proto.PulsarApi.BaseCommand)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteBufPair (org.apache.pulsar.common.api.ByteBufPair)1 MessageIdData (org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData)1 MessageMetadata (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata)1 ByteBufCodedInputStream (org.apache.pulsar.common.util.protobuf.ByteBufCodedInputStream)1 Test (org.testng.annotations.Test)1