Skip to content

Commit

Permalink
LPUSHX and RPUSHX now return 0 size when key does not exist. (#1010)
Browse files Browse the repository at this point in the history
Fixes #834

Signed-off-by: chakaz <chakaz@chakaz>
Co-authored-by: chakaz <chakaz@chakaz>
  • Loading branch information
chakaz and chakaz authored Mar 31, 2023
1 parent 5f41712 commit a7d40f3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server/list_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ OpResult<uint32_t> OpPush(const OpArgs& op_args, std::string_view key, ListDir d
if (skip_notexist) {
auto it_res = es->db_slice().Find(op_args.db_cntx, key, OBJ_LIST);
if (!it_res)
return it_res.status();
return 0; // Redis returns 0 for nonexisting keys for the *PUSHX actions.
it = *it_res;
} else {
try {
Expand Down
24 changes: 24 additions & 0 deletions src/server/list_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -813,4 +813,28 @@ TEST_F(ListFamilyTest, BLMove) {
ASSERT_THAT(resp.GetVec(), ElementsAre("val1", "val2"));
}

TEST_F(ListFamilyTest, LPushX) {
// No push for 'lpushx' on nonexisting key.
EXPECT_THAT(Run({"lpushx", kKey1, "val1"}), IntArg(0));
EXPECT_THAT(Run({"llen", kKey1}), IntArg(0));

EXPECT_THAT(Run({"lpush", kKey1, "val1"}), IntArg(1));
EXPECT_THAT(Run({"lrange", kKey1, "0", "-1"}), "val1");

EXPECT_THAT(Run({"lpushx", kKey1, "val2"}), IntArg(2));
EXPECT_THAT(Run({"lrange", kKey1, "0", "-1"}).GetVec(), ElementsAre("val2", "val1"));
}

TEST_F(ListFamilyTest, RPushX) {
// No push for 'rpushx' on nonexisting key.
EXPECT_THAT(Run({"rpushx", kKey1, "val1"}), IntArg(0));
EXPECT_THAT(Run({"llen", kKey1}), IntArg(0));

EXPECT_THAT(Run({"rpush", kKey1, "val1"}), IntArg(1));
EXPECT_THAT(Run({"lrange", kKey1, "0", "-1"}), "val1");

EXPECT_THAT(Run({"rpushx", kKey1, "val2"}), IntArg(2));
EXPECT_THAT(Run({"lrange", kKey1, "0", "-1"}).GetVec(), ElementsAre("val1", "val2"));
}

} // namespace dfly

0 comments on commit a7d40f3

Please sign in to comment.