Skip to content

Commit

Permalink
fix rename and String2int error
Browse files Browse the repository at this point in the history
  • Loading branch information
hahahashen committed Oct 11, 2024
1 parent 2e13ab5 commit fcd70da
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
23 changes: 17 additions & 6 deletions src/cmd_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,21 +387,32 @@ 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]);
return true;
}

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<std::string> v;
v.emplace_back(client->Key());
PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Del(v);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/cmd_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/pstd/pstd_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ inline std::string Int2string(T val) {
template <std::integral T>
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;
}
}

Expand Down
14 changes: 7 additions & 7 deletions tests/unit/basic.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/type/hash.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down

0 comments on commit fcd70da

Please sign in to comment.