Skip to content

Commit

Permalink
feat: add hash command: hexists
Browse files Browse the repository at this point in the history
  • Loading branch information
longfar-ncy committed Mar 31, 2024
1 parent b7f075e commit fca01e8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const std::string kCmdNameHIncrbyFloat = "hincrbyfloat";
const std::string kCmdNameHSetNX = "hsetnx";
const std::string kCmdNameHIncrby = "hincrby";
const std::string kCmdNameHRandField = "hrandfield";
const std::string kCmdNameHExists = "hexists";

// set cmd
const std::string kCmdNameSIsMember = "sismember";
Expand Down
23 changes: 23 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,27 @@ void HRandFieldCmd::DoCmd(PClient* client) {
}
}

HExistsCmd::HExistsCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategoryHash) {}

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

void HExistsCmd::DoCmd(PClient* client) {
// parse arguments
auto& field = client->argv_[2];

// execute command
std::vector<std::string> res;
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HExists(client->Key(), field);
if (!s.ok() && !s.IsNotFound()) {
return client->SetRes(CmdRes::kErrOther, s.ToString());
}

// reply
client->AppendInteger(s.IsNotFound() ? 0 : 1);
}

} // namespace pikiwidb
11 changes: 11 additions & 0 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,15 @@ class HRandFieldCmd : public BaseCmd {
static constexpr std::string_view kWithValueString = "withvalues";
};

class HExistsCmd : public BaseCmd {
public:
HExistsCmd(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 @@ -92,6 +92,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(HSetNX, 4);
ADD_COMMAND(HIncrby, 4);
ADD_COMMAND(HRandField, -2);
ADD_COMMAND(HExists, 3);

// set
ADD_COMMAND(SIsMember, 3);
Expand Down
15 changes: 14 additions & 1 deletion tests/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ var _ = Describe("Hash", Ordered, func() {
hGet := client.HGet(ctx, "hash", "key")
Expect(hGet.Err()).NotTo(HaveOccurred())
Expect(hGet.Val()).To(Equal("hello"))
})
})

It("should HIncrBy", func() {
hSet := client.HSet(ctx, "hash", "key", "5")
Expand Down Expand Up @@ -331,4 +331,17 @@ var _ = Describe("Hash", Ordered, func() {
res1 := client.HRandField(ctx, "not_exist_key", 1).Val()
Expect(len(res1)).To(Equal(0))
})

It("should HExists", func() {
hSet := client.HSet(ctx, "hash", "key", "hello")
Expect(hSet.Err()).NotTo(HaveOccurred())

hExists := client.HExists(ctx, "hash", "key")
Expect(hExists.Err()).NotTo(HaveOccurred())
Expect(hExists.Val()).To(Equal(true))

hExists = client.HExists(ctx, "hash", "key1")
Expect(hExists.Err()).NotTo(HaveOccurred())
Expect(hExists.Val()).To(Equal(false))
})
})

0 comments on commit fca01e8

Please sign in to comment.