You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "PragmaTwice (via GitHub)" <gi...@apache.org> on 2023/08/03 12:56:19 UTC

[GitHub] [kvrocks] PragmaTwice commented on a diff in pull request #1631: Fix MSETNX not allow overriding the same key

PragmaTwice commented on code in PR #1631:
URL: https://github.com/apache/kvrocks/pull/1631#discussion_r1283164144


##########
src/types/redis_string.cc:
##########
@@ -384,7 +384,10 @@ rocksdb::Status String::MSet(const std::vector<StringPair> &pairs, uint64_t ttl)
     batch->PutLogData(log_data.Encode());
     AppendNamespacePrefix(pair.key, &ns_key);
     batch->Put(metadata_cf_handle_, ns_key, bytes);
-    LockGuard guard(storage_->GetLockManager(), ns_key);
+    std::optional<LockGuard> guard;
+    if (lock) {
+      guard(storage_->GetLockManager(), ns_key);
+    }

Review Comment:
   TBH I cannot understand the C++ code at line 389. You can try something like:
   ```c++
   if (lock) storage_->GetLockManager().Lock(...);
   ...
   if (lock) storage_->GetLockManager().Unlock(...);
   ```
   
   Or, if you want a more decent way:
   ```c++
   std::optional<LockGuard> guard;
   if (lock) {
     guard.emplace(storage_->GetLockManager(), ns_key);
   }
   ```
   
   Since *move ctor* and *move assignment* for `LockGuard` is missing, the assignment `guard = LockGuard(storage_->GetLockManager(), ns_key)` after the ctor call of `std::optional<LockGuard>` may not possible.
   
   Fore more details, I think you can check [*C++ Primer*](https://www.amazon.co.jp/-/en/Stanley-Lippman/dp/0321714113).



-- 
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: issues-unsubscribe@kvrocks.apache.org

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