Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
super-Pan66 committed Nov 22, 2023
1 parent bba8f25 commit f58e61b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<int64_t>(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
8 changes: 8 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ void CmdTableManager::InitCmdTable() {
cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr)));
std::unique_ptr<BaseCmd> setPtr = std::make_unique<SetCmd>(kCmdNameSet, -3);
cmds_->insert(std::make_pair(kCmdNameSet, std::move(setPtr)));
std::unique_ptr<BaseCmd> strlenPtr = std::make_unique<StrlenCmd>(kCmdNameStrlen, 2);
cmds_->insert(std::make_pair(kCmdNameStrlen, std::move(strlenPtr)));
std::unique_ptr<BaseCmd> setexPtr = std::make_unique<SetexCmd>(kCmdNameSetex, 4);
cmds_->insert(std::make_pair(kCmdNameSetex, std::move(setexPtr)));
std::unique_ptr<BaseCmd> psetexPtr = std::make_unique<PsetexCmd>(kCmdNamePsetex, 4);
cmds_->insert(std::make_pair(kCmdNamePsetex, std::move(psetexPtr)));
std::unique_ptr<BaseCmd> setnxPtr = std::make_unique<SetnxCmd>(kCmdNameSetnx, 3);
cmds_->insert(std::make_pair(kCmdNameSetnx, std::move(setnxPtr)));
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down

0 comments on commit f58e61b

Please sign in to comment.