mapleFU commented on code in PR #2262:
URL: https://github.com/apache/kvrocks/pull/2262#discussion_r1591196445


##########
src/storage/redis_db.cc:
##########
@@ -768,4 +773,211 @@ rocksdb::Status Database::Copy(const std::string &key, 
const std::string &new_ke
   return storage_->Write(storage_->DefaultWriteOptions(), 
batch->GetWriteBatch());
 }
 
+std::optional<std::string> Database::lookupKeyByPattern(const std::string 
&pattern, const std::string &subst) {
+  if (pattern == "#") {
+    return subst;
+  }
+
+  auto match_pos = pattern.find('*');
+  if (match_pos == std::string::npos) {
+    return std::nullopt;
+  }
+
+  // hash field
+  std::string field;
+  auto arrow_pos = pattern.find("->", match_pos + 1);
+  if (arrow_pos != std::string::npos && arrow_pos + 2 < pattern.size()) {
+    field = pattern.substr(arrow_pos + 2);
+  }
+
+  std::string key = pattern.substr(0, match_pos + 1);
+  key.replace(match_pos, 1, subst);
+
+  std::string value;
+  RedisType type = RedisType::kRedisNone;
+  if (!field.empty()) {
+    auto hash_db = redis::Hash(storage_, namespace_);
+    if (auto s = hash_db.Type(key, &type); !s.ok() || type != 
RedisType::kRedisHash) {

Review Comment:
   ```suggestion
       if (auto s = hash_db.Type(key, &type); !s.ok() || type != 
RedisType::kRedisHash) {
   ```
   
   Maybe this can return `StatusOr<std::optional<std::string>>`, but current 
impl is also ok for me



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