Skip to content

Commit

Permalink
finish set cmd sdiff sdiffstore smembers
Browse files Browse the repository at this point in the history
  • Loading branch information
whr1118 committed Mar 10, 2024
1 parent de7ab49 commit 7c61e21
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ const std::string kCmdNameSCard = "scard";
const std::string kCmdNameSMove = "smove";
const std::string kCmdNameSRandMember = "srandmember";
const std::string kCmdNameSPop = "spop";
const std::string kCmdNameSMembers = "smembers";
const std::string kCmdNameSDiff = "sdiff";
const std::string kCmdNameSDiffStore = "sdiffstore";

// list cmd
const std::string kCmdNameLPush = "lpush";
Expand Down
60 changes: 60 additions & 0 deletions src/cmd_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,64 @@ void SPopCmd::DoCmd(PClient* client) {
return;
}
}

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

bool SMembersCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}
void SMembersCmd::DoCmd(PClient* client) {
std::vector<std::string> delete_members;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->SMembers(client->Key(), &delete_members);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "smembers cmd error");
return;
}
client->AppendStringVector(delete_members);
}

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

bool SDiffCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}
void SDiffCmd::DoCmd(PClient* client) {
std::vector<std::string> diff_members;
std::vector<std::string> set_name_list;
for (int i = 1; i <= client->argv_.size(); i++) {
set_name_list.push_back(client->argv_[i]);
}
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->SDiff(set_name_list, &diff_members);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "sdiff cmd error");
return;
}
client->AppendStringVector(diff_members);
}

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

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

void SDiffStoreCmd::DoCmd(PClient* client) {
std::vector<std::string> value_to_dest;
int32_t reply_num = 0;

std::vector<std::string> diff_keys(client->argv_.begin() + 2, client->argv_.end());
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->SDiffstore(client->Key(), diff_keys, value_to_dest, &reply_num);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "sdiffstore cmd error");
return;
}
client->AppendInteger(reply_num);
}
} // namespace pikiwidb
33 changes: 33 additions & 0 deletions src/cmd_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,37 @@ class SPopCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

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

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

} // namespace pikiwidb
3 changes: 3 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(SMove, 4);
ADD_COMMAND(SRandMember, -2); // Added the count argument since Redis 3.2.0
ADD_COMMAND(SPop, -2);
ADD_COMMAND(SMembers, 2);
ADD_COMMAND(SDiff, -2);
ADD_COMMAND(SDiffStore, -3);

// list
ADD_COMMAND(LPush, -3);
Expand Down
60 changes: 60 additions & 0 deletions tests/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,64 @@ var _ = Describe("Set", Ordered, func() {
Expect(err).NotTo(HaveOccurred())
Expect(members).To(HaveLen(2))
})

It("should SMembers", func() {
sAdd := client.SAdd(ctx, "setSMembers", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSMembers", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sMembers := client.SMembers(ctx, "setSMembers")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
})

It("should SDiff", func() {
sAdd := client.SAdd(ctx, "setSDiff1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiff1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiff1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sAdd = client.SAdd(ctx, "setSDiff2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiff2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiff2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sDiff := client.SDiff(ctx, "setSDiff1", "setSDiff2")
Expect(sDiff.Err()).NotTo(HaveOccurred())
Expect(sDiff.Val()).To(ConsistOf([]string{"a", "b"}))

sDiff = client.SDiff(ctx, "nonexistent_setSDiff1", "nonexistent_setSDiff2")
Expect(sDiff.Err()).NotTo(HaveOccurred())
Expect(sDiff.Val()).To(HaveLen(0))
})

It("should SDiffStore", func() {
sAdd := client.SAdd(ctx, "setSDiffStore1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiffStore1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiffStore1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sAdd = client.SAdd(ctx, "setSDiffStore2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiffStore2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSDiffStore2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sDiffStore := client.SDiffStore(ctx, "setKey", "setSDiffStore1", "setSDiffStore2")
Expect(sDiffStore.Err()).NotTo(HaveOccurred())
Expect(sDiffStore.Val()).To(Equal(int64(2)))

sMembers := client.SMembers(ctx, "setKey")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"a", "b"}))
})

})

0 comments on commit 7c61e21

Please sign in to comment.