diff --git a/src/client.h b/src/client.h index 81ea37da0..c1311868e 100644 --- a/src/client.h +++ b/src/client.h @@ -181,7 +181,6 @@ class PClient : public std::enable_shared_from_this, public CmdRes { // e.g:["set","key","value"] std::span argv_; std::vector keys_; - std::vector> kvs_; private: std::shared_ptr getTcpConnection() const { return tcp_connection_.lock(); } diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 2a1f2a974..12c38eb8d 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -65,16 +65,16 @@ void AppendCmd::DoCmd(PClient* client) { PSTORE.ClearExpire(client->argv_[1]); // clear key's old ttl PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2])); client->AppendInteger(static_cast(client->argv_[2].size())); - } else { // append string - auto str = GetDecodedString(value); - std::string old_value(str->c_str(), str->size()); - std::string new_value = old_value + client->argv_[2]; - PSTORE.SetValue(client->argv_[1], PObject::CreateString(new_value)); - client->AppendInteger(static_cast(new_value.size())); + } else { + client->SetRes(CmdRes::kErrOther, "append cmd error"); } - } else { - client->SetRes(CmdRes::kErrOther, "append cmd error"); + return; } + auto str = GetDecodedString(value); + std::string old_value(str->c_str(), str->size()); + std::string new_value = old_value + client->argv_[2]; + PSTORE.SetValue(client->argv_[1], PObject::CreateString(new_value)); + client->AppendInteger(static_cast(new_value.size())); } GetsetCmd::GetsetCmd(const std::string& name, int16_t arity) @@ -93,15 +93,15 @@ void GetsetCmd::DoCmd(PClient* client) { PSTORE.ClearExpire(client->argv_[1]); // clear key's old ttl PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2])); client->AppendString(""); - } else { // set new value - auto str = GetDecodedString(old_value); - std::string ret_value(str->c_str(), str->size()); - PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2])); - client->AppendString(ret_value); + } else { + client->SetRes(CmdRes::kErrOther, "getset cmd error"); } - } else { - client->SetRes(CmdRes::kErrOther, "getset cmd error"); + return; } + auto str = GetDecodedString(old_value); + std::string ret_value(str->c_str(), str->size()); + PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2])); + client->AppendString(ret_value); } MgetCmd::MgetCmd(const std::string& name, int16_t arity) @@ -121,7 +121,7 @@ void MgetCmd::DoCmd(PClient* client) { PObject* value; PError err = PSTORE.GetValueByType(k, value, PType_string); if (err == PError_notExist) { - client->AppendContent("$-1"); + client->AppendStringLen(-1); } else { auto str = GetDecodedString(value); std::string reply(str->c_str(), str->size()); @@ -139,18 +139,19 @@ bool MSetCmd::DoInitial(PClient* client) { client->SetRes(CmdRes::kWrongNum, kCmdNameMset); return false; } - client->kvs_.clear(); + client->keys_.clear(); for (size_t index = 1; index != argcSize; index += 2) { - client->kvs_.emplace_back(client->argv_[index], client->argv_[index + 1]); + client->keys_.emplace_back(client->argv_[index]); } return true; } void MSetCmd::DoCmd(PClient* client) { - std::vector>::const_iterator it; - for (it = client->kvs_.begin(); it != client->kvs_.end(); it++) { - PSTORE.ClearExpire(it->first); // clear key's old ttl - PSTORE.SetValue(it->first, PObject::CreateString(it->second)); + int valueIndex = 2; + for (const auto& it : client->keys_) { + PSTORE.ClearExpire(it); // clear key's old ttl + PSTORE.SetValue(it, PObject::CreateString(client->argv_[valueIndex])); + valueIndex += 2; } client->SetRes(CmdRes::kOk); } diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index c5f024d6c..0a03d54d6 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -32,7 +32,7 @@ void CmdTableManager::InitCmdTable() { cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr))); std::unique_ptr setPtr = std::make_unique(kCmdNameSet, -3); cmds_->insert(std::make_pair(kCmdNameSet, std::move(setPtr))); - std::unique_ptr appendPtr = std::make_unique(kCmdNameAppend, -3); + std::unique_ptr appendPtr = std::make_unique(kCmdNameAppend, 3); cmds_->insert(std::make_pair(kCmdNameAppend, std::move(appendPtr))); std::unique_ptr getsetPtr = std::make_unique(kCmdNameGetset, 3); cmds_->insert(std::make_pair(kCmdNameGetset, std::move(getsetPtr)));