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

feat: support zset cmd of zrank/zrevrank/zrem/zincrby #219

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
4 changes: 4 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ const std::string kCmdNameZScore = "zscore";
const std::string kCmdNameZRange = "zrange";
const std::string kCmdNameZRangebylex = "zrangebylex";
const std::string kCmdNameZRevrangebylex = "zrevrangebylex";
const std::string kCmdNameZRank = "zrank";
const std::string kCmdNameZRevrank = "zrevrank";
const std::string kCmdNameZRem = "zrem";
const std::string kCmdNameZIncrby = "zincrby";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
4 changes: 4 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(ZRange, -4);
ADD_COMMAND(ZRangebylex, -3);
ADD_COMMAND(ZRevrangebylex, -3);
ADD_COMMAND(ZRank, 3);
ADD_COMMAND(ZRevrank, 3);
ADD_COMMAND(ZRem, -3);
ADD_COMMAND(ZIncrby, 4);
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
299 changes: 195 additions & 104 deletions src/cmd_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,6 @@ void ZRangebyscoreCmd::DoCmd(PClient* client) {
}
}

ZCardCmd::ZCardCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategorySortedSet) {}

bool ZCardCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZCardCmd::DoCmd(PClient* client) {
int32_t reply_Num = 0;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZCard(client->Key(), &reply_Num);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "ZCard cmd error");
return;
}
client->AppendInteger(reply_Num);
}

ZRemrangebyrankCmd::ZRemrangebyrankCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryString) {}

Expand Down Expand Up @@ -303,6 +285,109 @@ void ZRemrangebyrankCmd::DoCmd(PClient* client) {
}
}

ZRevrangebyscoreCmd::ZRevrangebyscoreCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

bool ZRevrangebyscoreCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZRevrangebyscoreCmd::DoCmd(PClient* client) {
double min_score = 0;
double max_score = 0;
bool right_close = true;
bool left_close = true;
bool with_scores = false;
int64_t offset = 0, count = -1;
int32_t ret = DoScoreStrRange(client->argv_[3], client->argv_[2], &left_close, &right_close, &min_score, &max_score);
if (ret == -1) {
client->SetRes(CmdRes::kErrOther, "min or max is not a float");
return;
}
size_t argc = client->argv_.size();
if (argc >= 5) {
size_t index = 4;
while (index < argc) {
if (strcasecmp(client->argv_[index].data(), "withscores") == 0) {
with_scores = true;
} else if (strcasecmp(client->argv_[index].data(), "limit") == 0) {
if (index + 3 > argc) {
client->SetRes(CmdRes::kSyntaxErr);
return;
}
index++;
if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) {
client->SetRes(CmdRes::kInvalidInt);
return;
}
index++;
if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) {
client->SetRes(CmdRes::kInvalidInt);
return;
}
} else {
client->SetRes(CmdRes::kSyntaxErr);
return;
}
index++;
}
}

if (min_score == storage::ZSET_SCORE_MAX || max_score == storage::ZSET_SCORE_MIN) {
client->AppendContent("*0");
return;
}
std::vector<storage::ScoreMember> score_members;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())
->GetStorage()
->ZRevrangebyscore(client->Key(), min_score, max_score, left_close, right_close, count,
offset, &score_members);
if (!s.ok() && !s.IsNotFound()) {
client->SetRes(CmdRes::kErrOther, s.ToString());
return;
}
FitLimit(count, offset, static_cast<int64_t>(score_members.size()));
size_t start = offset;
size_t end = offset + count;
if (with_scores) {
char buf[32];
int64_t len = 0;
client->AppendArrayLen(count * 2);
for (; start < end; start++) {
client->AppendStringLenUint64(score_members[start].member.size());
client->AppendContent(score_members[start].member);
len = pstd::D2string(buf, sizeof(buf), score_members[start].score);
client->AppendStringLen(len);
client->AppendContent(buf);
}
} else {
client->AppendArrayLen(count);
for (; start < end; start++) {
client->AppendStringLenUint64(score_members[start].member.size());
client->AppendContent(score_members[start].member);
}
}
}

ZCardCmd::ZCardCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategorySortedSet) {}

bool ZCardCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZCardCmd::DoCmd(PClient* client) {
int32_t reply_Num = 0;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZCard(client->Key(), &reply_Num);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "ZCard cmd error");
return;
}
client->AppendInteger(reply_Num);
}

ZRangeCmd::ZRangeCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategorySortedSet) {}

Expand Down Expand Up @@ -466,91 +551,6 @@ void ZScoreCmd::DoCmd(PClient* client) {
}
}

ZRevrangebyscoreCmd::ZRevrangebyscoreCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

