From 14d6c8db4056b97316e3008f32bd5311318df109 Mon Sep 17 00:00:00 2001 From: longfar Date: Mon, 4 Dec 2023 00:03:23 +0800 Subject: [PATCH] fix: the return string of hset is different with hmset --- src/cmd_hash.cc | 34 ++++++++++++++++++++++++++++++++++ src/cmd_hash.h | 13 +++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/cmd_hash.cc b/src/cmd_hash.cc index 065349c6d..45704fe26 100644 --- a/src/cmd_hash.cc +++ b/src/cmd_hash.cc @@ -21,6 +21,40 @@ static inline PHash::iterator setHashForce(PHash& hash, const PString& key, cons return it; } +HSetCmd::HSetCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryHash) {} + +bool HSetCmd::DoInitial(PClient* client) { + if (client->argv_.size() % 2 != 0) { + client->SetRes(CmdRes::kWrongNum, kCmdNameHSet); + return false; + } + client->SetKey(client->argv_[1]); + return true; +} + +void HSetCmd::DoCmd(PClient* client) { + PObject* value = nullptr; + UnboundedBuffer reply; + PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash); + if (err != PError_ok && err != PError_notExist) { + ReplyError(err, &reply); + client->SetRes(CmdRes::kSyntaxErr, "hset cmd error"); + return; + } + if (err == PError_notExist) { + value = PSTORE.SetValue(client->Key(), PObject::CreateHash()); + } + + auto hash = value->CastHash(); + for (size_t i = 2; i < client->argv_.size(); i += 2) { + setHashForce(*hash, client->argv_[i], client->argv_[i + 1]); + } + auto argc = client->argv_.size() / 2 - 1; + FormatInt(argc, &reply); + client->AppendStringRaw(reply.ReadAddr()); +} + HGetCmd::HGetCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryHash) {} diff --git a/src/cmd_hash.h b/src/cmd_hash.h index bb44839a8..6ef91da34 100644 --- a/src/cmd_hash.h +++ b/src/cmd_hash.h @@ -11,6 +11,17 @@ namespace pikiwidb { +class HSetCmd : public BaseCmd { + public: + HSetCmd(const std::string &name, int16_t arity); + + protected: + bool DoInitial(PClient *client) override; + + private: + void DoCmd(PClient *client) override; +}; + class HGetCmd : public BaseCmd { public: HGetCmd(const std::string &name, int16_t arity); @@ -33,8 +44,6 @@ class HMSetCmd : public BaseCmd { void DoCmd(PClient *client) override; }; -using HSetCmd = HMSetCmd; - class HMGetCmd : public BaseCmd { public: HMGetCmd(const std::string &name, int16_t arity);