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: hash command hscan #110

Merged
merged 1 commit into from
Jan 7, 2024
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
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const std::string kCmdNameHGetAll = "hgetall";
const std::string kCmdNameHKeys = "hkeys";
const std::string kCmdNameHLen = "hlen";
const std::string kCmdNameHStrLen = "hstrlen";
const std::string kCmdNameHScan = "hscan";

// set cmd
const std::string kCmdNameSIsMember = "sismember";
Expand Down
3 changes: 3 additions & 0 deletions src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ void CmdRes::SetRes(CmdRes::CmdRet _ret, const std::string& content) {
AppendStringRaw(content);
AppendStringRaw(CRLF);
break;
case kInvalidCursor:
AppendStringRaw("-ERR invalid cursor");
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CmdRes {
kInconsistentHashTag,
kErrOther,
KIncrByOverFlow,
kInvalidCursor,
};

CmdRes() = default;
Expand Down
56 changes: 56 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <config.h>

#include "pstd/pstd_string.h"
#include "store.h"

namespace pikiwidb {
Expand Down Expand Up @@ -227,4 +228,59 @@ void HStrLenCmd::DoCmd(PClient* client) {
}
}

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

bool HScanCmd::DoInitial(PClient* client) {
if (auto size = client->argv_.size(); size != 3 && size != 5 && size != 7) {
client->SetRes(CmdRes::kSyntaxErr);
return false;
}
client->SetKey(client->argv_[1]);
return true;
}

void HScanCmd::DoCmd(PClient* client) {
const auto& argv = client->argv_;
// parse arguments
int64_t cursor{};
int64_t count{10};
std::string pattern{"*"};
if (pstd::String2int(argv[2], &cursor) == 0) {
client->SetRes(CmdRes::kInvalidCursor);
return;
}
for (size_t i = 3; i < argv.size(); i += 2) {
if (auto lower = pstd::StringToLower(argv[i]); kMatchSymbol == lower) {
pattern = argv[i + 1];
} else if (kCountSymbol == lower) {
if (pstd::String2int(argv[i + 1], &count) == 0) {
client->SetRes(CmdRes::kInvalidInt, kCmdNameHScan);
return;
}
} else {
client->SetRes(CmdRes::kErrOther, kCmdNameHScan);
return;
}
}

// execute command
std::vector<storage::FieldValue> fvs;
int64_t next_cursor{};
auto status = PSTORE.GetBackend()->HScan(client->Key(), cursor, pattern, count, &fvs, &next_cursor);
if (!status.ok() && !status.IsNotFound()) {
client->SetRes(CmdRes::kErrOther, status.ToString());
return;
}

// reply to client
client->AppendArrayLen(2);
client->AppendString(std::to_string(next_cursor));
client->AppendArrayLenUint64(fvs.size() * 2);
for (const auto& [field, value] : fvs) {
client->AppendString(field);
client->AppendString(value);
}
}

} // namespace pikiwidb
14 changes: 14 additions & 0 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,18 @@ class HStrLenCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

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

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;

static constexpr const char *kMatchSymbol = "match";
static constexpr const char *kCountSymbol = "count";
};

} // 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 @@ -73,6 +73,7 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(HKeys, 2);
ADD_COMMAND(HLen, 2);
ADD_COMMAND(HStrLen, 3);
ADD_COMMAND(HScan, -3);

// set
ADD_COMMAND(SIsMember, 3);
Expand Down