diff --git a/src/base_cmd.h b/src/base_cmd.h index 4284a06ac..a94d829ef 100644 --- a/src/base_cmd.h +++ b/src/base_cmd.h @@ -63,6 +63,7 @@ const std::string kCmdNameAuth = "auth"; // hash cmd const std::string kCmdNameHSet = "hset"; const std::string kCmdNameHGet = "hget"; +const std::string kCmdNameHDel = "hdel"; const std::string kCmdNameHMSet = "hmset"; const std::string kCmdNameHMGet = "hmget"; const std::string kCmdNameHGetAll = "hgetall"; @@ -275,4 +276,4 @@ class BaseCmdGroup : public BaseCmd { private: std::map> subCmds_; }; -} // namespace pikiwidb \ No newline at end of file +} // namespace pikiwidb diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 7f231150a..9368163a6 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -71,6 +71,25 @@ void HGetCmd::DoCmd(PClient* client) { } } +HDelCmd::HDelCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryHash) {} + +bool HDelCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + return true; +} + +void HDelCmd::DoCmd(PClient* client) { + int32_t res{}; + std::vector fields(client->argv_.begin() + 2, client->argv_.end()); + auto s = PSTORE.GetBackend()->HDel(client->Key(), fields, &res); + if (!s.ok() && !s.IsNotFound()) { + client->SetRes(CmdRes::kErrOther, s.ToString()); + return; + } + client->AppendInteger(res); +} + HMSetCmd::HMSetCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryHash) {} diff --git a/src/cmd_hash.h b/src/cmd_hash.h index 27ac64d82..198691014 100644 --- a/src/cmd_hash.h +++ b/src/cmd_hash.h @@ -33,6 +33,17 @@ class HGetCmd : public BaseCmd { void DoCmd(PClient *client) override; }; +class HDelCmd : public BaseCmd { + public: + HDelCmd(const std::string &name, int16_t arity); + + protected: + bool DoInitial(PClient *client) override; + + private: + void DoCmd(PClient *client) override; +}; + class HMSetCmd : public BaseCmd { public: HMSetCmd(const std::string &name, int16_t arity); diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index e4a9e5152..da6eadb59 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -67,6 +67,7 @@ void CmdTableManager::InitCmdTable() { // hash ADD_COMMAND(HSet, -4); ADD_COMMAND(HGet, 3); + ADD_COMMAND(HDel, -3); ADD_COMMAND(HMSet, -4); ADD_COMMAND(HMGet, -3); ADD_COMMAND(HGetAll, 2); @@ -107,4 +108,4 @@ bool CmdTableManager::CmdExist(const std::string& cmd) const { } uint32_t CmdTableManager::GetCmdId() { return ++cmdId_; } -} // namespace pikiwidb \ No newline at end of file +} // namespace pikiwidb diff --git a/tests/hash_test.go b/tests/hash_test.go index 60a65a248..7c36cff5d 100644 --- a/tests/hash_test.go +++ b/tests/hash_test.go @@ -63,8 +63,72 @@ var _ = Describe("Hash", Ordered, func() { }) //TODO(dingxiaoshuai) Add more test cases. - It("Cmd HSET", func() { - log.Println("Cmd HSET Begin") - Expect(client.HSet(ctx, "myhash", "one").Val()).NotTo(Equal("FooBar")) + It("HSet & HGet", func() { + hSet := client.HSet(ctx, "hash", "key", "hello") + Expect(hSet.Err()).NotTo(HaveOccurred()) + + hGet := client.HGet(ctx, "hash", "key") + Expect(hGet.Err()).NotTo(HaveOccurred()) + Expect(hGet.Val()).To(Equal("hello")) + + hGet = client.HGet(ctx, "hash", "key1") + Expect(hGet.Err()).To(Equal(redis.Nil)) + Expect(hGet.Val()).To(Equal("")) + }) + + It("HGet & HSet 2", func() { + testKey := "hget-hset2" + _, err := client.Del(ctx, testKey).Result() + Expect(err).NotTo(HaveOccurred()) + + ok, err := client.HSet(ctx, testKey, map[string]interface{}{ + "key1": "hello1", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(ok).To(Equal(int64(1))) + + ok, err = client.HSet(ctx, testKey, map[string]interface{}{ + "key2": "hello2", + }).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(ok).To(Equal(int64(1))) + + v, err := client.HGet(ctx, testKey, "key1").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(v).To(Equal("hello1")) + + v, err = client.HGet(ctx, testKey, "key2").Result() + Expect(err).NotTo(HaveOccurred()) + Expect(v).To(Equal("hello2")) + + keys, err := client.HKeys(ctx, testKey).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(keys).To(ConsistOf([]string{"key1", "key2"})) + }) + + It("HDel", func() { + testKey := "hdel" + hSet := client.HSet(ctx, testKey, "key", "hello") + Expect(hSet.Err()).NotTo(HaveOccurred()) + hSet = client.HSet(ctx, testKey, "key", "hello") + Expect(hSet.Err()).NotTo(HaveOccurred()) + Expect(hSet.Val()).To(Equal(int64(0))) + + hDel := client.HDel(ctx, testKey, "key") + Expect(hDel.Err()).NotTo(HaveOccurred()) + Expect(hDel.Val()).To(Equal(int64(1))) + + hDel = client.HDel(ctx, testKey, "key") + Expect(hDel.Err()).NotTo(HaveOccurred()) + Expect(hDel.Val()).To(Equal(int64(0))) + + hSet = client.HSet(ctx, testKey, "key", "hello") + Expect(hSet.Err()).NotTo(HaveOccurred()) + Expect(hSet.Val()).To(Equal(int64(1))) + + hDel = client.HDel(ctx, testKey, "key") + Expect(hDel.Err()).NotTo(HaveOccurred()) + Expect(hDel.Val()).To(Equal(int64(1))) }) + })