Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: string cmd incr #59

Merged
merged 12 commits into from
Dec 19, 2023
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const std::string kCmdNameGetset = "getset";
const std::string kCmdNameMget = "mget";
const std::string kCmdNameMset = "mset";
const std::string kCmdNameBitCount = "bitcount";
const std::string kCmdNameIncr = "incr";

const std::string kCmdNameAuth = "auth";

Expand Down
29 changes: 29 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,33 @@ void BitCountCmd::DoCmd(PClient* client) {
client->AppendInteger(static_cast<int64_t>(count));
}

IncrCmd::IncrCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryString) {}

bool IncrCmd::DoInitial(pikiwidb::PClient *client) {
callme-taota marked this conversation as resolved.
Show resolved Hide resolved
client->SetKey(client->argv_[1]);
return true;
}

void IncrCmd::DoCmd(pikiwidb::PClient *client) {
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(),value,PType_string);
Centurybbx marked this conversation as resolved.
Show resolved Hide resolved
if(err == PError_notExist){
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
client->SetRes(CmdRes::kNotFound);
}

if(err != PError_ok){
client->SetRes(CmdRes::kErrOther);
}

if(value->encoding != PEncode_int){
client->SetRes(CmdRes::kOk);
callme-taota marked this conversation as resolved.
Show resolved Hide resolved
}

intptr_t oldVal = (intptr_t)value->value;
value->Reset((void*)(oldVal+1));
callme-taota marked this conversation as resolved.
Show resolved Hide resolved

client->AppendInteger(oldVal+1);
}

} // namespace pikiwidb
11 changes: 11 additions & 0 deletions src/cmd_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,15 @@ class BitCountCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

class IncrCmd : public BaseCmd {
public:
IncrCmd(const std::string &name,int16_t arity);
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved

protected:
bool DoInitial(PClient *client) override;

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

} // namespace pikiwidb
2 changes: 2 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ 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> incrPtr = std::make_unique<IncrCmd>(kCmdNameIncr, 2);
cmds_->insert(std::make_pair(kCmdNameIncr, std::move(incrPtr)));
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down
1 change: 0 additions & 1 deletion src/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const PCommandInfo PCommandTable::s_info[] = {
{"bitop", PAttr_write, -4, &bitop},
{"getbit", PAttr_read, 3, &getbit},
{"setbit", PAttr_write, 4, &setbit},
{"incr", PAttr_write, 2, &incr},
{"decr", PAttr_write, 2, &decr},
{"incrby", PAttr_write, 3, &incrby},
{"incrbyfloat", PAttr_write, 3, &incrbyfloat},
Expand Down