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:set command sismember #99

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const std::string kCmdNameHKeys = "hkeys";
const std::string kCmdNameHLen = "hlen";
const std::string kCmdNameHStrLen = "hstrlen";

// set cmd
const std::string kCmdNameSIsMember = "sismember";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
kCmdFlagsReadonly = (1 << 1), // Doesn't modify the dataset
Expand Down
34 changes: 34 additions & 0 deletions src/cmd_set.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#include "cmd_set.h"

#include "store.h"

namespace pikiwidb {

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

bool SIsMemberCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}
void SIsMemberCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
auto replyNum = 0; // only change to 1 if ismember . key not exist it is 0
PError err = PSTORE.GetValueByType(client->Key(), value, kPTypeSet);
if (err == kPErrorOK && value->CastSet()->contains(client->argv_[2])) {
// only key exist and set has key , set 1

replyNum = 1;
}

client->AppendInteger(replyNum);
}

} // namespace pikiwidb
24 changes: 24 additions & 0 deletions src/cmd_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#pragma once
#include "base_cmd.h"

namespace pikiwidb {

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

protected:
bool DoInitial(PClient *client) override;

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

} // namespace pikiwidb
4 changes: 4 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cmd_hash.h"
#include "cmd_keys.h"
#include "cmd_kv.h"
#include "cmd_set.h"

namespace pikiwidb {

Expand Down Expand Up @@ -71,6 +72,9 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(HKeys, 2);
ADD_COMMAND(HLen, 2);
ADD_COMMAND(HStrLen, 3);

// set
ADD_COMMAND(SIsMember, 3);
}

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