BewareMyPower commented on code in PR #17125: URL: https://github.com/apache/pulsar/pull/17125#discussion_r963304311
########## pulsar-client-cpp/include/pulsar/KeyValue.h: ########## @@ -0,0 +1,75 @@ +/** + * 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. + */ +#ifndef KEY_VALUE_HPP_ +#define KEY_VALUE_HPP_ + +#include <map> +#include <string> +#include <memory> +#include "defines.h" +#include "Schema.h" + +namespace pulsar { + +class KeyValueImpl; + +/** + * Use to when the user uses key value schema. + */ +class PULSAR_PUBLIC KeyValue { + public: + KeyValue(); Review Comment: This constructor is redundant since `KeyValue` seems to be an immutable class (because it has no setters) ########## pulsar-client-cpp/examples/SampleKeyValueSchemaProducer.cc: ########## @@ -0,0 +1,60 @@ +/** + * 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. + */ +#include <iostream> +#include <thread> +#include <pulsar/Client.h> +#include <lib/LogUtils.h> + +DECLARE_LOG_OBJECT() + +using namespace pulsar; + +int main() { + Client client("pulsar://localhost:6650"); + + std::string jsonSchema = + R"({"type":"record","name":"cpx","fields":[{"name":"re","type":"double"},{"name":"im","type":"double"}]})"; + + SchemaInfo keySchema(JSON, "key-json", jsonSchema); + SchemaInfo valueSchema(JSON, "value-json", jsonSchema); + SchemaInfo keyValueSchema(keySchema, valueSchema, KeyValueEncodingType::INLINE); + std::cout << keyValueSchema.getSchema() << std::endl; Review Comment: Don't use `std::cout`, you can use `LOG_INFO` here. ########## pulsar-client-cpp/include/pulsar/KeyValue.h: ########## @@ -0,0 +1,75 @@ +/** + * 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. + */ +#ifndef KEY_VALUE_HPP_ +#define KEY_VALUE_HPP_ + +#include <map> +#include <string> +#include <memory> +#include "defines.h" +#include "Schema.h" + +namespace pulsar { + +class KeyValueImpl; + +/** + * Use to when the user uses key value schema. + */ +class PULSAR_PUBLIC KeyValue { + public: + KeyValue(); + /** + * Constructor key value, according to keyValueEncodingType, whether key and value be encoded together. + * + * @param key key data. + * @param value value data. + * @param keyValueEncodingType key value encoding type. + */ + KeyValue(const std::string &key, const std::string &value, + const KeyValueEncodingType &keyValueEncodingType); + /** + * Get the key of KeyValue. + * + * @return character stream for key + */ + std::string getKey() const; + /** + * Get the value of KeyValue. + * + * @return character stream for value + */ + std::string getValue() const; Review Comment: Return `const std::string&` instead of `std::string`. You might have referenced the API design of `Message::getDataAsString`, which returns `std::string` because the underlying type is `SharedBuffer`, not a `std::string`. The `getDataAsString` method always creates a `std::string` object as a returned value. Usually if users want to access the data without extra copying, they should use `getData` and `getLength` to access data by the pointer. ```c++ // Access the value without copying const char* ptr = static_cast<const char*>(msg.getData()); size_t length = msg.getLength(); for (size_t i = 0; i < length; i++) { std::cout << ptr[i]; } std::cout << std::endl; // Access the copied value std::string s = msg.getDataAsString(); for (size_t i = 0; i < s.size(); i++) { std::cout << s[i]; } std::cout << std::endl; ``` However, for a given `KeyValue` object, `getKey` and `getValue` just access the underlying `std::string` fields. ########## pulsar-client-cpp/lib/KeyValueImpl.cc: ########## @@ -0,0 +1,78 @@ +/** + * 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. + */ +#include <pulsar/Schema.h> +#include "SharedBuffer.h" +#include "KeyValueImpl.h" + +using namespace pulsar; + +namespace pulsar { + +KeyValueImpl::KeyValueImpl() {} + +KeyValueImpl::KeyValueImpl(const char *data, int length, const KeyValueEncodingType &keyValueEncodingType) { + keyValueEncodingType_ = keyValueEncodingType; + if (keyValueEncodingType == KeyValueEncodingType::INLINE) { + SharedBuffer buffer = SharedBuffer::wrap(const_cast<char *>(data), length); + int keySize = buffer.readUnsignedInt(); + if (keySize != -1) { + SharedBuffer keyContent = buffer.slice(0, keySize); + keyContent_ = std::string(keyContent.data(), keySize); + buffer.consume(keySize); + } + + int valueSize = buffer.readUnsignedInt(); + if (valueSize != -1) { + SharedBuffer valueContent = buffer.slice(0, valueSize); + valueContent_ = std::string(valueContent.data(), valueSize); Review Comment: It's better to avoid the copy in the internal implementation. We should save `valueContent` (and the `keyContent` in line 35) as the field of `KeyValueImpl`. See how `Message` does. You can also see the original PR for how to optimize the data copy: https://github.com/apache/pulsar/pull/13293 ########## pulsar-client-cpp/examples/SampleKeyValueSchemaProducer.cc: ########## @@ -0,0 +1,60 @@ +/** + * 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. + */ +#include <iostream> +#include <thread> +#include <pulsar/Client.h> +#include <lib/LogUtils.h> + +DECLARE_LOG_OBJECT() + +using namespace pulsar; + +int main() { + Client client("pulsar://localhost:6650"); + + std::string jsonSchema = + R"({"type":"record","name":"cpx","fields":[{"name":"re","type":"double"},{"name":"im","type":"double"}]})"; + + SchemaInfo keySchema(JSON, "key-json", jsonSchema); + SchemaInfo valueSchema(JSON, "value-json", jsonSchema); + SchemaInfo keyValueSchema(keySchema, valueSchema, KeyValueEncodingType::INLINE); + std::cout << keyValueSchema.getSchema() << std::endl; + + ProducerConfiguration producerConfiguration; + producerConfiguration.setSchema(keyValueSchema); + + Producer producer; + Result result = + client.createProducer("persistent://public/default/kv-schema", producerConfiguration, producer); + if (result != ResultOk) { + LOG_ERROR("Error creating producer: " << result); + return -1; + } + + std::string jsonData = "{\"re\":2.1,\"im\":1.23}"; + + KeyValue keyValue(jsonData, jsonData, KeyValueEncodingType::INLINE); + + Message msg = MessageBuilder().setContent(keyValue).setProperty("x", "1").build(); + producer.send(msg); + std::this_thread::sleep_for(std::chrono::seconds(1)); + LOG_INFO("send message ok"); Review Comment: `send` is a synchronous API, if you want to check if the send succeeded, you should write: ```c++ result = producer.send(msg); if (result == ResultOk) { /* ... */ } ``` -- 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]
