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: hrandfield #9

Closed
wants to merge 6 commits into from
Closed
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 @@ -68,6 +68,7 @@ const std::string kCmdNameHGetAll = "hgetall";
const std::string kCmdNameHKeys = "hkeys";
const std::string kCmdNameHLen = "hlen";
const std::string kCmdNameHStrLen = "hstrlen";
const std::string kCmdNameHRandField = "hrandfield";

enum CmdFlags {
kCmdFlagsWrite = (1 << 0), // May modify the dataset
Expand Down
109 changes: 109 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "cmd_hash.h"

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

namespace pikiwidb {
Expand Down Expand Up @@ -285,4 +286,112 @@ void HStrLenCmd::DoCmd(PClient* client) {
client->AppendStringRaw(reply.ReadAddr());
}

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

bool HRandFieldCmd::DoInitial(PClient* client) {
/*
* There should not be quantity detection here,
* because the quantity detection of redis is after the COUNT integer detection.
*/
// if (client->argv_.size() > 4) {
// client->SetRes(CmdRes::kSyntaxErr);
// return false;
// }
client->SetKey(client->argv_[1]);
return true;
}

void HRandFieldCmd::DoCmd(PClient* client) {
// parse arguments
int64_t count{};
bool with_values{false};
if (client->argv_.size() > 2) {
if (pstd::String2int(client->argv_[2], &count) == 0) {
client->SetRes(CmdRes::kInvalidInt);
return;
}

if (client->argv_.size() > 4) {
client->SetRes(CmdRes::kSyntaxErr);
return;
}

if (client->argv_.size() > 3) {
if (kWithValueString != pstd::StringToLower(client->argv_[3])) {
client->SetRes(CmdRes::kSyntaxErr);
return;
}
with_values = true;
}
}

// get hash
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(), value, kPTypeHash);
if (err != kPErrorOK) {
if (err == kPErrorNotExist) {
client->AppendString("");
} else {
client->SetRes(CmdRes::kSyntaxErr, "hrandfield cmd error");
}
return;
}
auto hash = value->CastHash();
if (hash->empty()) {
client->AppendString("");
return;
}

// fetch field(s) and reply
if (client->argv_.size() > 2) {
if (count >= 0) {
DoWithPositiveCount(client, hash, count, with_values);
} else {
DoWithNegativeCount(client, hash, count, with_values);
}
} else {
auto it = std::next(hash->begin(), rand() % hash->size());
client->AppendString(it->first);
}
}

void HRandFieldCmd::DoWithPositiveCount(PClient* client, const PHash* hash, int64_t count, bool with_value) {
if (hash->size() <= count) { // reply all fields
client->AppendArrayLen(with_value ? hash->size() * 2 : hash->size());
for (auto&& kv : *hash) {
client->AppendString(kv.first);
if (with_value) {
client->AppendString(kv.second);
}
}
} else { // reply [count] fields
std::vector<std::pair<PString, PString>> kvs;
for (auto&& kv : *hash) {
kvs.push_back(kv);
}
std::shuffle(kvs.begin(), kvs.end(), std::mt19937(rd_()));

client->AppendArrayLen(with_value ? count * 2 : count);
for (size_t i = 0; i < count; i++) {
client->AppendString(kvs[i].first);
if (with_value) {
client->AppendString(kvs[i].second);
}
}
}
}

void HRandFieldCmd::DoWithNegativeCount(PClient* client, const PHash* hash, int64_t count, bool with_value) {
count = -count;
client->AppendArrayLen(with_value ? count * 2 : count);
while (count--) {
auto it = std::next(hash->begin(), rand() % hash->size());
client->AppendString(it->first);
if (with_value) {
client->AppendString(it->second);
}
}
}

} // namespace pikiwidb
20 changes: 20 additions & 0 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

#pragma once

#include <random>

#include "base_cmd.h"
#include "hash.h"

namespace pikiwidb {

Expand Down Expand Up @@ -99,4 +102,21 @@ class HStrLenCmd : public BaseCmd {
void DoCmd(PClient *client) override;
};

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

protected:
bool DoInitial(PClient *client) override;

private:
void DoCmd(PClient *client) override;
void DoWithPositiveCount(PClient *client, const PHash *hash, int64_t count, bool with_value);
void DoWithNegativeCount(PClient *client, const PHash *hash, int64_t count, bool with_value);

std::random_device rd_;

static const inline std::string kWithValueString{"withvalues"};
};

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

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