Skip to content

Commit

Permalink
feat:add set cmd scard
Browse files Browse the repository at this point in the history
  • Loading branch information
whr1118 committed Jan 9, 2024
1 parent 232a1b4 commit 3abd6be
Show file tree
Hide file tree
Showing 4 changed files with 35 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 @@ -76,6 +76,7 @@ const std::string kCmdNameSIsMember = "sismember";
const std::string kCmdNameSAdd = "sadd";
const std::string kCmdNameSUnionStore = "sunionstore";
const std::string kCmdNameSRem = "srem";
const std::string kCmdNameSCard = "scard";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
22 changes: 22 additions & 0 deletions src/cmd_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,26 @@ void SRemCmd::DoCmd(PClient* client) {
client->AppendInteger(oldSize - unset->size());
}

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

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

void SCardCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(), value, kPTypeSet);
if (err != kPErrorOK) {
if (err == kPErrorNotExist) {
client->AppendInteger(0);
} else {
client->SetRes(CmdRes::kErrOther);
}
return;
}
client->AppendInteger(value->CastSet()->size());
}

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

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

protected:
bool DoInitial(PClient *client) override;

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

} // namespace pikiwidb
1 change: 1 addition & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(SAdd, -3);
ADD_COMMAND(SUnionStore, -3);
ADD_COMMAND(SRem, -3);
ADD_COMMAND(SCard, 2);
}

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

0 comments on commit 3abd6be

Please sign in to comment.