bool ZRevrangebyscoreCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZRevrangebyscoreCmd::DoCmd(PClient* client) {
double min_score = 0;
double max_score = 0;
bool right_close = true;
bool left_close = true;
bool with_scores = false;
int64_t offset = 0, count = -1;
int32_t ret = DoScoreStrRange(client->argv_[3], client->argv_[2], &left_close, &right_close, &min_score, &max_score);
if (ret == -1) {
client->SetRes(CmdRes::kErrOther, "min or max is not a float");
return;
}
size_t argc = client->argv_.size();
if (argc >= 5) {
size_t index = 4;
while (index < argc) {
if (strcasecmp(client->argv_[index].data(), "withscores") == 0) {
with_scores = true;
} else if (strcasecmp(client->argv_[index].data(), "limit") == 0) {
if (index + 3 > argc) {
client->SetRes(CmdRes::kSyntaxErr);
return;
}
index++;
if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &offset) == 0) {
client->SetRes(CmdRes::kInvalidInt);
return;
}
index++;
if (pstd::String2int(client->argv_[index].data(), client->argv_[index].size(), &count) == 0) {
client->SetRes(CmdRes::kInvalidInt);
return;
}
} else {
client->SetRes(CmdRes::kSyntaxErr);
return;
}
index++;
}
}

if (min_score == storage::ZSET_SCORE_MAX || max_score == storage::ZSET_SCORE_MIN) {
client->AppendContent("*0");
return;
}
std::vector<storage::ScoreMember> score_members;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())
->GetStorage()
->ZRevrangebyscore(client->Key(), min_score, max_score, left_close, right_close, count,
offset, &score_members);
if (!s.ok() && !s.IsNotFound()) {
client->SetRes(CmdRes::kErrOther, s.ToString());
return;
}
FitLimit(count, offset, static_cast<int64_t>(score_members.size()));
size_t start = offset;
size_t end = offset + count;
if (with_scores) {
char buf[32];
int64_t len = 0;
client->AppendArrayLen(count * 2);
for (; start < end; start++) {
client->AppendStringLenUint64(score_members[start].member.size());
client->AppendContent(score_members[start].member);
len = pstd::D2string(buf, sizeof(buf), score_members[start].score);
client->AppendStringLen(len);
client->AppendContent(buf);
}
} else {
client->AppendArrayLen(count);
for (; start < end; start++) {
client->AppendStringLenUint64(score_members[start].member.size());
client->AppendContent(score_members[start].member);
}
}
}

ZRangebylexCmd::ZRangebylexCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

Expand Down Expand Up @@ -668,6 +668,97 @@ void ZRevrangebylexCmd::DoCmd(PClient* client) {
}
}

ZRankCmd::ZRankCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategorySortedSet) {}

bool ZRankCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZRankCmd::DoCmd(PClient* client) {
int32_t rank = 0;
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRank(client->Key(), client->argv_[2], &rank);
if (s.ok()) {
client->AppendInteger(rank);
} else if (s.IsNotFound()) {
client->AppendContent("$-1");
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
}

ZRevrankCmd::ZRevrankCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategorySortedSet) {}

bool ZRevrankCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZRevrankCmd::DoCmd(PClient* client) {
int32_t revrank = 0;
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRevrank(client->Key(), client->argv_[2], &revrank);
if (s.ok()) {
client->AppendInteger(revrank);
} else if (s.IsNotFound()) {
client->AppendContent("$-1");
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
}

ZRemCmd::ZRemCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

bool ZRemCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZRemCmd::DoCmd(PClient* client) {
auto iter = client->argv_.begin() + 2;
std::vector<std::string> members(iter, client->argv_.end());
int32_t deleted = 0;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZRem(client->Key(), members, &deleted);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(deleted);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
}

ZIncrbyCmd::ZIncrbyCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

bool ZIncrbyCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void ZIncrbyCmd::DoCmd(PClient* client) {
double by = .0f;
double score = .0f;
if (pstd::String2d(client->argv_[2].data(), client->argv_[2].size(), &by) == 0) {
client->SetRes(CmdRes::kInvalidFloat);
return;
}

std::string member = client->argv_[3];
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZIncrby(client->Key(), member, by, &score);
if (s.ok()) {
char buf[32];
int64_t len = pstd::D2string(buf, sizeof(buf), score);
client->AppendStringLen(len);
client->AppendContent(buf);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
}

ZRemrangebyscoreCmd::ZRemrangebyscoreCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategorySortedSet) {}

Expand Down Expand Up @@ -698,4 +789,4 @@ void ZRemrangebyscoreCmd::DoCmd(PClient* client) {
}
}

} // namespace pikiwidb
} // namespace pikiwidb
Loading
Loading