Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HSET supports to set multiple fields #274

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1012,20 +1012,6 @@ class CommandHGet : public Commander {
}
};

class CommandHSet : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int ret;
Redis::Hash hash_db(svr->storage_, conn->GetNamespace());
rocksdb::Status s = hash_db.Set(args_[1], args_[2], args_[3], &ret);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
*output = Redis::Integer(ret);
return Status::OK();
}
};

class CommandHSetNX : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Expand Down Expand Up @@ -1193,7 +1179,11 @@ class CommandHMSet : public Commander {
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
*output = Redis::SimpleString("OK");
if (GetAttributes()->name == "hset") {
*output = Redis::Integer(ret);
} else {
*output = Redis::SimpleString("OK");
}
return Status::OK();
}
};
Expand Down Expand Up @@ -4132,7 +4122,7 @@ CommandAttributes redisCommandTable[] = {
ADD_CMD("hget", 3, false, 1, 1, 1, CommandHGet),
ADD_CMD("hincrby", 4, true, 1, 1, 1, CommandHIncrBy),
ADD_CMD("hincrbyfloat", 4, true, 1, 1, 1, CommandHIncrByFloat),
ADD_CMD("hset", 4, true, 1, 1, 1, CommandHSet),
ADD_CMD("hset", -4, true, 1, 1, 1, CommandHMSet),
ADD_CMD("hsetnx", 4, true, 1, 1, 1, CommandHSetNX),
ADD_CMD("hdel", -3, true, 1, 1, 1, CommandHDel),
ADD_CMD("hstrlen", 3, false, 1, 1, 1, CommandHStrlen),
Expand Down
11 changes: 11 additions & 0 deletions tests/tcl/tests/unit/type/hash.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ start_server {tags {"hash"}} {
list [r hlen bighash]
} {1024}

test {HSET wrong number of args} {
catch {r hset hmsetmulti key1 val1 key2} err
format $err
} {*wrong number*}

test {HSET supports multiple fields} {
assert_equal "2" [r hset hmsetmulti key1 val1 key2 val2]
assert_equal "0" [r hset hmsetmulti key1 val1 key2 val2]
assert_equal "1" [r hset hmsetmulti key1 val1 key3 val3]
}

test {HGET against the small hash} {
set err {}
foreach k [array names smallhash *] {
Expand Down