Skip to content

Commit

Permalink
feat:add Rpop cmd & go ut (#147)
Browse files Browse the repository at this point in the history
* complete string appendcmd getsetcmd

* complete string mset & mget cmd

* fixed some bugs

* feat:update code style

* update code style

* add rpop cmd & go ut
  • Loading branch information
hero-heng authored Jan 27, 2024
1 parent 7d7ef41 commit a98f7bf
Show file tree
Hide file tree
Showing 5 changed files with 56 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 @@ -81,8 +81,11 @@ const std::string kCmdNameSInter = "sinter";
const std::string kCmdNameSRem = "srem";
const std::string kCmdNameSInterStore = "sinterstore";
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 @@ -90,6 +90,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")))
})
})

0 comments on commit a98f7bf

Please sign in to comment.