From a919bf7eae1d43a7a9ddfbf2ba528e47420ac932 Mon Sep 17 00:00:00 2001 From: longfar Date: Tue, 16 Jan 2024 09:26:33 +0800 Subject: [PATCH 1/3] feat: add command hdel --- src/base_cmd.h | 3 ++- src/cmd_hash.cc | 19 +++++++++++++++++++ src/cmd_hash.h | 11 +++++++++++ src/cmd_table_manager.cc | 3 ++- 4 files changed, 34 insertions(+), 2 deletions(-) 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 From 3f22e98fb6b1220ac5a5dbc39aa186c12ca60f7d Mon Sep 17 00:00:00 2001 From: longfar Date: Tue, 16 Jan 2024 09:47:55 +0800 Subject: [PATCH 2/3] test: add test for hset & hget & hdel --- tests/hash_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/tests/hash_test.go b/tests/hash_test.go index 60a65a248..4e72401ca 100644 --- a/tests/hash_test.go +++ b/tests/hash_test.go @@ -63,8 +63,60 @@ 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("should HDel", func() { + hSet := client.HSet(ctx, "hash", "key", "hello") + Expect(hSet.Err()).NotTo(HaveOccurred()) + + hDel := client.HDel(ctx, "hash", "key") + Expect(hDel.Err()).NotTo(HaveOccurred()) + Expect(hDel.Val()).To(Equal(int64(1))) + + hDel = client.HDel(ctx, "hash", "key") + Expect(hDel.Err()).NotTo(HaveOccurred()) + Expect(hDel.Val()).To(Equal(int64(0))) + }) + }) From 852706dde4add2d9c753e390ba96e3dbbdd85866 Mon Sep 17 00:00:00 2001 From: longfar Date: Tue, 16 Jan 2024 11:36:04 +0800 Subject: [PATCH 3/3] test: enhance the test for hdel --- tests/hash_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/hash_test.go b/tests/hash_test.go index 4e72401ca..7c36cff5d 100644 --- a/tests/hash_test.go +++ b/tests/hash_test.go @@ -106,17 +106,29 @@ var _ = Describe("Hash", Ordered, func() { Expect(keys).To(ConsistOf([]string{"key1", "key2"})) }) - It("should HDel", func() { - hSet := client.HSet(ctx, "hash", "key", "hello") + 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, "hash", "key") + hDel := client.HDel(ctx, testKey, "key") Expect(hDel.Err()).NotTo(HaveOccurred()) Expect(hDel.Val()).To(Equal(int64(1))) - hDel = client.HDel(ctx, "hash", "key") + 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))) }) })