From fcd70da8b53830f01ee12ed15677fcf511e5e996 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Fri, 11 Oct 2024 14:42:20 +0800 Subject: [PATCH] fix rename and String2int error --- src/cmd_keys.cc | 23 +++++++++++++++++------ src/cmd_keys.h | 3 +++ src/pstd/pstd_string.h | 6 +++--- tests/unit/basic.tcl | 14 +++++++------- tests/unit/type/hash.tcl | 18 +++++++++--------- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc index 2b336337b..973ad60e2 100644 --- a/src/cmd_keys.cc +++ b/src/cmd_keys.cc @@ -387,7 +387,8 @@ void PttlCmd::DoThroughDB(PClient* client) { } RenameCmd::RenameCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool RenameCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -395,13 +396,23 @@ bool RenameCmd::DoInitial(PClient* client) { } void RenameCmd::DoCmd(PClient* client) { - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Rename(client->Key(), client->argv_[2]); - if (s.ok()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Rename(client->Key(), client->argv_[2]); + if (s_.ok()) { client->SetRes(CmdRes::kOK); - } else if (s.IsNotFound()) { - client->SetRes(CmdRes::kNotFound, s.ToString()); + } else if (s_.IsNotFound()) { + client->SetRes(CmdRes::kNotFound, s_.ToString()); } else { - client->SetRes(CmdRes::kErrOther, s.ToString()); + client->SetRes(CmdRes::kErrOther, s_.ToString()); + } +} + +void RenameCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void RenameCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + std::vector v; + v.emplace_back(client->Key()); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Del(v); } } diff --git a/src/cmd_keys.h b/src/cmd_keys.h index a671d710f..cfb4f5246 100644 --- a/src/cmd_keys.h +++ b/src/cmd_keys.h @@ -173,7 +173,10 @@ class RenameCmd : public BaseCmd { bool DoInitial(PClient* client) override; private: + rocksdb::Status s_; void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; }; class RenameNXCmd : public BaseCmd { diff --git a/src/pstd/pstd_string.h b/src/pstd/pstd_string.h index 87dde170a..3431d97a9 100755 --- a/src/pstd/pstd_string.h +++ b/src/pstd/pstd_string.h @@ -60,10 +60,10 @@ inline std::string Int2string(T val) { template int String2int(const char* s, size_t slen, T* val) { auto [ptr, ec] = std::from_chars(s, s + slen, *val); - if (ec != std::errc()) { - return 0; - } else { + if (ec == std::errc::no_error) { return 1; + } else { + return 0; } } diff --git a/tests/unit/basic.tcl b/tests/unit/basic.tcl index e73d02bc9..853742d07 100644 --- a/tests/unit/basic.tcl +++ b/tests/unit/basic.tcl @@ -331,13 +331,13 @@ start_server {tags {"basic"}} { r exists mykey } {0} - # test {RENAME against already existing key} { - # r set mykey a - # r set mykey2 b - # r rename mykey2 mykey - # set res [r get mykey] - # append res [r exists mykey2] - # } {b0} + test {RENAME against already existing key} { + r set mykey a + r set mykey2 b + r rename mykey2 mykey + set res [r get mykey] + append res [r exists mykey2] + } {b0} test {RENAMENX basic usage} { r del mykey diff --git a/tests/unit/type/hash.tcl b/tests/unit/type/hash.tcl index 02203d577..20199d82c 100644 --- a/tests/unit/type/hash.tcl +++ b/tests/unit/type/hash.tcl @@ -312,15 +312,15 @@ start_server {tags {"hash"}} { lappend rv [string match "ERR*not an integer*" $bigerr] } {1 1} - # test {HINCRBY fails against hash value with spaces (right)} { - # r hset smallhash str "11 " - # r hset bighash str "11 " - # catch {r hincrby smallhash str 1} smallerr - # catch {r hincrby smallhash str 1} bigerr - # set rv {} - # lappend rv [string match "ERR*not an integer*" $smallerr] - # lappend rv [string match "ERR*not an integer*" $bigerr] - # } {1 1} + test {HINCRBY fails against hash value with spaces (right)} { + r hset smallhash str "11 " + r hset bighash str "11 " + catch {r hincrby smallhash str 1} smallerr + catch {r hincrby smallhash str 1} bigerr + set rv {} + lappend rv [string match "ERR*not an integer*" $smallerr] + lappend rv [string match "ERR*not an integer*" $bigerr] + } {1 1} test {HINCRBY can detect overflows} { set e {}