Skip to content

Commit

Permalink
feat: add constexpr string (#4)
Browse files Browse the repository at this point in the history
* add constexpr string
  • Loading branch information
dingxiaoshuai123 authored and Mixficsol committed Apr 11, 2024
1 parent 8be47ed commit 3d7cc93
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 41 deletions.
3 changes: 3 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ enum class ClientState {
kClosed,
};

constexpr const char* ErrTypeMessage =
"Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value";

class DB;
struct PSlaveInfo;

Expand Down
26 changes: 13 additions & 13 deletions src/cmd_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void HSetCmd::DoCmd(PClient* client) {
s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HSet(client->Key(), field, value, &temp);
if (s.ok()) {
ret += temp;
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
} else {
Expand All @@ -66,7 +66,7 @@ void HGetCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HGet(client->Key(), field, &value);
if (s.ok()) {
client->AppendString(value);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else if (s.IsNotFound()) {
client->AppendString("");
Expand All @@ -87,7 +87,7 @@ void HDelCmd::DoCmd(PClient* client) {
int32_t res{};
std::vector<std::string> fields(client->argv_.begin() + 2, client->argv_.end());
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HDel(client->Key(), fields, &res);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ void HMSetCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HMSet(client->Key(), client->Fvs());
if (s.ok()) {
client->SetRes(CmdRes::kOK);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand Down Expand Up @@ -145,7 +145,7 @@ void HMGetCmd::DoCmd(PClient* client) {
client->AppendString("");
}
}
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand Down Expand Up @@ -197,7 +197,7 @@ void HGetAllCmd::DoCmd(PClient* client) {
if (s.ok() || s.IsNotFound()) {
client->AppendArrayLen(total_fv * 2);
client->AppendStringRaw(raw);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand All @@ -223,7 +223,7 @@ void HKeysCmd::DoCmd(PClient* client) {
}
// update fields
client->Fields() = std::move(fields);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand All @@ -243,7 +243,7 @@ void HLenCmd::DoCmd(PClient* client) {
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HLen(client->Key(), &len);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(len);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "something wrong in hlen");
Expand All @@ -263,7 +263,7 @@ void HStrLenCmd::DoCmd(PClient* client) {
auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HStrlen(client->Key(), client->argv_[2], &len);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(len);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "something wrong in hstrlen");
Expand Down Expand Up @@ -340,7 +340,7 @@ void HValsCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HVals(client->Key(), &valueVec);
if (s.ok() || s.IsNotFound()) {
client->AppendStringVector(valueVec);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "hvals cmd error");
Expand Down Expand Up @@ -372,7 +372,7 @@ void HIncrbyFloatCmd::DoCmd(PClient* client) {
->HIncrbyfloat(client->Key(), client->argv_[2], client->argv_[3], &newValue);
if (s.ok() || s.IsNotFound()) {
client->AppendString(newValue);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "hvals cmd error");
Expand All @@ -395,7 +395,7 @@ void HSetNXCmd::DoCmd(PClient* client) {
->HSetnx(client->Key(), client->argv_[2], client->argv_[3], &temp);
if (s.ok()) {
client->AppendInteger(temp);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kSyntaxErr, "hsetnx cmd error");
Expand Down Expand Up @@ -423,7 +423,7 @@ void HIncrbyCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->HIncrby(client->Key(), client->argv_[2], int_by, &temp);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(temp);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "hincrby cmd error");
Expand Down
26 changes: 13 additions & 13 deletions src/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void LPushCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPush(client->Key(), list_values, &reply_num);
if (s.ok()) {
client->AppendInteger(reply_num);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kSyntaxErr, "lpush cmd error");
Expand All @@ -47,7 +47,7 @@ void LPushxCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPushx(client->Key(), list_values, &reply_num);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(reply_num);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand All @@ -69,7 +69,7 @@ void RPushCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(client->Key(), list_values, &reply_num);
if (s.ok()) {
client->AppendInteger(reply_num);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kSyntaxErr, "rpush cmd error");
Expand All @@ -91,7 +91,7 @@ void RPushxCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPushx(client->Key(), list_values, &reply_num);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(reply_num);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand All @@ -111,7 +111,7 @@ void LPopCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPop(client->Key(), 1, &elements);
if (s.ok()) {
client->AppendString(elements[0]);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else if (s.IsNotFound()) {
client->AppendStringLen(-1);
Expand All @@ -133,7 +133,7 @@ void RPopCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPop(client->Key(), 1, &elements);
if (s.ok()) {
client->AppendString(elements[0]);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else if (s.IsNotFound()) {
client->AppendStringLen(-1);
Expand All @@ -160,7 +160,7 @@ void LRangeCmd::DoCmd(PClient* client) {
}
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), start_index, end_index, &ret);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ void LRemCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRem(client->Key(), freq_, client->argv_[3], &reply_num);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(reply_num);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, "lrem cmd error");
Expand All @@ -219,7 +219,7 @@ void LTrimCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LTrim(client->Key(), start_index, end_index);
if (s.ok() || s.IsNotFound()) {
client->SetRes(CmdRes::kOK);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kSyntaxErr, "ltrim cmd error");
Expand Down Expand Up @@ -249,7 +249,7 @@ void LSetCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LSet(client->Key(), val, client->argv_[3]);
if (s.ok()) {
client->SetRes(CmdRes::kOK);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else if (s.IsNotFound()) {
client->SetRes(CmdRes::kNotFound);
Expand Down Expand Up @@ -284,7 +284,7 @@ void LInsertCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())
->GetStorage()
->LInsert(client->Key(), before_or_after, client->argv_[3], client->argv_[4], &ret);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down Expand Up @@ -315,7 +315,7 @@ void LIndexCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LIndex(client->Key(), freq_, &value);
if (s.ok()) {
client->AppendString(value);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else if (s.IsNotFound()) {
client->AppendStringLen(-1);
Expand All @@ -337,7 +337,7 @@ void LLenCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LLen(client->Key(), &llen);
if (s.ok() || s.IsNotFound()) {
client->AppendInteger(static_cast<int64_t>(llen));
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand Down
24 changes: 12 additions & 12 deletions src/cmd_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void SAddCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SAdd(client->Key(), members, &ret);
if (s.ok()) {
client->AppendInteger(ret);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kSyntaxErr, "sadd cmd error");
Expand All @@ -69,7 +69,7 @@ void SUnionStoreCmd::DoCmd(PClient* client) {
if (!s.ok()) {
client->SetRes(CmdRes::kSyntaxErr, "sunionstore cmd error");
}
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
}
client->AppendInteger(ret);
Expand All @@ -91,7 +91,7 @@ void SInterCmd::DoCmd(PClient* client) {
client->SetRes(CmdRes::kErrOther, "sinter cmd error");
return;
}
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
}
client->AppendStringVector(res_vt);
Expand All @@ -113,7 +113,7 @@ void SRemCmd::DoCmd(PClient* client) {
if (!s.ok()) {
client->SetRes(CmdRes::kErrOther, "srem cmd error");
}
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
}
client->AppendInteger(reply_num);
Expand All @@ -134,7 +134,7 @@ void SUnionCmd::DoCmd(PClient* client) {
if (!s.ok()) {
client->SetRes(CmdRes::kErrOther, "sunion cmd error");
}
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
}
client->AppendStringVector(res_vt);
Expand All @@ -156,7 +156,7 @@ void SInterStoreCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())
->GetStorage()
->SInterstore(client->Key(), inter_keys, value_to_dest, &reply_num);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand All @@ -177,7 +177,7 @@ bool SCardCmd::DoInitial(PClient* client) {
void SCardCmd::DoCmd(PClient* client) {
int32_t reply_Num = 0;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SCard(client->Key(), &reply_Num);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand All @@ -198,7 +198,7 @@ void SMoveCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())
->GetStorage()
->SMove(client->argv_[1], client->argv_[2], client->argv_[3], &reply_num);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ void SRandMemberCmd::DoCmd(PClient* client) {
std::vector<std::string> vec_ret;
storage::Status s =
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SRandmember(client->Key(), this->num_rand, &vec_ret);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down Expand Up @@ -300,7 +300,7 @@ bool SMembersCmd::DoInitial(PClient* client) {
void SMembersCmd::DoCmd(PClient* client) {
std::vector<std::string> delete_members;
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SMembers(client->Key(), &delete_members);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand All @@ -323,7 +323,7 @@ void SDiffCmd::DoCmd(PClient* client) {
std::vector<std::string> diff_members;
std::vector<std::string> diff_keys(client->argv_.begin() + 1, client->argv_.end());
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->SDiff(diff_keys, &diff_members);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand All @@ -349,7 +349,7 @@ void SDiffstoreCmd::DoCmd(PClient* client) {
storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())
->GetStorage()
->SDiffstore(client->Key(), diffstore_keys, value_to_dest, &reply_num);
if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void ZAddCmd::DoCmd(PClient* client) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZAdd(client->Key(), score_members_, &count);
if (s.ok()) {
client->AppendInteger(count);
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand Down Expand Up @@ -170,7 +170,7 @@ void ZRevrangeCmd::DoCmd(PClient* client) {
client->AppendContent(sm.member);
}
}
} else if (s.IsInvalidArgument()) {
} else if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
} else {
client->SetRes(CmdRes::kErrOther, s.ToString());
Expand Down Expand Up @@ -232,7 +232,7 @@ void ZRangebyscoreCmd::DoCmd(PClient* client) {
->GetStorage()
->ZRangebyscore(client->Key(), min_score, max_score, left_close, right_close, &score_members);

if (s.IsInvalidArgument()) {
if (s.ToString() == ErrTypeMessage) {
client->SetRes(CmdRes::kmultikey);
return;
}
Expand Down

0 comments on commit 3d7cc93

Please sign in to comment.