git-hulk commented on code in PR #3317:
URL: https://github.com/apache/kvrocks/pull/3317#discussion_r2656868575


##########
src/types/redis_string.cc:
##########
@@ -182,6 +183,52 @@ rocksdb::Status String::GetEx(engine::Context &ctx, const 
std::string &user_key,
   return rocksdb::Status::OK();
 }
 
+rocksdb::Status String::DelEX(engine::Context &ctx, const std::string 
&user_key, const DelExOption &option,
+                              bool &deleted) {
+  deleted = false;
+  std::string ns_key = AppendNamespacePrefix(user_key);
+
+  if (option.type == DelExOption::NONE) {
+    rocksdb::Status s = KeyExist(ctx, user_key);
+    if (s.IsNotFound()) return s;
+    deleted = true;
+    return storage_->Delete(ctx, storage_->DefaultWriteOptions(), 
metadata_cf_handle_, ns_key);
+  }
+
+  std::string val;
+  rocksdb::Status s = getValue(ctx, ns_key, &val);
+  if (!s.ok()) return s;
+
+  switch (option.type) {
+    case DelExOption::IFDEQ:
+      if (option.value == util::StringDigest(val)) {
+        deleted = true;
+      }
+      break;
+    case DelExOption::IFDNE:
+      if (option.value != util::StringDigest(val)) {
+        deleted = true;
+      }
+      break;
+    case DelExOption::IFEQ:
+      if (option.value == val) {
+        deleted = true;
+      }
+      break;
+    case DelExOption::IFNE:
+      if (option.value != val) {
+        deleted = true;
+      }
+      break;
+    default:
+      return rocksdb::Status::InvalidArgument();
+  }
+  if (deleted) {
+    return storage_->Delete(ctx, storage_->DefaultWriteOptions(), 
metadata_cf_handle_, ns_key);
+  }
+  return rocksdb::Status::OK();

Review Comment:
   This can prevent returning the wrong deleted value in case of the rocksdb 
error.
   
   ```suggestion
     deleted = false;
   
     std::string ns_key = AppendNamespacePrefix(user_key);
     std::string val;
     rocksdb::Status s = getValue(ctx, ns_key, &val);
     if (!s.ok()) return s;
   
     bool matched = false;
     switch (option.type) {
       case DelExOption::NONE:
         matched = true;
       break;
       case DelExOption::IFDEQ:
       matched = option.value == util::StringDigest(val);
       break;
       case DelExOption::IFDNE:
       matched = option.value != util::StringDigest(val);
       break;
       case DelExOption::IFEQ:
       matched = option.value == val
       break;
       case DelExOption::IFNE:
       matched = option.value != val
       break;
       default:
       return rocksdb::Status::InvalidArgument();
     }
     if (matched) {
       s = storage_->Delete(ctx, storage_->DefaultWriteOptions(), 
metadata_cf_handle_, ns_key);
       deleted = s.ok();
       return s;
     }
     return rocksdb::Status::OK();
   ```



##########
src/types/redis_string.cc:
##########
@@ -182,6 +183,52 @@ rocksdb::Status String::GetEx(engine::Context &ctx, const 
std::string &user_key,
   return rocksdb::Status::OK();
 }
 
+rocksdb::Status String::DelEX(engine::Context &ctx, const std::string 
&user_key, const DelExOption &option,
+                              bool &deleted) {
+  deleted = false;
+  std::string ns_key = AppendNamespacePrefix(user_key);
+
+  if (option.type == DelExOption::NONE) {
+    rocksdb::Status s = KeyExist(ctx, user_key);
+    if (s.IsNotFound()) return s;
+    deleted = true;
+    return storage_->Delete(ctx, storage_->DefaultWriteOptions(), 
metadata_cf_handle_, ns_key);
+  }
+
+  std::string val;
+  rocksdb::Status s = getValue(ctx, ns_key, &val);
+  if (!s.ok()) return s;
+
+  switch (option.type) {
+    case DelExOption::IFDEQ:
+      if (option.value == util::StringDigest(val)) {
+        deleted = true;
+      }
+      break;
+    case DelExOption::IFDNE:
+      if (option.value != util::StringDigest(val)) {
+        deleted = true;
+      }
+      break;
+    case DelExOption::IFEQ:
+      if (option.value == val) {
+        deleted = true;
+      }
+      break;
+    case DelExOption::IFNE:
+      if (option.value != val) {
+        deleted = true;
+      }
+      break;
+    default:
+      return rocksdb::Status::InvalidArgument();
+  }
+  if (deleted) {
+    return storage_->Delete(ctx, storage_->DefaultWriteOptions(), 
metadata_cf_handle_, ns_key);
+  }
+  return rocksdb::Status::OK();

Review Comment:
   This can prevent returning the wrong deleted value in case of the rocksdb 
error.
   
   ```suggestion
     deleted = false;
   
     std::string ns_key = AppendNamespacePrefix(user_key);
     std::string val;
     rocksdb::Status s = getValue(ctx, ns_key, &val);
     if (!s.ok()) return s;
   
     bool matched = false;
     switch (option.type) {
       case DelExOption::NONE:
         matched = true;
       break;
       case DelExOption::IFDEQ:
       matched = option.value == util::StringDigest(val);
       break;
       case DelExOption::IFDNE:
       matched = option.value != util::StringDigest(val);
       break;
       case DelExOption::IFEQ:
       matched = option.value == val
       break;
       case DelExOption::IFNE:
       matched = option.value != val
       break;
       default:
       return rocksdb::Status::InvalidArgument();
     }
     if (matched) {
       s = storage_->Delete(ctx, storage_->DefaultWriteOptions(), 
metadata_cf_handle_, ns_key);
       deleted = s.ok();
       return s;
     }
     return rocksdb::Status::OK();
   ```



-- 
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]

Reply via email to