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 cmd (hset/hget, hmset/hmget, hgetall, hkeys) #57

Merged
merged 15 commits into from
Dec 10, 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
8 changes: 8 additions & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ const std::string kCmdNameBitCount = "bitcount";

const std::string kCmdNameAuth = "auth";

// hash cmd
const std::string kCmdNameHSet = "hset";
const std::string kCmdNameHGet = "hget";
const std::string kCmdNameHMSet = "hmset";
const std::string kCmdNameHMGet = "hmget";
const std::string kCmdNameHGetAll = "hgetall";
const std::string kCmdNameHKeys = "hkeys";

enum CmdFlags {
CmdFlagsWrite = (1 << 0), // May modify the dataset
CmdFlagsReadonly = (1 << 1), // Doesn't modify the dataset
Expand Down
228 changes: 228 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* 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_hash.h"
longfar-ncy marked this conversation as resolved.
Show resolved Hide resolved

#include "store.h"

namespace pikiwidb {

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

bool HSetCmd::DoInitial(PClient* client) {
if (client->argv_.size() % 2 != 0) {
client->SetRes(CmdRes::kWrongNum, kCmdNameHSet);
return false;
}
client->SetKey(client->argv_[1]);
return true;
}

void HSetCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
UnboundedBuffer reply;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash);
if (err != PError_ok && err != PError_notExist) {
ReplyError(err, &reply);
client->SetRes(CmdRes::kSyntaxErr, "hset cmd error");
return;
}
if (err == PError_notExist) {
value = PSTORE.SetValue(client->Key(), PObject::CreateHash());
}

auto new_cnt = 0;
auto hash = value->CastHash();
for (size_t i = 2; i < client->argv_.size(); i += 2) {
auto field = client->argv_[i];
auto value = client->argv_[i + 1];
auto it = hash->find(field);
if (it == hash->end()) {
hash->insert(PHash::value_type(field, value));
++new_cnt;
} else {
it->second = value;
}
}
FormatInt(new_cnt, &reply);
client->AppendStringRaw(reply.ReadAddr());
}

HGetCmd::HGetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryHash) {}

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

void HGetCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
UnboundedBuffer reply;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash);
if (err != PError_ok) {
ReplyError(err, &reply);
if (err == PError_notExist) {
client->AppendString("");
} else {
client->SetRes(CmdRes::kSyntaxErr, "hget cmd error");
}
return;
}

auto hash = value->CastHash();
auto it = hash->find(client->argv_[2]);

if (it != hash->end()) {
FormatBulk(it->second, &reply);
} else {
FormatNull(&reply);
}
client->AppendStringRaw(reply.ReadAddr());
}

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

bool HMSetCmd::DoInitial(PClient* client) {
if (client->argv_.size() % 2 != 0) {
client->SetRes(CmdRes::kWrongNum, kCmdNameHMSet);
return false;
}
client->SetKey(client->argv_[1]);
return true;
}

void HMSetCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
UnboundedBuffer reply;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash);
if (err != PError_ok && err != PError_notExist) {
ReplyError(err, &reply);
client->SetRes(CmdRes::kSyntaxErr, "hmset cmd error");
return;
}
if (err == PError_notExist) {
value = PSTORE.SetValue(client->Key(), PObject::CreateHash());
}

auto hash = value->CastHash();
for (size_t i = 2; i < client->argv_.size(); i += 2) {
auto field = client->argv_[i];
auto value = client->argv_[i + 1];
auto it = hash->find(field);
if (it == hash->end()) {
hash->insert(PHash::value_type(field, value));
} else {
it->second = value;
}
}
FormatOK(&reply);
client->AppendStringRaw(reply.ReadAddr());
}

HMGetCmd::HMGetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryHash) {}

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

void HMGetCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
UnboundedBuffer reply;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash);
if (err != PError_ok) {
ReplyError(err, &reply);
if (err == PError_notExist) {
client->AppendString("");
} else {
client->SetRes(CmdRes::kSyntaxErr, "hmget cmd error");
}
return;
}

