You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/01/09 01:42:22 UTC

[GitHub] sijie commented on a change in pull request #1039: Serialization and Deserialization for RawMessage

sijie commented on a change in pull request #1039: Serialization and Deserialization for RawMessage
URL: https://github.com/apache/incubator-pulsar/pull/1039#discussion_r160301522
 
 

 ##########
 File path: pulsar-broker/src/main/java/org/apache/pulsar/client/impl/RawMessageImpl.java
 ##########
 @@ -53,4 +60,47 @@ public ByteBuf getHeadersAndPayload() {
     public void close() {
         headersAndPayload.release();
     }
+
+    @Override
+    public ByteBuf serialize() {
+        // Format: [IdSize][Id][PayloadAndMetadataSize][PayloadAndMetadata]
+        int idSize = id.getSerializedSize();
+        int headerSize = 4 /* IdSize */ + idSize + 4 /* PayloadAndMetadataSize */;
+
+        ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headerSize);
+        headers.writeInt(idSize);
+        try {
+            ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
+            id.writeTo(outStream);
+            outStream.recycle();
+        } catch (IOException e) {
+            // This is in-memory serialization, should not fail
+            throw new RuntimeException(e);
+        }
+        headers.writeInt(headersAndPayload.readableBytes());
+
+        return DoubleByteBuf.get(headers, headersAndPayload);
+    }
+
+    static public RawMessage deserializeFrom(ByteBuf buffer) {
+        try {
+            int idSize = buffer.readInt();
+
+            int writerIndex = buffer.writerIndex();
+            buffer.writerIndex(buffer.readerIndex() + idSize);
+            ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer);
+            MessageIdData.Builder builder = MessageIdData.newBuilder();
+            MessageIdData id = builder.mergeFrom(stream, null).build();
+            buffer.writerIndex(writerIndex);
+            builder.recycle();
+
+            int payloadAndMetadataSize = buffer.readInt();
+            ByteBuf metadataAndPayload = buffer.slice(buffer.readerIndex(), payloadAndMetadataSize);
+
+            return new RawMessageImpl(id, metadataAndPayload);
+        } catch (IOException e) {
+            // This is in-memory deserialization, should not fail
 
 Review comment:
   nit: log a message here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services