Skip to content

Commit

Permalink
merge conflict done
Browse files Browse the repository at this point in the history
  • Loading branch information
hero-heng committed Nov 16, 2023
2 parents f198702 + b7c67a4 commit 3bc4740
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 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
45 changes: 23 additions & 22 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ void AppendCmd::DoCmd(PClient* client) {
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)
Expand All @@ -93,15 +93,15 @@ void GetsetCmd::DoCmd(PClient* client) {
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)
Expand All @@ -121,7 +121,7 @@ void MgetCmd::DoCmd(PClient* client) {
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 @@ -139,18 +139,19 @@ bool MSetCmd::DoInitial(PClient* client) {
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 3bc4740

Please sign in to comment.