Skip to content

Commit

Permalink
feat: add command hdel and tests (#131)
Browse files Browse the repository at this point in the history
* feat: add command hdel

* test: add test for hset & hget & hdel

* test: enhance the test for hdel
  • Loading branch information
longfar-ncy authored Jan 18, 2024
1 parent 386860d commit 596cccd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -275,4 +276,4 @@ class BaseCmdGroup : public BaseCmd {
private:
std::map<std::string, std::unique_ptr<BaseCmd>> subCmds_;
};
} // namespace pikiwidb
} // namespace pikiwidb
19 changes: 19 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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) {}

Expand Down
11 changes: 11 additions & 0 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -107,4 +108,4 @@ bool CmdTableManager::CmdExist(const std::string& cmd) const {
}

uint32_t CmdTableManager::GetCmdId() { return ++cmdId_; }
} // namespace pikiwidb
} // namespace pikiwidb
70 changes: 67 additions & 3 deletions tests/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})

})

0 comments on commit 596cccd

Please sign in to comment.