Skip to content

Commit

Permalink
feat: add set cmd spop (OpenAtomFoundation#129)
Browse files Browse the repository at this point in the history
* improve spop and add spop cmd gotest

* change client.Do to client.SPop
  • Loading branch information
whr1118 authored Mar 5, 2024
1 parent f47842c commit 1e7f205
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const std::string kCmdNameSUnion = "sunion";
const std::string kCmdNameSCard = "scard";
const std::string kCmdNameSMove = "smove";
const std::string kCmdNameSRandMember = "srandmember";
const std::string kCmdNameSPop = "spop";

// list cmd
const std::string kCmdNameLPush = "lpush";
Expand Down
41 changes: 40 additions & 1 deletion src/cmd_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cmd_set.h"
#include <memory>
#include <utility>
#include "pstd/pstd_string.h"
#include "store.h"

namespace pikiwidb {
Expand Down Expand Up @@ -192,7 +193,6 @@ bool SRandMemberCmd::DoInitial(PClient* client) {
return false;
}
}
client->SetKey(client->argv_[1]);
return true;
}

Expand All @@ -211,4 +211,43 @@ void SRandMemberCmd::DoCmd(PClient* client) {
return;
}

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

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

void SPopCmd::DoCmd(PClient* client) {
std::vector<std::string> delete_members;
if ((client->argv_.size()) == 2) {
int64_t cnt = 1;
std::vector<std::string> delete_member;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->SPop(client->Key(), &delete_member, cnt);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "spop cmd error");
return;
}
client->AppendString(delete_member[0]);

} else if ((client->argv_.size()) == 3) {
std::vector<std::string> delete_members;
int64_t cnt = 1;
if (client->argv_[2].find(".") != std::string::npos || !pstd::String2int(client->argv_[2], &cnt)) {
client->SetRes(CmdRes::kInvalidInt);
return;
}
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->SPop(client->Key(), &delete_members, cnt);
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "spop cmd error");
return;
}
client->AppendStringVector(delete_members);

} else {
client->SetRes(CmdRes::kWrongNum, "spop");
return;
}
}
} // namespace pikiwidb
11 changes: 11 additions & 0 deletions src/cmd_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,15 @@ class SRandMemberCmd : public BaseCmd {
int num_rand = 1;
};

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

protected:
bool DoInitial(PClient *client) override;

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

} // 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 @@ -97,6 +97,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(SCard, 2);
ADD_COMMAND(SMove, 4);
ADD_COMMAND(SRandMember, -2); // Added the count argument since Redis 3.2.0
ADD_COMMAND(SPop, -2);

// list
ADD_COMMAND(LPush, -3);
Expand Down
24 changes: 24 additions & 0 deletions tests/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@ var _ = Describe("Set", Ordered, func() {
sCard := client.SCard(ctx, "setScard")
Expect(sCard.Err()).NotTo(HaveOccurred())
Expect(sCard.Val()).To(Equal(int64(2)))
})


It("should SPop", func() {
sAdd := client.SAdd(ctx, "setSpop", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSpop", "two")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSpop", "three")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSpop", "four")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "setSpop", "five")
Expect(sAdd.Err()).NotTo(HaveOccurred())

sPopN := client.SPopN(ctx, "setSpop", 3)
Expect(sPopN.Err()).NotTo(HaveOccurred())
Expect(sPopN.Val()).To(HaveLen(3))
/*
sMembers := client.SMembers(ctx, "setSpop")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(HaveLen(2))
*/


})

Expand Down

0 comments on commit 1e7f205

Please sign in to comment.