4 Star 3 Fork 4

唯品会 / hiredis-vip

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
command.c 43.65 KB
一键复制 编辑 原始数据 按行查看 历史
deep011 提交于 2016-01-25 12:22 . Add info for command parse failed.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
#include <ctype.h>
#include <errno.h>
#include "command.h"
#include "hiutil.h"
#include "hiarray.h"
static uint64_t cmd_id = 0; /* command id counter */
/*
* Return true, if the redis command take no key, otherwise
* return false
*/
static int
redis_argz(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_PING:
case CMD_REQ_REDIS_QUIT:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts no arguments, otherwise
* return false
*/
static int
redis_arg0(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_EXISTS:
case CMD_REQ_REDIS_PERSIST:
case CMD_REQ_REDIS_PTTL:
case CMD_REQ_REDIS_SORT:
case CMD_REQ_REDIS_TTL:
case CMD_REQ_REDIS_TYPE:
case CMD_REQ_REDIS_DUMP:
case CMD_REQ_REDIS_DECR:
case CMD_REQ_REDIS_GET:
case CMD_REQ_REDIS_INCR:
case CMD_REQ_REDIS_STRLEN:
case CMD_REQ_REDIS_HGETALL:
case CMD_REQ_REDIS_HKEYS:
case CMD_REQ_REDIS_HLEN:
case CMD_REQ_REDIS_HVALS:
case CMD_REQ_REDIS_LLEN:
case CMD_REQ_REDIS_LPOP:
case CMD_REQ_REDIS_RPOP:
case CMD_REQ_REDIS_SCARD:
case CMD_REQ_REDIS_SMEMBERS:
case CMD_REQ_REDIS_SPOP:
case CMD_REQ_REDIS_ZCARD:
case CMD_REQ_REDIS_PFCOUNT:
case CMD_REQ_REDIS_AUTH:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts exactly 1 argument, otherwise
* return false
*/
static int
redis_arg1(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_EXPIRE:
case CMD_REQ_REDIS_EXPIREAT:
case CMD_REQ_REDIS_PEXPIRE:
case CMD_REQ_REDIS_PEXPIREAT:
case CMD_REQ_REDIS_APPEND:
case CMD_REQ_REDIS_DECRBY:
case CMD_REQ_REDIS_GETBIT:
case CMD_REQ_REDIS_GETSET:
case CMD_REQ_REDIS_INCRBY:
case CMD_REQ_REDIS_INCRBYFLOAT:
case CMD_REQ_REDIS_SETNX:
case CMD_REQ_REDIS_HEXISTS:
case CMD_REQ_REDIS_HGET:
case CMD_REQ_REDIS_LINDEX:
case CMD_REQ_REDIS_LPUSHX:
case CMD_REQ_REDIS_RPOPLPUSH:
case CMD_REQ_REDIS_RPUSHX:
case CMD_REQ_REDIS_SISMEMBER:
case CMD_REQ_REDIS_ZRANK:
case CMD_REQ_REDIS_ZREVRANK:
case CMD_REQ_REDIS_ZSCORE:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts exactly 2 arguments, otherwise
* return false
*/
static int
redis_arg2(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_GETRANGE:
case CMD_REQ_REDIS_PSETEX:
case CMD_REQ_REDIS_SETBIT:
case CMD_REQ_REDIS_SETEX:
case CMD_REQ_REDIS_SETRANGE:
case CMD_REQ_REDIS_HINCRBY:
case CMD_REQ_REDIS_HINCRBYFLOAT:
case CMD_REQ_REDIS_HSET:
case CMD_REQ_REDIS_HSETNX:
case CMD_REQ_REDIS_LRANGE:
case CMD_REQ_REDIS_LREM:
case CMD_REQ_REDIS_LSET:
case CMD_REQ_REDIS_LTRIM:
case CMD_REQ_REDIS_SMOVE:
case CMD_REQ_REDIS_ZCOUNT:
case CMD_REQ_REDIS_ZLEXCOUNT:
case CMD_REQ_REDIS_ZINCRBY:
case CMD_REQ_REDIS_ZREMRANGEBYLEX:
case CMD_REQ_REDIS_ZREMRANGEBYRANK:
case CMD_REQ_REDIS_ZREMRANGEBYSCORE:
case CMD_REQ_REDIS_RESTORE:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts exactly 3 arguments, otherwise
* return false
*/
static int
redis_arg3(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_LINSERT:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command accepts 0 or more arguments, otherwise
* return false
*/
static int
redis_argn(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_BITCOUNT:
case CMD_REQ_REDIS_SET:
case CMD_REQ_REDIS_HDEL:
case CMD_REQ_REDIS_HMGET:
case CMD_REQ_REDIS_HMSET:
case CMD_REQ_REDIS_HSCAN:
case CMD_REQ_REDIS_LPUSH:
case CMD_REQ_REDIS_RPUSH:
case CMD_REQ_REDIS_SADD:
case CMD_REQ_REDIS_SDIFF:
case CMD_REQ_REDIS_SDIFFSTORE:
case CMD_REQ_REDIS_SINTER:
case CMD_REQ_REDIS_SINTERSTORE:
case CMD_REQ_REDIS_SREM:
case CMD_REQ_REDIS_SUNION:
case CMD_REQ_REDIS_SUNIONSTORE:
case CMD_REQ_REDIS_SRANDMEMBER:
case CMD_REQ_REDIS_SSCAN:
case CMD_REQ_REDIS_PFADD:
case CMD_REQ_REDIS_PFMERGE:
case CMD_REQ_REDIS_ZADD:
case CMD_REQ_REDIS_ZINTERSTORE:
case CMD_REQ_REDIS_ZRANGE:
case CMD_REQ_REDIS_ZRANGEBYSCORE:
case CMD_REQ_REDIS_ZREM:
case CMD_REQ_REDIS_ZREVRANGE:
case CMD_REQ_REDIS_ZRANGEBYLEX:
case CMD_REQ_REDIS_ZREVRANGEBYSCORE:
case CMD_REQ_REDIS_ZUNIONSTORE:
case CMD_REQ_REDIS_ZSCAN:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command is a vector command accepting one or
* more keys, otherwise return false
*/
static int
redis_argx(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_MGET:
case CMD_REQ_REDIS_DEL:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command is a vector command accepting one or
* more key-value pairs, otherwise return false
*/
static int
redis_argkvx(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_MSET:
return 1;
default:
break;
}
return 0;
}
/*
* Return true, if the redis command is either EVAL or EVALSHA. These commands
* have a special format with exactly 2 arguments, followed by one or more keys,
* followed by zero or more arguments (the documentation online seems to suggest
* that at least one argument is required, but that shouldn't be the case).
*/
static int
redis_argeval(struct cmd *r)
{
switch (r->type) {
case CMD_REQ_REDIS_EVAL:
case CMD_REQ_REDIS_EVALSHA:
return 1;
default:
break;
}
return 0;
}
/*
* Reference: http://redis.io/topics/protocol
*
* Redis >= 1.2 uses the unified protocol to send requests to the Redis
* server. In the unified protocol all the arguments sent to the server
* are binary safe and every request has the following general form:
*
* *<number of arguments> CR LF
* $<number of bytes of argument 1> CR LF
* <argument data> CR LF
* ...
* $<number of bytes of argument N> CR LF
* <argument data> CR LF
*
* Before the unified request protocol, redis protocol for requests supported
* the following commands
* 1). Inline commands: simple commands where arguments are just space
* separated strings. No binary safeness is possible.
* 2). Bulk commands: bulk commands are exactly like inline commands, but
* the last argument is handled in a special way in order to allow for
* a binary-safe last argument.
*
* only supports the Redis unified protocol for requests.
*/
void
redis_parse_cmd(struct cmd *r)
{
int len;
char *p, *m, *token = NULL;
char *cmd_end;
char ch;
uint32_t rlen = 0; /* running length in parsing fsa */
uint32_t rnarg = 0; /* running # arg used by parsing fsa */
enum {
SW_START,
SW_NARG,
SW_NARG_LF,
SW_REQ_TYPE_LEN,
SW_REQ_TYPE_LEN_LF,
SW_REQ_TYPE,
SW_REQ_TYPE_LF,
SW_KEY_LEN,
SW_KEY_LEN_LF,
SW_KEY,
SW_KEY_LF,
SW_ARG1_LEN,
SW_ARG1_LEN_LF,
SW_ARG1,
SW_ARG1_LF,
SW_ARG2_LEN,
SW_ARG2_LEN_LF,
SW_ARG2,
SW_ARG2_LF,
SW_ARG3_LEN,
SW_ARG3_LEN_LF,
SW_ARG3,
SW_ARG3_LF,
SW_ARGN_LEN,
SW_ARGN_LEN_LF,
SW_ARGN,
SW_ARGN_LF,
SW_SENTINEL
} state;
state = SW_START;
cmd_end = r->cmd + r->clen;
ASSERT(state >= SW_START && state < SW_SENTINEL);
ASSERT(r->cmd != NULL && r->clen > 0);
for (p = r->cmd; p < cmd_end; p++) {
ch = *p;
switch (state) {
case SW_START:
case SW_NARG:
if (token == NULL) {
if (ch != '*') {
goto error;
}
token = p;
/* req_start <- p */
r->narg_start = p;
rnarg = 0;
state = SW_NARG;
} else if (isdigit(ch)) {
rnarg = rnarg * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if (rnarg == 0) {
goto error;
}
r->narg = rnarg;
r->narg_end = p;
token = NULL;
state = SW_NARG_LF;
} else {
goto error;
}
break;
case SW_NARG_LF:
switch (ch) {
case LF:
state = SW_REQ_TYPE_LEN;
break;
default:
goto error;
}
break;
case SW_REQ_TYPE_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
token = p;
rlen = 0;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if (rlen == 0 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_REQ_TYPE_LEN_LF;
} else {
goto error;
}
break;
case SW_REQ_TYPE_LEN_LF:
switch (ch) {
case LF:
state = SW_REQ_TYPE;
break;
default:
goto error;
}
break;
case SW_REQ_TYPE:
if (token == NULL) {
token = p;
}
m = token + rlen;
if (m >= cmd_end) {
//m = cmd_end - 1;
//p = m;
//break;
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
m = token;
token = NULL;
r->type = CMD_UNKNOWN;
switch (p - m) {
case 3:
if (str3icmp(m, 'g', 'e', 't')) {
r->type = CMD_REQ_REDIS_GET;
break;
}
if (str3icmp(m, 's', 'e', 't')) {
r->type = CMD_REQ_REDIS_SET;
break;
}
if (str3icmp(m, 't', 't', 'l')) {
r->type = CMD_REQ_REDIS_TTL;
break;
}
if (str3icmp(m, 'd', 'e', 'l')) {
r->type = CMD_REQ_REDIS_DEL;
break;
}
break;
case 4:
if (str4icmp(m, 'p', 't', 't', 'l')) {
r->type = CMD_REQ_REDIS_PTTL;
break;
}
if (str4icmp(m, 'd', 'e', 'c', 'r')) {
r->type = CMD_REQ_REDIS_DECR;
break;
}
if (str4icmp(m, 'd', 'u', 'm', 'p')) {
r->type = CMD_REQ_REDIS_DUMP;
break;
}
if (str4icmp(m, 'h', 'd', 'e', 'l')) {
r->type = CMD_REQ_REDIS_HDEL;
break;
}
if (str4icmp(m, 'h', 'g', 'e', 't')) {
r->type = CMD_REQ_REDIS_HGET;
break;
}
if (str4icmp(m, 'h', 'l', 'e', 'n')) {
r->type = CMD_REQ_REDIS_HLEN;
break;
}
if (str4icmp(m, 'h', 's', 'e', 't')) {
r->type = CMD_REQ_REDIS_HSET;
break;
}
if (str4icmp(m, 'i', 'n', 'c', 'r')) {
r->type = CMD_REQ_REDIS_INCR;
break;
}
if (str4icmp(m, 'l', 'l', 'e', 'n')) {
r->type = CMD_REQ_REDIS_LLEN;
break;
}
if (str4icmp(m, 'l', 'p', 'o', 'p')) {
r->type = CMD_REQ_REDIS_LPOP;
break;
}
if (str4icmp(m, 'l', 'r', 'e', 'm')) {
r->type = CMD_REQ_REDIS_LREM;
break;
}
if (str4icmp(m, 'l', 's', 'e', 't')) {
r->type = CMD_REQ_REDIS_LSET;
break;
}
if (str4icmp(m, 'r', 'p', 'o', 'p')) {
r->type = CMD_REQ_REDIS_RPOP;
break;
}
if (str4icmp(m, 's', 'a', 'd', 'd')) {
r->type = CMD_REQ_REDIS_SADD;
break;
}
if (str4icmp(m, 's', 'p', 'o', 'p')) {
r->type = CMD_REQ_REDIS_SPOP;
break;
}
if (str4icmp(m, 's', 'r', 'e', 'm')) {
r->type = CMD_REQ_REDIS_SREM;
break;
}
if (str4icmp(m, 't', 'y', 'p', 'e')) {
r->type = CMD_REQ_REDIS_TYPE;
break;
}
if (str4icmp(m, 'm', 'g', 'e', 't')) {
r->type = CMD_REQ_REDIS_MGET;
break;
}
if (str4icmp(m, 'm', 's', 'e', 't')) {
r->type = CMD_REQ_REDIS_MSET;
break;
}
if (str4icmp(m, 'z', 'a', 'd', 'd')) {
r->type = CMD_REQ_REDIS_ZADD;
break;
}
if (str4icmp(m, 'z', 'r', 'e', 'm')) {
r->type = CMD_REQ_REDIS_ZREM;
break;
}
if (str4icmp(m, 'e', 'v', 'a', 'l')) {
r->type = CMD_REQ_REDIS_EVAL;
break;
}
if (str4icmp(m, 's', 'o', 'r', 't')) {
r->type = CMD_REQ_REDIS_SORT;
break;
}
if (str4icmp(m, 'p', 'i', 'n', 'g')) {
r->type = CMD_REQ_REDIS_PING;
r->noforward = 1;
break;
}
if (str4icmp(m, 'q', 'u', 'i', 't')) {
r->type = CMD_REQ_REDIS_QUIT;
r->quit = 1;
break;
}
if (str4icmp(m, 'a', 'u', 't', 'h')) {
r->type = CMD_REQ_REDIS_AUTH;
r->noforward = 1;
break;
}
break;
case 5:
if (str5icmp(m, 'h', 'k', 'e', 'y', 's')) {
r->type = CMD_REQ_REDIS_HKEYS;
break;
}
if (str5icmp(m, 'h', 'm', 'g', 'e', 't')) {
r->type = CMD_REQ_REDIS_HMGET;
break;
}
if (str5icmp(m, 'h', 'm', 's', 'e', 't')) {
r->type = CMD_REQ_REDIS_HMSET;
break;
}
if (str5icmp(m, 'h', 'v', 'a', 'l', 's')) {
r->type = CMD_REQ_REDIS_HVALS;
break;
}
if (str5icmp(m, 'h', 's', 'c', 'a', 'n')) {
r->type = CMD_REQ_REDIS_HSCAN;
break;
}
if (str5icmp(m, 'l', 'p', 'u', 's', 'h')) {
r->type = CMD_REQ_REDIS_LPUSH;
break;
}
if (str5icmp(m, 'l', 't', 'r', 'i', 'm')) {
r->type = CMD_REQ_REDIS_LTRIM;
break;
}
if (str5icmp(m, 'r', 'p', 'u', 's', 'h')) {
r->type = CMD_REQ_REDIS_RPUSH;
break;
}
if (str5icmp(m, 's', 'c', 'a', 'r', 'd')) {
r->type = CMD_REQ_REDIS_SCARD;
break;
}
if (str5icmp(m, 's', 'd', 'i', 'f', 'f')) {
r->type = CMD_REQ_REDIS_SDIFF;
break;
}
if (str5icmp(m, 's', 'e', 't', 'e', 'x')) {
r->type = CMD_REQ_REDIS_SETEX;
break;
}
if (str5icmp(m, 's', 'e', 't', 'n', 'x')) {
r->type = CMD_REQ_REDIS_SETNX;
break;
}
if (str5icmp(m, 's', 'm', 'o', 'v', 'e')) {
r->type = CMD_REQ_REDIS_SMOVE;
break;
}
if (str5icmp(m, 's', 's', 'c', 'a', 'n')) {
r->type = CMD_REQ_REDIS_SSCAN;
break;
}
if (str5icmp(m, 'z', 'c', 'a', 'r', 'd')) {
r->type = CMD_REQ_REDIS_ZCARD;
break;
}
if (str5icmp(m, 'z', 'r', 'a', 'n', 'k')) {
r->type = CMD_REQ_REDIS_ZRANK;
break;
}
if (str5icmp(m, 'z', 's', 'c', 'a', 'n')) {
r->type = CMD_REQ_REDIS_ZSCAN;
break;
}
if (str5icmp(m, 'p', 'f', 'a', 'd', 'd')) {
r->type = CMD_REQ_REDIS_PFADD;
break;
}
break;
case 6:
if (str6icmp(m, 'a', 'p', 'p', 'e', 'n', 'd')) {
r->type = CMD_REQ_REDIS_APPEND;
break;
}
if (str6icmp(m, 'd', 'e', 'c', 'r', 'b', 'y')) {
r->type = CMD_REQ_REDIS_DECRBY;
break;
}
if (str6icmp(m, 'e', 'x', 'i', 's', 't', 's')) {
r->type = CMD_REQ_REDIS_EXISTS;
break;
}
if (str6icmp(m, 'e', 'x', 'p', 'i', 'r', 'e')) {
r->type = CMD_REQ_REDIS_EXPIRE;
break;
}
if (str6icmp(m, 'g', 'e', 't', 'b', 'i', 't')) {
r->type = CMD_REQ_REDIS_GETBIT;
break;
}
if (str6icmp(m, 'g', 'e', 't', 's', 'e', 't')) {
r->type = CMD_REQ_REDIS_GETSET;
break;
}
if (str6icmp(m, 'p', 's', 'e', 't', 'e', 'x')) {
r->type = CMD_REQ_REDIS_PSETEX;
break;
}
if (str6icmp(m, 'h', 's', 'e', 't', 'n', 'x')) {
r->type = CMD_REQ_REDIS_HSETNX;
break;
}
if (str6icmp(m, 'i', 'n', 'c', 'r', 'b', 'y')) {
r->type = CMD_REQ_REDIS_INCRBY;
break;
}
if (str6icmp(m, 'l', 'i', 'n', 'd', 'e', 'x')) {
r->type = CMD_REQ_REDIS_LINDEX;
break;
}
if (str6icmp(m, 'l', 'p', 'u', 's', 'h', 'x')) {
r->type = CMD_REQ_REDIS_LPUSHX;
break;
}
if (str6icmp(m, 'l', 'r', 'a', 'n', 'g', 'e')) {
r->type = CMD_REQ_REDIS_LRANGE;
break;
}
if (str6icmp(m, 'r', 'p', 'u', 's', 'h', 'x')) {
r->type = CMD_REQ_REDIS_RPUSHX;
break;
}
if (str6icmp(m, 's', 'e', 't', 'b', 'i', 't')) {
r->type = CMD_REQ_REDIS_SETBIT;
break;
}
if (str6icmp(m, 's', 'i', 'n', 't', 'e', 'r')) {
r->type = CMD_REQ_REDIS_SINTER;
break;
}
if (str6icmp(m, 's', 't', 'r', 'l', 'e', 'n')) {
r->type = CMD_REQ_REDIS_STRLEN;
break;
}
if (str6icmp(m, 's', 'u', 'n', 'i', 'o', 'n')) {
r->type = CMD_REQ_REDIS_SUNION;
break;
}
if (str6icmp(m, 'z', 'c', 'o', 'u', 'n', 't')) {
r->type = CMD_REQ_REDIS_ZCOUNT;
break;
}
if (str6icmp(m, 'z', 'r', 'a', 'n', 'g', 'e')) {
r->type = CMD_REQ_REDIS_ZRANGE;
break;
}
if (str6icmp(m, 'z', 's', 'c', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_ZSCORE;
break;
}
break;
case 7:
if (str7icmp(m, 'p', 'e', 'r', 's', 'i', 's', 't')) {
r->type = CMD_REQ_REDIS_PERSIST;
break;
}
if (str7icmp(m, 'p', 'e', 'x', 'p', 'i', 'r', 'e')) {
r->type = CMD_REQ_REDIS_PEXPIRE;
break;
}
if (str7icmp(m, 'h', 'e', 'x', 'i', 's', 't', 's')) {
r->type = CMD_REQ_REDIS_HEXISTS;
break;
}
if (str7icmp(m, 'h', 'g', 'e', 't', 'a', 'l', 'l')) {
r->type = CMD_REQ_REDIS_HGETALL;
break;
}
if (str7icmp(m, 'h', 'i', 'n', 'c', 'r', 'b', 'y')) {
r->type = CMD_REQ_REDIS_HINCRBY;
break;
}
if (str7icmp(m, 'l', 'i', 'n', 's', 'e', 'r', 't')) {
r->type = CMD_REQ_REDIS_LINSERT;
break;
}
if (str7icmp(m, 'z', 'i', 'n', 'c', 'r', 'b', 'y')) {
r->type = CMD_REQ_REDIS_ZINCRBY;
break;
}
if (str7icmp(m, 'e', 'v', 'a', 'l', 's', 'h', 'a')) {
r->type = CMD_REQ_REDIS_EVALSHA;
break;
}
if (str7icmp(m, 'r', 'e', 's', 't', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_RESTORE;
break;
}
if (str7icmp(m, 'p', 'f', 'c', 'o', 'u', 'n', 't')) {
r->type = CMD_REQ_REDIS_PFCOUNT;
break;
}
if (str7icmp(m, 'p', 'f', 'm', 'e', 'r', 'g', 'e')) {
r->type = CMD_REQ_REDIS_PFMERGE;
break;
}
break;
case 8:
if (str8icmp(m, 'e', 'x', 'p', 'i', 'r', 'e', 'a', 't')) {
r->type = CMD_REQ_REDIS_EXPIREAT;
break;
}
if (str8icmp(m, 'b', 'i', 't', 'c', 'o', 'u', 'n', 't')) {
r->type = CMD_REQ_REDIS_BITCOUNT;
break;
}
if (str8icmp(m, 'g', 'e', 't', 'r', 'a', 'n', 'g', 'e')) {
r->type = CMD_REQ_REDIS_GETRANGE;
break;
}
if (str8icmp(m, 's', 'e', 't', 'r', 'a', 'n', 'g', 'e')) {
r->type = CMD_REQ_REDIS_SETRANGE;
break;
}
if (str8icmp(m, 's', 'm', 'e', 'm', 'b', 'e', 'r', 's')) {
r->type = CMD_REQ_REDIS_SMEMBERS;
break;
}
if (str8icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'k')) {
r->type = CMD_REQ_REDIS_ZREVRANK;
break;
}
break;
case 9:
if (str9icmp(m, 'p', 'e', 'x', 'p', 'i', 'r', 'e', 'a', 't')) {
r->type = CMD_REQ_REDIS_PEXPIREAT;
break;
}
if (str9icmp(m, 'r', 'p', 'o', 'p', 'l', 'p', 'u', 's', 'h')) {
r->type = CMD_REQ_REDIS_RPOPLPUSH;
break;
}
if (str9icmp(m, 's', 'i', 's', 'm', 'e', 'm', 'b', 'e', 'r')) {
r->type = CMD_REQ_REDIS_SISMEMBER;
break;
}
if (str9icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e')) {
r->type = CMD_REQ_REDIS_ZREVRANGE;
break;
}
if (str9icmp(m, 'z', 'l', 'e', 'x', 'c', 'o', 'u', 'n', 't')) {
r->type = CMD_REQ_REDIS_ZLEXCOUNT;
break;
}
break;
case 10:
if (str10icmp(m, 's', 'd', 'i', 'f', 'f', 's', 't', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_SDIFFSTORE;
break;
}
case 11:
if (str11icmp(m, 'i', 'n', 'c', 'r', 'b', 'y', 'f', 'l', 'o', 'a', 't')) {
r->type = CMD_REQ_REDIS_INCRBYFLOAT;
break;
}
if (str11icmp(m, 's', 'i', 'n', 't', 'e', 'r', 's', 't', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_SINTERSTORE;
break;
}
if (str11icmp(m, 's', 'r', 'a', 'n', 'd', 'm', 'e', 'm', 'b', 'e', 'r')) {
r->type = CMD_REQ_REDIS_SRANDMEMBER;
break;
}
if (str11icmp(m, 's', 'u', 'n', 'i', 'o', 'n', 's', 't', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_SUNIONSTORE;
break;
}
if (str11icmp(m, 'z', 'i', 'n', 't', 'e', 'r', 's', 't', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_ZINTERSTORE;
break;
}
if (str11icmp(m, 'z', 'u', 'n', 'i', 'o', 'n', 's', 't', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_ZUNIONSTORE;
break;
}
if (str11icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
r->type = CMD_REQ_REDIS_ZRANGEBYLEX;
break;
}
break;
case 12:
if (str12icmp(m, 'h', 'i', 'n', 'c', 'r', 'b', 'y', 'f', 'l', 'o', 'a', 't')) {
r->type = CMD_REQ_REDIS_HINCRBYFLOAT;
break;
}
break;
case 13:
if (str13icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_ZRANGEBYSCORE;
break;
}
break;
case 14:
if (str14icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
r->type = CMD_REQ_REDIS_ZREMRANGEBYLEX;
break;
}
break;
case 15:
if (str15icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'r', 'a', 'n', 'k')) {
r->type = CMD_REQ_REDIS_ZREMRANGEBYRANK;
break;
}
break;
case 16:
if (str16icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_ZREMRANGEBYSCORE;
break;
}
if (str16icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
r->type = CMD_REQ_REDIS_ZREVRANGEBYSCORE;
break;
}
break;
default:
break;
}
if (r->type == CMD_UNKNOWN) {
goto error;
}
state = SW_REQ_TYPE_LF;
break;
case SW_REQ_TYPE_LF:
switch (ch) {
case LF:
if (redis_argz(r)) {
goto done;
} else if (redis_argeval(r)) {
state = SW_ARG1_LEN;
} else {
state = SW_KEY_LEN;
}
break;
default:
goto error;
}
break;
case SW_KEY_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
token = p;
rlen = 0;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if (rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_KEY_LEN_LF;
} else {
goto error;
}
break;
case SW_KEY_LEN_LF:
switch (ch) {
case LF:
state = SW_KEY;
break;
default:
goto error;
}
break;
case SW_KEY:
if (token == NULL) {
token = p;
}
m = token + rlen;
if (m >= cmd_end) {
//m = b->last - 1;
//p = m;
//break;
goto error;
}
if (*m != CR) {
goto error;
} else { /* got a key */
struct keypos *kpos;
p = m; /* move forward by rlen bytes */
rlen = 0;
m = token;
token = NULL;
kpos = hiarray_push(r->keys);
if (kpos == NULL) {
goto enomem;
}
kpos->start = m;
kpos->end = p;
//kpos->v_len = 0;
state = SW_KEY_LF;
}
break;
case SW_KEY_LF:
switch (ch) {
case LF:
if (redis_arg0(r)) {
if (rnarg != 0) {
goto error;
}
goto done;
} else if (redis_arg1(r)) {
if (rnarg != 1) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_arg2(r)) {
if (rnarg != 2) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_arg3(r)) {
if (rnarg != 3) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_argn(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARG1_LEN;
} else if (redis_argx(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_KEY_LEN;
} else if (redis_argkvx(r)) {
if (rnarg == 0) {
goto done;
}
if (r->narg % 2 == 0) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_argeval(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_ARG1_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
rlen = 0;
token = p;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if ((p - token) <= 1 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
/*
//for mset value length
if(redis_argkvx(r))
{
struct keypos *kpos;
uint32_t array_len = array_n(r->keys);
if(array_len == 0)
{
goto error;
}
kpos = array_n(r->keys, array_len-1);
if (kpos == NULL || kpos->v_len != 0) {
goto error;
}
kpos->v_len = rlen;
}
*/
state = SW_ARG1_LEN_LF;
} else {
goto error;
}
break;
case SW_ARG1_LEN_LF:
switch (ch) {
case LF:
state = SW_ARG1;
break;
default:
goto error;
}
break;
case SW_ARG1:
m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
//m = b->last - 1;
//p = m;
//break;
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
state = SW_ARG1_LF;
break;
case SW_ARG1_LF:
switch (ch) {
case LF:
if (redis_arg1(r)) {
if (rnarg != 0) {
goto error;
}
goto done;
} else if (redis_arg2(r)) {
if (rnarg != 1) {
goto error;
}
state = SW_ARG2_LEN;
} else if (redis_arg3(r)) {
if (rnarg != 2) {
goto error;
}
state = SW_ARG2_LEN;
} else if (redis_argn(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else if (redis_argeval(r)) {
if (rnarg < 2) {
goto error;
}
state = SW_ARG2_LEN;
} else if (redis_argkvx(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_KEY_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_ARG2_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
rlen = 0;
token = p;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if ((p - token) <= 1 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_ARG2_LEN_LF;
} else {
goto error;
}
break;
case SW_ARG2_LEN_LF:
switch (ch) {
case LF:
state = SW_ARG2;
break;
default:
goto error;
}
break;
case SW_ARG2:
if (token == NULL && redis_argeval(r)) {
/*
* For EVAL/EVALSHA, ARG2 represents the # key/arg pairs which must
* be tokenized and stored in contiguous memory.
*/
token = p;
}
m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
//m = b->last - 1;
//p = m;
//break;
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
if (redis_argeval(r)) {
uint32_t nkey;
char *chp;
/*
* For EVAL/EVALSHA, we need to find the integer value of this
* argument. It tells us the number of keys in the script, and
* we need to error out if number of keys is 0. At this point,
* both p and m point to the end of the argument and r->token
* points to the start.
*/
if (p - token < 1) {
goto error;
}
for (nkey = 0, chp = token; chp < p; chp++) {
if (isdigit(*chp)) {
nkey = nkey * 10 + (uint32_t)(*chp - '0');
} else {
goto error;
}
}
if (nkey == 0) {
goto error;
}
token = NULL;
}
state = SW_ARG2_LF;
break;
case SW_ARG2_LF:
switch (ch) {
case LF:
if (redis_arg2(r)) {
if (rnarg != 0) {
goto error;
}
goto done;
} else if (redis_arg3(r)) {
if (rnarg != 1) {
goto error;
}
state = SW_ARG3_LEN;
} else if (redis_argn(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else if (redis_argeval(r)) {
if (rnarg < 1) {
goto error;
}
state = SW_KEY_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_ARG3_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
rlen = 0;
token = p;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if ((p - token) <= 1 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_ARG3_LEN_LF;
} else {
goto error;
}
break;
case SW_ARG3_LEN_LF:
switch (ch) {
case LF:
state = SW_ARG3;
break;
default:
goto error;
}
break;
case SW_ARG3:
m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
//m = b->last - 1;
//p = m;
//break;
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
state = SW_ARG3_LF;
break;
case SW_ARG3_LF:
switch (ch) {
case LF:
if (redis_arg3(r)) {
if (rnarg != 0) {
goto error;
}
goto done;
} else if (redis_argn(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_ARGN_LEN:
if (token == NULL) {
if (ch != '$') {
goto error;
}
rlen = 0;
token = p;
} else if (isdigit(ch)) {
rlen = rlen * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
if ((p - token) <= 1 || rnarg == 0) {
goto error;
}
rnarg--;
token = NULL;
state = SW_ARGN_LEN_LF;
} else {
goto error;
}
break;
case SW_ARGN_LEN_LF:
switch (ch) {
case LF:
state = SW_ARGN;
break;
default:
goto error;
}
break;
case SW_ARGN:
m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
//m = b->last - 1;
//p = m;
//break;
goto error;
}
if (*m != CR) {
goto error;
}
p = m; /* move forward by rlen bytes */
rlen = 0;
state = SW_ARGN_LF;
break;
case SW_ARGN_LF:
switch (ch) {
case LF:
if (redis_argn(r) || redis_argeval(r)) {
if (rnarg == 0) {
goto done;
}
state = SW_ARGN_LEN;
} else {
goto error;
}
break;
default:
goto error;
}
break;
case SW_SENTINEL:
default:
NOT_REACHED();
break;
}
}
ASSERT(p == cmd_end);
return;
done:
ASSERT(r->type > CMD_UNKNOWN && r->type < CMD_SENTINEL);
r->result = CMD_PARSE_OK;
return;
enomem:
r->result = CMD_PARSE_ENOMEM;
return;
error:
r->result = CMD_PARSE_ERROR;
errno = EINVAL;
if(r->errstr == NULL){
r->errstr = hi_alloc(100*sizeof(*r->errstr));
}
len = _scnprintf(r->errstr, 100, "Parse command error. Cmd type: %d, state: %d, break position: %d.",
r->type, state, (int)(p - r->cmd));
r->errstr[len] = '\0';
}
struct cmd *command_get()
{
struct cmd *command;
command = hi_alloc(sizeof(struct cmd));
if(command == NULL)
{
return NULL;
}
command->id = ++cmd_id;
command->result = CMD_PARSE_OK;
command->errstr = NULL;
command->type = CMD_UNKNOWN;
command->cmd = NULL;
command->clen = 0;
command->keys = NULL;
command->narg_start = NULL;
command->narg_end = NULL;
command->narg = 0;
command->quit = 0;
command->noforward = 0;
command->slot_num = -1;
command->frag_seq = NULL;
command->reply = NULL;
command->sub_commands = NULL;
command->keys = hiarray_create(1, sizeof(struct keypos));
if (command->keys == NULL)
{
hi_free(command);
return NULL;
}
return command;
}
void command_destroy(struct cmd *command)
{
if(command == NULL)
{
return;
}
if(command->cmd != NULL)
{
free(command->cmd);
}
if(command->errstr != NULL){
hi_free(command->errstr);
}
if(command->keys != NULL)
{
command->keys->nelem = 0;
hiarray_destroy(command->keys);
}
if(command->frag_seq != NULL)
{
hi_free(command->frag_seq);
command->frag_seq = NULL;
}
if(command->reply != NULL)
{
freeReplyObject(command->reply);
}
if(command->sub_commands != NULL)
{
listRelease(command->sub_commands);
}
hi_free(command);
}
C
1
https://gitee.com/vipshop/hiredis-vip.git
git@gitee.com:vipshop/hiredis-vip.git
vipshop
hiredis-vip
hiredis-vip
master

搜索帮助