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:add rpoplpush command #317

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -130,6 +130,7 @@ const std::string kCmdNameLSet = "lset";
const std::string kCmdNameLInsert = "linsert";
const std::string kCmdNameLIndex = "lindex";
const std::string kCmdNameLLen = "llen";
const std::string kCmdNameRPoplpush = "rpoplpush";

// zset cmd
const std::string kCmdNameZAdd = "zadd";
Expand Down
26 changes: 26 additions & 0 deletions src/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ void LPushxCmd::DoCmd(PClient* client) {
}
}

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

bool RPoplpushCmd::DoInitial(PClient* client) {
if(((arity_ > 0 && client->argv_.size() != arity_) || (arity_ < 0 && client->argv_.size() < -arity_))) {
client->SetRes(CmdRes::kWrongNum, kCmdNameRPoplpush);
return false;
}
source_ = client->argv_[1];
receiver_ = client->argv_[2];
return true;
}

void RPoplpushCmd::DoCmd(PClient* client) {
std::string value;
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPoplpush(source_, receiver_, &value);
if (s.ok()) {
client->AppendString(value);
} else if (s.IsNotFound()) {
client->AppendStringLen(-1);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
}
}

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

Expand Down
14 changes: 14 additions & 0 deletions src/cmd_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ class RPushxCmd : public BaseCmd {
void DoCmd(PClient* client) override;
};

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

protected:
bool DoInitial(PClient* client) override;

private:
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
std::string source_;
std::string receiver_;
std::string value_poped_from_source_;
void DoCmd(PClient* client) override;
};

class LPopCmd : public BaseCmd {
public:
LPopCmd(const std::string& name, int16_t arity);
Expand Down
1 change: 1 addition & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(LPop, 2);
ADD_COMMAND(LIndex, 3);
ADD_COMMAND(LLen, 2);
ADD_COMMAND(RPoplpush, 3);

// zset
ADD_COMMAND(ZAdd, -4);
Expand Down
25 changes: 25 additions & 0 deletions tests/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ var _ = Describe("List", Ordered, func() {
Expect(del.Err()).NotTo(HaveOccurred())
})

It("Cmd RPopLPush", func() {
rPush := client.RPush(ctx, DefaultKey, s2s["key_1"])
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, DefaultKey, s2s["key_2"])
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, DefaultKey, s2s["key_3"])
Expect(rPush.Err()).NotTo(HaveOccurred())

rPopLPush := client.RPopLPush(ctx, DefaultKey, "destination_key")
Expect(rPopLPush.Err()).NotTo(HaveOccurred())
Expect(rPopLPush.Val()).To(Equal(s2s["key_3"]))

lRange := client.LRange(ctx, DefaultKey, 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{s2s["key_1"], s2s["key_2"]}))

lRangeDest := client.LRange(ctx, "destination_key", 0, -1)
Expect(lRangeDest.Err()).NotTo(HaveOccurred())
Expect(lRangeDest.Val()).To(Equal([]string{s2s["key_3"]}))

//del
del := client.Del(ctx, DefaultKey)
Expect(del.Err()).NotTo(HaveOccurred())
})

It("Cmd LRem", func() {
rPush := client.RPush(ctx, DefaultKey, s2s["key_1"])
Expect(rPush.Err()).NotTo(HaveOccurred())
Expand Down
Loading