skyitachi commented on code in PR #1865:
URL: https://github.com/apache/kvrocks/pull/1865#discussion_r1380319854


##########
src/types/json.h:
##########
@@ -134,6 +134,34 @@ struct JsonValue {
     return result_count;
   }
 
+  StatusOr<std::vector<ssize_t>> ArrIndex(std::string_view path, const 
jsoncons::json &needle, int start,
+                                          int end) const {
+    std::vector<ssize_t> result;
+    try {
+      jsoncons::jsonpath::json_query(value, path, [&](const std::string & 
/*path*/, const jsoncons::json &val) {
+        if (!val.is_array()) {
+          result.emplace_back(-2);
+          return;
+        }
+        int pstart = std::max<int>(start, 0);
+        int pend = std::max(std::min(static_cast<int>(val.size()), end), 0);
+        auto arr_begin = val.array_range().begin();
+        auto begin_it = arr_begin + pstart;
+
+        auto end_it = arr_begin + pend;
+        auto it = std::find(begin_it, end_it, needle);
+        if (it != end_it) {
+          result.emplace_back(it - arr_begin);
+          return;
+        }

Review Comment:
   yes, I have rewrite by normalize array indices as 
[redis_json](https://github.com/RedisJSON/RedisJSON/blob/master/redis_json/src/redisjson.rs#L40C4-L40C4)
 does.



##########
src/commands/cmd_json.cc:
##########
@@ -204,11 +205,59 @@ class CommandJsonArrLen : public Commander {
   }
 };
 
+class CommanderJsonArrIndex : public Commander {
+ public:
+  Status Execute(Server *svr, Connection *conn, std::string *output) override {
+    redis::Json json(svr->storage, conn->GetNamespace());
+
+    std::vector<ssize_t> result;
+    int start = -1;

Review Comment:
   negative start index also need to be normalized, so default value should be 0



##########
src/types/json.h:
##########
@@ -134,6 +134,34 @@ struct JsonValue {
     return result_count;
   }
 
+  StatusOr<std::vector<ssize_t>> ArrIndex(std::string_view path, const 
jsoncons::json &needle, int start,
+                                          int end) const {
+    std::vector<ssize_t> result;
+    try {
+      jsoncons::jsonpath::json_query(value, path, [&](const std::string & 
/*path*/, const jsoncons::json &val) {
+        if (!val.is_array()) {
+          result.emplace_back(-2);

Review Comment:
   fixed it



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