Add a fix(by @fortrue)

This commit is contained in:
Yoon 2018-04-13 15:44:49 +09:00
parent a559ac0d4f
commit a13bb36159
4 changed files with 18 additions and 19 deletions

View File

@ -9,6 +9,7 @@
#include <map>
#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++;
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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