Skip to content

Commit

Permalink
No Edit
Browse files Browse the repository at this point in the history
  • Loading branch information
brother-jin committed Feb 8, 2025
1 parent fceda27 commit aeb41ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/pika_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class HSetCmd : public Cmd {
private:
std::string key_, field_, value_;
std::vector<std::string> fields_;
std::vector<storage::FieldValue> vss_;
std::vector<storage::FieldValue> fields_values_;
void DoInitial() override;
rocksdb::Status s_;
};
Expand Down
11 changes: 6 additions & 5 deletions src/pika_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void HSetCmd::DoInitial() {

else if (argv_.size() > 4 && argv_.size() % 2 == 0) {
for (; index < argc; index += 2) {
vss_.push_back({argv_[index], argv_[index + 1]});
fields_values_.push_back({argv_[index], argv_[index + 1]});
}
} else {
res_.SetRes(CmdRes::kWrongNum, kCmdNameHSet);
Expand All @@ -81,9 +81,9 @@ void HSetCmd::Do() {
res_.SetRes(CmdRes::kErrOther, s_.ToString());
}
} else if (argv_.size() > 4 && argv_.size() % 2 == 0) {
s_ = db_->storage()->HMSet(key_, vss_);
s_ = db_->storage()->HMSet(key_, fields_values_);
if (s_.ok()) {
res_.AppendContent(":" + std::to_string(vss_.size()));
res_.AppendContent(":" + std::to_string(fields_values_.size()));
AddSlotKey("h", key_, db_);
} else {
res_.SetRes(CmdRes::kErrOther, s_.ToString());
Expand All @@ -97,11 +97,12 @@ void HSetCmd::DoThroughDB() {
}

void HSetCmd::DoUpdateCache() {
std::string CachePrefixKeyH = PCacheKeyPrefixH + key_;
if (argv_.size() == 4) {
db_->cache()->HSetIfKeyExist(key_, field_, value_);
db_->cache()->HSetIfKeyExist(CachePrefixKeyH, field_, value_);
}
else if (argv_.size() > 4 && argv_.size() % 2 == 0) {
db_->cache()->HMSet(key_, vss_);
db_->cache()->HMSet(CachePrefixKeyH, fields_values_);
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/type/hash.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ start_server {tags {"hash"}} {
set _ $rv
} {{} {}}

test {HSET in update and insert mode} {
set rv {}
set k [lindex [array names smallhash *] 0]
lappend rv [r hset smallhash $k newval1]
set smallhash($k) newval1
lappend rv [r hget smallhash $k]
lappend rv [r hset smallhash __foobar123__ newval]
set k [lindex [array names bighash *] 0]
lappend rv [r hset bighash $k newval2]
set bighash($k) newval2
lappend rv [r hget bighash $k]
lappend rv [r hset bighash __foobar123__ newval]
lappend rv [r hdel smallhash __foobar123__]
lappend rv [r hdel bighash __foobar123__]
set _ $rv
} {0 newval1 1 0 newval2 1 1 1}

test {HSETNX target key missing - small hash} {
r hsetnx smallhash __123123123__ foo
r hget smallhash __123123123__
Expand Down

0 comments on commit aeb41ff

Please sign in to comment.