Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): fix JSON.ARRTRIM implementation (#844) #864

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/server/json_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,40 +677,43 @@ OpResult<vector<OptSizeT>> OpArrTrim(const OpArgs& op_args, string_view key, str
int start_index, int stop_index) {
vector<OptSizeT> vec;
auto cb = [&](const string& path, JsonType& val) {
if (!val.is_array() || val.empty()) {
if (!val.is_array()) {
vec.emplace_back(nullopt);
return;
}

if (val.empty()) {
vec.emplace_back(0);
return;
}

size_t trim_start_index;
if (start_index < 0) {
trim_start_index = 0;
} else {
trim_start_index = start_index;
}

size_t trim_stop_index;
size_t trim_end_index;
if ((size_t)stop_index >= val.size()) {
trim_stop_index = val.size();
trim_end_index = val.size();
} else {
trim_stop_index = stop_index;
trim_end_index = stop_index;
}

if (trim_start_index >= val.size() || trim_start_index > trim_stop_index) {
if (trim_start_index >= val.size() || trim_start_index > trim_end_index) {
val.erase(val.array_range().begin(), val.array_range().end());
vec.emplace_back(val.size());
vec.emplace_back(0);
return;
}

auto it = std::next(val.array_range().begin(), trim_start_index);
while (it != val.array_range().end()) {
if (trim_start_index++ == trim_stop_index) {
break;
}

it = val.erase(it);
auto trim_start_it = std::next(val.array_range().begin(), trim_start_index);
auto trim_end_it = val.array_range().end();
if (trim_end_index < val.size()) {
trim_end_it = std::next(val.array_range().begin(), trim_end_index + 1);
}

val = json_array<JsonType>(trim_start_it, trim_end_it);
vec.emplace_back(val.size());
};

Expand Down
25 changes: 19 additions & 6 deletions src/server/json_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,10 @@ TEST_F(JsonFamilyTest, ArrTrim) {

resp = Run({"JSON.ARRTRIM", "json", "$[*]", "0", "1"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);
EXPECT_THAT(resp.GetVec(), ElementsAre(ArgType(RespExpr::NIL), IntArg(0), IntArg(1), IntArg(2)));
EXPECT_THAT(resp.GetVec(), ElementsAre(IntArg(0), IntArg(1), IntArg(2), IntArg(2)));

resp = Run({"JSON.GET", "json"});
EXPECT_EQ(resp, R"([[],[],["b"],["b","c"]])");
EXPECT_EQ(resp, R"([[],["a"],["a","b"],["a","b"]])");

json = R"(
{"a":[], "nested": {"a": [1,4]}}
Expand All @@ -729,10 +729,10 @@ TEST_F(JsonFamilyTest, ArrTrim) {

resp = Run({"JSON.ARRTRIM", "json", "$..a", "0", "1"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);
EXPECT_THAT(resp.GetVec(), ElementsAre(ArgType(RespExpr::NIL), IntArg(1)));
EXPECT_THAT(resp.GetVec(), ElementsAre(IntArg(0), IntArg(2)));

resp = Run({"JSON.GET", "json"});
EXPECT_EQ(resp, R"({"a":[],"nested":{"a":[4]}})");
EXPECT_EQ(resp, R"({"a":[],"nested":{"a":[1,4]}})");

json = R"(
{"a":[1,2,3,2], "nested": {"a": false}}
Expand All @@ -743,10 +743,23 @@ TEST_F(JsonFamilyTest, ArrTrim) {

resp = Run({"JSON.ARRTRIM", "json", "$..a", "1", "2"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);
EXPECT_THAT(resp.GetVec(), ElementsAre(IntArg(3), ArgType(RespExpr::NIL)));
EXPECT_THAT(resp.GetVec(), ElementsAre(IntArg(2), ArgType(RespExpr::NIL)));

resp = Run({"JSON.GET", "json"});
EXPECT_EQ(resp, R"({"a":[2,3],"nested":{"a":false}})");

json = R"(
[1,2,3,4,5,6,7]
)";

resp = Run({"JSON.SET", "json", "$", json});
ASSERT_THAT(resp, "OK");

resp = Run({"JSON.ARRTRIM", "json", "$", "2", "3"});
EXPECT_THAT(resp, IntArg(2));

resp = Run({"JSON.GET", "json"});
EXPECT_EQ(resp, R"({"a":[1,3,2],"nested":{"a":false}})");
EXPECT_EQ(resp, R"([3,4])");
}

TEST_F(JsonFamilyTest, ArrInsert) {
Expand Down