sijie commented on a change in pull request #1908:  adding protobuf schema
URL: https://github.com/apache/incubator-pulsar/pull/1908#discussion_r192982595
 
 

 ##########
 File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/ProtobufSchema.java
 ##########
 @@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.client.impl.schema;
+
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SchemaSerializationException;
+import org.apache.pulsar.common.schema.SchemaInfo;
+import org.apache.pulsar.common.schema.SchemaType;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.Map;
+
+public class ProtobufSchema<T extends com.google.protobuf.GeneratedMessageV3> 
implements Schema<T> {
+
+
+    private Method fromBytes;
+    private SchemaInfo schemaInfo;
+    private ProtobufSchema(SchemaInfo schemaInfo, Class<T> pojo) {
+        this.schemaInfo = schemaInfo;
+        try {
+            this.fromBytes = pojo.getMethod("parseFrom", byte[].class);
+        } catch (NoSuchMethodException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public byte[] encode(T message) {
+        return message.toByteArray();
+    }
+
+    @Override
+    public T decode(byte[] bytes) {
+        try {
+            return (T) this.fromBytes.invoke(null, bytes);
 
 Review comment:
   I would take a different approach here. because reflection still has 
performance penalty.
   
   in proto3, generated class has getDefaultInstance. you can use 
getDefaultInstance to retrieve a default instance, and use that instance to get 
a `Parser<T>` object. Then you can use that Parser<T> object to parse bytes.
   
   get the parser in the constructor:
   ```
         try {
           T protoMessageInstance = (T) 
pojo.getMethod("getDefaultInstance").invoke(null);
           Parser<T> tParser = (Parser<T>) 
protoMessageInstance.getParserForType();
         } catch (IllegalAccessException | InvocationTargetException | 
NoSuchMethodException e) {
           throw new IllegalArgumentException(e);
         }
   ```
   
   ```
   parser. parseFrom(..)
   ```

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to