Skip to content

Commit

Permalink
feat: string cmd getbit (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
callme-taota committed Dec 5, 2023
1 parent 2494d19 commit b730505
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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 kCmdNameGetBit = "getbit";

// multi
const std::string kCmdNameMulti = "multi";
Expand Down
43 changes: 43 additions & 0 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,47 @@ void SetnxCmd::DoCmd(PClient* client) {
}
}

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

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

void GetBitCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_string);
if (err != PError_ok) {
client->SetRes(CmdRes::kErrOther);
return;
}

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

auto str = GetDecodedString(value);
const uint8_t* buf = (const uint8_t*)str->c_str();
size_t size = 8 * str->size();

if (offset < 0 || offset >= static_cast<long>(size)) {
client->AppendInteger(0);
return;
}

size_t bytesOffset = offset / 8;
size_t bitsOffset = offset % 8;
uint8_t byte = buf[bytesOffset];
if (byte & (0x1 << bitsOffset)) {
client->AppendInteger(1);
} else {
client->AppendInteger(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,4 +160,15 @@ class IncrbyCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

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

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 @@ -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> getbitPtr = std::make_unique<GetBitCmd>(kCmdNameGetBit, 3);
cmds_->insert(std::make_pair(kCmdNameGetBit, std::move(getbitPtr)));
}

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

0 comments on commit b730505

Please sign in to comment.