auto hash = value->CastHash();
PreFormatMultiBulk(client->argv_.size() - 2, &reply);

for (size_t i = 2; i < client->argv_.size(); ++i) {
auto it = hash->find(client->argv_[i]);
if (it != hash->end()) {
FormatBulk(it->second, &reply);
} else {
FormatNull(&reply);
}
}
client->AppendStringRaw(reply.ReadAddr());
}

HGetAllCmd::HGetAllCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryHash) {}

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

void HGetAllCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
UnboundedBuffer reply;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash);
if (err != PError_ok) {
ReplyError(err, &reply);
if (err == PError_notExist) {
client->AppendString("");
} else {
client->SetRes(CmdRes::kSyntaxErr, "hgetall cmd error");
}
return;
}

auto hash = value->CastHash();
PreFormatMultiBulk(2 * hash->size(), &reply);

for (const auto& kv : *hash) {
FormatBulk(kv.first, &reply);
FormatBulk(kv.second, &reply);
}
client->AppendStringRaw(reply.ReadAddr());
}

HKeysCmd::HKeysCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryHash) {}

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

void HKeysCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
UnboundedBuffer reply;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_hash);
if (err != PError_ok) {
ReplyError(err, &reply);
if (err == PError_notExist) {
client->AppendString("");
} else {
client->SetRes(CmdRes::kSyntaxErr, "hkeys cmd error");
}
return;
}

auto hash = value->CastHash();
PreFormatMultiBulk(hash->size(), &reply);

for (const auto& kv : *hash) {
FormatBulk(kv.first, &reply);
}
client->AppendStringRaw(reply.ReadAddr());
}

} // namespace pikiwidb
80 changes: 80 additions & 0 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 HSetCmd : public BaseCmd {
public:
HSetCmd(const std::string &name, int16_t arity);

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

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

protected:
bool DoInitial(PClient *client) override;

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

} // namespace pikiwidb
17 changes: 16 additions & 1 deletion src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "cmd_table_manager.h"
#include <memory>
#include "cmd_admin.h"
#include "cmd_hash.h"
#include "cmd_keys.h"
#include "cmd_kv.h"

Expand Down Expand Up @@ -67,6 +68,20 @@ void CmdTableManager::InitCmdTable() {
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)));

// hash
std::unique_ptr<BaseCmd> hsetPtr = std::make_unique<HSetCmd>(kCmdNameHSet, -4);
cmds_->insert(std::make_pair(kCmdNameHSet, std::move(hsetPtr)));
std::unique_ptr<BaseCmd> hgetPtr = std::make_unique<HGetCmd>(kCmdNameHGet, 3);
cmds_->insert(std::make_pair(kCmdNameHGet, std::move(hgetPtr)));
std::unique_ptr<BaseCmd> hmsetPtr = std::make_unique<HMSetCmd>(kCmdNameHMSet, -4);
cmds_->insert(std::make_pair(kCmdNameHMSet, std::move(hmsetPtr)));
std::unique_ptr<BaseCmd> hmgetPtr = std::make_unique<HMGetCmd>(kCmdNameHMGet, -3);
cmds_->insert(std::make_pair(kCmdNameHMGet, std::move(hmgetPtr)));
std::unique_ptr<BaseCmd> hgetallPtr = std::make_unique<HGetAllCmd>(kCmdNameHGetAll, 2);
cmds_->insert(std::make_pair(kCmdNameHGetAll, std::move(hgetallPtr)));
std::unique_ptr<BaseCmd> hkeysPtr = std::make_unique<HKeysCmd>(kCmdNameHKeys, 2);
cmds_->insert(std::make_pair(kCmdNameHKeys, std::move(hkeysPtr)));
}

std::pair<BaseCmd*, CmdRes::CmdRet> CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) {
Expand Down Expand Up @@ -94,4 +109,4 @@ bool CmdTableManager::CmdExist(const std::string& cmd) const {

uint32_t CmdTableManager::GetCmdId() { return ++cmdId_; }

} // namespace pikiwidb
} // namespace pikiwidb