From dcdc9a1ba02fc10b3596bad43f3dbf8fa9e9c2da Mon Sep 17 00:00:00 2001 From: Yoon Date: Thu, 12 Apr 2018 11:59:15 +0900 Subject: [PATCH] Change config description style --- conf/command.conf | 174 +++++++++++++++++++++++++--------------------- src/Conf.cpp | 19 ++--- 2 files changed, 104 insertions(+), 89 deletions(-) diff --git a/conf/command.conf b/conf/command.conf index da17dc1..ac6ac01 100644 --- a/conf/command.conf +++ b/conf/command.conf @@ -1,83 +1,95 @@ -# CustomCommand section must be defined before Latency monitor to support all commands -# -# NOTE: only support maximum 16 custom commands -# -# [...] must be specify -# (...) optional. it has default value -# -# CustomCommand [alias1] { ## module alias. just a delimter -# [command1] { ## command string, must be lower case. i.e. setnx, expire, ... -# (minArgs) (val) ## minimum arguments(including command itself). default is 2(key only) -# (maxArgs) (val) ## maximum arguments(including command itself). default is 2(key only) -# (mode) (val) ## a command mode Read/Write/Admin is exclusive. default is Write. -# } -# [command2] { -# ... -# } -# ... -# } -# -# default key position is first parameter. i.e. command1 key ... -# if you want to use other position, add KeyAt2(key is second) or KeyAt3(key is third) to mode with '|' -# i.e. mode Write|KeyAt2 -# -# in case of 'mode KeyAt2', it will resolved as Write|KeyAt2 +## Custom Command +## CustomCommand { +## command { #command string, must be lowercase +## [Mode read|write|admin[|[keyAt2|keyAt3]] #default write, default key position is 1 +## [MinArgs [2-]] #default 2, including command itself +## [MaxArgs [2-]] #default 2, must be MaxArgs >= MinArgs +## }... +## } -# from redis source src/modules/hello.c -CustomCommand hello { - # hello.push.native key value - hello.push.native { - minArgs 3 - maxArgs 3 - mode Write - } - # hello.repl2 - # just skipped since minArgs = 2, maxArgs = 2, mode = Write are default values - hello.repl2 { - } - # hello.toggle.case key - # same as hello.repl2 - hello.toggle.case { - } - # hello.more.expire key milliseconds - hello.more.expire { - minArgs 3 - maxArgs 3 - } - # hello.zsumrange key startscore endscore - hello.zsumrange { - minArgs 4 - maxArgs 4 - mode Read - } - # hello.lexrange key min_lex max_lex min_age max_age - hello.lexrange { - minArgs 6 - maxArgs 6 - mode Read - } - # hello.hcopy key srcfield dstfield - hello.hcopy { - minArgs 4 - maxArgs 4 - } -} +## This section must be defined before Latency monitor to support all commands +## Currently support maximum 16 custom commands + +## Example: +#CustomCommand { +##------------------------------------------------------------------------ +# custom.ttl { +# Mode keyAt2 +# MinArgs 3 +# MaxArgs 3 +# } +#### custom.ttl miliseconds key +#### Mode = write|keyAt2, MinArgs/MaxArgs = 3 = command + miliseconds + key +##------------------------------------------------------------------------ +## from redis source src/modules/hello.c +# hello.push.native { +# MinArgs 3 +# MaxArgs 3 +# } +#### hello.push.native key value +#### Mode = write, MinArgs/MaxArgs = 3 = command) + key + value +##------------------------------------------------------------------------ +# hello.repl2 { +# } +#### hello.repl2 +#### Mode = write, MinArgs/MaxArgs = 2 = command + list-key +##------------------------------------------------------------------------ +# hello.toggle.case { +# } +#### hello.toggle.case key +#### Mode = write, MinArgs/MaxArgs = 2 = command + key +##------------------------------------------------------------------------ +# hello.more.expire { +# MinArgs 3 +# MaxArgs 3 +# } +#### hello.more.expire key milliseconds +#### Mode = write, MinArgs/MaxArgs = 3 = command + key + milliseconds +##------------------------------------------------------------------------ +# hello.zsumrange { +# MinArgs 4 +# MaxArgs 4 +# Mode read +# } +#### hello.zsumrange key startscore endscore +#### Mode = read, MinArgs/MaxArgs = 4 = command + key + startscore + endscore +##------------------------------------------------------------------------ +# hello.lexrange { +# MinArgs 6 +# MaxArgs 6 +# Mode read +# } +#### hello.lexrange key min_lex max_lex min_age max_age +#### Mode = read, MinArgs/MaxArgs = 6 = command + key + min_lex + max_lex + min_age + max_age +##------------------------------------------------------------------------ +# hello.hcopy { +# MinArgs 4 +# MaxArgs 4 +# } +#### hello.hcopy key srcfield dstfield +#### Mode = write, MinArgs/MaxArgs = 4 = command + key + srcfield) + dstfield +##------------------------------------------------------------------------ +## from redis source src/modules/hellotype.c +# hellotype.insert { +# MinArgs 3 +# MaxArgs 3 +# } +#### hellotype.insert key value +#### Mode = write, MinArgs/MaxArgs = 3 = command + key + value +##------------------------------------------------------------------------ +# hellotype.range { +# MinArgs 4 +# MaxArgs 4 +# Mode read +# } +#### hellotype.range key first count +#### Mode = read, MinArgs/MaxArgs = 4 = command + key + first + count +##------------------------------------------------------------------------ +# hellotype.len { +# Mode read +# } +#### hellotype.len key +#### Mode = read, MinArgs/MaxArgs = 2 = command + key +##------------------------------------------------------------------------ +#} -# from redis source src/modules/hellotype.c -CustomCommand hellotype { - # hellotype.insert key value - hellotype.insert { - minArgs 3 - maxArgs 3 - } - # hellotype.range key first count - hellotype.range { - minArgs 4 - maxArgs 4 - mode Read - } - # hellotype.len key - hello.len { - mode Read - } -} diff --git a/src/Conf.cpp b/src/Conf.cpp index df406af..2bb8bd2 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -381,13 +381,16 @@ void Conf::setCustomCommand(const ConfParser::Node* node) CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel); auto s = p->sub; for (;s ; s = s->next) { - if (setInt(cc.cmd.minArgs, "minArgs", s, 2)) { - } else if (setInt(cc.cmd.maxArgs, "maxArgs", s, 2, 9999)) { - } else if (setCommandMode(cc.cmd.mode, "mode", s)) { + if (setInt(cc.cmd.minArgs, "MinArgs", s, 2)) { + } else if (setInt(cc.cmd.maxArgs, "MaxArgs", s, 2, 9999)) { + } else if (setCommandMode(cc.cmd.mode, "Mode", s)) { } else { Throw(UnknownKey, "%s:%d unknown key %s", s->file, s->line, s->key.c_str()); } } + if (cc.cmd.maxArgs < cc.cmd.minArgs) { + Throw(InvalidValue, "%s:%d must be MaxArgs >= MinArgs", p->file, p->line); + } Command::addCustomCommand(&cc.cmd); } } @@ -405,15 +408,15 @@ bool Conf::setCommandMode(int& mode, const char* name, const ConfParser::Node* n std::string mask; std::istringstream is(n->val); while (std::getline(is, mask, '|')) { - if ((strcasecmp(mask.c_str(), "Write") == 0)) { + if ((strcasecmp(mask.c_str(), "write") == 0)) { mode |= Command::Write; - } else if ((strcasecmp(mask.c_str(), "Read") == 0)) { + } else if ((strcasecmp(mask.c_str(), "read") == 0)) { mode |= Command::Read; - } else if ((strcasecmp(mask.c_str(), "Admin") == 0)) { + } else if ((strcasecmp(mask.c_str(), "admin") == 0)) { mode |= Command::Admin; - } else if ((strcasecmp(mask.c_str(), "KeyAt2") == 0)) { + } else if ((strcasecmp(mask.c_str(), "keyAt2") == 0)) { mode |= Command::KeyAt2; - } else if ((strcasecmp(mask.c_str(), "KeyAt3") == 0)) { + } else if ((strcasecmp(mask.c_str(), "keyAt3") == 0)) { mode |= Command::KeyAt3; } else { Throw(InvalidValue, "%s:%d unknown mode %s", n->file, n->line, mask.c_str());