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: set command sinterstore and go ut #115

Merged
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 @@ -79,6 +79,7 @@ const std::string kCmdNameSAdd = "sadd";
const std::string kCmdNameSUnionStore = "sunionstore";
const std::string kCmdNameSInter = "sinter";
const std::string kCmdNameSRem = "srem";
const std::string kCmdNameSInterStore = "sinterstore";
const std::string kCmdNameSUnion = "sunion";
const std::string kCmdNameLPush = "lpush";
const std::string kCmdNameRPush = "rpush";
Expand Down
24 changes: 22 additions & 2 deletions src/cmd_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ bool SIsMemberCmd::DoInitial(PClient* client) {
return true;
}
void SIsMemberCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
int32_t reply_Num = 0; // only change to 1 if ismember . key not exist it is 0
PSTORE.GetBackend()->SIsmember(client->Key(), client->argv_[2], &reply_Num);

Expand All @@ -37,7 +36,6 @@ bool SAddCmd::DoInitial(PClient* client) {
// Integer reply: the number of elements that were added to the set,
// not including all the elements already present in the set.
void SAddCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
const std::vector<std::string> members(client->argv_.begin() + 2, client->argv_.end());
int32_t ret = 0;
storage::Status s = PSTORE.GetBackend()->SAdd(client->Key(), members, &ret);
Expand Down Expand Up @@ -82,6 +80,7 @@ void SInterCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend()->SInter(client->Keys(), &res_vt);
if (!s.ok()) {
client->SetRes(CmdRes::kErrOther, "sinter cmd error");
return;
}
client->AppendStringVector(res_vt);
}
Expand Down Expand Up @@ -121,4 +120,25 @@ void SUnionCmd::DoCmd(PClient* client) {
}
client->AppendStringVector(res_vt);
}

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

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

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

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

class SInterStoreCmd : public BaseCmd {
public:
SInterStoreCmd(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 @@ -85,6 +85,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(SRem, -3);
ADD_COMMAND(SInter, -2);
ADD_COMMAND(SUnion, -2);
ADD_COMMAND(SInterStore, -3);

// 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 @@ -168,4 +168,28 @@ var _ = Describe("Set", Ordered, func() {
Expect(sInter.Err()).NotTo(HaveOccurred())
Expect(sInter.Val()).To(HaveLen(0))
})

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

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

sInterStore := client.SInterStore(ctx, "set", "set1", "set2")
Expect(sInterStore.Err()).NotTo(HaveOccurred())
Expect(sInterStore.Val()).To(Equal(int64(1)))

// sMembers := client.SMembers(ctx, "set") // After the smember command is developed, uncomment it to test command.
// Expect(sMembers.Err()).NotTo(HaveOccurred())
// Expect(sMembers.Val()).To(Equal([]string{"c"}))
})
})