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 for GT and LT flags in ZADD #777

Closed
wants to merge 16 commits into from
Closed
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
15 changes: 12 additions & 3 deletions redis/include/userver/storages/redis/command_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,28 @@ struct GeosearchOptions {

struct ZaddOptions {
enum class Exist { kAddAlways, kAddIfNotExist, kAddIfExist };
enum class Compare { kNone, kGreaterThan, kLessThan };
enum class ReturnValue { kAddedCount, kChangedCount };

ZaddOptions() = default;
ZaddOptions(Exist exist, ReturnValue return_value = ReturnValue::kAddedCount)
: exist(exist), return_value(return_value) {}
ZaddOptions(ReturnValue return_value, Exist exist = Exist::kAddAlways) : exist(exist), return_value(return_value) {}
ZaddOptions(Exist exist, Compare compare = Compare::kNone, ReturnValue return_value = ReturnValue::kAddedCount)
: exist(exist), compare(compare), return_value(return_value) {}
ZaddOptions(Compare compare, Exist exist = Exist::kAddAlways, ReturnValue return_value = ReturnValue::kAddedCount)
: exist(exist), compare(compare), return_value(return_value) {}
ZaddOptions(ReturnValue return_value, Exist exist = Exist::kAddAlways, Compare compare = Compare::kNone)
: exist(exist), compare(compare), return_value(return_value) {}

Exist exist = Exist::kAddAlways;
Compare compare = Compare::kNone;
ReturnValue return_value = ReturnValue::kAddedCount;
};

ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::ReturnValue return_value);
ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::Compare compare);
ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::Exist exist);
ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::ReturnValue return_value);
ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Exist exist);
ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Compare compare);

class ScanOptionsBase {
public:
Expand Down
34 changes: 34 additions & 0 deletions redis/src/storages/redis/client_redistest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,40 @@ UTEST_F(RedisClientTest, Zadd) {
EXPECT_FALSE(client->ZaddIncrExisting("zset", 1.1, "five", {}).Get().has_value());
}

UTEST_F(RedisClientTest, ZaddGtLt) {
Version since{6, 2, 0};
if (!CheckVersion(since)) GTEST_SKIP() << SkipMsgByVersion("Zadd gt/lt", since);

auto client = GetClient();

storages::redis::ZaddOptions options;
options.compare = storages::redis::ZaddOptions::Compare::kGreaterThan;
EXPECT_EQ(client->Zadd("zset_gt_lt", {{1., "one"}, {3., "two"}}, options, {}).Get(), 2);
EXPECT_EQ(client->Zscore("zset_gt_lt", "one", {}).Get(), 1);
EXPECT_EQ(client->Zscore("zset_gt_lt", "two", {}).Get(), 3);

EXPECT_EQ(client->Zadd("zset_gt_lt", {{3., "one"}, {1., "two"}}, options, {}).Get(), 0);
EXPECT_EQ(client->Zscore("zset_gt_lt", "one", {}).Get(), 3);
EXPECT_EQ(client->Zscore("zset_gt_lt", "two", {}).Get(), 3);

EXPECT_EQ(client->Zadd("zset_gt_lt", {{4., "one"}, {4., "two"}}, options, {}).Get(), 0);
EXPECT_EQ(client->Zscore("zset_gt_lt", "one", {}).Get(), 4);
EXPECT_EQ(client->Zscore("zset_gt_lt", "two", {}).Get(), 4);

options.compare = storages::redis::ZaddOptions::Compare::kLessThan;
EXPECT_EQ(client->Zadd("zset_gt_lt", {{3., "one"}, {5., "two"}}, options, {}).Get(), 0);
EXPECT_EQ(client->Zscore("zset_gt_lt", "one", {}).Get(), 3);
EXPECT_EQ(client->Zscore("zset_gt_lt", "two", {}).Get(), 4);

EXPECT_EQ(client->Zadd("zset_gt_lt", {{5., "one"}, {3., "two"}}, options, {}).Get(), 0);
EXPECT_EQ(client->Zscore("zset_gt_lt", "one", {}).Get(), 3);
EXPECT_EQ(client->Zscore("zset_gt_lt", "two", {}).Get(), 3);

EXPECT_EQ(client->Zadd("zset_gt_lt", {{1., "one"}, {1., "two"}}, options, {}).Get(), 0);
EXPECT_EQ(client->Zscore("zset_gt_lt", "one", {}).Get(), 1);
EXPECT_EQ(client->Zscore("zset_gt_lt", "two", {}).Get(), 1);
}

UTEST_F(RedisClientTest, Zcard) {
auto client = GetClient();

Expand Down
25 changes: 23 additions & 2 deletions redis/src/storages/redis/command_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ void PutArg(CmdArgs::CmdArgsArray& args_, std::optional<ScanOptionsBase::Count>
}
}

ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::ReturnValue return_value) { return {exist, return_value}; }
ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::ReturnValue return_value) {
return {exist, ZaddOptions::Compare::kNone, return_value};
}

ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::Compare compare) { return {exist, compare}; }

ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Exist exist) { return {exist, return_value}; }
ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::Exist exist) { return {exist, compare}; }

ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::ReturnValue return_value) {
return {ZaddOptions::Exist::kAddAlways, compare, return_value};
}

ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Exist exist) {
return {exist, ZaddOptions::Compare::kNone, return_value};
}

ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Compare compare) {
return {ZaddOptions::Exist::kAddAlways, compare, return_value};
}

void PutArg(CmdArgs::CmdArgsArray& args_, GeoaddArg arg) {
args_.emplace_back(std::to_string(arg.lon));
Expand Down Expand Up @@ -114,6 +130,11 @@ void PutArg(CmdArgs::CmdArgsArray& args_, const ZaddOptions& arg) {
else if (arg.exist == ZaddOptions::Exist::kAddIfExist)
args_.emplace_back("XX");

if (arg.compare == ZaddOptions::Compare::kGreaterThan)
args_.emplace_back("GT");
else if (arg.compare == ZaddOptions::Compare::kLessThan)
args_.emplace_back("LT");

if (arg.return_value == ZaddOptions::ReturnValue::kChangedCount) args_.emplace_back("CH");
}

Expand Down
Loading