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

Add support of the command ZDIFF and ZDIFFSTORE #2021

Merged
merged 8 commits into from
Jan 17, 2024
8 changes: 0 additions & 8 deletions src/types/redis_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -931,14 +931,6 @@ rocksdb::Status ZSet::RandMember(const Slice &user_key, int64_t command_count,
}

rocksdb::Status ZSet::Diff(const std::vector<Slice> &keys, MemberScores *members) {
std::vector<std::string> lock_keys;
MaheshMadushan marked this conversation as resolved.
Show resolved Hide resolved
lock_keys.reserve(keys.size());
for (const auto key : keys) {
std::string ns_key = AppendNamespacePrefix(key);
lock_keys.emplace_back(std::move(ns_key));
}
MultiLockGuard guard(storage_->GetLockManager(), lock_keys);

members->clear();
MemberScores source_member_scores;
RangeScoreSpec spec;
Expand Down
42 changes: 42 additions & 0 deletions tests/cppunit/types/zset_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,48 @@ TEST_F(RedisZSetTest, Diff) {
}

s = zset_->Del(k1);
EXPECT_TRUE(s.ok());
s = zset_->Del(k2);
EXPECT_TRUE(s.ok());
s = zset_->Del(k3);
EXPECT_TRUE(s.ok());
}

TEST_F(RedisZSetTest, DiffStore) {
uint64_t ret = 0;

std::string k1 = "key1";
std::vector<MemberScore> k1_mscores = {{"a", -100.1}, {"b", -100.1}, {"c", 0}, {"d", 1.234}};

std::string k2 = "key2";
std::vector<MemberScore> k2_mscores = {{"c", -150.1}};

auto s = zset_->Add(k1, ZAddFlags::Default(), &k1_mscores, &ret);
EXPECT_EQ(ret, 4);
zset_->Add(k2, ZAddFlags::Default(), &k2_mscores, &ret);
EXPECT_EQ(ret, 1);

uint64_t stored_count = 0;
zset_->DiffStore("zsetdiff", {k1, k2}, &stored_count);
EXPECT_EQ(stored_count, 3);

RangeScoreSpec spec;
std::vector<MemberScore> mscores;
zset_->RangeByScore("zsetdiff", spec, &mscores, nullptr);
EXPECT_EQ(mscores.size(), 3);

std::vector<MemberScore> expected_mscores = {{"a", -100.1}, {"b", -100.1}, {"d", 1.234}};
int index = 0;
for (const auto &mscore : expected_mscores) {
EXPECT_EQ(mscore.member, mscores[index].member);
EXPECT_EQ(mscore.score, mscores[index].score);
index++;
}

s = zset_->Del(k1);
EXPECT_TRUE(s.ok());
s = zset_->Del(k2);
EXPECT_TRUE(s.ok());
s = zset_->Del("zsetdiff");
EXPECT_TRUE(s.ok());
}
81 changes: 78 additions & 3 deletions tests/gocase/unit/type/zset/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,48 @@ func basicTests(t *testing.T, rdb *redis.Client, ctx context.Context, encoding s
require.EqualValues(t, []redis.Z([]redis.Z{{Score: 1, Member: "a"}, {Score: 3, Member: "d"}}), cmd.Val())
})

t.Run(fmt.Sprintf("ZDIFF with empty sets - %s", encoding), func(t *testing.T) {
createZset(rdb, ctx, "zseta", []redis.Z{})
createZset(rdb, ctx, "zsetb", []redis.Z{})
cmd := rdb.ZDiff(ctx, "zseta", "zsetb")
require.NoError(t, cmd.Err())
require.EqualValues(t, []string([]string{}), cmd.Val())
})

t.Run(fmt.Sprintf("ZDIFF with non existing sets - %s", encoding), func(t *testing.T) {
rdb.Del(ctx, "zseta")
rdb.Del(ctx, "zsetb")
cmd := rdb.ZDiff(ctx, "zseta", "zsetb")
require.NoError(t, cmd.Err())
require.EqualValues(t, []string([]string{}), cmd.Val())
})

