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 7 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 @@ -48,6 +48,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
162 changes: 162 additions & 0 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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 {

#define GET_HASH(cmd) \
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
PObject* value; \
longfar-ncy marked this conversation as resolved.
Show resolved Hide resolved
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, #cmd " cmd error"); \
} \
return; \
}

#define GET_OR_SET_HASH(cmd) \
PObject* value; \
longfar-ncy marked this conversation as resolved.
Show resolved Hide resolved
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, #cmd " cmd error"); \
return; \
} \
if (err == PError_notExist) { \
value = PSTORE.SetValue(client->Key(), PObject::CreateHash()); \
}

static inline PHash::iterator _set_hash_force(PHash& hash, const PString& key, const PString& val) {
longfar-ncy marked this conversation as resolved.
Show resolved Hide resolved
auto it(hash.find(key));
if (it != hash.end()) {
it->second = val;
} else {
it = hash.insert(PHash::value_type(key, val)).first;
}
return it;
}

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) {
GET_HASH(hget);

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) {
GET_OR_SET_HASH(hmset);

auto hash = value->CastHash();
for (size_t i = 2; i < client->argv_.size(); i += 2) {
_set_hash_force(*hash, client->argv_[i], client->argv_[i + 1]);
}
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) {
GET_HASH(hmget);

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) {
GET_HASH(hgetall);

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) {
GET_HASH(hkeys);

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

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

} // namespace pikiwidb
71 changes: 71 additions & 0 deletions src/cmd_hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 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;
};

using HSetCmd = HMSetCmd;

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_kv.h"

namespace pikiwidb {
Expand Down Expand Up @@ -52,6 +53,20 @@ void CmdTableManager::InitCmdTable() {
cmds_->insert(std::make_pair(kCmdNamePsetex, std::move(psetexPtr)));
std::unique_ptr<BaseCmd> setnxPtr = std::make_unique<SetnxCmd>(kCmdNameSetnx, 3);
cmds_->insert(std::make_pair(kCmdNameSetnx, std::move(setnxPtr)));

// 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 @@ -79,4 +94,4 @@ bool CmdTableManager::CmdExist(const std::string& cmd) const {

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

} // namespace pikiwidb
} // namespace pikiwidb