Skip to content

Commit

Permalink
feat: Add strlen,setex,psetex and setnx cmds (#39)
Browse files Browse the repository at this point in the history
* resolve conflicts

* resolve conflicts

* resolve conflicts

* variable init

* fix merge wrong

* resolve conflits

* replace with pstd time function

* code format
  • Loading branch information
super-Pan66 authored Dec 1, 2023
1 parent 21eb2d9 commit 5f8ee3b
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ namespace pikiwidb {
const std::string kCmdNameSet = "set";
const std::string kCmdNameGet = "get";
const std::string kCmdNameIncrby = "incrby";
const std::string kCmdNameStrlen = "strlen";
const std::string kCmdNameSetex = "setex";
const std::string kCmdNamePsetex = "psetex";
const std::string kCmdNameSetnx = "setnx";

// multi
const std::string kCmdNameMulti = "multi";
Expand Down
95 changes: 95 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "cmd_kv.h"
#include "pstd_string.h"
#include "pstd_util.h"
#include "store.h"

namespace pikiwidb {
Expand Down Expand Up @@ -215,6 +216,78 @@ void BitCountCmd::DoCmd(PClient* client) {
client->AppendInteger(static_cast<int64_t>(count));
}

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 = nullptr;
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], pstd::UnixMilliTimestamp() + 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], pstd::UnixMilliTimestamp() + msec);
client->SetRes(CmdRes::kOk);
}

IncrbyCmd::IncrbyCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {}

Expand Down Expand Up @@ -250,4 +323,26 @@ void IncrbyCmd::DoCmd(PClient* client) {
break;
}
}

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 = nullptr;
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
44 changes: 44 additions & 0 deletions src/cmd_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,50 @@ class SetCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

class StrlenCmd : public BaseCmd {
public:
StrlenCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
};

class SetexCmd : public BaseCmd {
public:
SetexCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
};

class PsetexCmd : public BaseCmd {
public:
PsetexCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
};

class SetnxCmd : public BaseCmd {
public:
SetnxCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
};

class AppendCmd : public BaseCmd {
public:
AppendCmd(const std::string &name, int16_t arity);
Expand Down
9 changes: 8 additions & 1 deletion src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ void CmdTableManager::InitCmdTable() {
cmds_->insert(std::make_pair(kCmdNameMset, std::move(msetPtr)));
std::unique_ptr<BaseCmd> bitcountPtr = std::make_unique<BitCountCmd>(kCmdNameBitCount, -2);
cmds_->insert(std::make_pair(kCmdNameBitCount, std::move(bitcountPtr)));

std::unique_ptr<BaseCmd> incrbyPtr = std::make_unique<IncrbyCmd>(kCmdNameIncrby, 3);
cmds_->insert(std::make_pair(kCmdNameIncrby, std::move(incrbyPtr)));
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 5f8ee3b

Please sign in to comment.