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 @@ -42,6 +42,7 @@ const std::string kCmdNamePSetEx = "psetex";
const std::string kCmdNameBitOp = "bitop";
const std::string kCmdNameGetBit = "getbit";
const std::string kCmdNameBitCount = "bitcount";
const std::string kCmdNameIncr = "incr";

// multi
const std::string kCmdNameMulti = "multi";
Expand Down
33 changes: 33 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,39 @@ void BitCountCmd::DoCmd(PClient* client) {
client->AppendInteger(static_cast<int64_t>(count));
}

IncrCmd::IncrCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategoryString) {}

bool IncrCmd::DoInitial(pikiwidb::PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}

void IncrCmd::DoCmd(pikiwidb::PClient* client) {
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(), value, kPTypeString);
if (err == kPErrorNotExist) {
value = PSTORE.SetValue(client->Key(), PObject::CreateString(1));
client->AppendInteger(1);
return;
}

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

if (value->encoding != kPEncodeInt) {
client->SetRes(CmdRes::kInvalidInt);
return;
}

intptr_t oldVal = static_cast<intptr_t>(reinterpret_cast<std::intptr_t>(value->value));
value->Reset(reinterpret_cast<void*>(oldVal + 1));

client->AppendInteger(oldVal + 1);
}

BitOpCmd::BitOpCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryString) {}

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

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

protected:
bool DoInitial(PClient *client) override;
callme-taota marked this conversation as resolved.
Show resolved Hide resolved

private:
void DoCmd(PClient *client) override;
callme-taota marked this conversation as resolved.
Show resolved Hide resolved
};

class IncrbyCmd : public BaseCmd {
public:
IncrbyCmd(const std::string &name, int16_t arity);
Expand Down
1 change: 1 addition & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(SetNX, 3);
ADD_COMMAND(Append, 3);
ADD_COMMAND(Strlen, 2);
ADD_COMMAND(Incr, 2);
ADD_COMMAND(Incrby, 3);
ADD_COMMAND(Decrby, 3);
ADD_COMMAND(IncrbyFloat, 3);
Expand Down
2 changes: 1 addition & 1 deletion src/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const PCommandInfo PCommandTable::s_info[] = {
{"mget", kPAttrRead, -2, &mget},
{"append", kPAttrWrite, 3, &append},
{"bitcount", kPAttrRead, -2, &bitcount},
// {"bitop", PAttr_write, -4, &bitop},
// {"bitop", PAttr_write, -4, &bitop},
{"getbit", kPAttrRead, 3, &getbit},
{"setbit", kPAttrWrite, 4, &setbit},
{"incr", kPAttrWrite, 2, &incr},
Expand Down