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 setbit #69

Merged
merged 8 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const std::string kCmdNameStrlen = "strlen";
const std::string kCmdNameSetex = "setex";
const std::string kCmdNamePsetex = "psetex";
const std::string kCmdNameSetnx = "setnx";
const std::string kCmdNameSetBit = "setbit";
const std::string kCmdNameGetBit = "getbit";

// multi
Expand Down
63 changes: 63 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,67 @@ void GetBitCmd::DoCmd(PClient* client) {
return;
}

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

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

void SetBitCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_string);
if (err == PError_notExist) {
value = PSTORE.SetValue(client->Key(), PObject::CreateString(""));
err = PError_ok;
}

if (err != PError_ok) {
client->AppendInteger(0);
return;
}

long offset = 0;
long on = 0;
if (!Strtol(client->argv_[2].c_str(), client->argv_[2].size(), &offset) ||
!Strtol(client->argv_[3].c_str(), client->argv_[3].size(), &on)) {
client->SetRes(CmdRes::kInvalidInt);
return;
}

if (offset < 0 || offset > kStringMaxBytes) {
client->AppendInteger(0);
return;
}

PString* pStringPtr = value->CastString();
if (!pStringPtr) {
client->AppendInteger(0);
return;
}

PString& newVal = *pStringPtr;

size_t bytes = offset / 8;
size_t bits = offset % 8;

if (bytes + 1 > newVal.size()) {
newVal.resize(bytes + 1, '\0');
}

const char oldByte = newVal[bytes];
char& byte = newVal[bytes];
if (on) {
byte |= (0x1 << bits);
} else {
byte &= ~(0x1 << bits);
}

value->Reset(new PString(newVal));
value->encoding = PEncode_raw;
client->AppendInteger((oldByte & (0x1 << bits)) ? 1 : 0);
return;
}

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

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

protected:
bool DoInitial(PClient *client) override;

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

class GetBitCmd : public BaseCmd {
public:
GetBitCmd(const std::string &name, int16_t arity);
Expand Down
2 changes: 2 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ void CmdTableManager::InitCmdTable() {
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::unique_ptr<BaseCmd> setbitPtr = std::make_unique<SetBitCmd>(kCmdNameSetBit, 4);
cmds_->insert(std::make_pair(kCmdNameSetBit, std::move(setbitPtr)));
std::unique_ptr<BaseCmd> getbitPtr = std::make_unique<GetBitCmd>(kCmdNameGetBit, 3);
cmds_->insert(std::make_pair(kCmdNameGetBit, std::move(getbitPtr)));
}
Expand Down