From a13bb36159fab51d5af3fed27699db2b21ec2286 Mon Sep 17 00:00:00 2001 From: Yoon Date: Fri, 13 Apr 2018 15:44:49 +0900 Subject: [PATCH] Add a fix(by @fortrue) --- src/Command.cpp | 16 +++++++++++----- src/Command.h | 4 +++- src/Conf.cpp | 16 ++++------------ src/Conf.h | 1 - 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Command.cpp b/src/Command.cpp index 0e3fc0b..385e4ae 100644 --- a/src/Command.cpp +++ b/src/Command.cpp @@ -9,6 +9,7 @@ #include #include "String.h" #include "Command.h" +#include "Conf.h" Command Command::CmdPool[AvailableCommands] = { {None, "", 0, MaxArgs, Read}, @@ -190,15 +191,20 @@ void Command::init() } } -void Command::addCustomCommand(const Command& c) { +void Command::addCustomCommand(const CustomCommandConf& ccc) { if (Sentinel >= AvailableCommands) { Throw(InitFail, "too many custom commands(>%d)", MaxCustomCommands); } - if (nullptr != find(c.name)) { - Throw(InitFail, "custom command %s is duplicated", c.name); + if (nullptr != find(ccc.name)) { + Throw(InitFail, "custom command %s is duplicated", ccc.name); } - CmdPool[Sentinel] = c; - CmdMap[c.name] = &CmdPool[Sentinel]; + auto* p = &CmdPool[Sentinel]; + p->name = ccc.name.c_str(); + p->minArgs = ccc.minArgs; + p->maxArgs = ccc.maxArgs; + p->mode = ccc.mode; + p->type = (Command::Type)ccc.type; + CmdMap[ccc.name] = p; Sentinel++; } diff --git a/src/Command.h b/src/Command.h index b4d346f..d77c97a 100644 --- a/src/Command.h +++ b/src/Command.h @@ -11,6 +11,8 @@ #include "Exception.h" #include "HashFunc.h" +struct CustomCommandConf; + class Command { public: @@ -251,7 +253,7 @@ public: auto it = CmdMap.find(cmd); return it == CmdMap.end() ? nullptr : it->second; } - static void addCustomCommand(const Command& pc); + static void addCustomCommand(const CustomCommandConf& pc); static int Sentinel; private: static const int MaxArgs = 100000000; diff --git a/src/Conf.cpp b/src/Conf.cpp index ed76d78..82ac5c7 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -41,14 +41,6 @@ void CustomCommandConf::init(CustomCommandConf&c, const char* name, const int ty c.mode = Command::Write; } -void CustomCommandConf::convert(const CustomCommandConf&c, Command &cmd) { - cmd.name = strdup(c.name.c_str()); - cmd.minArgs = c.minArgs; - cmd.maxArgs = c.maxArgs; - cmd.mode = c.mode; - cmd.type = (Command::Type)c.type; -} - Conf::Conf(): mBind("0.0.0.0:7617"), mWorkerThreads(1), @@ -197,7 +189,7 @@ void Conf::setGlobal(const ConfParser::Node* node) if (dataCenter) { setDataCenter(dataCenter); } - for (auto latencyMonitor : latencyMonitors) { + for (auto& latencyMonitor : latencyMonitors) { mLatencyMonitors.push_back(LatencyMonitorConf{}); setLatencyMonitor(mLatencyMonitors.back(), latencyMonitor); } @@ -402,9 +394,9 @@ void Conf::setCustomCommand(const ConfParser::Node* node) if (cc.maxArgs < cc.minArgs) { Throw(InvalidValue, "%s:%d must be MaxArgs >= MinArgs", p->file, p->line); } - Command cmd; - CustomCommandConf::convert(cc, cmd); - Command::addCustomCommand(cmd); + } + for (const auto& cc : mCustomCommands) { + Command::addCustomCommand(cc); } } diff --git a/src/Conf.h b/src/Conf.h index 4e94659..ecd19c0 100644 --- a/src/Conf.h +++ b/src/Conf.h @@ -103,7 +103,6 @@ struct CustomCommandConf int mode; static void init(CustomCommandConf &c, const char* name, const int type); - static void convert(const CustomCommandConf&c, Command &cmd); }; class Conf