BewareMyPower commented on code in PR #17125:
URL: https://github.com/apache/pulsar/pull/17125#discussion_r976084933
##########
pulsar-client-cpp/lib/Schema.cc:
##########
@@ -90,6 +131,43 @@ SchemaInfo::SchemaInfo(SchemaType schemaType, const
std::string &name, const std
const StringMap &properties)
: impl_(std::make_shared<SchemaInfoImpl>(schemaType, name, schema,
properties)) {}
+SchemaInfo::SchemaInfo(const SchemaInfo &keySchema, const SchemaInfo
&valueSchema,
+ const KeyValueEncodingType &keyValueEncodingType) {
+ std::string keySchemaStr = keySchema.getSchema();
+ std::string valueSchemaStr = valueSchema.getSchema();
+ int keySize = keySchemaStr.size();
+ int valueSize = valueSchemaStr.size();
+
+ int buffSize = sizeof keySize + keySize + sizeof valueSize + valueSize;
+ SharedBuffer buffer = SharedBuffer::allocate(buffSize);
+ buffer.writeUnsignedInt(keySize == 0 ? -1 : keySize);
+ buffer.write(keySchemaStr.c_str(), keySize);
+ buffer.writeUnsignedInt(valueSize == 0 ? -1 : valueSize);
+ buffer.write(valueSchemaStr.c_str(), valueSize);
+
+ auto writeJson = [](const StringMap &properties) {
+ ptree pt;
+ for (auto &entry : properties) {
+ pt.put(entry.first, entry.second);
+ }
+ std::ostringstream buf;
+ write_json(buf, pt, false);
+ return buf.str();
+ };
+
+ StringMap properties;
+ properties.emplace(KEY_SCHEMA_NAME, keySchema.getName());
+ properties.emplace(KEY_SCHEMA_TYPE,
strSchemaType(keySchema.getSchemaType()));
+ properties.emplace(KEY_SCHEMA_PROPS, writeJson(keySchema.getProperties()));
+ properties.emplace(VALUE_SCHEMA_NAME, valueSchema.getName());
+ properties.emplace(VALUE_SCHEMA_TYPE,
strSchemaType(valueSchema.getSchemaType()));
+ properties.emplace(VALUE_SCHEMA_PROPS,
writeJson(valueSchema.getProperties()));
+ properties.emplace(KV_ENCODING_TYPE,
strEncodingType(keyValueEncodingType));
Review Comment:
Just compared the properties with what Java client generates. I found
`boost::property_tree::write_json` writes a newline at the end. Here are the
`properties` when I create a KeyValue schema by `SchemaInfo
schema(SchemaInfo(SchemaType::STRING, "String", ""),
SchemaInfo(SchemaType::STRING, "String", ""))`.
```
[0] = (first = "key.schema.name", second = "String")
[1] = (first = "key.schema.properties", second = "{}\n")
[2] = (first = "key.schema.type", second = "STRING")
[3] = (first = "kv.encoding.type", second = "INLINE")
[4] = (first = "value.schema.name", second = "String")
[5] = (first = "value.schema.properties", second = "{}\n")
[6] = (first = "value.schema.type", second = "STRING")
```
Here are the properties of a Java creation `Schema.KeyValue(Schema.STRING,
Schema.STRING)`.
<img width="347" alt="image"
src="https://user-images.githubusercontent.com/18204803/191430529-76671bca-6fd6-4807-b0ce-a1bc401f80d2.png">
(BTW, I think the `key.schema.type` and `value.schema.name` of Java client
are wrong)
The minor inconsistency with Java client might not affect anything, but IMO
it's better to remove the newline at the end.
```c++
// in writeJson
auto s = buf.str();
s.pop_back();
return s;
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]