Skip to content

Commit

Permalink
fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hero-heng committed Nov 16, 2023
1 parent e3c416b commit b7c67a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
// e.g:["set","key","value"]
std::span<std::string> argv_;
std::vector<std::string> keys_;
std::vector<std::pair<std::string, std::string>> kvs_;

private:
std::shared_ptr<TcpConnection> getTcpConnection() const { return tcp_connection_.lock(); }
Expand Down
71 changes: 35 additions & 36 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace pikiwidb {
GetCmd::GetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsReadonly, AclCategoryRead | AclCategoryString) {}


bool GetCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
Expand All @@ -38,7 +37,6 @@ void GetCmd::DoCmd(PClient* client) {
SetCmd::SetCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {}


bool SetCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
Expand All @@ -53,76 +51,76 @@ void SetCmd::DoCmd(PClient* client) {
AppendCmd::AppendCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {}

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

void AppendCmd::DoCmd(PClient *client) {
void AppendCmd::DoCmd(PClient* client) {
PObject* value;
PError err = PSTORE.GetValueByType(client->Key(), value, PType_string);
if (err != PError_ok) {
if (err == PError_notExist) { // = set command
if (err == PError_notExist) { // = set command
PSTORE.ClearExpire(client->argv_[1]); // clear key's old ttl
PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2]));
client->AppendInteger(static_cast<int64_t>(client->argv_[2].size()));
} else { // append string
auto str = GetDecodedString(value);
std::string old_value(str->c_str(), str->size());
std::string new_value = old_value + client->argv_[2];
PSTORE.SetValue(client->argv_[1], PObject::CreateString(new_value));
client->AppendInteger(static_cast<int64_t>(new_value.size()));
} else {
client->SetRes(CmdRes::kErrOther, "append cmd error");
}
}else {
client->SetRes(CmdRes::kErrOther, "append cmd error");
return;
}
auto str = GetDecodedString(value);
std::string old_value(str->c_str(), str->size());
std::string new_value = old_value + client->argv_[2];
PSTORE.SetValue(client->argv_[1], PObject::CreateString(new_value));
client->AppendInteger(static_cast<int64_t>(new_value.size()));
}

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

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

void GetsetCmd::DoCmd(PClient *client) {
void GetsetCmd::DoCmd(PClient* client) {
PObject* old_value;
PError err = PSTORE.GetValueByType(client->Key(), old_value, PType_string);
if (err != PError_ok) {
if (err == PError_notExist) { // = set command
if (err == PError_notExist) { // = set command
PSTORE.ClearExpire(client->argv_[1]); // clear key's old ttl
PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2]));
client->AppendString("");
} else { // set new value
auto str = GetDecodedString(old_value);
std::string ret_value(str->c_str(), str->size());
PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2]));
client->AppendString(ret_value);
} else {
client->SetRes(CmdRes::kErrOther, "getset cmd error");
}
}else {
client->SetRes(CmdRes::kErrOther, "getset cmd error");
return;
}
auto str = GetDecodedString(old_value);
std::string ret_value(str->c_str(), str->size());
PSTORE.SetValue(client->argv_[1], PObject::CreateString(client->argv_[2]));
client->AppendString(ret_value);
}

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

bool MgetCmd::DoInitial(PClient *client) {
bool MgetCmd::DoInitial(PClient* client) {
std::vector<std::string> keys(client->argv_.begin(), client->argv_.end());
client->keys_ = keys;
client->keys_.erase(client->keys_.begin());
return true;
}

void MgetCmd::DoCmd(PClient *client) {
void MgetCmd::DoCmd(PClient* client) {
size_t valueSize = client->keys_.size();
client->AppendArrayLen(static_cast<int64_t >(valueSize));
for(const auto& k : client->keys_) {
client->AppendArrayLen(static_cast<int64_t>(valueSize));
for (const auto& k : client->keys_) {
PObject* value;
PError err = PSTORE.GetValueByType(k, value, PType_string);
if (err == PError_notExist) {
client->AppendContent("$-1");
client->AppendStringLen(-1);
} else {
auto str = GetDecodedString(value);
std::string reply(str->c_str(), str->size());
Expand All @@ -132,26 +130,27 @@ void MgetCmd::DoCmd(PClient *client) {
}

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

bool MSetCmd::DoInitial(PClient* client) {
size_t argcSize = client->argv_.size();
if (argcSize % 2 == 0) {
client->SetRes(CmdRes::kWrongNum, kCmdNameMset);
return false;
}
client->kvs_.clear();
client->keys_.clear();
for (size_t index = 1; index != argcSize; index += 2) {
client->kvs_.emplace_back(client->argv_[index], client->argv_[index + 1]);
client->keys_.emplace_back(client->argv_[index]);
}
return true;
}

void MSetCmd::DoCmd(PClient* client) {
std::vector<std::pair<std::string, std::string>>::const_iterator it;
for (it = client->kvs_.begin(); it != client->kvs_.end(); it++) {
PSTORE.ClearExpire(it->first); // clear key's old ttl
PSTORE.SetValue(it->first, PObject::CreateString(it->second));
int valueIndex = 2;
for (const auto& it : client->keys_) {
PSTORE.ClearExpire(it); // clear key's old ttl
PSTORE.SetValue(it, PObject::CreateString(client->argv_[valueIndex]));
valueIndex += 2;
}
client->SetRes(CmdRes::kOk);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void CmdTableManager::InitCmdTable() {
cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr)));
std::unique_ptr<BaseCmd> setPtr = std::make_unique<SetCmd>(kCmdNameSet, -3);
cmds_->insert(std::make_pair(kCmdNameSet, std::move(setPtr)));
std::unique_ptr<BaseCmd> appendPtr = std::make_unique<AppendCmd>(kCmdNameAppend, -3);
std::unique_ptr<BaseCmd> appendPtr = std::make_unique<AppendCmd>(kCmdNameAppend, 3);
cmds_->insert(std::make_pair(kCmdNameAppend, std::move(appendPtr)));
std::unique_ptr<BaseCmd> getsetPtr = std::make_unique<GetsetCmd>(kCmdNameGetset, 3);
cmds_->insert(std::make_pair(kCmdNameGetset, std::move(getsetPtr)));
Expand Down

0 comments on commit b7c67a4

Please sign in to comment.