Skip to content

Commit

Permalink
fix: the return string of hset is different with hmset
Browse files Browse the repository at this point in the history
  • Loading branch information
longfar-ncy committed Dec 3, 2023
1 parent dc245a3 commit 14d6c8d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down
13 changes: 11 additions & 2 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 14d6c8d

Please sign in to comment.