Search in sources :

Example 6 with ByteBufCodedOutputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project pulsar by yahoo.

the class ManagedLedgerTest method getMessageWithMetadata.

public ByteBuf getMessageWithMetadata(byte[] data) throws IOException {
    MessageMetadata messageData = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()).setProducerName("prod-name").setSequenceId(0).build();
    ByteBuf payload = Unpooled.wrappedBuffer(data, 0, data.length);
    int msgMetadataSize = messageData.getSerializedSize();
    int headersSize = 4 + msgMetadataSize;
    ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize, headersSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
    headers.writeInt(msgMetadataSize);
    messageData.writeTo(outStream);
    outStream.recycle();
    return DoubleByteBuf.get(headers, payload);
}
Also used : MessageMetadata(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata) DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Example 7 with ByteBufCodedOutputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project pulsar by yahoo.

the class Commands method serializeCommandMessageWithSize.

private static ByteBuf serializeCommandMessageWithSize(BaseCommand cmd, ByteBuf metadataAndPayload) {
    // / Wire format
    // [TOTAL_SIZE] [CMD_SIZE][CMD] [METADATA_SIZE][METADATA] [PAYLOAD]
    int cmdSize = cmd.getSerializedSize();
    int totalSize = 4 + cmdSize + metadataAndPayload.readableBytes();
    int headersSize = 4 + 4 + cmdSize;
    ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize);
    // External frame
    headers.writeInt(totalSize);
    try {
        // Write cmd
        headers.writeInt(cmdSize);
        ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
        cmd.writeTo(outStream);
        outStream.recycle();
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }
    return DoubleByteBuf.get(headers, metadataAndPayload);
}
Also used : IOException(java.io.IOException) UnpooledHeapByteBuf(io.netty.buffer.UnpooledHeapByteBuf) RecyclableDuplicateByteBuf(io.netty.buffer.RecyclableDuplicateByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Example 8 with ByteBufCodedOutputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream in project pulsar by yahoo.

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();
    ByteBuf metaPayloadBuf = DoubleByteBuf.get(metaPayloadFrame, payload);
    int computedChecksum = Crc32cChecksum.computeChecksum(metaPayloadBuf);
    outStream.recycle();
    metaPayloadBuf.release();
    return computedChecksum;
}
Also used : DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Aggregations

ByteBufCodedOutputStream (com.yahoo.pulsar.common.util.protobuf.ByteBufCodedOutputStream)8 ByteBuf (io.netty.buffer.ByteBuf)7 IOException (java.io.IOException)5 DoubleByteBuf (com.yahoo.pulsar.common.api.DoubleByteBuf)3 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)3 UnpooledHeapByteBuf (io.netty.buffer.UnpooledHeapByteBuf)3 PulsarApi (com.yahoo.pulsar.common.api.proto.PulsarApi)2 BaseCommand (com.yahoo.pulsar.common.api.proto.PulsarApi.BaseCommand)2 MessageIdData (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData)1 MessageMetadata (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata)1