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: zset cmd zcard #192

Merged
merged 7 commits into from
Mar 9, 2024
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
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const std::string kCmdNameLInsert = "linsert";
const std::string kCmdNameZAdd = "zadd";
const std::string kCmdNameZRevrange = "zrevrange";
const std::string kCmdNameZRangebyscore = "zrangebyscore";
const std::string kCmdNameZCard = "zcard";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,4 @@ void SetRangeCmd::DoCmd(PClient* client) {
}
client->AppendInteger(static_cast<int>(ret));
}
} // namespace pikiwidb
} // namespace pikiwidb
1 change: 1 addition & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(ZAdd, -4);
ADD_COMMAND(ZRevrange, -4);
ADD_COMMAND(ZRangebyscore, -4);
ADD_COMMAND(ZCard, 2);
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
17 changes: 17 additions & 0 deletions src/cmd_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,21 @@ 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())->ZCard(client->Key(), &reply_Num);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "ZCard cmd error");
return;
}
client->AppendInteger(reply_Num);
}
} // namespace pikiwidb
11 changes: 11 additions & 0 deletions src/cmd_zset.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ class ZRangebyscoreCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

class ZCardCmd : public BaseCmd {
public:
ZCardCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
};

} // namespace pikiwidb
18 changes: 18 additions & 0 deletions tests/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,22 @@ var _ = Describe("Zset", Ordered, func() {
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
})

It("should ZCard", func() {
err :=client.ZAdd(ctx, "zsetZCard", redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err =client.ZAdd(ctx, "zsetZCard", redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())

card, err :=client.ZCard(ctx, "zsetZCard").Result()
Expect(err).NotTo(HaveOccurred())
Expect(card).To(Equal(int64(2)))
})

})
Loading