BewareMyPower commented on code in PR #17125: URL: https://github.com/apache/pulsar/pull/17125#discussion_r975983873
########## pulsar-client-cpp/lib/KeyValueImpl.cc: ########## @@ -0,0 +1,83 @@ +/** + * 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(const char *data, int length, KeyValueEncodingType keyValueEncodingType) { + if (keyValueEncodingType == KeyValueEncodingType::INLINE) { + SharedBuffer buffer = SharedBuffer::wrap(const_cast<char *>(data), length); + int keySize = buffer.readUnsignedInt(); Review Comment: It actually returns a `uint32_t`, not `int`, though the conversion between `uint32_t` and `int` is safe. Use `auto` or specify the correct type to avoid implicit type conversion. Using a magic number `-1` might harm the code quality. You can define a constant like: ```c++ constexpr uint32_t INVALID_SIZE = 0xFFFFFFFF; ``` Then check key size and value size like: ```c++ auto keySize = buffer.readUnsignedInt(); if (keySize != INVALID_SIZE) { /* ... */ } ``` ########## pulsar-client-cpp/lib/KeyValueImpl.cc: ########## @@ -0,0 +1,83 @@ +/** + * 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(const char *data, int length, 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); + key_ = std::string(keyContent.data(), keySize); + buffer.consume(keySize); + } + + int valueSize = buffer.readUnsignedInt(); + if (valueSize != -1) { + valueBuffer_ = buffer.slice(0, valueSize); + } + } else { + valueBuffer_ = SharedBuffer::wrap(const_cast<char *>(data), length); + } +} + +KeyValueImpl::KeyValueImpl(std::string &&key, std::string &&value) + : key_(key), valueBuffer_(SharedBuffer::take(std::move(value))) {} Review Comment: ```suggestion : key_(std::move(key)), valueBuffer_(SharedBuffer::take(std::move(value))) {} ``` ########## pulsar-client-cpp/lib/KeyValueImpl.cc: ########## @@ -0,0 +1,83 @@ +/** + * 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(const char *data, int length, 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); + key_ = std::string(keyContent.data(), keySize); + buffer.consume(keySize); + } + + int valueSize = buffer.readUnsignedInt(); + if (valueSize != -1) { + valueBuffer_ = buffer.slice(0, valueSize); + } + } else { + valueBuffer_ = SharedBuffer::wrap(const_cast<char *>(data), length); + } +} + +KeyValueImpl::KeyValueImpl(std::string &&key, std::string &&value) + : key_(key), valueBuffer_(SharedBuffer::take(std::move(value))) {} + +SharedBuffer KeyValueImpl::getContent(KeyValueEncodingType keyValueEncodingType) { + if (contentBufferCache_.is_present()) { + return contentBufferCache_.value(); + } Review Comment: The cache is not thread safe, but `getContent` could be called in different threads as far as I see. ``` ProducerImpl::sendAsync ProducerImpl::sendAsyncWithStatsUpdate MessageImpl::convertKeyValueToPayload KeyValueImpl::getContent ``` But `sendAsync` should be thread safe. -- 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]
