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 2019/06/18 14:55:49 UTC

[GitHub] [pulsar] sijie commented on a change in pull request #4548: [schema] key/value schema enhancement

sijie commented on a change in pull request #4548: [schema] key/value schema enhancement
URL: https://github.com/apache/pulsar/pull/4548#discussion_r294868822
 
 

 ##########
 File path: pulsar-client-api/src/main/java/org/apache/pulsar/common/schema/KeyValue.java
 ##########
 @@ -37,4 +41,85 @@ public K getKey() {
     public V getValue() {
         return value;
     }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(key, value);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!(obj instanceof KeyValue)) {
+            return false;
+        }
+        KeyValue<K, V> another = (KeyValue<K, V>) obj;
+        return Objects.equals(key, another.key)
+            && Objects.equals(value, another.value);
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append("(key = \"")
+          .append(key)
+          .append("\", value = \"")
+          .append(value)
+          .append("\")");
+        return sb.toString();
+    }
+
+    /**
+     * Decoder to decode key/value bytes.
+     */
+    @FunctionalInterface
+    public interface KeyValueDecoder<K, V> {
+
+        /**
+         * Decode key and value bytes into a {@link KeyValue} pair.
+         *
+         * @param keyData key data
+         * @param valueData value data
+         * @return the decoded {@link KeyValue} pair
+         */
+        KeyValue<K, V> decode(byte[] keyData, byte[] valueData);
+
+    }
+
+    /**
+     * Encode a <tt>key</tt> and <tt>value</tt> pair into a bytes array.
+     *
+     * @param key key object to encode
+     * @param keyWriter a writer to encode key object
+     * @param value value object to encode
+     * @param valueWriter a writer to encode value object
+     * @return the encoded bytes array
+     */
+    public static <K, V> byte[] encode(K key, SchemaWriter<K> keyWriter,
 
 Review comment:
   @codelipenghui since I only need a serialize method for serializing K or V into bytes. so I use `SchemaWriter` as the interface in the util functions. To use existing Schema interface, I extend Schema interface to `SchemaWriter`. Maybe I can just rewrite it using a different approach to get rid of extending Schema from SchemaWriter. Let me give it a shot.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services