t.Run(fmt.Sprintf("ZDIFF with missing set with scores - %s", encoding), func(t *testing.T) {
createZset(rdb, ctx, "zseta", []redis.Z{
{Score: 1, Member: "a"},
{Score: 2, Member: "b"},
{Score: 3, Member: "c"},
{Score: 3, Member: "d"},
})
createZset(rdb, ctx, "zsetb", []redis.Z{
{Score: 1, Member: "b"},
{Score: 2, Member: "c"},
{Score: 4, Member: "f"},
})
rdb.Del(ctx, "zsetc")
cmd := rdb.ZDiffWithScores(ctx, "zseta", "zsetb", "zsetc")
require.NoError(t, cmd.Err())
require.EqualValues(t, []redis.Z([]redis.Z{{Score: 1, Member: "a"}, {Score: 3, Member: "d"}}), cmd.Val())
})

t.Run(fmt.Sprintf("ZDIFF with empty sets with scores - %s", encoding), func(t *testing.T) {
createZset(rdb, ctx, "zseta", []redis.Z{})
createZset(rdb, ctx, "zsetb", []redis.Z{})
cmd := rdb.ZDiffWithScores(ctx, "zseta", "zsetb")
require.NoError(t, cmd.Err())
require.EqualValues(t, []redis.Z([]redis.Z{}), cmd.Val())
})

t.Run("ZDIFFSTORE with three sets - ", func(t *testing.T) {
createZset(rdb, ctx, "zseta", []redis.Z{
{Score: 1, Member: "a"},
Expand All @@ -1546,9 +1588,42 @@ func basicTests(t *testing.T, rdb *redis.Client, ctx context.Context, encoding s
{Score: 4, Member: "c"},
{Score: 5, Member: "e"},
})
require.NoError(t, rdb.ZDiffStore(ctx, "setres", "zseta", "zsetb", "zsetc").Err())
cmd := rdb.ZDiffWithScores(ctx, "zseta", "zsetb", "zsetc")
require.EqualValues(t, []redis.Z([]redis.Z{{Score: 1, Member: "a"}, {Score: 3, Member: "d"}}), cmd.Val())
cmd := rdb.ZDiffStore(ctx, "setres", "zseta", "zsetb", "zsetc")
require.NoError(t, cmd.Err())
require.EqualValues(t, int64(2), cmd.Val())
require.Equal(t, []redis.Z([]redis.Z{{Score: 1, Member: "a"}, {Score: 3, Member: "d"}}), rdb.ZRangeWithScores(ctx, "setres", 0, -1).Val())
})

t.Run("ZDIFFSTORE with missing sets - ", func(t *testing.T) {
createZset(rdb, ctx, "zseta", []redis.Z{
{Score: 1, Member: "a"},
{Score: 2, Member: "b"},
{Score: 3, Member: "c"},
{Score: 3, Member: "d"},
{Score: 4, Member: "e"},
})
createZset(rdb, ctx, "zsetb", []redis.Z{
{Score: 1, Member: "b"},
{Score: 2, Member: "c"},
{Score: 4, Member: "f"},
{Score: 4, Member: "e"},
})
rdb.Del(ctx, "zsetc")
rdb.Del(ctx, "zsetc")
MaheshMadushan marked this conversation as resolved.
Show resolved Hide resolved
cmd := rdb.ZDiffStore(ctx, "setres", "zseta", "zsetb", "zsetc")
require.NoError(t, cmd.Err())
require.EqualValues(t, int64(2), cmd.Val())
require.Equal(t, []redis.Z([]redis.Z{{Score: 1, Member: "a"}, {Score: 3, Member: "d"}}), rdb.ZRangeWithScores(ctx, "setres", 0, -1).Val())
})

t.Run("ZDIFFSTORE with missing sets - ", func(t *testing.T) {
rdb.Del(ctx, "zseta")
rdb.Del(ctx, "zsetb")
rdb.Del(ctx, "zsetc")
cmd := rdb.ZDiffStore(ctx, "setres", "zseta", "zsetb", "zsetc")
require.NoError(t, cmd.Err())
require.EqualValues(t, int64(0), cmd.Val())
require.Equal(t, []redis.Z([]redis.Z{}), rdb.ZRangeWithScores(ctx, "setres", 0, -1).Val())
})
}

Expand Down