You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "BewareMyPower (via GitHub)" <gi...@apache.org> on 2025/05/02 09:22:09 UTC

[PR] Fix TableView's existing key-value will never be updated [pulsar-client-cpp]

BewareMyPower opened a new pull request, #487:
URL: https://github.com/apache/pulsar-client-cpp/pull/487

   Fixes #486
   
   ### Motivation
   
   The `emplace` method of `SynchronizedHashMap` adopts the same semantics as https://en.cppreference.com/w/cpp/container/map/emplace, which inserts the key-value only when the key does not exist. `TableViewImpl` leverages this method to update the key-values, so it will never succeed. In addition, this method is not thread safe because if the key exists, an iterator will be returned to the caller, then the caller could use the iterator to modify the value without any synchronization.
   
   ### Modifications
   
   Add two new methods `create` and `update` and remove the `emplace` method.
   - `create`: inserts the key-value only when the key does not exist and return the optional existing value
   - `update`: inserts the key-value no matter if the key exists.
   
   Then, replace the `emplace` call with `update` in `TableViewImpl`.
   
   ### Verifying this change
   
   `testSimpleTableView` is improved to verify the update case.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TableView's existing key-value will never be updated [pulsar-client-cpp]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #487:
URL: https://github.com/apache/pulsar-client-cpp/pull/487#discussion_r2071898299


##########
lib/ClientImpl.cc:
##########
@@ -222,12 +222,12 @@ void ClientImpl::handleCreateProducer(const Result result, const LookupDataResul
 void ClientImpl::handleProducerCreated(Result result, ProducerImplBaseWeakPtr producerBaseWeakPtr,
                                        CreateProducerCallback callback, ProducerImplBasePtr producer) {
     if (result == ResultOk) {
-        auto pair = producers_.emplace(producer.get(), producer);
-        if (!pair.second) {
-            auto existingProducer = pair.first->second.lock();
+        auto address = producer.get();
+        auto existingProducer = producers_.create(address, producer);

Review Comment:
   `create()` is a bit confusing here as a name. I was wondering why do we need to "create a new consumer here"... 
   
   Could we rename the method as `putIfAbsent()`?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TableView's existing key-value will never be updated [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower merged PR #487:
URL: https://github.com/apache/pulsar-client-cpp/pull/487


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TableView's existing key-value will never be updated [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #487:
URL: https://github.com/apache/pulsar-client-cpp/pull/487#discussion_r2072533605


##########
lib/ClientImpl.cc:
##########
@@ -222,12 +222,12 @@ void ClientImpl::handleCreateProducer(const Result result, const LookupDataResul
 void ClientImpl::handleProducerCreated(Result result, ProducerImplBaseWeakPtr producerBaseWeakPtr,
                                        CreateProducerCallback callback, ProducerImplBasePtr producer) {
     if (result == ResultOk) {
-        auto pair = producers_.emplace(producer.get(), producer);
-        if (!pair.second) {
-            auto existingProducer = pair.first->second.lock();
+        auto address = producer.get();
+        auto existingProducer = producers_.create(address, producer);

Review Comment:
   Yes, it makes sense.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix TableView's existing key-value will never be updated [pulsar-client-cpp]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #487:
URL: https://github.com/apache/pulsar-client-cpp/pull/487#discussion_r2072536762


##########
lib/ClientImpl.cc:
##########
@@ -222,12 +222,12 @@ void ClientImpl::handleCreateProducer(const Result result, const LookupDataResul
 void ClientImpl::handleProducerCreated(Result result, ProducerImplBaseWeakPtr producerBaseWeakPtr,
                                        CreateProducerCallback callback, ProducerImplBasePtr producer) {
     if (result == ResultOk) {
-        auto pair = producers_.emplace(producer.get(), producer);
-        if (!pair.second) {
-            auto existingProducer = pair.first->second.lock();
+        auto address = producer.get();
+        auto existingProducer = producers_.create(address, producer);

Review Comment:
   I renamed these methods as `put` and `putIfAbsent` now.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org