From f58e61bd015a593965d346a2ae08be2484a79de6 Mon Sep 17 00:00:00 2001 From: super-Pan66 <1719739132@qq.com> Date: Wed, 22 Nov 2023 20:24:21 +0800 Subject: [PATCH] fix bug --- src/cmd_kv.cc | 98 ++++++++++++++++++++++++++++++++++++++++ src/cmd_table_manager.cc | 8 ++++ 2 files changed, 106 insertions(+) diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 9a626d157..a649baf71 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -6,8 +6,10 @@ */ #include "cmd_kv.h" +#include "pstd_string.h" #include "store.h" + namespace pikiwidb { GetCmd::GetCmd(const std::string& name, int16_t arity) @@ -48,4 +50,100 @@ void SetCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kOk); } +StrlenCmd::StrlenCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryString) {} + +bool StrlenCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + return true; +} + +void StrlenCmd::DoCmd(PClient* client) { + PObject* value; + PError err = PSTORE.GetValueByType(client->Key(), value, PType_string); + + switch (err){ + case PError_ok: + { + auto str = GetDecodedString(value); + size_t len = str->size(); + client->AppendInteger(static_cast(len)); + break; + } + case PError_notExist: + { + client->AppendInteger(0); + break; + } + default: + { + client->SetRes(CmdRes::kErrOther, "error other"); + break; + } + } +} + +SetexCmd::SetexCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString){} + +bool SetexCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + int64_t sec = 0; + if (pstd::String2int(client->argv_[2], &sec) == 0) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } + return true; +} + +void SetexCmd::DoCmd(PClient* client) { + PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[3])); + int64_t sec = 0; + pstd::String2int(client->argv_[2], &sec); + PSTORE.SetExpire(client->argv_[1], ::Now() + sec * 1000); + client->SetRes(CmdRes::kOk); +} + +PsetexCmd::PsetexCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString){} + +bool PsetexCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + int64_t msec = 0; + if (pstd::String2int(client->argv_[2], &msec) == 0) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } + return true; +} + +void PsetexCmd::DoCmd(PClient* client) { + PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[3])); + int64_t msec = 0; + pstd::String2int(client->argv_[2], &msec); + PSTORE.SetExpire(client->argv_[1], ::Now() + msec); + client->SetRes(CmdRes::kOk); +} + +SetnxCmd::SetnxCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString){} + +bool SetnxCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + return true; +} + +void SetnxCmd::DoCmd(PClient* client) { + int iSuccess = 1; + PObject* value; + PError err = PSTORE.GetValue(client->argv_[1], value); + if (err == PError_notExist) { + PSTORE.ClearExpire(client->argv_[1]); // clear key's old ttl + PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2])); + client->AppendInteger(iSuccess); + }else{ + client->AppendInteger(!iSuccess); + } +} + } // namespace pikiwidb \ No newline at end of file diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index 36f4e845e..53015217e 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -32,6 +32,14 @@ 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 strlenPtr = std::make_unique(kCmdNameStrlen, 2); + cmds_->insert(std::make_pair(kCmdNameStrlen, std::move(strlenPtr))); + std::unique_ptr setexPtr = std::make_unique(kCmdNameSetex, 4); + cmds_->insert(std::make_pair(kCmdNameSetex, std::move(setexPtr))); + std::unique_ptr psetexPtr = std::make_unique(kCmdNamePsetex, 4); + cmds_->insert(std::make_pair(kCmdNamePsetex, std::move(psetexPtr))); + std::unique_ptr setnxPtr = std::make_unique(kCmdNameSetnx, 3); + cmds_->insert(std::make_pair(kCmdNameSetnx, std::move(setnxPtr))); } std::pair CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {