sijie commented on a change in pull request #4211: 
[issue#4155][pulsar-clients]Support key value schema versioning
URL: https://github.com/apache/pulsar/pull/4211#discussion_r281460595
 
 

 ##########
 File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/KeyValueSchema.java
 ##########
 @@ -154,14 +221,51 @@ private KeyValueSchema(Schema<K> keySchema,
         byte[] valueBytes = new byte[valueLength];
         byteBuffer.get(valueBytes);
 
-        return decode(keyBytes, valueBytes);
+        return decode(keyBytes, valueBytes, schemaVersion);
     }
 
-    public KeyValue<K, V> decode(byte[] keyBytes, byte[] valueBytes) {
-        return new KeyValue<>(keySchema.decode(keyBytes), 
valueSchema.decode(valueBytes));
+    public KeyValue<K, V> decode(byte[] keyBytes, byte[] valueBytes, byte[] 
schemaVersion) {
+        K k;
+        if (keySchema.supportSchemaVersioning() && schemaVersion != null) {
+            k = keySchema.decode(keyBytes, schemaVersion);
+        } else {
+            k = keySchema.decode(keyBytes);
+        }
+        V v;
+        if (valueSchema.supportSchemaVersioning() && schemaVersion != null) {
+            v = valueSchema.decode(valueBytes, schemaVersion);
+        } else {
+            v = valueSchema.decode(valueBytes);
+        }
+        return new KeyValue<>(k, v);
     }
 
     public SchemaInfo getSchemaInfo() {
         return this.schemaInfo;
     }
+
+    public void setSchemaInfoProvider(SchemaInfoProvider schemaInfoProvider) {
+        this.schemaInfoProvider = schemaInfoProvider;
+    }
+
+    private static KeyValue<SchemaInfo, SchemaInfo> 
decodeKeyValueSchemaInfo(SchemaInfo schemaInfo) {
+        ByteBuffer byteBuffer = ByteBuffer.wrap(schemaInfo.getSchema());
+        int keySchemaLength = byteBuffer.getInt();
+        byte[] key = new byte[keySchemaLength];
+        byteBuffer.get(key);
+        int valueSchemaLength = byteBuffer.getInt();
+        byte[] value = new byte[valueSchemaLength];
+        byteBuffer.get(value);
+        Gson keySchemaGson = new Gson();
+        SchemaInfo keySchemaInfo = SchemaInfo.builder().schema(key)
+                
.properties(keySchemaGson.fromJson(schemaInfo.getProperties().get("key.schema.properties"),
 Map.class))
 
 Review comment:
   fromJson can return null. so I would suggest doing null check and use 
`Collections.emptyMap` if fromJson returns null.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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