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


##########
src/commands/cmd_key.cc:
##########
@@ -424,6 +424,111 @@ class CommandCopy : public Commander {
   bool replace_ = false;
 };
 
+template <bool ReadOnly>
+class CommandSort : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    CommandParser parser(args, 2);
+    while (parser.Good()) {
+      if (parser.EatEqICase("BY")) {
+        if (parser.Remains() < 1) {

Review Comment:
   Isn't `TakeStr` already checks remaining?



##########
src/storage/redis_db.cc:
##########
@@ -777,4 +782,208 @@ rocksdb::Status Database::Copy(const std::string &key, 
const std::string &new_ke
   return storage_->Write(storage_->DefaultWriteOptions(), 
batch->GetWriteBatch());
 }
 
+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 "";
+  }
+
+  // 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;
+  if (!field.empty()) {
+    auto hash_db = redis::Hash(storage_, namespace_);
+    RedisType type = RedisType::kRedisNone;
+    if (auto s = hash_db.Type(key, &type); !s.ok() || type >= 
RedisTypeNames.size()) {
+      return "";
+    }
+
+    hash_db.Get(key, field, &value);
+  } else {
+    auto string_db = redis::String(storage_, namespace_);
+    RedisType type = RedisType::kRedisNone;
+    if (auto s = string_db.Type(key, &type); !s.ok() || type >= 
RedisTypeNames.size()) {
+      return "";
+    }
+    string_db.Get(key, &value);
+  }
+  return value;
+}
+
+rocksdb::Status Database::Sort(const RedisType &type, const std::string &key, 
SortArgument &args, const RESP &version,
+                               std::vector<std::string> *output_vec, 
SortResult *res) {
+  /* When sorting a set with no sort specified, we must sort the output
+   * so the result is consistent across scripting and replication.
+   *
+   * The other types (list, sorted set) will retain their native order
+   * even if no sort order is requested, so they remain stable across
+   * scripting and replication.
+   *
+   * TODO: support CLIENT_SCRIPT flag, (!storekey_.empty() || c->flags & 
CLIENT_SCRIPT)) */
+  if (args.dontsort && type == RedisType::kRedisSet && 
(!args.storekey.empty())) {
+    /* Force ALPHA sorting */
+    args.dontsort = false;
+    args.alpha = true;
+    args.sortby = "";
+  }

Review Comment:
   Can we normalize this when build the args? Now the `SortArgument` is build 
outside and normalized in `Database::Sort`. Which is a bit weird



##########
src/storage/redis_db.cc:
##########
@@ -777,4 +782,208 @@ rocksdb::Status Database::Copy(const std::string &key, 
const std::string &new_ke
   return storage_->Write(storage_->DefaultWriteOptions(), 
batch->GetWriteBatch());
 }
 
+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 "";
+  }
+
+  // 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;
+  if (!field.empty()) {
+    auto hash_db = redis::Hash(storage_, namespace_);
+    RedisType type = RedisType::kRedisNone;
+    if (auto s = hash_db.Type(key, &type); !s.ok() || type >= 
RedisTypeNames.size()) {
+      return "";
+    }
+
+    hash_db.Get(key, field, &value);
+  } else {
+    auto string_db = redis::String(storage_, namespace_);
+    RedisType type = RedisType::kRedisNone;
+    if (auto s = string_db.Type(key, &type); !s.ok() || type >= 
RedisTypeNames.size()) {
+      return "";
+    }
+    string_db.Get(key, &value);
+  }
+  return value;
+}
+
+rocksdb::Status Database::Sort(const RedisType &type, const std::string &key, 
SortArgument &args, const RESP &version,
+                               std::vector<std::string> *output_vec, 
SortResult *res) {
+  /* When sorting a set with no sort specified, we must sort the output
+   * so the result is consistent across scripting and replication.
+   *
+   * The other types (list, sorted set) will retain their native order
+   * even if no sort order is requested, so they remain stable across
+   * scripting and replication.
+   *
+   * TODO: support CLIENT_SCRIPT flag, (!storekey_.empty() || c->flags & 
CLIENT_SCRIPT)) */
+  if (args.dontsort && type == RedisType::kRedisSet && 
(!args.storekey.empty())) {
+    /* Force ALPHA sorting */
+    args.dontsort = false;
+    args.alpha = true;
+    args.sortby = "";
+  }
+
+  // Obtain the length of the object to sort.
+  const std::string ns_key = AppendNamespacePrefix(key);
+  Metadata metadata(type, false);
+  auto s = GetMetadata(GetOptions{}, {type}, ns_key, &metadata);
+  if (!s.ok()) {
+    return s;
+  }
+
+  int vectorlen = (int)metadata.size;

Review Comment:
   Better using `static_cast`



##########
src/commands/cmd_key.cc:
##########
@@ -424,6 +424,111 @@ class CommandCopy : public Commander {
   bool replace_ = false;
 };
 
+template <bool ReadOnly>
+class CommandSort : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    CommandParser parser(args, 2);
+    while (parser.Good()) {
+      if (parser.EatEqICase("BY")) {
+        if (parser.Remains() < 1) {
+          return parser.InvalidSyntax();
+        }
+        sort_argument_.sortby = GET_OR_RET(parser.TakeStr());
+
+        if (sort_argument_.sortby.find('*') == std::string::npos) {
+          sort_argument_.dontsort = true;
+        } else {
+          /* TODO:
+           * If BY is specified with a real pattern, we can't accept it in 
cluster mode,
+           * unless we can make sure the keys formed by the pattern are in the 
same slot
+           * as the key to sort.
+           * If BY is specified with a real pattern, we can't accept
+           * it if no full ACL key access is applied for this command. */
+        }
+      } else if (parser.EatEqICase("LIMIT")) {
+        if (parser.Remains() < 2) {
+          return parser.InvalidSyntax();
+        }
+        sort_argument_.offset = GET_OR_RET(parser.template TakeInt<int>());
+        sort_argument_.count = GET_OR_RET(parser.template TakeInt<int>());
+      } else if (parser.EatEqICase("GET") && parser.Remains() >= 1) {
+        if (parser.Remains() < 1) {
+          return parser.InvalidSyntax();
+        }
+        /* TODO:
+         * If GET is specified with a real pattern, we can't accept it in 
cluster mode,
+         * unless we can make sure the keys formed by the pattern are in the 
same slot
+         * as the key to sort. */
+        sort_argument_.getpatterns.push_back(GET_OR_RET(parser.TakeStr()));
+      } else if (parser.EatEqICase("ASC")) {
+        sort_argument_.desc = false;
+      } else if (parser.EatEqICase("DESC")) {
+        sort_argument_.desc = true;
+      } else if (parser.EatEqICase("ALPHA")) {
+        sort_argument_.alpha = true;
+      } else if (parser.EatEqICase("STORE")) {
+        if constexpr (ReadOnly) {
+          return parser.InvalidSyntax();

Review Comment:
   Can the error code be more specifically?



##########
src/commands/cmd_key.cc:
##########
@@ -424,6 +424,111 @@ class CommandCopy : public Commander {
   bool replace_ = false;
 };
 
+template <bool ReadOnly>
+class CommandSort : public Commander {
+ public:
+  Status Parse(const std::vector<std::string> &args) override {
+    CommandParser parser(args, 2);
+    while (parser.Good()) {
+      if (parser.EatEqICase("BY")) {
+        if (parser.Remains() < 1) {
+          return parser.InvalidSyntax();
+        }
+        sort_argument_.sortby = GET_OR_RET(parser.TakeStr());
+
+        if (sort_argument_.sortby.find('*') == std::string::npos) {
+          sort_argument_.dontsort = true;
+        } else {
+          /* TODO:
+           * If BY is specified with a real pattern, we can't accept it in 
cluster mode,
+           * unless we can make sure the keys formed by the pattern are in the 
same slot
+           * as the key to sort.
+           * If BY is specified with a real pattern, we can't accept
+           * it if no full ACL key access is applied for this command. */
+        }
+      } else if (parser.EatEqICase("LIMIT")) {
+        if (parser.Remains() < 2) {
+          return parser.InvalidSyntax();
+        }
+        sort_argument_.offset = GET_OR_RET(parser.template TakeInt<int>());
+        sort_argument_.count = GET_OR_RET(parser.template TakeInt<int>());
+      } else if (parser.EatEqICase("GET") && parser.Remains() >= 1) {
+        if (parser.Remains() < 1) {
+          return parser.InvalidSyntax();
+        }
+        /* TODO:

Review Comment:
   Why checks `Remains() < 1` after `Remains >= 1` ?



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