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 Rpop cmd & go ut #147

Merged
merged 11 commits into from
Jan 27, 2024
3 changes: 3 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ const std::string kCmdNameSUnionStore = "sunionstore";
const std::string kCmdNameSInter = "sinter";
const std::string kCmdNameSRem = "srem";
const std::string kCmdNameSUnion = "sunion";

// list cmd
const std::string kCmdNameLPush = "lpush";
const std::string kCmdNameRPush = "rpush";
const std::string kCmdNameRPop = "rpop";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
20 changes: 20 additions & 0 deletions src/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ void RPushCmd::DoCmd(PClient* client) {
}
}

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

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

void RPopCmd::DoCmd(PClient* client) {
std::vector<std::string> elements;
storage::Status s = PSTORE.GetBackend()->RPop(client->Key(), 1, &elements);
if (s.ok()) {
client->AppendString(elements[0]);
} else if (s.IsNotFound()) {
client->AppendStringLen(-1);
} else {
client->SetRes(CmdRes::kSyntaxErr, "rpop cmd error");
}
}

} // namespace pikiwidb
12 changes: 12 additions & 0 deletions src/cmd_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ class RPushCmd : public BaseCmd {
private:
void DoCmd(PClient* client) override;
};

class RPopCmd : public BaseCmd {
public:
RPopCmd(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 @@ -89,6 +89,7 @@ void CmdTableManager::InitCmdTable() {
// list
ADD_COMMAND(LPush, -3);
ADD_COMMAND(RPush, -3);
ADD_COMMAND(RPop, 2);
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
20 changes: 20 additions & 0 deletions tests/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,24 @@ var _ = Describe("List", Ordered, func() {

// Expect(client.LRange(ctx,"mylistRPUSH",0,-1).Val()).To(Equal([]string{"hello", "world"})) //After the LRange command is developed, uncomment it to test LRange command.
})

It("should RPop", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())

rPop := client.RPop(ctx, "list")
Expect(rPop.Err()).NotTo(HaveOccurred())
Expect(rPop.Val()).To(Equal("three"))

//lRange := client.LRange(ctx, "list", 0, -1)
//Expect(lRange.Err()).NotTo(HaveOccurred())
//Expect(lRange.Val()).To(Equal([]string{"one", "two"}))

err := client.Do(ctx, "RPOP", "list", 1, 2).Err()
Expect(err).To(MatchError(ContainSubstring("ERR wrong number of arguments for 'rpop' command")))
})
})
Loading