Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ public boolean equals(Object obj) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(key = \"")
.append(key)
.append("\", value = \"")
.append(value)
.append("\")");
sb.append("(");
if (null == key) {
sb.append("key is null, ");
} else {
sb.append("key = \"").append(key).append("\", ");
}

if (null == value) {
sb.append("value is null");
} else {
sb.append("value = \"").append(value).append("\"");
}
sb.append(")");
return sb.toString();
}

Expand Down Expand Up @@ -98,8 +106,23 @@ public static <K, V> byte[] encode(K key, Schema<K> keyWriter,
V value, Schema<V> valueWriter) {
byte [] keyBytes = keyWriter.encode(key);
byte [] valueBytes = valueWriter.encode(value);
ByteBuffer byteBuffer = ByteBuffer.allocate(4 + keyBytes.length + 4 + valueBytes.length);
byteBuffer.putInt(keyBytes.length).put(keyBytes).putInt(valueBytes.length).put(valueBytes);

int keyEncodeLength = 1 + (null == keyBytes ? 0 : 4 + keyBytes.length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it is breaking the backward compatibility. Because it introduces a new format of how key/value is stored. Can we introduce versioning to the format and use message properties to indicate which version of the forrmat that is used for serializing key/value pairs?

int valueEncodeLength = 1 + (null == valueBytes ? 0 : 4 + valueBytes.length);
ByteBuffer byteBuffer = ByteBuffer.allocate(keyEncodeLength + valueEncodeLength);

if (null == keyBytes) {
byteBuffer.put((byte) 0);
} else {
byteBuffer.put((byte) 1).putInt(keyBytes.length).put(keyBytes);
}

if (null == valueBytes) {
byteBuffer.put((byte) 0);
} else {
byteBuffer.put((byte) 1).putInt(valueBytes.length).put(valueBytes);
}

return byteBuffer.array();
}

Expand All @@ -112,14 +135,23 @@ public static <K, V> byte[] encode(K key, Schema<K> keyWriter,
*/
public static <K, V> KeyValue<K, V> decode(byte[] data, KeyValueDecoder<K, V> decoder) {
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
int keyLength = byteBuffer.getInt();
byte[] keyBytes = new byte[keyLength];
byteBuffer.get(keyBytes);

int valueLength = byteBuffer.getInt();
byte[] valueBytes = new byte[valueLength];
byteBuffer.get(valueBytes);
byte[] keyBytes = null;
byte isKeySet = byteBuffer.get();
if (isKeySet == (byte) 1) {
int keyLength = byteBuffer.getInt();
keyBytes = new byte[keyLength];
byteBuffer.get(keyBytes);
}

byte[] valueBytes = null;
byte isValueSet = byteBuffer.get();
if (isValueSet == (byte) 1) {
int valueLength = byteBuffer.getInt();
valueBytes = new byte[valueLength];
byteBuffer.get(valueBytes);
}

return decoder.decode(keyBytes, valueBytes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ public TypedMessageBuilder<T> value(T value) {
org.apache.pulsar.common.schema.KeyValue kv = (org.apache.pulsar.common.schema.KeyValue) value;
if (kvSchema.getKeyValueEncodingType() == KeyValueEncodingType.SEPARATED) {
// set key as the message key
msgMetadataBuilder.setPartitionKey(
Base64.getEncoder().encodeToString(kvSchema.getKeySchema().encode(kv.getKey())));
msgMetadataBuilder.setPartitionKeyB64Encoded(true);
if (kv.getKey() != null) {
msgMetadataBuilder.setPartitionKey(
Base64.getEncoder().encodeToString(kvSchema.getKeySchema().encode(kv.getKey())));
msgMetadataBuilder.setPartitionKeyB64Encoded(true);
}
// set value as the payload
this.content = ByteBuffer.wrap(kvSchema.getValueSchema().encode(kv.getValue()));
return this;
Expand Down