From: antirez Date: Mon, 31 May 2010 08:08:14 +0000 (+0200) Subject: Merge branch 'no-appendfsync-on-rewrite' X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/752203d86d9667c07a69a11043df411703c32640?hp=d5d23dabd7a72c63ceda017d560c362b4065d85b Merge branch 'no-appendfsync-on-rewrite' --- diff --git a/redis-cli.c b/redis-cli.c index 279bf10c..2daa7c46 100644 --- a/redis-cli.c +++ b/redis-cli.c @@ -53,6 +53,7 @@ static struct config { int hostport; long repeat; int dbnum; + int argn_from_stdin; int interactive; int shutdown; int monitor_mode; @@ -61,137 +62,9 @@ static struct config { char *auth; } config; -struct redisCommand { - char *name; - int arity; - int flags; -}; - -#define CMDFLAG_NONE 0 -#define CMDFLAG_RAWOUTPUT 1 - -static struct redisCommand cmdTable[] = { - {"auth",2,CMDFLAG_NONE}, - {"get",2,CMDFLAG_NONE}, - {"set",3,CMDFLAG_NONE}, - {"setnx",3,CMDFLAG_NONE}, - {"setex",4,CMDFLAG_NONE}, - {"append",3,CMDFLAG_NONE}, - {"substr",4,CMDFLAG_NONE}, - {"del",-2,CMDFLAG_NONE}, - {"exists",2,CMDFLAG_NONE}, - {"incr",2,CMDFLAG_NONE}, - {"decr",2,CMDFLAG_NONE}, - {"rpush",3,CMDFLAG_NONE}, - {"lpush",3,CMDFLAG_NONE}, - {"rpop",2,CMDFLAG_NONE}, - {"lpop",2,CMDFLAG_NONE}, - {"brpop",-3,CMDFLAG_NONE}, - {"blpop",-3,CMDFLAG_NONE}, - {"llen",2,CMDFLAG_NONE}, - {"lindex",3,CMDFLAG_NONE}, - {"lset",4,CMDFLAG_NONE}, - {"lrange",4,CMDFLAG_NONE}, - {"ltrim",4,CMDFLAG_NONE}, - {"lrem",4,CMDFLAG_NONE}, - {"rpoplpush",3,CMDFLAG_NONE}, - {"sadd",3,CMDFLAG_NONE}, - {"srem",3,CMDFLAG_NONE}, - {"smove",4,CMDFLAG_NONE}, - {"sismember",3,CMDFLAG_NONE}, - {"scard",2,CMDFLAG_NONE}, - {"spop",2,CMDFLAG_NONE}, - {"srandmember",2,CMDFLAG_NONE}, - {"sinter",-2,CMDFLAG_NONE}, - {"sinterstore",-3,CMDFLAG_NONE}, - {"sunion",-2,CMDFLAG_NONE}, - {"sunionstore",-3,CMDFLAG_NONE}, - {"sdiff",-2,CMDFLAG_NONE}, - {"sdiffstore",-3,CMDFLAG_NONE}, - {"smembers",2,CMDFLAG_NONE}, - {"zadd",4,CMDFLAG_NONE}, - {"zincrby",4,CMDFLAG_NONE}, - {"zrem",3,CMDFLAG_NONE}, - {"zremrangebyscore",4,CMDFLAG_NONE}, - {"zunion",-4,CMDFLAG_NONE}, - {"zinter",-4,CMDFLAG_NONE}, - {"zrange",-4,CMDFLAG_NONE}, - {"zrank",3,CMDFLAG_NONE}, - {"zrevrank",3,CMDFLAG_NONE}, - {"zrangebyscore",-4,CMDFLAG_NONE}, - {"zcount",4,CMDFLAG_NONE}, - {"zrevrange",-4,CMDFLAG_NONE}, - {"zcard",2,CMDFLAG_NONE}, - {"zscore",3,CMDFLAG_NONE}, - {"incrby",3,CMDFLAG_NONE}, - {"decrby",3,CMDFLAG_NONE}, - {"getset",3,CMDFLAG_NONE}, - {"randomkey",1,CMDFLAG_NONE}, - {"select",2,CMDFLAG_NONE}, - {"move",3,CMDFLAG_NONE}, - {"rename",3,CMDFLAG_NONE}, - {"renamenx",3,CMDFLAG_NONE}, - {"keys",2,CMDFLAG_NONE}, - {"dbsize",1,CMDFLAG_NONE}, - {"ping",1,CMDFLAG_NONE}, - {"echo",2,CMDFLAG_NONE}, - {"save",1,CMDFLAG_NONE}, - {"bgsave",1,CMDFLAG_NONE}, - {"rewriteaof",1,CMDFLAG_NONE}, - {"bgrewriteaof",1,CMDFLAG_NONE}, - {"shutdown",1,CMDFLAG_NONE}, - {"lastsave",1,CMDFLAG_NONE}, - {"type",2,CMDFLAG_NONE}, - {"flushdb",1,CMDFLAG_NONE}, - {"flushall",1,CMDFLAG_NONE}, - {"sort",-2,CMDFLAG_NONE}, - {"info",1,CMDFLAG_RAWOUTPUT}, - {"mget",-2,CMDFLAG_NONE}, - {"expire",3,CMDFLAG_NONE}, - {"expireat",3,CMDFLAG_NONE}, - {"ttl",2,CMDFLAG_NONE}, - {"slaveof",3,CMDFLAG_NONE}, - {"debug",-2,CMDFLAG_NONE}, - {"mset",-3,CMDFLAG_NONE}, - {"msetnx",-3,CMDFLAG_NONE}, - {"monitor",1,CMDFLAG_NONE}, - {"multi",1,CMDFLAG_NONE}, - {"exec",1,CMDFLAG_NONE}, - {"discard",1,CMDFLAG_NONE}, - {"hset",4,CMDFLAG_NONE}, - {"hget",3,CMDFLAG_NONE}, - {"hmset",-4,CMDFLAG_NONE}, - {"hmget",-3,CMDFLAG_NONE}, - {"hincrby",4,CMDFLAG_NONE}, - {"hdel",3,CMDFLAG_NONE}, - {"hlen",2,CMDFLAG_NONE}, - {"hkeys",2,CMDFLAG_NONE}, - {"hvals",2,CMDFLAG_NONE}, - {"hgetall",2,CMDFLAG_NONE}, - {"hexists",3,CMDFLAG_NONE}, - {"config",-2,CMDFLAG_NONE}, - {"subscribe",-2,CMDFLAG_NONE}, - {"unsubscribe",-1,CMDFLAG_NONE}, - {"psubscribe",-2,CMDFLAG_NONE}, - {"punsubscribe",-1,CMDFLAG_NONE}, - {"publish",3,CMDFLAG_NONE}, - {"watch",-2,CMDFLAG_NONE}, - {"unwatch",1,CMDFLAG_NONE}, - {NULL,0,CMDFLAG_NONE} -}; - static int cliReadReply(int fd); static void usage(); -static struct redisCommand *lookupCommand(char *name) { - int j = 0; - while(cmdTable[j].name != NULL) { - if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; - j++; - } - return NULL; -} - static int cliConnect(void) { char err[ANET_ERR_LEN]; static int fd = ANET_ERR; @@ -361,26 +234,15 @@ static int selectDb(int fd) { } static int cliSendCommand(int argc, char **argv, int repeat) { - struct redisCommand *rc = lookupCommand(argv[0]); + char *command = argv[0]; int fd, j, retval = 0; sds cmd; - if (!rc) { - fprintf(stderr,"Unknown command '%s'\n",argv[0]); - return 1; - } - config.raw_output = (rc->flags & CMDFLAG_RAWOUTPUT); - - if ((rc->arity > 0 && argc != rc->arity) || - (rc->arity < 0 && argc < -rc->arity)) { - fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name); - return 1; - } - - if (!strcasecmp(rc->name,"shutdown")) config.shutdown = 1; - if (!strcasecmp(rc->name,"monitor")) config.monitor_mode = 1; - if (!strcasecmp(rc->name,"subscribe") || - !strcasecmp(rc->name,"psubscribe")) config.pubsub_mode = 1; + config.raw_output = !strcasecmp(command,"info"); + if (!strcasecmp(command,"shutdown")) config.shutdown = 1; + if (!strcasecmp(command,"monitor")) config.monitor_mode = 1; + if (!strcasecmp(command,"subscribe") || + !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1; if ((fd = cliConnect()) == -1) return 1; /* Select db number */ @@ -390,18 +252,17 @@ static int cliSendCommand(int argc, char **argv, int repeat) { return 1; } + /* Build the command to send */ + cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc); + for (j = 0; j < argc; j++) { + cmd = sdscatprintf(cmd,"$%lu\r\n", + (unsigned long)sdslen(argv[j])); + cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); + cmd = sdscatlen(cmd,"\r\n",2); + } + while(repeat--) { - /* Build the command to send */ - cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc); - for (j = 0; j < argc; j++) { - cmd = sdscatprintf(cmd,"$%lu\r\n", - (unsigned long)sdslen(argv[j])); - cmd = sdscatlen(cmd,argv[j],sdslen(argv[j])); - cmd = sdscatlen(cmd,"\r\n",2); - } anetWrite(fd,cmd,sdslen(cmd)); - sdsfree(cmd); - while (config.monitor_mode) { cliReadSingleLineReply(fd,0); } @@ -415,7 +276,6 @@ static int cliSendCommand(int argc, char **argv, int repeat) { } retval = cliReadReply(fd); - if (retval) { return retval; } @@ -453,6 +313,8 @@ static int parseOptions(int argc, char **argv) { i++; } else if (!strcmp(argv[i],"-i")) { config.interactive = 1; + } else if (!strcmp(argv[i],"-c")) { + config.argn_from_stdin = 1; } else { break; } @@ -479,7 +341,7 @@ static sds readArgFromStdin(void) { static void usage() { fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n"); - fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-a authpw] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n"); + fprintf(stderr, "usage: echo \"argN\" | redis-cli -c [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n"); fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n"); fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n"); fprintf(stderr, "example: redis-cli get my_passwd\n"); @@ -491,7 +353,7 @@ static void usage() { /* Turn the plain C strings into Sds strings */ static char **convertToSds(int count, char** args) { int j; - char **sds = zmalloc(sizeof(char*)*count+1); + char **sds = zmalloc(sizeof(char*)*count); for(j = 0; j < count; j++) sds[j] = sdsnew(args[j]); @@ -594,12 +456,12 @@ static void repl() { int main(int argc, char **argv) { int firstarg; char **argvcopy; - struct redisCommand *rc; config.hostip = "127.0.0.1"; config.hostport = 6379; config.repeat = 1; config.dbnum = 0; + config.argn_from_stdin = 0; config.shutdown = 0; config.interactive = 0; config.monitor_mode = 0; @@ -621,16 +483,11 @@ int main(int argc, char **argv) { if (argc == 0 || config.interactive == 1) repl(); - argvcopy = convertToSds(argc, argv); - - /* Read the last argument from stdandard input if needed */ - if ((rc = lookupCommand(argv[0])) != NULL) { - if (rc->arity > 0 && argc == rc->arity-1) { + argvcopy = convertToSds(argc+1, argv); + if (config.argn_from_stdin) { sds lastarg = readArgFromStdin(); argvcopy[argc] = lastarg; argc++; - } } - return cliSendCommand(argc, argvcopy, config.repeat); } diff --git a/redis.c b/redis.c index 8c4e3ab2..949bb58e 100644 --- a/redis.c +++ b/redis.c @@ -753,7 +753,8 @@ static void unwatchCommand(redisClient *c); /* Global vars */ static struct redisServer server; /* server global state */ -static struct redisCommand cmdTable[] = { +static struct redisCommand *commandTable; +static struct redisCommand readonlyCommandTable[] = { {"get",getCommand,2,REDIS_CMD_INLINE,NULL,1,1,1}, {"set",setCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0}, {"setnx",setnxCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0}, @@ -861,8 +862,7 @@ static struct redisCommand cmdTable[] = { {"punsubscribe",punsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0}, {"publish",publishCommand,3,REDIS_CMD_BULK|REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0}, {"watch",watchCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0}, - {"unwatch",unwatchCommand,1,REDIS_CMD_INLINE,NULL,0,0,0}, - {NULL,NULL,0,0,NULL,0,0,0} + {"unwatch",unwatchCommand,1,REDIS_CMD_INLINE,NULL,0,0,0} }; /*============================ Utility functions ============================ */ @@ -2254,13 +2254,29 @@ static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int } } +static int qsortRedisCommands(const void *r1, const void *r2) { + return strcasecmp( + ((struct redisCommand*)r1)->name, + ((struct redisCommand*)r2)->name); +} + +static void sortCommandTable() { + /* Copy and sort the read-only version of the command table */ + commandTable = (struct redisCommand*)malloc(sizeof(readonlyCommandTable)); + memcpy(commandTable,readonlyCommandTable,sizeof(readonlyCommandTable)); + qsort(commandTable, + sizeof(readonlyCommandTable)/sizeof(struct redisCommand), + sizeof(struct redisCommand),qsortRedisCommands); +} + static struct redisCommand *lookupCommand(char *name) { - int j = 0; - while(cmdTable[j].name != NULL) { - if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j]; - j++; - } - return NULL; + struct redisCommand tmp = {name,NULL,0,0,NULL,0,0,0}; + return bsearch( + &tmp, + commandTable, + sizeof(readonlyCommandTable)/sizeof(struct redisCommand), + sizeof(struct redisCommand), + qsortRedisCommands); } /* resetClient prepare the client to process the next command */ @@ -5741,6 +5757,11 @@ static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scor zset *zs; double *score; + if (isnan(scoreval)) { + addReplySds(c,sdsnew("-ERR provide score is Not A Number (nan)\r\n")); + return; + } + zsetobj = lookupKeyWrite(c->db,key); if (zsetobj == NULL) { zsetobj = createZsetObject(); @@ -5769,6 +5790,15 @@ static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scor } else { *score = scoreval; } + if (isnan(*score)) { + addReplySds(c, + sdsnew("-ERR resulting score is Not A Number (nan)\r\n")); + zfree(score); + /* Note that we don't need to check if the zset may be empty and + * should be removed here, as we can only obtain Nan as score if + * there was already an element in the sorted set. */ + return; + } } else { *score = scoreval; } @@ -10943,6 +10973,7 @@ int main(int argc, char **argv) { time_t start; initServerConfig(); + sortCommandTable(); if (argc == 2) { if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) version(); diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index cb78515d..9eb61f25 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -397,4 +397,23 @@ start_server default.conf {} { } set _ $err } {} + + test {ZSET element can't be set to nan with ZADD} { + set e {} + catch {r zadd myzset nan abc} e + set _ $e + } {*Not A Number*} + + test {ZSET element can't be set to nan with ZINCRBY} { + set e {} + catch {r zincrby myzset nan abc} e + set _ $e + } {*Not A Number*} + + test {ZINCRBY calls leading to Nan are refused} { + set e {} + r zincrby myzset +inf abc + catch {r zincrby myzset -inf abc} e + set _ $e + } {*Not A Number*} }