2 * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #define REDIS_VERSION "1.050"
40 #define __USE_POSIX199309
46 #endif /* HAVE_BACKTRACE */
54 #include <arpa/inet.h>
58 #include <sys/resource.h>
63 #include "ae.h" /* Event driven programming library */
64 #include "sds.h" /* Dynamic safe strings */
65 #include "anet.h" /* Networking the easy way */
66 #include "dict.h" /* Hash tables */
67 #include "adlist.h" /* Linked lists */
68 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
69 #include "lzf.h" /* LZF compression library */
70 #include "pqsort.h" /* Partial qsort for SORT+LIMIT */
76 /* Static server configuration */
77 #define REDIS_SERVERPORT 6379 /* TCP port */
78 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
79 #define REDIS_IOBUF_LEN 1024
80 #define REDIS_LOADBUF_LEN 1024
81 #define REDIS_STATIC_ARGS 4
82 #define REDIS_DEFAULT_DBNUM 16
83 #define REDIS_CONFIGLINE_MAX 1024
84 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
85 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
86 #define REDIS_EXPIRELOOKUPS_PER_CRON 100 /* try to expire 100 keys/second */
87 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
88 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
90 /* Hash table parameters */
91 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
94 #define REDIS_CMD_BULK 1 /* Bulk write command */
95 #define REDIS_CMD_INLINE 2 /* Inline command */
96 /* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
97 this flags will return an error when the 'maxmemory' option is set in the
98 config file and the server is using more than maxmemory bytes of memory.
99 In short this commands are denied on low memory conditions. */
100 #define REDIS_CMD_DENYOOM 4
103 #define REDIS_STRING 0
109 /* Objects encoding */
110 #define REDIS_ENCODING_RAW 0 /* Raw representation */
111 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
113 /* Object types only used for dumping to disk */
114 #define REDIS_EXPIRETIME 253
115 #define REDIS_SELECTDB 254
116 #define REDIS_EOF 255
118 /* Defines related to the dump file format. To store 32 bits lengths for short
119 * keys requires a lot of space, so we check the most significant 2 bits of
120 * the first byte to interpreter the length:
122 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
123 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
124 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
125 * 11|000000 this means: specially encoded object will follow. The six bits
126 * number specify the kind of object that follows.
127 * See the REDIS_RDB_ENC_* defines.
129 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
130 * values, will fit inside. */
131 #define REDIS_RDB_6BITLEN 0
132 #define REDIS_RDB_14BITLEN 1
133 #define REDIS_RDB_32BITLEN 2
134 #define REDIS_RDB_ENCVAL 3
135 #define REDIS_RDB_LENERR UINT_MAX
137 /* When a length of a string object stored on disk has the first two bits
138 * set, the remaining two bits specify a special encoding for the object
139 * accordingly to the following defines: */
140 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
141 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
142 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
143 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
146 #define REDIS_CLOSE 1 /* This client connection should be closed ASAP */
147 #define REDIS_SLAVE 2 /* This client is a slave server */
148 #define REDIS_MASTER 4 /* This client is a master server */
149 #define REDIS_MONITOR 8 /* This client is a slave monitor, see MONITOR */
151 /* Slave replication state - slave side */
152 #define REDIS_REPL_NONE 0 /* No active replication */
153 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
154 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
156 /* Slave replication state - from the point of view of master
157 * Note that in SEND_BULK and ONLINE state the slave receives new updates
158 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
159 * to start the next background saving in order to send updates to it. */
160 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
161 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
162 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
163 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
165 /* List related stuff */
169 /* Sort operations */
170 #define REDIS_SORT_GET 0
171 #define REDIS_SORT_DEL 1
172 #define REDIS_SORT_INCR 2
173 #define REDIS_SORT_DECR 3
174 #define REDIS_SORT_ASC 4
175 #define REDIS_SORT_DESC 5
176 #define REDIS_SORTKEY_MAX 1024
179 #define REDIS_DEBUG 0
180 #define REDIS_NOTICE 1
181 #define REDIS_WARNING 2
183 /* Anti-warning macro... */
184 #define REDIS_NOTUSED(V) ((void) V)
186 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
187 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
189 /*================================= Data types ============================== */
191 /* A redis object, that is a type able to hold a string / list / set */
192 typedef struct redisObject
{
195 unsigned char encoding
;
196 unsigned char notused
[2];
200 typedef struct redisDb
{
206 /* With multiplexing we need to take per-clinet state.
207 * Clients are taken in a liked list. */
208 typedef struct redisClient
{
213 robj
**argv
, **mbargv
;
215 int bulklen
; /* bulk read len. -1 if not in bulk read mode */
216 int multibulk
; /* multi bulk command format active */
219 time_t lastinteraction
; /* time of the last interaction, used for timeout */
220 int flags
; /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
221 int slaveseldb
; /* slave selected db, if this client is a slave */
222 int authenticated
; /* when requirepass is non-NULL */
223 int replstate
; /* replication state if this is a slave */
224 int repldbfd
; /* replication DB file descriptor */
225 long repldboff
; /* replication DB file offset */
226 off_t repldbsize
; /* replication DB file size */
234 /* Global server state structure */
240 unsigned int sharingpoolsize
;
241 long long dirty
; /* changes to DB from the last save */
243 list
*slaves
, *monitors
;
244 char neterr
[ANET_ERR_LEN
];
246 int cronloops
; /* number of times the cron function run */
247 list
*objfreelist
; /* A list of freed objects to avoid malloc() */
248 time_t lastsave
; /* Unix time of last save succeeede */
249 size_t usedmemory
; /* Used memory in megabytes */
250 /* Fields used only for stats */
251 time_t stat_starttime
; /* server start time */
252 long long stat_numcommands
; /* number of processed commands */
253 long long stat_numconnections
; /* number of connections received */
261 int bgsaveinprogress
;
262 pid_t bgsavechildpid
;
263 struct saveparam
*saveparams
;
270 /* Replication related */
274 redisClient
*master
; /* client that is master for this slave */
276 unsigned int maxclients
;
277 unsigned long maxmemory
;
278 /* Sort parameters - qsort_r() is only available under BSD so we
279 * have to take this state global, in order to pass it to sortCompare() */
285 typedef void redisCommandProc(redisClient
*c
);
286 struct redisCommand
{
288 redisCommandProc
*proc
;
293 struct redisFunctionSym
{
295 unsigned long pointer
;
298 typedef struct _redisSortObject
{
306 typedef struct _redisSortOperation
{
309 } redisSortOperation
;
311 /* ZSETs use a specialized version of Skiplists */
313 typedef struct zskiplistNode
{
314 struct zskiplistNode
**forward
;
315 struct zskiplistNode
*backward
;
320 typedef struct zskiplist
{
321 struct zskiplistNode
*header
, *tail
;
326 typedef struct zset
{
331 /* Our shared "common" objects */
333 struct sharedObjectsStruct
{
334 robj
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *pong
, *space
,
335 *colon
, *nullbulk
, *nullmultibulk
,
336 *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
,
337 *outofrangeerr
, *plus
,
338 *select0
, *select1
, *select2
, *select3
, *select4
,
339 *select5
, *select6
, *select7
, *select8
, *select9
;
342 /* Global vars that are actally used as constants. The following double
343 * values are used for double on-disk serialization, and are initialized
344 * at runtime to avoid strange compiler optimizations. */
346 static double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
;
348 /*================================ Prototypes =============================== */
350 static void freeStringObject(robj
*o
);
351 static void freeListObject(robj
*o
);
352 static void freeSetObject(robj
*o
);
353 static void decrRefCount(void *o
);
354 static robj
*createObject(int type
, void *ptr
);
355 static void freeClient(redisClient
*c
);
356 static int rdbLoad(char *filename
);
357 static void addReply(redisClient
*c
, robj
*obj
);
358 static void addReplySds(redisClient
*c
, sds s
);
359 static void incrRefCount(robj
*o
);
360 static int rdbSaveBackground(char *filename
);
361 static robj
*createStringObject(char *ptr
, size_t len
);
362 static void replicationFeedSlaves(list
*slaves
, struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
);
363 static int syncWithMaster(void);
364 static robj
*tryObjectSharing(robj
*o
);
365 static int tryObjectEncoding(robj
*o
);
366 static robj
*getDecodedObject(const robj
*o
);
367 static int removeExpire(redisDb
*db
, robj
*key
);
368 static int expireIfNeeded(redisDb
*db
, robj
*key
);
369 static int deleteIfVolatile(redisDb
*db
, robj
*key
);
370 static int deleteKey(redisDb
*db
, robj
*key
);
371 static time_t getExpire(redisDb
*db
, robj
*key
);
372 static int setExpire(redisDb
*db
, robj
*key
, time_t when
);
373 static void updateSlavesWaitingBgsave(int bgsaveerr
);
374 static void freeMemoryIfNeeded(void);
375 static int processCommand(redisClient
*c
);
376 static void setupSigSegvAction(void);
377 static void rdbRemoveTempFile(pid_t childpid
);
378 static size_t stringObjectLen(robj
*o
);
379 static void processInputBuffer(redisClient
*c
);
380 static zskiplist
*zslCreate(void);
381 static void zslFree(zskiplist
*zsl
);
382 static void zslInsert(zskiplist
*zsl
, double score
, robj
*obj
);
384 static void authCommand(redisClient
*c
);
385 static void pingCommand(redisClient
*c
);
386 static void echoCommand(redisClient
*c
);
387 static void setCommand(redisClient
*c
);
388 static void setnxCommand(redisClient
*c
);
389 static void getCommand(redisClient
*c
);
390 static void delCommand(redisClient
*c
);
391 static void existsCommand(redisClient
*c
);
392 static void incrCommand(redisClient
*c
);
393 static void decrCommand(redisClient
*c
);
394 static void incrbyCommand(redisClient
*c
);
395 static void decrbyCommand(redisClient
*c
);
396 static void selectCommand(redisClient
*c
);
397 static void randomkeyCommand(redisClient
*c
);
398 static void keysCommand(redisClient
*c
);
399 static void dbsizeCommand(redisClient
*c
);
400 static void lastsaveCommand(redisClient
*c
);
401 static void saveCommand(redisClient
*c
);
402 static void bgsaveCommand(redisClient
*c
);
403 static void shutdownCommand(redisClient
*c
);
404 static void moveCommand(redisClient
*c
);
405 static void renameCommand(redisClient
*c
);
406 static void renamenxCommand(redisClient
*c
);
407 static void lpushCommand(redisClient
*c
);
408 static void rpushCommand(redisClient
*c
);
409 static void lpopCommand(redisClient
*c
);
410 static void rpopCommand(redisClient
*c
);
411 static void llenCommand(redisClient
*c
);
412 static void lindexCommand(redisClient
*c
);
413 static void lrangeCommand(redisClient
*c
);
414 static void ltrimCommand(redisClient
*c
);
415 static void typeCommand(redisClient
*c
);
416 static void lsetCommand(redisClient
*c
);
417 static void saddCommand(redisClient
*c
);
418 static void sremCommand(redisClient
*c
);
419 static void smoveCommand(redisClient
*c
);
420 static void sismemberCommand(redisClient
*c
);
421 static void scardCommand(redisClient
*c
);
422 static void spopCommand(redisClient
*c
);
423 static void srandmemberCommand(redisClient
*c
);
424 static void sinterCommand(redisClient
*c
);
425 static void sinterstoreCommand(redisClient
*c
);
426 static void sunionCommand(redisClient
*c
);
427 static void sunionstoreCommand(redisClient
*c
);
428 static void sdiffCommand(redisClient
*c
);
429 static void sdiffstoreCommand(redisClient
*c
);
430 static void syncCommand(redisClient
*c
);
431 static void flushdbCommand(redisClient
*c
);
432 static void flushallCommand(redisClient
*c
);
433 static void sortCommand(redisClient
*c
);
434 static void lremCommand(redisClient
*c
);
435 static void infoCommand(redisClient
*c
);
436 static void mgetCommand(redisClient
*c
);
437 static void monitorCommand(redisClient
*c
);
438 static void expireCommand(redisClient
*c
);
439 static void getsetCommand(redisClient
*c
);
440 static void ttlCommand(redisClient
*c
);
441 static void slaveofCommand(redisClient
*c
);
442 static void debugCommand(redisClient
*c
);
443 static void msetCommand(redisClient
*c
);
444 static void msetnxCommand(redisClient
*c
);
445 static void zaddCommand(redisClient
*c
);
446 static void zrangeCommand(redisClient
*c
);
447 static void zrangebyscoreCommand(redisClient
*c
);
448 static void zrevrangeCommand(redisClient
*c
);
449 static void zlenCommand(redisClient
*c
);
450 static void zremCommand(redisClient
*c
);
451 static void zscoreCommand(redisClient
*c
);
453 /*================================= Globals ================================= */
456 static struct redisServer server
; /* server global state */
457 static struct redisCommand cmdTable
[] = {
458 {"get",getCommand
,2,REDIS_CMD_INLINE
},
459 {"set",setCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
460 {"setnx",setnxCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
461 {"del",delCommand
,-2,REDIS_CMD_INLINE
},
462 {"exists",existsCommand
,2,REDIS_CMD_INLINE
},
463 {"incr",incrCommand
,2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
464 {"decr",decrCommand
,2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
465 {"mget",mgetCommand
,-2,REDIS_CMD_INLINE
},
466 {"rpush",rpushCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
467 {"lpush",lpushCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
468 {"rpop",rpopCommand
,2,REDIS_CMD_INLINE
},
469 {"lpop",lpopCommand
,2,REDIS_CMD_INLINE
},
470 {"llen",llenCommand
,2,REDIS_CMD_INLINE
},
471 {"lindex",lindexCommand
,3,REDIS_CMD_INLINE
},
472 {"lset",lsetCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
473 {"lrange",lrangeCommand
,4,REDIS_CMD_INLINE
},
474 {"ltrim",ltrimCommand
,4,REDIS_CMD_INLINE
},
475 {"lrem",lremCommand
,4,REDIS_CMD_BULK
},
476 {"sadd",saddCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
477 {"srem",sremCommand
,3,REDIS_CMD_BULK
},
478 {"smove",smoveCommand
,4,REDIS_CMD_BULK
},
479 {"sismember",sismemberCommand
,3,REDIS_CMD_BULK
},
480 {"scard",scardCommand
,2,REDIS_CMD_INLINE
},
481 {"spop",spopCommand
,2,REDIS_CMD_INLINE
},
482 {"srandmember",srandmemberCommand
,2,REDIS_CMD_INLINE
},
483 {"sinter",sinterCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
484 {"sinterstore",sinterstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
485 {"sunion",sunionCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
486 {"sunionstore",sunionstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
487 {"sdiff",sdiffCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
488 {"sdiffstore",sdiffstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
489 {"smembers",sinterCommand
,2,REDIS_CMD_INLINE
},
490 {"zadd",zaddCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
491 {"zrem",zremCommand
,3,REDIS_CMD_BULK
},
492 {"zrange",zrangeCommand
,4,REDIS_CMD_INLINE
},
493 {"zrangebyscore",zrangebyscoreCommand
,4,REDIS_CMD_INLINE
},
494 {"zrevrange",zrevrangeCommand
,4,REDIS_CMD_INLINE
},
495 {"zlen",zlenCommand
,2,REDIS_CMD_INLINE
},
496 {"zscore",zscoreCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
497 {"incrby",incrbyCommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
498 {"decrby",decrbyCommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
499 {"getset",getsetCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
500 {"mset",msetCommand
,-3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
501 {"msetnx",msetnxCommand
,-3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
},
502 {"randomkey",randomkeyCommand
,1,REDIS_CMD_INLINE
},
503 {"select",selectCommand
,2,REDIS_CMD_INLINE
},
504 {"move",moveCommand
,3,REDIS_CMD_INLINE
},
505 {"rename",renameCommand
,3,REDIS_CMD_INLINE
},
506 {"renamenx",renamenxCommand
,3,REDIS_CMD_INLINE
},
507 {"expire",expireCommand
,3,REDIS_CMD_INLINE
},
508 {"keys",keysCommand
,2,REDIS_CMD_INLINE
},
509 {"dbsize",dbsizeCommand
,1,REDIS_CMD_INLINE
},
510 {"auth",authCommand
,2,REDIS_CMD_INLINE
},
511 {"ping",pingCommand
,1,REDIS_CMD_INLINE
},
512 {"echo",echoCommand
,2,REDIS_CMD_BULK
},
513 {"save",saveCommand
,1,REDIS_CMD_INLINE
},
514 {"bgsave",bgsaveCommand
,1,REDIS_CMD_INLINE
},
515 {"shutdown",shutdownCommand
,1,REDIS_CMD_INLINE
},
516 {"lastsave",lastsaveCommand
,1,REDIS_CMD_INLINE
},
517 {"type",typeCommand
,2,REDIS_CMD_INLINE
},
518 {"sync",syncCommand
,1,REDIS_CMD_INLINE
},
519 {"flushdb",flushdbCommand
,1,REDIS_CMD_INLINE
},
520 {"flushall",flushallCommand
,1,REDIS_CMD_INLINE
},
521 {"sort",sortCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
},
522 {"info",infoCommand
,1,REDIS_CMD_INLINE
},
523 {"monitor",monitorCommand
,1,REDIS_CMD_INLINE
},
524 {"ttl",ttlCommand
,2,REDIS_CMD_INLINE
},
525 {"slaveof",slaveofCommand
,3,REDIS_CMD_INLINE
},
526 {"debug",debugCommand
,-2,REDIS_CMD_INLINE
},
529 /*============================ Utility functions ============================ */
531 /* Glob-style pattern matching. */
532 int stringmatchlen(const char *pattern
, int patternLen
,
533 const char *string
, int stringLen
, int nocase
)
538 while (pattern
[1] == '*') {
543 return 1; /* match */
545 if (stringmatchlen(pattern
+1, patternLen
-1,
546 string
, stringLen
, nocase
))
547 return 1; /* match */
551 return 0; /* no match */
555 return 0; /* no match */
565 not = pattern
[0] == '^';
572 if (pattern
[0] == '\\') {
575 if (pattern
[0] == string
[0])
577 } else if (pattern
[0] == ']') {
579 } else if (patternLen
== 0) {
583 } else if (pattern
[1] == '-' && patternLen
>= 3) {
584 int start
= pattern
[0];
585 int end
= pattern
[2];
593 start
= tolower(start
);
599 if (c
>= start
&& c
<= end
)
603 if (pattern
[0] == string
[0])
606 if (tolower((int)pattern
[0]) == tolower((int)string
[0]))
616 return 0; /* no match */
622 if (patternLen
>= 2) {
629 if (pattern
[0] != string
[0])
630 return 0; /* no match */
632 if (tolower((int)pattern
[0]) != tolower((int)string
[0]))
633 return 0; /* no match */
641 if (stringLen
== 0) {
642 while(*pattern
== '*') {
649 if (patternLen
== 0 && stringLen
== 0)
654 static void redisLog(int level
, const char *fmt
, ...) {
658 fp
= (server
.logfile
== NULL
) ? stdout
: fopen(server
.logfile
,"a");
662 if (level
>= server
.verbosity
) {
668 strftime(buf
,64,"%d %b %H:%M:%S",gmtime(&now
));
669 fprintf(fp
,"%s %c ",buf
,c
[level
]);
670 vfprintf(fp
, fmt
, ap
);
676 if (server
.logfile
) fclose(fp
);
679 /*====================== Hash table type implementation ==================== */
681 /* This is an hash table type that uses the SDS dynamic strings libary as
682 * keys and radis objects as values (objects can hold SDS strings,
685 static void dictVanillaFree(void *privdata
, void *val
)
687 DICT_NOTUSED(privdata
);
691 static int sdsDictKeyCompare(void *privdata
, const void *key1
,
695 DICT_NOTUSED(privdata
);
697 l1
= sdslen((sds
)key1
);
698 l2
= sdslen((sds
)key2
);
699 if (l1
!= l2
) return 0;
700 return memcmp(key1
, key2
, l1
) == 0;
703 static void dictRedisObjectDestructor(void *privdata
, void *val
)
705 DICT_NOTUSED(privdata
);
710 static int dictObjKeyCompare(void *privdata
, const void *key1
,
713 const robj
*o1
= key1
, *o2
= key2
;
714 return sdsDictKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
717 static unsigned int dictObjHash(const void *key
) {
719 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
722 static int dictEncObjKeyCompare(void *privdata
, const void *key1
,
725 const robj
*o1
= key1
, *o2
= key2
;
727 if (o1
->encoding
== REDIS_ENCODING_RAW
&&
728 o2
->encoding
== REDIS_ENCODING_RAW
)
729 return sdsDictKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
734 dec1
= o1
->encoding
!= REDIS_ENCODING_RAW
?
735 getDecodedObject(o1
) : (robj
*)o1
;
736 dec2
= o2
->encoding
!= REDIS_ENCODING_RAW
?
737 getDecodedObject(o2
) : (robj
*)o2
;
738 cmp
= sdsDictKeyCompare(privdata
,dec1
->ptr
,dec2
->ptr
);
739 if (dec1
!= o1
) decrRefCount(dec1
);
740 if (dec2
!= o2
) decrRefCount(dec2
);
745 static unsigned int dictEncObjHash(const void *key
) {
748 if (o
->encoding
== REDIS_ENCODING_RAW
)
749 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
751 robj
*dec
= getDecodedObject(o
);
752 unsigned int hash
= dictGenHashFunction(dec
->ptr
, sdslen((sds
)dec
->ptr
));
758 static dictType setDictType
= {
759 dictEncObjHash
, /* hash function */
762 dictEncObjKeyCompare
, /* key compare */
763 dictRedisObjectDestructor
, /* key destructor */
764 NULL
/* val destructor */
767 static dictType zsetDictType
= {
768 dictEncObjHash
, /* hash function */
771 dictEncObjKeyCompare
, /* key compare */
772 dictRedisObjectDestructor
, /* key destructor */
773 dictVanillaFree
/* val destructor */
776 static dictType hashDictType
= {
777 dictObjHash
, /* hash function */
780 dictObjKeyCompare
, /* key compare */
781 dictRedisObjectDestructor
, /* key destructor */
782 dictRedisObjectDestructor
/* val destructor */
785 /* ========================= Random utility functions ======================= */
787 /* Redis generally does not try to recover from out of memory conditions
788 * when allocating objects or strings, it is not clear if it will be possible
789 * to report this condition to the client since the networking layer itself
790 * is based on heap allocation for send buffers, so we simply abort.
791 * At least the code will be simpler to read... */
792 static void oom(const char *msg
) {
793 fprintf(stderr
, "%s: Out of memory\n",msg
);
799 /* ====================== Redis server networking stuff ===================== */
800 static void closeTimedoutClients(void) {
803 time_t now
= time(NULL
);
805 listRewind(server
.clients
);
806 while ((ln
= listYield(server
.clients
)) != NULL
) {
807 c
= listNodeValue(ln
);
808 if (!(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
809 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
810 (now
- c
->lastinteraction
> server
.maxidletime
)) {
811 redisLog(REDIS_DEBUG
,"Closing idle client");
817 static int htNeedsResize(dict
*dict
) {
818 long long size
, used
;
820 size
= dictSlots(dict
);
821 used
= dictSize(dict
);
822 return (size
&& used
&& size
> DICT_HT_INITIAL_SIZE
&&
823 (used
*100/size
< REDIS_HT_MINFILL
));
826 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
827 * we resize the hash table to save memory */
828 static void tryResizeHashTables(void) {
831 for (j
= 0; j
< server
.dbnum
; j
++) {
832 if (htNeedsResize(server
.db
[j
].dict
)) {
833 redisLog(REDIS_DEBUG
,"The hash table %d is too sparse, resize it...",j
);
834 dictResize(server
.db
[j
].dict
);
835 redisLog(REDIS_DEBUG
,"Hash table %d resized.",j
);
837 if (htNeedsResize(server
.db
[j
].expires
))
838 dictResize(server
.db
[j
].expires
);
842 static int serverCron(struct aeEventLoop
*eventLoop
, long long id
, void *clientData
) {
843 int j
, loops
= server
.cronloops
++;
844 REDIS_NOTUSED(eventLoop
);
846 REDIS_NOTUSED(clientData
);
848 /* Update the global state with the amount of used memory */
849 server
.usedmemory
= zmalloc_used_memory();
851 /* Show some info about non-empty databases */
852 for (j
= 0; j
< server
.dbnum
; j
++) {
853 long long size
, used
, vkeys
;
855 size
= dictSlots(server
.db
[j
].dict
);
856 used
= dictSize(server
.db
[j
].dict
);
857 vkeys
= dictSize(server
.db
[j
].expires
);
858 if (!(loops
% 5) && (used
|| vkeys
)) {
859 redisLog(REDIS_DEBUG
,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j
,used
,vkeys
,size
);
860 /* dictPrintStats(server.dict); */
864 /* We don't want to resize the hash tables while a bacground saving
865 * is in progress: the saving child is created using fork() that is
866 * implemented with a copy-on-write semantic in most modern systems, so
867 * if we resize the HT while there is the saving child at work actually
868 * a lot of memory movements in the parent will cause a lot of pages
870 if (!server
.bgsaveinprogress
) tryResizeHashTables();
872 /* Show information about connected clients */
874 redisLog(REDIS_DEBUG
,"%d clients connected (%d slaves), %zu bytes in use, %d shared objects",
875 listLength(server
.clients
)-listLength(server
.slaves
),
876 listLength(server
.slaves
),
878 dictSize(server
.sharingpool
));
881 /* Close connections of timedout clients */
882 if (server
.maxidletime
&& !(loops
% 10))
883 closeTimedoutClients();
885 /* Check if a background saving in progress terminated */
886 if (server
.bgsaveinprogress
) {
888 if (wait4(-1,&statloc
,WNOHANG
,NULL
)) {
889 int exitcode
= WEXITSTATUS(statloc
);
890 int bysignal
= WIFSIGNALED(statloc
);
892 if (!bysignal
&& exitcode
== 0) {
893 redisLog(REDIS_NOTICE
,
894 "Background saving terminated with success");
896 server
.lastsave
= time(NULL
);
897 } else if (!bysignal
&& exitcode
!= 0) {
898 redisLog(REDIS_WARNING
, "Background saving error");
900 redisLog(REDIS_WARNING
,
901 "Background saving terminated by signal");
902 rdbRemoveTempFile(server
.bgsavechildpid
);
904 server
.bgsaveinprogress
= 0;
905 server
.bgsavechildpid
= -1;
906 updateSlavesWaitingBgsave(exitcode
== 0 ? REDIS_OK
: REDIS_ERR
);
909 /* If there is not a background saving in progress check if
910 * we have to save now */
911 time_t now
= time(NULL
);
912 for (j
= 0; j
< server
.saveparamslen
; j
++) {
913 struct saveparam
*sp
= server
.saveparams
+j
;
915 if (server
.dirty
>= sp
->changes
&&
916 now
-server
.lastsave
> sp
->seconds
) {
917 redisLog(REDIS_NOTICE
,"%d changes in %d seconds. Saving...",
918 sp
->changes
, sp
->seconds
);
919 rdbSaveBackground(server
.dbfilename
);
925 /* Try to expire a few timed out keys */
926 for (j
= 0; j
< server
.dbnum
; j
++) {
927 redisDb
*db
= server
.db
+j
;
928 int num
= dictSize(db
->expires
);
931 time_t now
= time(NULL
);
933 if (num
> REDIS_EXPIRELOOKUPS_PER_CRON
)
934 num
= REDIS_EXPIRELOOKUPS_PER_CRON
;
939 if ((de
= dictGetRandomKey(db
->expires
)) == NULL
) break;
940 t
= (time_t) dictGetEntryVal(de
);
942 deleteKey(db
,dictGetEntryKey(de
));
948 /* Check if we should connect to a MASTER */
949 if (server
.replstate
== REDIS_REPL_CONNECT
) {
950 redisLog(REDIS_NOTICE
,"Connecting to MASTER...");
951 if (syncWithMaster() == REDIS_OK
) {
952 redisLog(REDIS_NOTICE
,"MASTER <-> SLAVE sync succeeded");
958 static void createSharedObjects(void) {
959 shared
.crlf
= createObject(REDIS_STRING
,sdsnew("\r\n"));
960 shared
.ok
= createObject(REDIS_STRING
,sdsnew("+OK\r\n"));
961 shared
.err
= createObject(REDIS_STRING
,sdsnew("-ERR\r\n"));
962 shared
.emptybulk
= createObject(REDIS_STRING
,sdsnew("$0\r\n\r\n"));
963 shared
.czero
= createObject(REDIS_STRING
,sdsnew(":0\r\n"));
964 shared
.cone
= createObject(REDIS_STRING
,sdsnew(":1\r\n"));
965 shared
.nullbulk
= createObject(REDIS_STRING
,sdsnew("$-1\r\n"));
966 shared
.nullmultibulk
= createObject(REDIS_STRING
,sdsnew("*-1\r\n"));
967 shared
.emptymultibulk
= createObject(REDIS_STRING
,sdsnew("*0\r\n"));
969 shared
.pong
= createObject(REDIS_STRING
,sdsnew("+PONG\r\n"));
970 shared
.wrongtypeerr
= createObject(REDIS_STRING
,sdsnew(
971 "-ERR Operation against a key holding the wrong kind of value\r\n"));
972 shared
.nokeyerr
= createObject(REDIS_STRING
,sdsnew(
973 "-ERR no such key\r\n"));
974 shared
.syntaxerr
= createObject(REDIS_STRING
,sdsnew(
975 "-ERR syntax error\r\n"));
976 shared
.sameobjecterr
= createObject(REDIS_STRING
,sdsnew(
977 "-ERR source and destination objects are the same\r\n"));
978 shared
.outofrangeerr
= createObject(REDIS_STRING
,sdsnew(
979 "-ERR index out of range\r\n"));
980 shared
.space
= createObject(REDIS_STRING
,sdsnew(" "));
981 shared
.colon
= createObject(REDIS_STRING
,sdsnew(":"));
982 shared
.plus
= createObject(REDIS_STRING
,sdsnew("+"));
983 shared
.select0
= createStringObject("select 0\r\n",10);
984 shared
.select1
= createStringObject("select 1\r\n",10);
985 shared
.select2
= createStringObject("select 2\r\n",10);
986 shared
.select3
= createStringObject("select 3\r\n",10);
987 shared
.select4
= createStringObject("select 4\r\n",10);
988 shared
.select5
= createStringObject("select 5\r\n",10);
989 shared
.select6
= createStringObject("select 6\r\n",10);
990 shared
.select7
= createStringObject("select 7\r\n",10);
991 shared
.select8
= createStringObject("select 8\r\n",10);
992 shared
.select9
= createStringObject("select 9\r\n",10);
995 static void appendServerSaveParams(time_t seconds
, int changes
) {
996 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
997 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
998 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
999 server
.saveparamslen
++;
1002 static void ResetServerSaveParams() {
1003 zfree(server
.saveparams
);
1004 server
.saveparams
= NULL
;
1005 server
.saveparamslen
= 0;
1008 static void initServerConfig() {
1009 server
.dbnum
= REDIS_DEFAULT_DBNUM
;
1010 server
.port
= REDIS_SERVERPORT
;
1011 server
.verbosity
= REDIS_DEBUG
;
1012 server
.maxidletime
= REDIS_MAXIDLETIME
;
1013 server
.saveparams
= NULL
;
1014 server
.logfile
= NULL
; /* NULL = log on standard output */
1015 server
.bindaddr
= NULL
;
1016 server
.glueoutputbuf
= 1;
1017 server
.daemonize
= 0;
1018 server
.pidfile
= "/var/run/redis.pid";
1019 server
.dbfilename
= "dump.rdb";
1020 server
.requirepass
= NULL
;
1021 server
.shareobjects
= 0;
1022 server
.sharingpoolsize
= 1024;
1023 server
.maxclients
= 0;
1024 server
.maxmemory
= 0;
1025 ResetServerSaveParams();
1027 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1028 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1029 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1030 /* Replication related */
1032 server
.masterhost
= NULL
;
1033 server
.masterport
= 6379;
1034 server
.master
= NULL
;
1035 server
.replstate
= REDIS_REPL_NONE
;
1037 /* Double constants initialization */
1039 R_PosInf
= 1.0/R_Zero
;
1040 R_NegInf
= -1.0/R_Zero
;
1041 R_Nan
= R_Zero
/R_Zero
;
1044 static void initServer() {
1047 signal(SIGHUP
, SIG_IGN
);
1048 signal(SIGPIPE
, SIG_IGN
);
1049 setupSigSegvAction();
1051 server
.clients
= listCreate();
1052 server
.slaves
= listCreate();
1053 server
.monitors
= listCreate();
1054 server
.objfreelist
= listCreate();
1055 createSharedObjects();
1056 server
.el
= aeCreateEventLoop();
1057 server
.db
= zmalloc(sizeof(redisDb
)*server
.dbnum
);
1058 server
.sharingpool
= dictCreate(&setDictType
,NULL
);
1059 server
.fd
= anetTcpServer(server
.neterr
, server
.port
, server
.bindaddr
);
1060 if (server
.fd
== -1) {
1061 redisLog(REDIS_WARNING
, "Opening TCP port: %s", server
.neterr
);
1064 for (j
= 0; j
< server
.dbnum
; j
++) {
1065 server
.db
[j
].dict
= dictCreate(&hashDictType
,NULL
);
1066 server
.db
[j
].expires
= dictCreate(&setDictType
,NULL
);
1067 server
.db
[j
].id
= j
;
1069 server
.cronloops
= 0;
1070 server
.bgsaveinprogress
= 0;
1071 server
.bgsavechildpid
= -1;
1072 server
.lastsave
= time(NULL
);
1074 server
.usedmemory
= 0;
1075 server
.stat_numcommands
= 0;
1076 server
.stat_numconnections
= 0;
1077 server
.stat_starttime
= time(NULL
);
1078 aeCreateTimeEvent(server
.el
, 1000, serverCron
, NULL
, NULL
);
1081 /* Empty the whole database */
1082 static long long emptyDb() {
1084 long long removed
= 0;
1086 for (j
= 0; j
< server
.dbnum
; j
++) {
1087 removed
+= dictSize(server
.db
[j
].dict
);
1088 dictEmpty(server
.db
[j
].dict
);
1089 dictEmpty(server
.db
[j
].expires
);
1094 static int yesnotoi(char *s
) {
1095 if (!strcasecmp(s
,"yes")) return 1;
1096 else if (!strcasecmp(s
,"no")) return 0;
1100 /* I agree, this is a very rudimental way to load a configuration...
1101 will improve later if the config gets more complex */
1102 static void loadServerConfig(char *filename
) {
1104 char buf
[REDIS_CONFIGLINE_MAX
+1], *err
= NULL
;
1108 if (filename
[0] == '-' && filename
[1] == '\0')
1111 if ((fp
= fopen(filename
,"r")) == NULL
) {
1112 redisLog(REDIS_WARNING
,"Fatal error, can't open config file");
1117 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
) {
1123 line
= sdstrim(line
," \t\r\n");
1125 /* Skip comments and blank lines*/
1126 if (line
[0] == '#' || line
[0] == '\0') {
1131 /* Split into arguments */
1132 argv
= sdssplitlen(line
,sdslen(line
)," ",1,&argc
);
1133 sdstolower(argv
[0]);
1135 /* Execute config directives */
1136 if (!strcasecmp(argv
[0],"timeout") && argc
== 2) {
1137 server
.maxidletime
= atoi(argv
[1]);
1138 if (server
.maxidletime
< 0) {
1139 err
= "Invalid timeout value"; goto loaderr
;
1141 } else if (!strcasecmp(argv
[0],"port") && argc
== 2) {
1142 server
.port
= atoi(argv
[1]);
1143 if (server
.port
< 1 || server
.port
> 65535) {
1144 err
= "Invalid port"; goto loaderr
;
1146 } else if (!strcasecmp(argv
[0],"bind") && argc
== 2) {
1147 server
.bindaddr
= zstrdup(argv
[1]);
1148 } else if (!strcasecmp(argv
[0],"save") && argc
== 3) {
1149 int seconds
= atoi(argv
[1]);
1150 int changes
= atoi(argv
[2]);
1151 if (seconds
< 1 || changes
< 0) {
1152 err
= "Invalid save parameters"; goto loaderr
;
1154 appendServerSaveParams(seconds
,changes
);
1155 } else if (!strcasecmp(argv
[0],"dir") && argc
== 2) {
1156 if (chdir(argv
[1]) == -1) {
1157 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
1158 argv
[1], strerror(errno
));
1161 } else if (!strcasecmp(argv
[0],"loglevel") && argc
== 2) {
1162 if (!strcasecmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
1163 else if (!strcasecmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
1164 else if (!strcasecmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
1166 err
= "Invalid log level. Must be one of debug, notice, warning";
1169 } else if (!strcasecmp(argv
[0],"logfile") && argc
== 2) {
1172 server
.logfile
= zstrdup(argv
[1]);
1173 if (!strcasecmp(server
.logfile
,"stdout")) {
1174 zfree(server
.logfile
);
1175 server
.logfile
= NULL
;
1177 if (server
.logfile
) {
1178 /* Test if we are able to open the file. The server will not
1179 * be able to abort just for this problem later... */
1180 logfp
= fopen(server
.logfile
,"a");
1181 if (logfp
== NULL
) {
1182 err
= sdscatprintf(sdsempty(),
1183 "Can't open the log file: %s", strerror(errno
));
1188 } else if (!strcasecmp(argv
[0],"databases") && argc
== 2) {
1189 server
.dbnum
= atoi(argv
[1]);
1190 if (server
.dbnum
< 1) {
1191 err
= "Invalid number of databases"; goto loaderr
;
1193 } else if (!strcasecmp(argv
[0],"maxclients") && argc
== 2) {
1194 server
.maxclients
= atoi(argv
[1]);
1195 } else if (!strcasecmp(argv
[0],"maxmemory") && argc
== 2) {
1196 server
.maxmemory
= strtoll(argv
[1], NULL
, 10);
1197 } else if (!strcasecmp(argv
[0],"slaveof") && argc
== 3) {
1198 server
.masterhost
= sdsnew(argv
[1]);
1199 server
.masterport
= atoi(argv
[2]);
1200 server
.replstate
= REDIS_REPL_CONNECT
;
1201 } else if (!strcasecmp(argv
[0],"glueoutputbuf") && argc
== 2) {
1202 if ((server
.glueoutputbuf
= yesnotoi(argv
[1])) == -1) {
1203 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1205 } else if (!strcasecmp(argv
[0],"shareobjects") && argc
== 2) {
1206 if ((server
.shareobjects
= yesnotoi(argv
[1])) == -1) {
1207 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1209 } else if (!strcasecmp(argv
[0],"shareobjectspoolsize") && argc
== 2) {
1210 server
.sharingpoolsize
= atoi(argv
[1]);
1211 if (server
.sharingpoolsize
< 1) {
1212 err
= "invalid object sharing pool size"; goto loaderr
;
1214 } else if (!strcasecmp(argv
[0],"daemonize") && argc
== 2) {
1215 if ((server
.daemonize
= yesnotoi(argv
[1])) == -1) {
1216 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1218 } else if (!strcasecmp(argv
[0],"requirepass") && argc
== 2) {
1219 server
.requirepass
= zstrdup(argv
[1]);
1220 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
1221 server
.pidfile
= zstrdup(argv
[1]);
1222 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
1223 server
.dbfilename
= zstrdup(argv
[1]);
1225 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
1227 for (j
= 0; j
< argc
; j
++)
1232 if (fp
!= stdin
) fclose(fp
);
1236 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
1237 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
1238 fprintf(stderr
, ">>> '%s'\n", line
);
1239 fprintf(stderr
, "%s\n", err
);
1243 static void freeClientArgv(redisClient
*c
) {
1246 for (j
= 0; j
< c
->argc
; j
++)
1247 decrRefCount(c
->argv
[j
]);
1248 for (j
= 0; j
< c
->mbargc
; j
++)
1249 decrRefCount(c
->mbargv
[j
]);
1254 static void freeClient(redisClient
*c
) {
1257 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
1258 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
1259 sdsfree(c
->querybuf
);
1260 listRelease(c
->reply
);
1263 ln
= listSearchKey(server
.clients
,c
);
1265 listDelNode(server
.clients
,ln
);
1266 if (c
->flags
& REDIS_SLAVE
) {
1267 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
1269 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
1270 ln
= listSearchKey(l
,c
);
1274 if (c
->flags
& REDIS_MASTER
) {
1275 server
.master
= NULL
;
1276 server
.replstate
= REDIS_REPL_CONNECT
;
1283 static void glueReplyBuffersIfNeeded(redisClient
*c
) {
1288 listRewind(c
->reply
);
1289 while((ln
= listYield(c
->reply
))) {
1291 totlen
+= sdslen(o
->ptr
);
1292 /* This optimization makes more sense if we don't have to copy
1294 if (totlen
> 1024) return;
1300 listRewind(c
->reply
);
1301 while((ln
= listYield(c
->reply
))) {
1303 memcpy(buf
+copylen
,o
->ptr
,sdslen(o
->ptr
));
1304 copylen
+= sdslen(o
->ptr
);
1305 listDelNode(c
->reply
,ln
);
1307 /* Now the output buffer is empty, add the new single element */
1308 o
= createObject(REDIS_STRING
,sdsnewlen(buf
,totlen
));
1309 listAddNodeTail(c
->reply
,o
);
1313 static void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1314 redisClient
*c
= privdata
;
1315 int nwritten
= 0, totwritten
= 0, objlen
;
1318 REDIS_NOTUSED(mask
);
1320 if (server
.glueoutputbuf
&& listLength(c
->reply
) > 1)
1321 glueReplyBuffersIfNeeded(c
);
1322 while(listLength(c
->reply
)) {
1323 o
= listNodeValue(listFirst(c
->reply
));
1324 objlen
= sdslen(o
->ptr
);
1327 listDelNode(c
->reply
,listFirst(c
->reply
));
1331 if (c
->flags
& REDIS_MASTER
) {
1332 /* Don't reply to a master */
1333 nwritten
= objlen
- c
->sentlen
;
1335 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
, objlen
- c
->sentlen
);
1336 if (nwritten
<= 0) break;
1338 c
->sentlen
+= nwritten
;
1339 totwritten
+= nwritten
;
1340 /* If we fully sent the object on head go to the next one */
1341 if (c
->sentlen
== objlen
) {
1342 listDelNode(c
->reply
,listFirst(c
->reply
));
1345 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
1346 * bytes, in a single threaded server it's a good idea to server
1347 * other clients as well, even if a very large request comes from
1348 * super fast link that is always able to accept data (in real world
1349 * terms think to 'KEYS *' against the loopback interfae) */
1350 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
1352 if (nwritten
== -1) {
1353 if (errno
== EAGAIN
) {
1356 redisLog(REDIS_DEBUG
,
1357 "Error writing to client: %s", strerror(errno
));
1362 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
1363 if (listLength(c
->reply
) == 0) {
1365 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
1369 static struct redisCommand
*lookupCommand(char *name
) {
1371 while(cmdTable
[j
].name
!= NULL
) {
1372 if (!strcasecmp(name
,cmdTable
[j
].name
)) return &cmdTable
[j
];
1378 /* resetClient prepare the client to process the next command */
1379 static void resetClient(redisClient
*c
) {
1385 /* If this function gets called we already read a whole
1386 * command, argments are in the client argv/argc fields.
1387 * processCommand() execute the command or prepare the
1388 * server for a bulk read from the client.
1390 * If 1 is returned the client is still alive and valid and
1391 * and other operations can be performed by the caller. Otherwise
1392 * if 0 is returned the client was destroied (i.e. after QUIT). */
1393 static int processCommand(redisClient
*c
) {
1394 struct redisCommand
*cmd
;
1397 /* Free some memory if needed (maxmemory setting) */
1398 if (server
.maxmemory
) freeMemoryIfNeeded();
1400 /* Handle the multi bulk command type. This is an alternative protocol
1401 * supported by Redis in order to receive commands that are composed of
1402 * multiple binary-safe "bulk" arguments. The latency of processing is
1403 * a bit higher but this allows things like multi-sets, so if this
1404 * protocol is used only for MSET and similar commands this is a big win. */
1405 if (c
->multibulk
== 0 && c
->argc
== 1 && ((char*)(c
->argv
[0]->ptr
))[0] == '*') {
1406 c
->multibulk
= atoi(((char*)c
->argv
[0]->ptr
)+1);
1407 if (c
->multibulk
<= 0) {
1411 decrRefCount(c
->argv
[c
->argc
-1]);
1415 } else if (c
->multibulk
) {
1416 if (c
->bulklen
== -1) {
1417 if (((char*)c
->argv
[0]->ptr
)[0] != '$') {
1418 addReplySds(c
,sdsnew("-ERR multi bulk protocol error\r\n"));
1422 int bulklen
= atoi(((char*)c
->argv
[0]->ptr
)+1);
1423 decrRefCount(c
->argv
[0]);
1424 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
1426 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
1431 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
1435 c
->mbargv
= zrealloc(c
->mbargv
,(sizeof(robj
*))*(c
->mbargc
+1));
1436 c
->mbargv
[c
->mbargc
] = c
->argv
[0];
1440 if (c
->multibulk
== 0) {
1444 /* Here we need to swap the multi-bulk argc/argv with the
1445 * normal argc/argv of the client structure. */
1447 c
->argv
= c
->mbargv
;
1448 c
->mbargv
= auxargv
;
1451 c
->argc
= c
->mbargc
;
1452 c
->mbargc
= auxargc
;
1454 /* We need to set bulklen to something different than -1
1455 * in order for the code below to process the command without
1456 * to try to read the last argument of a bulk command as
1457 * a special argument. */
1459 /* continue below and process the command */
1466 /* -- end of multi bulk commands processing -- */
1468 /* The QUIT command is handled as a special case. Normal command
1469 * procs are unable to close the client connection safely */
1470 if (!strcasecmp(c
->argv
[0]->ptr
,"quit")) {
1474 cmd
= lookupCommand(c
->argv
[0]->ptr
);
1476 addReplySds(c
,sdsnew("-ERR unknown command\r\n"));
1479 } else if ((cmd
->arity
> 0 && cmd
->arity
!= c
->argc
) ||
1480 (c
->argc
< -cmd
->arity
)) {
1481 addReplySds(c
,sdsnew("-ERR wrong number of arguments\r\n"));
1484 } else if (server
.maxmemory
&& cmd
->flags
& REDIS_CMD_DENYOOM
&& zmalloc_used_memory() > server
.maxmemory
) {
1485 addReplySds(c
,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
1488 } else if (cmd
->flags
& REDIS_CMD_BULK
&& c
->bulklen
== -1) {
1489 int bulklen
= atoi(c
->argv
[c
->argc
-1]->ptr
);
1491 decrRefCount(c
->argv
[c
->argc
-1]);
1492 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
1494 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
1499 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
1500 /* It is possible that the bulk read is already in the
1501 * buffer. Check this condition and handle it accordingly.
1502 * This is just a fast path, alternative to call processInputBuffer().
1503 * It's a good idea since the code is small and this condition
1504 * happens most of the times. */
1505 if ((signed)sdslen(c
->querybuf
) >= c
->bulklen
) {
1506 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
1508 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
1513 /* Let's try to share objects on the command arguments vector */
1514 if (server
.shareobjects
) {
1516 for(j
= 1; j
< c
->argc
; j
++)
1517 c
->argv
[j
] = tryObjectSharing(c
->argv
[j
]);
1519 /* Let's try to encode the bulk object to save space. */
1520 if (cmd
->flags
& REDIS_CMD_BULK
)
1521 tryObjectEncoding(c
->argv
[c
->argc
-1]);
1523 /* Check if the user is authenticated */
1524 if (server
.requirepass
&& !c
->authenticated
&& cmd
->proc
!= authCommand
) {
1525 addReplySds(c
,sdsnew("-ERR operation not permitted\r\n"));
1530 /* Exec the command */
1531 dirty
= server
.dirty
;
1533 if (server
.dirty
-dirty
!= 0 && listLength(server
.slaves
))
1534 replicationFeedSlaves(server
.slaves
,cmd
,c
->db
->id
,c
->argv
,c
->argc
);
1535 if (listLength(server
.monitors
))
1536 replicationFeedSlaves(server
.monitors
,cmd
,c
->db
->id
,c
->argv
,c
->argc
);
1537 server
.stat_numcommands
++;
1539 /* Prepare the client for the next command */
1540 if (c
->flags
& REDIS_CLOSE
) {
1548 static void replicationFeedSlaves(list
*slaves
, struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
) {
1552 /* (args*2)+1 is enough room for args, spaces, newlines */
1553 robj
*static_outv
[REDIS_STATIC_ARGS
*2+1];
1555 if (argc
<= REDIS_STATIC_ARGS
) {
1558 outv
= zmalloc(sizeof(robj
*)*(argc
*2+1));
1561 for (j
= 0; j
< argc
; j
++) {
1562 if (j
!= 0) outv
[outc
++] = shared
.space
;
1563 if ((cmd
->flags
& REDIS_CMD_BULK
) && j
== argc
-1) {
1566 lenobj
= createObject(REDIS_STRING
,
1567 sdscatprintf(sdsempty(),"%d\r\n",
1568 stringObjectLen(argv
[j
])));
1569 lenobj
->refcount
= 0;
1570 outv
[outc
++] = lenobj
;
1572 outv
[outc
++] = argv
[j
];
1574 outv
[outc
++] = shared
.crlf
;
1576 /* Increment all the refcounts at start and decrement at end in order to
1577 * be sure to free objects if there is no slave in a replication state
1578 * able to be feed with commands */
1579 for (j
= 0; j
< outc
; j
++) incrRefCount(outv
[j
]);
1581 while((ln
= listYield(slaves
))) {
1582 redisClient
*slave
= ln
->value
;
1584 /* Don't feed slaves that are still waiting for BGSAVE to start */
1585 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
) continue;
1587 /* Feed all the other slaves, MONITORs and so on */
1588 if (slave
->slaveseldb
!= dictid
) {
1592 case 0: selectcmd
= shared
.select0
; break;
1593 case 1: selectcmd
= shared
.select1
; break;
1594 case 2: selectcmd
= shared
.select2
; break;
1595 case 3: selectcmd
= shared
.select3
; break;
1596 case 4: selectcmd
= shared
.select4
; break;
1597 case 5: selectcmd
= shared
.select5
; break;
1598 case 6: selectcmd
= shared
.select6
; break;
1599 case 7: selectcmd
= shared
.select7
; break;
1600 case 8: selectcmd
= shared
.select8
; break;
1601 case 9: selectcmd
= shared
.select9
; break;
1603 selectcmd
= createObject(REDIS_STRING
,
1604 sdscatprintf(sdsempty(),"select %d\r\n",dictid
));
1605 selectcmd
->refcount
= 0;
1608 addReply(slave
,selectcmd
);
1609 slave
->slaveseldb
= dictid
;
1611 for (j
= 0; j
< outc
; j
++) addReply(slave
,outv
[j
]);
1613 for (j
= 0; j
< outc
; j
++) decrRefCount(outv
[j
]);
1614 if (outv
!= static_outv
) zfree(outv
);
1617 static void processInputBuffer(redisClient
*c
) {
1619 if (c
->bulklen
== -1) {
1620 /* Read the first line of the query */
1621 char *p
= strchr(c
->querybuf
,'\n');
1628 query
= c
->querybuf
;
1629 c
->querybuf
= sdsempty();
1630 querylen
= 1+(p
-(query
));
1631 if (sdslen(query
) > querylen
) {
1632 /* leave data after the first line of the query in the buffer */
1633 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
1635 *p
= '\0'; /* remove "\n" */
1636 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
1637 sdsupdatelen(query
);
1639 /* Now we can split the query in arguments */
1640 if (sdslen(query
) == 0) {
1641 /* Ignore empty query */
1645 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
1648 if (c
->argv
) zfree(c
->argv
);
1649 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
1651 for (j
= 0; j
< argc
; j
++) {
1652 if (sdslen(argv
[j
])) {
1653 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
1660 /* Execute the command. If the client is still valid
1661 * after processCommand() return and there is something
1662 * on the query buffer try to process the next command. */
1663 if (c
->argc
&& processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
1665 } else if (sdslen(c
->querybuf
) >= REDIS_REQUEST_MAX_SIZE
) {
1666 redisLog(REDIS_DEBUG
, "Client protocol error");
1671 /* Bulk read handling. Note that if we are at this point
1672 the client already sent a command terminated with a newline,
1673 we are reading the bulk data that is actually the last
1674 argument of the command. */
1675 int qbl
= sdslen(c
->querybuf
);
1677 if (c
->bulklen
<= qbl
) {
1678 /* Copy everything but the final CRLF as final argument */
1679 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
1681 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
1682 /* Process the command. If the client is still valid after
1683 * the processing and there is more data in the buffer
1684 * try to parse it. */
1685 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
1691 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1692 redisClient
*c
= (redisClient
*) privdata
;
1693 char buf
[REDIS_IOBUF_LEN
];
1696 REDIS_NOTUSED(mask
);
1698 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
1700 if (errno
== EAGAIN
) {
1703 redisLog(REDIS_DEBUG
, "Reading from client: %s",strerror(errno
));
1707 } else if (nread
== 0) {
1708 redisLog(REDIS_DEBUG
, "Client closed connection");
1713 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
1714 c
->lastinteraction
= time(NULL
);
1718 processInputBuffer(c
);
1721 static int selectDb(redisClient
*c
, int id
) {
1722 if (id
< 0 || id
>= server
.dbnum
)
1724 c
->db
= &server
.db
[id
];
1728 static void *dupClientReplyValue(void *o
) {
1729 incrRefCount((robj
*)o
);
1733 static redisClient
*createClient(int fd
) {
1734 redisClient
*c
= zmalloc(sizeof(*c
));
1736 anetNonBlock(NULL
,fd
);
1737 anetTcpNoDelay(NULL
,fd
);
1738 if (!c
) return NULL
;
1741 c
->querybuf
= sdsempty();
1750 c
->lastinteraction
= time(NULL
);
1751 c
->authenticated
= 0;
1752 c
->replstate
= REDIS_REPL_NONE
;
1753 c
->reply
= listCreate();
1754 listSetFreeMethod(c
->reply
,decrRefCount
);
1755 listSetDupMethod(c
->reply
,dupClientReplyValue
);
1756 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
1757 readQueryFromClient
, c
, NULL
) == AE_ERR
) {
1761 listAddNodeTail(server
.clients
,c
);
1765 static void addReply(redisClient
*c
, robj
*obj
) {
1766 if (listLength(c
->reply
) == 0 &&
1767 (c
->replstate
== REDIS_REPL_NONE
||
1768 c
->replstate
== REDIS_REPL_ONLINE
) &&
1769 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
1770 sendReplyToClient
, c
, NULL
) == AE_ERR
) return;
1771 if (obj
->encoding
!= REDIS_ENCODING_RAW
) {
1772 obj
= getDecodedObject(obj
);
1776 listAddNodeTail(c
->reply
,obj
);
1779 static void addReplySds(redisClient
*c
, sds s
) {
1780 robj
*o
= createObject(REDIS_STRING
,s
);
1785 static void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
1788 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
1789 len
= sdslen(obj
->ptr
);
1791 long n
= (long)obj
->ptr
;
1798 while((n
= n
/10) != 0) {
1802 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",len
));
1805 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1810 REDIS_NOTUSED(mask
);
1811 REDIS_NOTUSED(privdata
);
1813 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
1814 if (cfd
== AE_ERR
) {
1815 redisLog(REDIS_DEBUG
,"Accepting client connection: %s", server
.neterr
);
1818 redisLog(REDIS_DEBUG
,"Accepted %s:%d", cip
, cport
);
1819 if ((c
= createClient(cfd
)) == NULL
) {
1820 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
1821 close(cfd
); /* May be already closed, just ingore errors */
1824 /* If maxclient directive is set and this is one client more... close the
1825 * connection. Note that we create the client instead to check before
1826 * for this condition, since now the socket is already set in nonblocking
1827 * mode and we can send an error for free using the Kernel I/O */
1828 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
1829 char *err
= "-ERR max number of clients reached\r\n";
1831 /* That's a best effort error message, don't check write errors */
1832 (void) write(c
->fd
,err
,strlen(err
));
1836 server
.stat_numconnections
++;
1839 /* ======================= Redis objects implementation ===================== */
1841 static robj
*createObject(int type
, void *ptr
) {
1844 if (listLength(server
.objfreelist
)) {
1845 listNode
*head
= listFirst(server
.objfreelist
);
1846 o
= listNodeValue(head
);
1847 listDelNode(server
.objfreelist
,head
);
1849 o
= zmalloc(sizeof(*o
));
1852 o
->encoding
= REDIS_ENCODING_RAW
;
1858 static robj
*createStringObject(char *ptr
, size_t len
) {
1859 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
1862 static robj
*createListObject(void) {
1863 list
*l
= listCreate();
1865 listSetFreeMethod(l
,decrRefCount
);
1866 return createObject(REDIS_LIST
,l
);
1869 static robj
*createSetObject(void) {
1870 dict
*d
= dictCreate(&setDictType
,NULL
);
1871 return createObject(REDIS_SET
,d
);
1874 static robj
*createZsetObject(void) {
1875 zset
*zs
= zmalloc(sizeof(*zs
));
1877 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
1878 zs
->zsl
= zslCreate();
1879 return createObject(REDIS_ZSET
,zs
);
1882 static void freeStringObject(robj
*o
) {
1883 if (o
->encoding
== REDIS_ENCODING_RAW
) {
1888 static void freeListObject(robj
*o
) {
1889 listRelease((list
*) o
->ptr
);
1892 static void freeSetObject(robj
*o
) {
1893 dictRelease((dict
*) o
->ptr
);
1896 static void freeZsetObject(robj
*o
) {
1899 dictRelease(zs
->dict
);
1904 static void freeHashObject(robj
*o
) {
1905 dictRelease((dict
*) o
->ptr
);
1908 static void incrRefCount(robj
*o
) {
1910 #ifdef DEBUG_REFCOUNT
1911 if (o
->type
== REDIS_STRING
)
1912 printf("Increment '%s'(%p), now is: %d\n",o
->ptr
,o
,o
->refcount
);
1916 static void decrRefCount(void *obj
) {
1919 #ifdef DEBUG_REFCOUNT
1920 if (o
->type
== REDIS_STRING
)
1921 printf("Decrement '%s'(%p), now is: %d\n",o
->ptr
,o
,o
->refcount
-1);
1923 if (--(o
->refcount
) == 0) {
1925 case REDIS_STRING
: freeStringObject(o
); break;
1926 case REDIS_LIST
: freeListObject(o
); break;
1927 case REDIS_SET
: freeSetObject(o
); break;
1928 case REDIS_ZSET
: freeZsetObject(o
); break;
1929 case REDIS_HASH
: freeHashObject(o
); break;
1930 default: assert(0 != 0); break;
1932 if (listLength(server
.objfreelist
) > REDIS_OBJFREELIST_MAX
||
1933 !listAddNodeHead(server
.objfreelist
,o
))
1938 static robj
*lookupKey(redisDb
*db
, robj
*key
) {
1939 dictEntry
*de
= dictFind(db
->dict
,key
);
1940 return de
? dictGetEntryVal(de
) : NULL
;
1943 static robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
1944 expireIfNeeded(db
,key
);
1945 return lookupKey(db
,key
);
1948 static robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
1949 deleteIfVolatile(db
,key
);
1950 return lookupKey(db
,key
);
1953 static int deleteKey(redisDb
*db
, robj
*key
) {
1956 /* We need to protect key from destruction: after the first dictDelete()
1957 * it may happen that 'key' is no longer valid if we don't increment
1958 * it's count. This may happen when we get the object reference directly
1959 * from the hash table with dictRandomKey() or dict iterators */
1961 if (dictSize(db
->expires
)) dictDelete(db
->expires
,key
);
1962 retval
= dictDelete(db
->dict
,key
);
1965 return retval
== DICT_OK
;
1968 /* Try to share an object against the shared objects pool */
1969 static robj
*tryObjectSharing(robj
*o
) {
1970 struct dictEntry
*de
;
1973 if (o
== NULL
|| server
.shareobjects
== 0) return o
;
1975 assert(o
->type
== REDIS_STRING
);
1976 de
= dictFind(server
.sharingpool
,o
);
1978 robj
*shared
= dictGetEntryKey(de
);
1980 c
= ((unsigned long) dictGetEntryVal(de
))+1;
1981 dictGetEntryVal(de
) = (void*) c
;
1982 incrRefCount(shared
);
1986 /* Here we are using a stream algorihtm: Every time an object is
1987 * shared we increment its count, everytime there is a miss we
1988 * recrement the counter of a random object. If this object reaches
1989 * zero we remove the object and put the current object instead. */
1990 if (dictSize(server
.sharingpool
) >=
1991 server
.sharingpoolsize
) {
1992 de
= dictGetRandomKey(server
.sharingpool
);
1994 c
= ((unsigned long) dictGetEntryVal(de
))-1;
1995 dictGetEntryVal(de
) = (void*) c
;
1997 dictDelete(server
.sharingpool
,de
->key
);
2000 c
= 0; /* If the pool is empty we want to add this object */
2005 retval
= dictAdd(server
.sharingpool
,o
,(void*)1);
2006 assert(retval
== DICT_OK
);
2013 /* Check if the nul-terminated string 's' can be represented by a long
2014 * (that is, is a number that fits into long without any other space or
2015 * character before or after the digits).
2017 * If so, the function returns REDIS_OK and *longval is set to the value
2018 * of the number. Otherwise REDIS_ERR is returned */
2019 static int isStringRepresentableAsLong(sds s
, long *longval
) {
2020 char buf
[32], *endptr
;
2024 value
= strtol(s
, &endptr
, 10);
2025 if (endptr
[0] != '\0') return REDIS_ERR
;
2026 slen
= snprintf(buf
,32,"%ld",value
);
2028 /* If the number converted back into a string is not identical
2029 * then it's not possible to encode the string as integer */
2030 if (sdslen(s
) != (unsigned)slen
|| memcmp(buf
,s
,slen
)) return REDIS_ERR
;
2031 if (longval
) *longval
= value
;
2035 /* Try to encode a string object in order to save space */
2036 static int tryObjectEncoding(robj
*o
) {
2040 if (o
->encoding
!= REDIS_ENCODING_RAW
)
2041 return REDIS_ERR
; /* Already encoded */
2043 /* It's not save to encode shared objects: shared objects can be shared
2044 * everywhere in the "object space" of Redis. Encoded objects can only
2045 * appear as "values" (and not, for instance, as keys) */
2046 if (o
->refcount
> 1) return REDIS_ERR
;
2048 /* Currently we try to encode only strings */
2049 assert(o
->type
== REDIS_STRING
);
2051 /* Check if we can represent this string as a long integer */
2052 if (isStringRepresentableAsLong(s
,&value
) == REDIS_ERR
) return REDIS_ERR
;
2054 /* Ok, this object can be encoded */
2055 o
->encoding
= REDIS_ENCODING_INT
;
2057 o
->ptr
= (void*) value
;
2061 /* Get a decoded version of an encoded object (returned as a new object) */
2062 static robj
*getDecodedObject(const robj
*o
) {
2065 assert(o
->encoding
!= REDIS_ENCODING_RAW
);
2066 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
2069 snprintf(buf
,32,"%ld",(long)o
->ptr
);
2070 dec
= createStringObject(buf
,strlen(buf
));
2077 static int compareStringObjects(robj
*a
, robj
*b
) {
2078 assert(a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
2080 if (a
== b
) return 0;
2081 if (a
->encoding
== REDIS_ENCODING_INT
&& b
->encoding
== REDIS_ENCODING_INT
){
2082 return (long)a
->ptr
- (long)b
->ptr
;
2088 if (a
->encoding
!= REDIS_ENCODING_RAW
) a
= getDecodedObject(a
);
2089 if (b
->encoding
!= REDIS_ENCODING_RAW
) b
= getDecodedObject(a
);
2090 retval
= sdscmp(a
->ptr
,b
->ptr
);
2097 static size_t stringObjectLen(robj
*o
) {
2098 assert(o
->type
== REDIS_STRING
);
2099 if (o
->encoding
== REDIS_ENCODING_RAW
) {
2100 return sdslen(o
->ptr
);
2104 return snprintf(buf
,32,"%ld",(long)o
->ptr
);
2108 /*============================ DB saving/loading ============================ */
2110 static int rdbSaveType(FILE *fp
, unsigned char type
) {
2111 if (fwrite(&type
,1,1,fp
) == 0) return -1;
2115 static int rdbSaveTime(FILE *fp
, time_t t
) {
2116 int32_t t32
= (int32_t) t
;
2117 if (fwrite(&t32
,4,1,fp
) == 0) return -1;
2121 /* check rdbLoadLen() comments for more info */
2122 static int rdbSaveLen(FILE *fp
, uint32_t len
) {
2123 unsigned char buf
[2];
2126 /* Save a 6 bit len */
2127 buf
[0] = (len
&0xFF)|(REDIS_RDB_6BITLEN
<<6);
2128 if (fwrite(buf
,1,1,fp
) == 0) return -1;
2129 } else if (len
< (1<<14)) {
2130 /* Save a 14 bit len */
2131 buf
[0] = ((len
>>8)&0xFF)|(REDIS_RDB_14BITLEN
<<6);
2133 if (fwrite(buf
,2,1,fp
) == 0) return -1;
2135 /* Save a 32 bit len */
2136 buf
[0] = (REDIS_RDB_32BITLEN
<<6);
2137 if (fwrite(buf
,1,1,fp
) == 0) return -1;
2139 if (fwrite(&len
,4,1,fp
) == 0) return -1;
2144 /* String objects in the form "2391" "-100" without any space and with a
2145 * range of values that can fit in an 8, 16 or 32 bit signed value can be
2146 * encoded as integers to save space */
2147 static int rdbTryIntegerEncoding(sds s
, unsigned char *enc
) {
2149 char *endptr
, buf
[32];
2151 /* Check if it's possible to encode this value as a number */
2152 value
= strtoll(s
, &endptr
, 10);
2153 if (endptr
[0] != '\0') return 0;
2154 snprintf(buf
,32,"%lld",value
);
2156 /* If the number converted back into a string is not identical
2157 * then it's not possible to encode the string as integer */
2158 if (strlen(buf
) != sdslen(s
) || memcmp(buf
,s
,sdslen(s
))) return 0;
2160 /* Finally check if it fits in our ranges */
2161 if (value
>= -(1<<7) && value
<= (1<<7)-1) {
2162 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT8
;
2163 enc
[1] = value
&0xFF;
2165 } else if (value
>= -(1<<15) && value
<= (1<<15)-1) {
2166 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT16
;
2167 enc
[1] = value
&0xFF;
2168 enc
[2] = (value
>>8)&0xFF;
2170 } else if (value
>= -((long long)1<<31) && value
<= ((long long)1<<31)-1) {
2171 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT32
;
2172 enc
[1] = value
&0xFF;
2173 enc
[2] = (value
>>8)&0xFF;
2174 enc
[3] = (value
>>16)&0xFF;
2175 enc
[4] = (value
>>24)&0xFF;
2182 static int rdbSaveLzfStringObject(FILE *fp
, robj
*obj
) {
2183 unsigned int comprlen
, outlen
;
2187 /* We require at least four bytes compression for this to be worth it */
2188 outlen
= sdslen(obj
->ptr
)-4;
2189 if (outlen
<= 0) return 0;
2190 if ((out
= zmalloc(outlen
+1)) == NULL
) return 0;
2191 comprlen
= lzf_compress(obj
->ptr
, sdslen(obj
->ptr
), out
, outlen
);
2192 if (comprlen
== 0) {
2196 /* Data compressed! Let's save it on disk */
2197 byte
= (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_LZF
;
2198 if (fwrite(&byte
,1,1,fp
) == 0) goto writeerr
;
2199 if (rdbSaveLen(fp
,comprlen
) == -1) goto writeerr
;
2200 if (rdbSaveLen(fp
,sdslen(obj
->ptr
)) == -1) goto writeerr
;
2201 if (fwrite(out
,comprlen
,1,fp
) == 0) goto writeerr
;
2210 /* Save a string objet as [len][data] on disk. If the object is a string
2211 * representation of an integer value we try to safe it in a special form */
2212 static int rdbSaveStringObjectRaw(FILE *fp
, robj
*obj
) {
2216 len
= sdslen(obj
->ptr
);
2218 /* Try integer encoding */
2220 unsigned char buf
[5];
2221 if ((enclen
= rdbTryIntegerEncoding(obj
->ptr
,buf
)) > 0) {
2222 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
2227 /* Try LZF compression - under 20 bytes it's unable to compress even
2228 * aaaaaaaaaaaaaaaaaa so skip it */
2232 retval
= rdbSaveLzfStringObject(fp
,obj
);
2233 if (retval
== -1) return -1;
2234 if (retval
> 0) return 0;
2235 /* retval == 0 means data can't be compressed, save the old way */
2238 /* Store verbatim */
2239 if (rdbSaveLen(fp
,len
) == -1) return -1;
2240 if (len
&& fwrite(obj
->ptr
,len
,1,fp
) == 0) return -1;
2244 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
2245 static int rdbSaveStringObject(FILE *fp
, robj
*obj
) {
2249 if (obj
->encoding
!= REDIS_ENCODING_RAW
) {
2250 dec
= getDecodedObject(obj
);
2251 retval
= rdbSaveStringObjectRaw(fp
,dec
);
2255 return rdbSaveStringObjectRaw(fp
,obj
);
2259 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
2260 * 8 bit integer specifing the length of the representation.
2261 * This 8 bit integer has special values in order to specify the following
2267 static int rdbSaveDoubleValue(FILE *fp
, double val
) {
2268 unsigned char buf
[128];
2274 } else if (!isfinite(val
)) {
2276 buf
[0] = (val
< 0) ? 255 : 254;
2278 snprintf((char*)buf
+1,sizeof(buf
)-1,"%.16g",val
);
2279 buf
[0] = strlen((char*)buf
);
2282 if (fwrite(buf
,len
,1,fp
) == 0) return -1;
2286 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
2287 static int rdbSave(char *filename
) {
2288 dictIterator
*di
= NULL
;
2293 time_t now
= time(NULL
);
2295 snprintf(tmpfile
,256,"temp-%d.rdb", (int) getpid());
2296 fp
= fopen(tmpfile
,"w");
2298 redisLog(REDIS_WARNING
, "Failed saving the DB: %s", strerror(errno
));
2301 if (fwrite("REDIS0001",9,1,fp
) == 0) goto werr
;
2302 for (j
= 0; j
< server
.dbnum
; j
++) {
2303 redisDb
*db
= server
.db
+j
;
2305 if (dictSize(d
) == 0) continue;
2306 di
= dictGetIterator(d
);
2312 /* Write the SELECT DB opcode */
2313 if (rdbSaveType(fp
,REDIS_SELECTDB
) == -1) goto werr
;
2314 if (rdbSaveLen(fp
,j
) == -1) goto werr
;
2316 /* Iterate this DB writing every entry */
2317 while((de
= dictNext(di
)) != NULL
) {
2318 robj
*key
= dictGetEntryKey(de
);
2319 robj
*o
= dictGetEntryVal(de
);
2320 time_t expiretime
= getExpire(db
,key
);
2322 /* Save the expire time */
2323 if (expiretime
!= -1) {
2324 /* If this key is already expired skip it */
2325 if (expiretime
< now
) continue;
2326 if (rdbSaveType(fp
,REDIS_EXPIRETIME
) == -1) goto werr
;
2327 if (rdbSaveTime(fp
,expiretime
) == -1) goto werr
;
2329 /* Save the key and associated value */
2330 if (rdbSaveType(fp
,o
->type
) == -1) goto werr
;
2331 if (rdbSaveStringObject(fp
,key
) == -1) goto werr
;
2332 if (o
->type
== REDIS_STRING
) {
2333 /* Save a string value */
2334 if (rdbSaveStringObject(fp
,o
) == -1) goto werr
;
2335 } else if (o
->type
== REDIS_LIST
) {
2336 /* Save a list value */
2337 list
*list
= o
->ptr
;
2341 if (rdbSaveLen(fp
,listLength(list
)) == -1) goto werr
;
2342 while((ln
= listYield(list
))) {
2343 robj
*eleobj
= listNodeValue(ln
);
2345 if (rdbSaveStringObject(fp
,eleobj
) == -1) goto werr
;
2347 } else if (o
->type
== REDIS_SET
) {
2348 /* Save a set value */
2350 dictIterator
*di
= dictGetIterator(set
);
2353 if (rdbSaveLen(fp
,dictSize(set
)) == -1) goto werr
;
2354 while((de
= dictNext(di
)) != NULL
) {
2355 robj
*eleobj
= dictGetEntryKey(de
);
2357 if (rdbSaveStringObject(fp
,eleobj
) == -1) goto werr
;
2359 dictReleaseIterator(di
);
2360 } else if (o
->type
== REDIS_ZSET
) {
2361 /* Save a set value */
2363 dictIterator
*di
= dictGetIterator(zs
->dict
);
2366 if (rdbSaveLen(fp
,dictSize(zs
->dict
)) == -1) goto werr
;
2367 while((de
= dictNext(di
)) != NULL
) {
2368 robj
*eleobj
= dictGetEntryKey(de
);
2369 double *score
= dictGetEntryVal(de
);
2371 if (rdbSaveStringObject(fp
,eleobj
) == -1) goto werr
;
2372 if (rdbSaveDoubleValue(fp
,*score
) == -1) goto werr
;
2374 dictReleaseIterator(di
);
2379 dictReleaseIterator(di
);
2382 if (rdbSaveType(fp
,REDIS_EOF
) == -1) goto werr
;
2384 /* Make sure data will not remain on the OS's output buffers */
2389 /* Use RENAME to make sure the DB file is changed atomically only
2390 * if the generate DB file is ok. */
2391 if (rename(tmpfile
,filename
) == -1) {
2392 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destination: %s", strerror(errno
));
2396 redisLog(REDIS_NOTICE
,"DB saved on disk");
2398 server
.lastsave
= time(NULL
);
2404 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
2405 if (di
) dictReleaseIterator(di
);
2409 static int rdbSaveBackground(char *filename
) {
2412 if (server
.bgsaveinprogress
) return REDIS_ERR
;
2413 if ((childpid
= fork()) == 0) {
2416 if (rdbSave(filename
) == REDIS_OK
) {
2423 if (childpid
== -1) {
2424 redisLog(REDIS_WARNING
,"Can't save in background: fork: %s",
2428 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
2429 server
.bgsaveinprogress
= 1;
2430 server
.bgsavechildpid
= childpid
;
2433 return REDIS_OK
; /* unreached */
2436 static void rdbRemoveTempFile(pid_t childpid
) {
2439 snprintf(tmpfile
,256,"temp-%d.rdb", (int) childpid
);
2443 static int rdbLoadType(FILE *fp
) {
2445 if (fread(&type
,1,1,fp
) == 0) return -1;
2449 static time_t rdbLoadTime(FILE *fp
) {
2451 if (fread(&t32
,4,1,fp
) == 0) return -1;
2452 return (time_t) t32
;
2455 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
2456 * of this file for a description of how this are stored on disk.
2458 * isencoded is set to 1 if the readed length is not actually a length but
2459 * an "encoding type", check the above comments for more info */
2460 static uint32_t rdbLoadLen(FILE *fp
, int rdbver
, int *isencoded
) {
2461 unsigned char buf
[2];
2464 if (isencoded
) *isencoded
= 0;
2466 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
2471 if (fread(buf
,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
2472 type
= (buf
[0]&0xC0)>>6;
2473 if (type
== REDIS_RDB_6BITLEN
) {
2474 /* Read a 6 bit len */
2476 } else if (type
== REDIS_RDB_ENCVAL
) {
2477 /* Read a 6 bit len encoding type */
2478 if (isencoded
) *isencoded
= 1;
2480 } else if (type
== REDIS_RDB_14BITLEN
) {
2481 /* Read a 14 bit len */
2482 if (fread(buf
+1,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
2483 return ((buf
[0]&0x3F)<<8)|buf
[1];
2485 /* Read a 32 bit len */
2486 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
2492 static robj
*rdbLoadIntegerObject(FILE *fp
, int enctype
) {
2493 unsigned char enc
[4];
2496 if (enctype
== REDIS_RDB_ENC_INT8
) {
2497 if (fread(enc
,1,1,fp
) == 0) return NULL
;
2498 val
= (signed char)enc
[0];
2499 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
2501 if (fread(enc
,2,1,fp
) == 0) return NULL
;
2502 v
= enc
[0]|(enc
[1]<<8);
2504 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
2506 if (fread(enc
,4,1,fp
) == 0) return NULL
;
2507 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
2510 val
= 0; /* anti-warning */
2513 return createObject(REDIS_STRING
,sdscatprintf(sdsempty(),"%lld",val
));
2516 static robj
*rdbLoadLzfStringObject(FILE*fp
, int rdbver
) {
2517 unsigned int len
, clen
;
2518 unsigned char *c
= NULL
;
2521 if ((clen
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
2522 if ((len
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
2523 if ((c
= zmalloc(clen
)) == NULL
) goto err
;
2524 if ((val
= sdsnewlen(NULL
,len
)) == NULL
) goto err
;
2525 if (fread(c
,clen
,1,fp
) == 0) goto err
;
2526 if (lzf_decompress(c
,clen
,val
,len
) == 0) goto err
;
2528 return createObject(REDIS_STRING
,val
);
2535 static robj
*rdbLoadStringObject(FILE*fp
, int rdbver
) {
2540 len
= rdbLoadLen(fp
,rdbver
,&isencoded
);
2543 case REDIS_RDB_ENC_INT8
:
2544 case REDIS_RDB_ENC_INT16
:
2545 case REDIS_RDB_ENC_INT32
:
2546 return tryObjectSharing(rdbLoadIntegerObject(fp
,len
));
2547 case REDIS_RDB_ENC_LZF
:
2548 return tryObjectSharing(rdbLoadLzfStringObject(fp
,rdbver
));
2554 if (len
== REDIS_RDB_LENERR
) return NULL
;
2555 val
= sdsnewlen(NULL
,len
);
2556 if (len
&& fread(val
,len
,1,fp
) == 0) {
2560 return tryObjectSharing(createObject(REDIS_STRING
,val
));
2563 /* For information about double serialization check rdbSaveDoubleValue() */
2564 static int rdbLoadDoubleValue(FILE *fp
, double *val
) {
2568 if (fread(&len
,1,1,fp
) == 0) return -1;
2570 case 255: *val
= R_NegInf
; return 0;
2571 case 254: *val
= R_PosInf
; return 0;
2572 case 253: *val
= R_Nan
; return 0;
2574 if (fread(buf
,len
,1,fp
) == 0) return -1;
2575 sscanf(buf
, "%lg", val
);
2580 static int rdbLoad(char *filename
) {
2582 robj
*keyobj
= NULL
;
2584 int type
, retval
, rdbver
;
2585 dict
*d
= server
.db
[0].dict
;
2586 redisDb
*db
= server
.db
+0;
2588 time_t expiretime
= -1, now
= time(NULL
);
2590 fp
= fopen(filename
,"r");
2591 if (!fp
) return REDIS_ERR
;
2592 if (fread(buf
,9,1,fp
) == 0) goto eoferr
;
2594 if (memcmp(buf
,"REDIS",5) != 0) {
2596 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
2599 rdbver
= atoi(buf
+5);
2602 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
2609 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
2610 if (type
== REDIS_EXPIRETIME
) {
2611 if ((expiretime
= rdbLoadTime(fp
)) == -1) goto eoferr
;
2612 /* We read the time so we need to read the object type again */
2613 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
2615 if (type
== REDIS_EOF
) break;
2616 /* Handle SELECT DB opcode as a special case */
2617 if (type
== REDIS_SELECTDB
) {
2618 if ((dbid
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
)
2620 if (dbid
>= (unsigned)server
.dbnum
) {
2621 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
2624 db
= server
.db
+dbid
;
2629 if ((keyobj
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
2631 if (type
== REDIS_STRING
) {
2632 /* Read string value */
2633 if ((o
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
2634 tryObjectEncoding(o
);
2635 } else if (type
== REDIS_LIST
|| type
== REDIS_SET
) {
2636 /* Read list/set value */
2639 if ((listlen
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
)
2641 o
= (type
== REDIS_LIST
) ? createListObject() : createSetObject();
2642 /* Load every single element of the list/set */
2646 if ((ele
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
2647 tryObjectEncoding(ele
);
2648 if (type
== REDIS_LIST
) {
2649 listAddNodeTail((list
*)o
->ptr
,ele
);
2651 dictAdd((dict
*)o
->ptr
,ele
,NULL
);
2654 } else if (type
== REDIS_ZSET
) {
2655 /* Read list/set value */
2659 if ((zsetlen
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
)
2661 o
= createZsetObject();
2663 /* Load every single element of the list/set */
2666 double *score
= zmalloc(sizeof(double));
2668 if ((ele
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
2669 tryObjectEncoding(ele
);
2670 if (rdbLoadDoubleValue(fp
,score
) == -1) goto eoferr
;
2671 dictAdd(zs
->dict
,ele
,score
);
2672 zslInsert(zs
->zsl
,*score
,ele
);
2673 incrRefCount(ele
); /* added to skiplist */
2678 /* Add the new object in the hash table */
2679 retval
= dictAdd(d
,keyobj
,o
);
2680 if (retval
== DICT_ERR
) {
2681 redisLog(REDIS_WARNING
,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", keyobj
->ptr
);
2684 /* Set the expire time if needed */
2685 if (expiretime
!= -1) {
2686 setExpire(db
,keyobj
,expiretime
);
2687 /* Delete this key if already expired */
2688 if (expiretime
< now
) deleteKey(db
,keyobj
);
2696 eoferr
: /* unexpected end of file is handled here with a fatal exit */
2697 if (keyobj
) decrRefCount(keyobj
);
2698 redisLog(REDIS_WARNING
,"Short read or OOM loading DB. Unrecoverable error, exiting now.");
2700 return REDIS_ERR
; /* Just to avoid warning */
2703 /*================================== Commands =============================== */
2705 static void authCommand(redisClient
*c
) {
2706 if (!server
.requirepass
|| !strcmp(c
->argv
[1]->ptr
, server
.requirepass
)) {
2707 c
->authenticated
= 1;
2708 addReply(c
,shared
.ok
);
2710 c
->authenticated
= 0;
2711 addReply(c
,shared
.err
);
2715 static void pingCommand(redisClient
*c
) {
2716 addReply(c
,shared
.pong
);
2719 static void echoCommand(redisClient
*c
) {
2720 addReplyBulkLen(c
,c
->argv
[1]);
2721 addReply(c
,c
->argv
[1]);
2722 addReply(c
,shared
.crlf
);
2725 /*=================================== Strings =============================== */
2727 static void setGenericCommand(redisClient
*c
, int nx
) {
2730 retval
= dictAdd(c
->db
->dict
,c
->argv
[1],c
->argv
[2]);
2731 if (retval
== DICT_ERR
) {
2733 dictReplace(c
->db
->dict
,c
->argv
[1],c
->argv
[2]);
2734 incrRefCount(c
->argv
[2]);
2736 addReply(c
,shared
.czero
);
2740 incrRefCount(c
->argv
[1]);
2741 incrRefCount(c
->argv
[2]);
2744 removeExpire(c
->db
,c
->argv
[1]);
2745 addReply(c
, nx
? shared
.cone
: shared
.ok
);
2748 static void setCommand(redisClient
*c
) {
2749 setGenericCommand(c
,0);
2752 static void setnxCommand(redisClient
*c
) {
2753 setGenericCommand(c
,1);
2756 static void getCommand(redisClient
*c
) {
2757 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[1]);
2760 addReply(c
,shared
.nullbulk
);
2762 if (o
->type
!= REDIS_STRING
) {
2763 addReply(c
,shared
.wrongtypeerr
);
2765 addReplyBulkLen(c
,o
);
2767 addReply(c
,shared
.crlf
);
2772 static void getsetCommand(redisClient
*c
) {
2774 if (dictAdd(c
->db
->dict
,c
->argv
[1],c
->argv
[2]) == DICT_ERR
) {
2775 dictReplace(c
->db
->dict
,c
->argv
[1],c
->argv
[2]);
2777 incrRefCount(c
->argv
[1]);
2779 incrRefCount(c
->argv
[2]);
2781 removeExpire(c
->db
,c
->argv
[1]);
2784 static void mgetCommand(redisClient
*c
) {
2787 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-1));
2788 for (j
= 1; j
< c
->argc
; j
++) {
2789 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
2791 addReply(c
,shared
.nullbulk
);
2793 if (o
->type
!= REDIS_STRING
) {
2794 addReply(c
,shared
.nullbulk
);
2796 addReplyBulkLen(c
,o
);
2798 addReply(c
,shared
.crlf
);
2804 static void incrDecrCommand(redisClient
*c
, long long incr
) {
2809 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
2813 if (o
->type
!= REDIS_STRING
) {
2818 if (o
->encoding
== REDIS_ENCODING_RAW
)
2819 value
= strtoll(o
->ptr
, &eptr
, 10);
2820 else if (o
->encoding
== REDIS_ENCODING_INT
)
2821 value
= (long)o
->ptr
;
2828 o
= createObject(REDIS_STRING
,sdscatprintf(sdsempty(),"%lld",value
));
2829 tryObjectEncoding(o
);
2830 retval
= dictAdd(c
->db
->dict
,c
->argv
[1],o
);
2831 if (retval
== DICT_ERR
) {
2832 dictReplace(c
->db
->dict
,c
->argv
[1],o
);
2833 removeExpire(c
->db
,c
->argv
[1]);
2835 incrRefCount(c
->argv
[1]);
2838 addReply(c
,shared
.colon
);
2840 addReply(c
,shared
.crlf
);
2843 static void incrCommand(redisClient
*c
) {
2844 incrDecrCommand(c
,1);
2847 static void decrCommand(redisClient
*c
) {
2848 incrDecrCommand(c
,-1);
2851 static void incrbyCommand(redisClient
*c
) {
2852 long long incr
= strtoll(c
->argv
[2]->ptr
, NULL
, 10);
2853 incrDecrCommand(c
,incr
);
2856 static void decrbyCommand(redisClient
*c
) {
2857 long long incr
= strtoll(c
->argv
[2]->ptr
, NULL
, 10);
2858 incrDecrCommand(c
,-incr
);
2861 /* ========================= Type agnostic commands ========================= */
2863 static void delCommand(redisClient
*c
) {
2866 for (j
= 1; j
< c
->argc
; j
++) {
2867 if (deleteKey(c
->db
,c
->argv
[j
])) {
2874 addReply(c
,shared
.czero
);
2877 addReply(c
,shared
.cone
);
2880 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",deleted
));
2885 static void existsCommand(redisClient
*c
) {
2886 addReply(c
,lookupKeyRead(c
->db
,c
->argv
[1]) ? shared
.cone
: shared
.czero
);
2889 static void selectCommand(redisClient
*c
) {
2890 int id
= atoi(c
->argv
[1]->ptr
);
2892 if (selectDb(c
,id
) == REDIS_ERR
) {
2893 addReplySds(c
,sdsnew("-ERR invalid DB index\r\n"));
2895 addReply(c
,shared
.ok
);
2899 static void randomkeyCommand(redisClient
*c
) {
2903 de
= dictGetRandomKey(c
->db
->dict
);
2904 if (!de
|| expireIfNeeded(c
->db
,dictGetEntryKey(de
)) == 0) break;
2907 addReply(c
,shared
.plus
);
2908 addReply(c
,shared
.crlf
);
2910 addReply(c
,shared
.plus
);
2911 addReply(c
,dictGetEntryKey(de
));
2912 addReply(c
,shared
.crlf
);
2916 static void keysCommand(redisClient
*c
) {
2919 sds pattern
= c
->argv
[1]->ptr
;
2920 int plen
= sdslen(pattern
);
2921 int numkeys
= 0, keyslen
= 0;
2922 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
2924 di
= dictGetIterator(c
->db
->dict
);
2926 decrRefCount(lenobj
);
2927 while((de
= dictNext(di
)) != NULL
) {
2928 robj
*keyobj
= dictGetEntryKey(de
);
2930 sds key
= keyobj
->ptr
;
2931 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
2932 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
2933 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
2935 addReply(c
,shared
.space
);
2938 keyslen
+= sdslen(key
);
2942 dictReleaseIterator(di
);
2943 lenobj
->ptr
= sdscatprintf(sdsempty(),"$%lu\r\n",keyslen
+(numkeys
? (numkeys
-1) : 0));
2944 addReply(c
,shared
.crlf
);
2947 static void dbsizeCommand(redisClient
*c
) {
2949 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c
->db
->dict
)));
2952 static void lastsaveCommand(redisClient
*c
) {
2954 sdscatprintf(sdsempty(),":%lu\r\n",server
.lastsave
));
2957 static void typeCommand(redisClient
*c
) {
2961 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
2966 case REDIS_STRING
: type
= "+string"; break;
2967 case REDIS_LIST
: type
= "+list"; break;
2968 case REDIS_SET
: type
= "+set"; break;
2969 default: type
= "unknown"; break;
2972 addReplySds(c
,sdsnew(type
));
2973 addReply(c
,shared
.crlf
);
2976 static void saveCommand(redisClient
*c
) {
2977 if (server
.bgsaveinprogress
) {
2978 addReplySds(c
,sdsnew("-ERR background save in progress\r\n"));
2981 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
2982 addReply(c
,shared
.ok
);
2984 addReply(c
,shared
.err
);
2988 static void bgsaveCommand(redisClient
*c
) {
2989 if (server
.bgsaveinprogress
) {
2990 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
2993 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
2994 addReply(c
,shared
.ok
);
2996 addReply(c
,shared
.err
);
3000 static void shutdownCommand(redisClient
*c
) {
3001 redisLog(REDIS_WARNING
,"User requested shutdown, saving DB...");
3002 /* Kill the saving child if there is a background saving in progress.
3003 We want to avoid race conditions, for instance our saving child may
3004 overwrite the synchronous saving did by SHUTDOWN. */
3005 if (server
.bgsaveinprogress
) {
3006 redisLog(REDIS_WARNING
,"There is a live saving child. Killing it!");
3007 kill(server
.bgsavechildpid
,SIGKILL
);
3008 rdbRemoveTempFile(server
.bgsavechildpid
);
3011 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
3012 if (server
.daemonize
)
3013 unlink(server
.pidfile
);
3014 redisLog(REDIS_WARNING
,"%zu bytes used at exit",zmalloc_used_memory());
3015 redisLog(REDIS_WARNING
,"Server exit now, bye bye...");
3018 /* Ooops.. error saving! The best we can do is to continue operating.
3019 * Note that if there was a background saving process, in the next
3020 * cron() Redis will be notified that the background saving aborted,
3021 * handling special stuff like slaves pending for synchronization... */
3022 redisLog(REDIS_WARNING
,"Error trying to save the DB, can't exit");
3023 addReplySds(c
,sdsnew("-ERR can't quit, problems saving the DB\r\n"));
3027 static void renameGenericCommand(redisClient
*c
, int nx
) {
3030 /* To use the same key as src and dst is probably an error */
3031 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
3032 addReply(c
,shared
.sameobjecterr
);
3036 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3038 addReply(c
,shared
.nokeyerr
);
3042 deleteIfVolatile(c
->db
,c
->argv
[2]);
3043 if (dictAdd(c
->db
->dict
,c
->argv
[2],o
) == DICT_ERR
) {
3046 addReply(c
,shared
.czero
);
3049 dictReplace(c
->db
->dict
,c
->argv
[2],o
);
3051 incrRefCount(c
->argv
[2]);
3053 deleteKey(c
->db
,c
->argv
[1]);
3055 addReply(c
,nx
? shared
.cone
: shared
.ok
);
3058 static void renameCommand(redisClient
*c
) {
3059 renameGenericCommand(c
,0);
3062 static void renamenxCommand(redisClient
*c
) {
3063 renameGenericCommand(c
,1);
3066 static void moveCommand(redisClient
*c
) {
3071 /* Obtain source and target DB pointers */
3074 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
3075 addReply(c
,shared
.outofrangeerr
);
3079 selectDb(c
,srcid
); /* Back to the source DB */
3081 /* If the user is moving using as target the same
3082 * DB as the source DB it is probably an error. */
3084 addReply(c
,shared
.sameobjecterr
);
3088 /* Check if the element exists and get a reference */
3089 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3091 addReply(c
,shared
.czero
);
3095 /* Try to add the element to the target DB */
3096 deleteIfVolatile(dst
,c
->argv
[1]);
3097 if (dictAdd(dst
->dict
,c
->argv
[1],o
) == DICT_ERR
) {
3098 addReply(c
,shared
.czero
);
3101 incrRefCount(c
->argv
[1]);
3104 /* OK! key moved, free the entry in the source DB */
3105 deleteKey(src
,c
->argv
[1]);
3107 addReply(c
,shared
.cone
);
3110 /* =================================== Lists ================================ */
3111 static void pushGenericCommand(redisClient
*c
, int where
) {
3115 lobj
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3117 lobj
= createListObject();
3119 if (where
== REDIS_HEAD
) {
3120 listAddNodeHead(list
,c
->argv
[2]);
3122 listAddNodeTail(list
,c
->argv
[2]);
3124 dictAdd(c
->db
->dict
,c
->argv
[1],lobj
);
3125 incrRefCount(c
->argv
[1]);
3126 incrRefCount(c
->argv
[2]);
3128 if (lobj
->type
!= REDIS_LIST
) {
3129 addReply(c
,shared
.wrongtypeerr
);
3133 if (where
== REDIS_HEAD
) {
3134 listAddNodeHead(list
,c
->argv
[2]);
3136 listAddNodeTail(list
,c
->argv
[2]);
3138 incrRefCount(c
->argv
[2]);
3141 addReply(c
,shared
.ok
);
3144 static void lpushCommand(redisClient
*c
) {
3145 pushGenericCommand(c
,REDIS_HEAD
);
3148 static void rpushCommand(redisClient
*c
) {
3149 pushGenericCommand(c
,REDIS_TAIL
);
3152 static void llenCommand(redisClient
*c
) {
3156 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
3158 addReply(c
,shared
.czero
);
3161 if (o
->type
!= REDIS_LIST
) {
3162 addReply(c
,shared
.wrongtypeerr
);
3165 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",listLength(l
)));
3170 static void lindexCommand(redisClient
*c
) {
3172 int index
= atoi(c
->argv
[2]->ptr
);
3174 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
3176 addReply(c
,shared
.nullbulk
);
3178 if (o
->type
!= REDIS_LIST
) {
3179 addReply(c
,shared
.wrongtypeerr
);
3181 list
*list
= o
->ptr
;
3184 ln
= listIndex(list
, index
);
3186 addReply(c
,shared
.nullbulk
);
3188 robj
*ele
= listNodeValue(ln
);
3189 addReplyBulkLen(c
,ele
);
3191 addReply(c
,shared
.crlf
);
3197 static void lsetCommand(redisClient
*c
) {
3199 int index
= atoi(c
->argv
[2]->ptr
);
3201 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3203 addReply(c
,shared
.nokeyerr
);
3205 if (o
->type
!= REDIS_LIST
) {
3206 addReply(c
,shared
.wrongtypeerr
);
3208 list
*list
= o
->ptr
;
3211 ln
= listIndex(list
, index
);
3213 addReply(c
,shared
.outofrangeerr
);
3215 robj
*ele
= listNodeValue(ln
);
3218 listNodeValue(ln
) = c
->argv
[3];
3219 incrRefCount(c
->argv
[3]);
3220 addReply(c
,shared
.ok
);
3227 static void popGenericCommand(redisClient
*c
, int where
) {
3230 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3232 addReply(c
,shared
.nullbulk
);
3234 if (o
->type
!= REDIS_LIST
) {
3235 addReply(c
,shared
.wrongtypeerr
);
3237 list
*list
= o
->ptr
;
3240 if (where
== REDIS_HEAD
)
3241 ln
= listFirst(list
);
3243 ln
= listLast(list
);
3246 addReply(c
,shared
.nullbulk
);
3248 robj
*ele
= listNodeValue(ln
);
3249 addReplyBulkLen(c
,ele
);
3251 addReply(c
,shared
.crlf
);
3252 listDelNode(list
,ln
);
3259 static void lpopCommand(redisClient
*c
) {
3260 popGenericCommand(c
,REDIS_HEAD
);
3263 static void rpopCommand(redisClient
*c
) {
3264 popGenericCommand(c
,REDIS_TAIL
);
3267 static void lrangeCommand(redisClient
*c
) {
3269 int start
= atoi(c
->argv
[2]->ptr
);
3270 int end
= atoi(c
->argv
[3]->ptr
);
3272 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
3274 addReply(c
,shared
.nullmultibulk
);
3276 if (o
->type
!= REDIS_LIST
) {
3277 addReply(c
,shared
.wrongtypeerr
);
3279 list
*list
= o
->ptr
;
3281 int llen
= listLength(list
);
3285 /* convert negative indexes */
3286 if (start
< 0) start
= llen
+start
;
3287 if (end
< 0) end
= llen
+end
;
3288 if (start
< 0) start
= 0;
3289 if (end
< 0) end
= 0;
3291 /* indexes sanity checks */
3292 if (start
> end
|| start
>= llen
) {
3293 /* Out of range start or start > end result in empty list */
3294 addReply(c
,shared
.emptymultibulk
);
3297 if (end
>= llen
) end
= llen
-1;
3298 rangelen
= (end
-start
)+1;
3300 /* Return the result in form of a multi-bulk reply */
3301 ln
= listIndex(list
, start
);
3302 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",rangelen
));
3303 for (j
= 0; j
< rangelen
; j
++) {
3304 ele
= listNodeValue(ln
);
3305 addReplyBulkLen(c
,ele
);
3307 addReply(c
,shared
.crlf
);
3314 static void ltrimCommand(redisClient
*c
) {
3316 int start
= atoi(c
->argv
[2]->ptr
);
3317 int end
= atoi(c
->argv
[3]->ptr
);
3319 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3321 addReply(c
,shared
.nokeyerr
);
3323 if (o
->type
!= REDIS_LIST
) {
3324 addReply(c
,shared
.wrongtypeerr
);
3326 list
*list
= o
->ptr
;
3328 int llen
= listLength(list
);
3329 int j
, ltrim
, rtrim
;
3331 /* convert negative indexes */
3332 if (start
< 0) start
= llen
+start
;
3333 if (end
< 0) end
= llen
+end
;
3334 if (start
< 0) start
= 0;
3335 if (end
< 0) end
= 0;
3337 /* indexes sanity checks */
3338 if (start
> end
|| start
>= llen
) {
3339 /* Out of range start or start > end result in empty list */
3343 if (end
>= llen
) end
= llen
-1;
3348 /* Remove list elements to perform the trim */
3349 for (j
= 0; j
< ltrim
; j
++) {
3350 ln
= listFirst(list
);
3351 listDelNode(list
,ln
);
3353 for (j
= 0; j
< rtrim
; j
++) {
3354 ln
= listLast(list
);
3355 listDelNode(list
,ln
);
3358 addReply(c
,shared
.ok
);
3363 static void lremCommand(redisClient
*c
) {
3366 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3368 addReply(c
,shared
.czero
);
3370 if (o
->type
!= REDIS_LIST
) {
3371 addReply(c
,shared
.wrongtypeerr
);
3373 list
*list
= o
->ptr
;
3374 listNode
*ln
, *next
;
3375 int toremove
= atoi(c
->argv
[2]->ptr
);
3380 toremove
= -toremove
;
3383 ln
= fromtail
? list
->tail
: list
->head
;
3385 robj
*ele
= listNodeValue(ln
);
3387 next
= fromtail
? ln
->prev
: ln
->next
;
3388 if (compareStringObjects(ele
,c
->argv
[3]) == 0) {
3389 listDelNode(list
,ln
);
3392 if (toremove
&& removed
== toremove
) break;
3396 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",removed
));
3401 /* ==================================== Sets ================================ */
3403 static void saddCommand(redisClient
*c
) {
3406 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3408 set
= createSetObject();
3409 dictAdd(c
->db
->dict
,c
->argv
[1],set
);
3410 incrRefCount(c
->argv
[1]);
3412 if (set
->type
!= REDIS_SET
) {
3413 addReply(c
,shared
.wrongtypeerr
);
3417 if (dictAdd(set
->ptr
,c
->argv
[2],NULL
) == DICT_OK
) {
3418 incrRefCount(c
->argv
[2]);
3420 addReply(c
,shared
.cone
);
3422 addReply(c
,shared
.czero
);
3426 static void sremCommand(redisClient
*c
) {
3429 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3431 addReply(c
,shared
.czero
);
3433 if (set
->type
!= REDIS_SET
) {
3434 addReply(c
,shared
.wrongtypeerr
);
3437 if (dictDelete(set
->ptr
,c
->argv
[2]) == DICT_OK
) {
3439 if (htNeedsResize(set
->ptr
)) dictResize(set
->ptr
);
3440 addReply(c
,shared
.cone
);
3442 addReply(c
,shared
.czero
);
3447 static void smoveCommand(redisClient
*c
) {
3448 robj
*srcset
, *dstset
;
3450 srcset
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3451 dstset
= lookupKeyWrite(c
->db
,c
->argv
[2]);
3453 /* If the source key does not exist return 0, if it's of the wrong type
3455 if (srcset
== NULL
|| srcset
->type
!= REDIS_SET
) {
3456 addReply(c
, srcset
? shared
.wrongtypeerr
: shared
.czero
);
3459 /* Error if the destination key is not a set as well */
3460 if (dstset
&& dstset
->type
!= REDIS_SET
) {
3461 addReply(c
,shared
.wrongtypeerr
);
3464 /* Remove the element from the source set */
3465 if (dictDelete(srcset
->ptr
,c
->argv
[3]) == DICT_ERR
) {
3466 /* Key not found in the src set! return zero */
3467 addReply(c
,shared
.czero
);
3471 /* Add the element to the destination set */
3473 dstset
= createSetObject();
3474 dictAdd(c
->db
->dict
,c
->argv
[2],dstset
);
3475 incrRefCount(c
->argv
[2]);
3477 if (dictAdd(dstset
->ptr
,c
->argv
[3],NULL
) == DICT_OK
)
3478 incrRefCount(c
->argv
[3]);
3479 addReply(c
,shared
.cone
);
3482 static void sismemberCommand(redisClient
*c
) {
3485 set
= lookupKeyRead(c
->db
,c
->argv
[1]);
3487 addReply(c
,shared
.czero
);
3489 if (set
->type
!= REDIS_SET
) {
3490 addReply(c
,shared
.wrongtypeerr
);
3493 if (dictFind(set
->ptr
,c
->argv
[2]))
3494 addReply(c
,shared
.cone
);
3496 addReply(c
,shared
.czero
);
3500 static void scardCommand(redisClient
*c
) {
3504 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
3506 addReply(c
,shared
.czero
);
3509 if (o
->type
!= REDIS_SET
) {
3510 addReply(c
,shared
.wrongtypeerr
);
3513 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",
3519 static void spopCommand(redisClient
*c
) {
3523 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3525 addReply(c
,shared
.nullbulk
);
3527 if (set
->type
!= REDIS_SET
) {
3528 addReply(c
,shared
.wrongtypeerr
);
3531 de
= dictGetRandomKey(set
->ptr
);
3533 addReply(c
,shared
.nullbulk
);
3535 robj
*ele
= dictGetEntryKey(de
);
3537 addReplyBulkLen(c
,ele
);
3539 addReply(c
,shared
.crlf
);
3540 dictDelete(set
->ptr
,ele
);
3541 if (htNeedsResize(set
->ptr
)) dictResize(set
->ptr
);
3547 static void srandmemberCommand(redisClient
*c
) {
3551 set
= lookupKeyRead(c
->db
,c
->argv
[1]);
3553 addReply(c
,shared
.nullbulk
);
3555 if (set
->type
!= REDIS_SET
) {
3556 addReply(c
,shared
.wrongtypeerr
);
3559 de
= dictGetRandomKey(set
->ptr
);
3561 addReply(c
,shared
.nullbulk
);
3563 robj
*ele
= dictGetEntryKey(de
);
3565 addReplyBulkLen(c
,ele
);
3567 addReply(c
,shared
.crlf
);
3572 static int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
3573 dict
**d1
= (void*) s1
, **d2
= (void*) s2
;
3575 return dictSize(*d1
)-dictSize(*d2
);
3578 static void sinterGenericCommand(redisClient
*c
, robj
**setskeys
, int setsnum
, robj
*dstkey
) {
3579 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
3582 robj
*lenobj
= NULL
, *dstset
= NULL
;
3583 int j
, cardinality
= 0;
3585 for (j
= 0; j
< setsnum
; j
++) {
3589 lookupKeyWrite(c
->db
,setskeys
[j
]) :
3590 lookupKeyRead(c
->db
,setskeys
[j
]);
3594 deleteKey(c
->db
,dstkey
);
3595 addReply(c
,shared
.ok
);
3597 addReply(c
,shared
.nullmultibulk
);
3601 if (setobj
->type
!= REDIS_SET
) {
3603 addReply(c
,shared
.wrongtypeerr
);
3606 dv
[j
] = setobj
->ptr
;
3608 /* Sort sets from the smallest to largest, this will improve our
3609 * algorithm's performace */
3610 qsort(dv
,setsnum
,sizeof(dict
*),qsortCompareSetsByCardinality
);
3612 /* The first thing we should output is the total number of elements...
3613 * since this is a multi-bulk write, but at this stage we don't know
3614 * the intersection set size, so we use a trick, append an empty object
3615 * to the output list and save the pointer to later modify it with the
3618 lenobj
= createObject(REDIS_STRING
,NULL
);
3620 decrRefCount(lenobj
);
3622 /* If we have a target key where to store the resulting set
3623 * create this key with an empty set inside */
3624 dstset
= createSetObject();
3627 /* Iterate all the elements of the first (smallest) set, and test
3628 * the element against all the other sets, if at least one set does
3629 * not include the element it is discarded */
3630 di
= dictGetIterator(dv
[0]);
3632 while((de
= dictNext(di
)) != NULL
) {
3635 for (j
= 1; j
< setsnum
; j
++)
3636 if (dictFind(dv
[j
],dictGetEntryKey(de
)) == NULL
) break;
3638 continue; /* at least one set does not contain the member */
3639 ele
= dictGetEntryKey(de
);
3641 addReplyBulkLen(c
,ele
);
3643 addReply(c
,shared
.crlf
);
3646 dictAdd(dstset
->ptr
,ele
,NULL
);
3650 dictReleaseIterator(di
);
3653 /* Store the resulting set into the target */
3654 deleteKey(c
->db
,dstkey
);
3655 dictAdd(c
->db
->dict
,dstkey
,dstset
);
3656 incrRefCount(dstkey
);
3660 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%d\r\n",cardinality
);
3662 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",
3663 dictSize((dict
*)dstset
->ptr
)));
3669 static void sinterCommand(redisClient
*c
) {
3670 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
3673 static void sinterstoreCommand(redisClient
*c
) {
3674 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
3677 #define REDIS_OP_UNION 0
3678 #define REDIS_OP_DIFF 1
3680 static void sunionDiffGenericCommand(redisClient
*c
, robj
**setskeys
, int setsnum
, robj
*dstkey
, int op
) {
3681 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
3684 robj
*dstset
= NULL
;
3685 int j
, cardinality
= 0;
3687 for (j
= 0; j
< setsnum
; j
++) {
3691 lookupKeyWrite(c
->db
,setskeys
[j
]) :
3692 lookupKeyRead(c
->db
,setskeys
[j
]);
3697 if (setobj
->type
!= REDIS_SET
) {
3699 addReply(c
,shared
.wrongtypeerr
);
3702 dv
[j
] = setobj
->ptr
;
3705 /* We need a temp set object to store our union. If the dstkey
3706 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
3707 * this set object will be the resulting object to set into the target key*/
3708 dstset
= createSetObject();
3710 /* Iterate all the elements of all the sets, add every element a single
3711 * time to the result set */
3712 for (j
= 0; j
< setsnum
; j
++) {
3713 if (op
== REDIS_OP_DIFF
&& j
== 0 && !dv
[j
]) break; /* result set is empty */
3714 if (!dv
[j
]) continue; /* non existing keys are like empty sets */
3716 di
= dictGetIterator(dv
[j
]);
3718 while((de
= dictNext(di
)) != NULL
) {
3721 /* dictAdd will not add the same element multiple times */
3722 ele
= dictGetEntryKey(de
);
3723 if (op
== REDIS_OP_UNION
|| j
== 0) {
3724 if (dictAdd(dstset
->ptr
,ele
,NULL
) == DICT_OK
) {
3728 } else if (op
== REDIS_OP_DIFF
) {
3729 if (dictDelete(dstset
->ptr
,ele
) == DICT_OK
) {
3734 dictReleaseIterator(di
);
3736 if (op
== REDIS_OP_DIFF
&& cardinality
== 0) break; /* result set is empty */
3739 /* Output the content of the resulting set, if not in STORE mode */
3741 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",cardinality
));
3742 di
= dictGetIterator(dstset
->ptr
);
3743 while((de
= dictNext(di
)) != NULL
) {
3746 ele
= dictGetEntryKey(de
);
3747 addReplyBulkLen(c
,ele
);
3749 addReply(c
,shared
.crlf
);
3751 dictReleaseIterator(di
);
3753 /* If we have a target key where to store the resulting set
3754 * create this key with the result set inside */
3755 deleteKey(c
->db
,dstkey
);
3756 dictAdd(c
->db
->dict
,dstkey
,dstset
);
3757 incrRefCount(dstkey
);
3762 decrRefCount(dstset
);
3764 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",
3765 dictSize((dict
*)dstset
->ptr
)));
3771 static void sunionCommand(redisClient
*c
) {
3772 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_UNION
);
3775 static void sunionstoreCommand(redisClient
*c
) {
3776 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_UNION
);
3779 static void sdiffCommand(redisClient
*c
) {
3780 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_DIFF
);
3783 static void sdiffstoreCommand(redisClient
*c
) {
3784 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_DIFF
);
3787 /* ==================================== ZSets =============================== */
3789 /* ZSETs are ordered sets using two data structures to hold the same elements
3790 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
3793 * The elements are added to an hash table mapping Redis objects to scores.
3794 * At the same time the elements are added to a skip list mapping scores
3795 * to Redis objects (so objects are sorted by scores in this "view"). */
3797 /* This skiplist implementation is almost a C translation of the original
3798 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
3799 * Alternative to Balanced Trees", modified in three ways:
3800 * a) this implementation allows for repeated values.
3801 * b) the comparison is not just by key (our 'score') but by satellite data.
3802 * c) there is a back pointer, so it's a doubly linked list with the back
3803 * pointers being only at "level 1". This allows to traverse the list
3804 * from tail to head, useful for ZREVRANGE. */
3806 static zskiplistNode
*zslCreateNode(int level
, double score
, robj
*obj
) {
3807 zskiplistNode
*zn
= zmalloc(sizeof(*zn
));
3809 zn
->forward
= zmalloc(sizeof(zskiplistNode
*) * level
);
3815 static zskiplist
*zslCreate(void) {
3819 zsl
= zmalloc(sizeof(*zsl
));
3822 zsl
->header
= zslCreateNode(ZSKIPLIST_MAXLEVEL
,0,NULL
);
3823 for (j
= 0; j
< ZSKIPLIST_MAXLEVEL
; j
++)
3824 zsl
->header
->forward
[j
] = NULL
;
3825 zsl
->header
->backward
= NULL
;
3830 static void zslFreeNode(zskiplistNode
*node
) {
3831 decrRefCount(node
->obj
);
3832 zfree(node
->forward
);
3836 static void zslFree(zskiplist
*zsl
) {
3837 zskiplistNode
*node
= zsl
->header
->forward
[0], *next
;
3839 zfree(zsl
->header
->forward
);
3842 next
= node
->forward
[0];
3849 static int zslRandomLevel(void) {
3851 while ((random()&0xFFFF) < (ZSKIPLIST_P
* 0xFFFF))
3856 static void zslInsert(zskiplist
*zsl
, double score
, robj
*obj
) {
3857 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
3861 for (i
= zsl
->level
-1; i
>= 0; i
--) {
3862 while (x
->forward
[i
] &&
3863 (x
->forward
[i
]->score
< score
||
3864 (x
->forward
[i
]->score
== score
&&
3865 compareStringObjects(x
->forward
[i
]->obj
,obj
) < 0)))
3869 /* we assume the key is not already inside, since we allow duplicated
3870 * scores, and the re-insertion of score and redis object should never
3871 * happpen since the caller of zslInsert() should test in the hash table
3872 * if the element is already inside or not. */
3873 level
= zslRandomLevel();
3874 if (level
> zsl
->level
) {
3875 for (i
= zsl
->level
; i
< level
; i
++)
3876 update
[i
] = zsl
->header
;
3879 x
= zslCreateNode(level
,score
,obj
);
3880 for (i
= 0; i
< level
; i
++) {
3881 x
->forward
[i
] = update
[i
]->forward
[i
];
3882 update
[i
]->forward
[i
] = x
;
3884 x
->backward
= (update
[0] == zsl
->header
) ? NULL
: update
[0];
3886 x
->forward
[0]->backward
= x
;
3892 /* Delete an element with matching score/object from the skiplist. */
3893 static int zslDelete(zskiplist
*zsl
, double score
, robj
*obj
) {
3894 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
3898 for (i
= zsl
->level
-1; i
>= 0; i
--) {
3899 while (x
->forward
[i
] &&
3900 (x
->forward
[i
]->score
< score
||
3901 (x
->forward
[i
]->score
== score
&&
3902 compareStringObjects(x
->forward
[i
]->obj
,obj
) < 0)))
3906 /* We may have multiple elements with the same score, what we need
3907 * is to find the element with both the right score and object. */
3909 if (x
&& score
== x
->score
&& compareStringObjects(x
->obj
,obj
) == 0) {
3910 for (i
= 0; i
< zsl
->level
; i
++) {
3911 if (update
[i
]->forward
[i
] != x
) break;
3912 update
[i
]->forward
[i
] = x
->forward
[i
];
3914 if (x
->forward
[0]) {
3915 x
->forward
[0]->backward
= (x
->backward
== zsl
->header
) ?
3918 zsl
->tail
= x
->backward
;
3921 while(zsl
->level
> 1 && zsl
->header
->forward
[zsl
->level
-1] == NULL
)
3926 return 0; /* not found */
3928 return 0; /* not found */
3931 /* Find the first node having a score equal or greater than the specified one.
3932 * Returns NULL if there is no match. */
3933 static zskiplistNode
*zslFirstWithScore(zskiplist
*zsl
, double score
) {
3938 for (i
= zsl
->level
-1; i
>= 0; i
--) {
3939 while (x
->forward
[i
] && x
->forward
[i
]->score
< score
)
3942 /* We may have multiple elements with the same score, what we need
3943 * is to find the element with both the right score and object. */
3944 return x
->forward
[0];
3947 /* The actual Z-commands implementations */
3949 static void zaddCommand(redisClient
*c
) {
3954 zsetobj
= lookupKeyWrite(c
->db
,c
->argv
[1]);
3955 if (zsetobj
== NULL
) {
3956 zsetobj
= createZsetObject();
3957 dictAdd(c
->db
->dict
,c
->argv
[1],zsetobj
);
3958 incrRefCount(c
->argv
[1]);
3960 if (zsetobj
->type
!= REDIS_ZSET
) {
3961 addReply(c
,shared
.wrongtypeerr
);
3965 score
= zmalloc(sizeof(double));
3966 *score
= strtod(c
->argv
[2]->ptr
,NULL
);
3968 if (dictAdd(zs
->dict
,c
->argv
[3],score
) == DICT_OK
) {
3969 /* case 1: New element */
3970 incrRefCount(c
->argv
[3]); /* added to hash */
3971 zslInsert(zs
->zsl
,*score
,c
->argv
[3]);
3972 incrRefCount(c
->argv
[3]); /* added to skiplist */
3974 addReply(c
,shared
.cone
);
3979 /* case 2: Score update operation */
3980 de
= dictFind(zs
->dict
,c
->argv
[3]);
3982 oldscore
= dictGetEntryVal(de
);
3983 if (*score
!= *oldscore
) {
3986 deleted
= zslDelete(zs
->zsl
,*oldscore
,c
->argv
[3]);
3987 assert(deleted
!= 0);
3988 zslInsert(zs
->zsl
,*score
,c
->argv
[3]);
3989 incrRefCount(c
->argv
[3]);
3990 dictReplace(zs
->dict
,c
->argv
[3],score
);
3995 addReply(c
,shared
.czero
);
3999 static void zremCommand(redisClient
*c
) {
4003 zsetobj
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4004 if (zsetobj
== NULL
) {
4005 addReply(c
,shared
.czero
);
4011 if (zsetobj
->type
!= REDIS_ZSET
) {
4012 addReply(c
,shared
.wrongtypeerr
);
4016 de
= dictFind(zs
->dict
,c
->argv
[2]);
4018 addReply(c
,shared
.czero
);
4021 /* Delete from the skiplist */
4022 oldscore
= dictGetEntryVal(de
);
4023 deleted
= zslDelete(zs
->zsl
,*oldscore
,c
->argv
[2]);
4024 assert(deleted
!= 0);
4026 /* Delete from the hash table */
4027 dictDelete(zs
->dict
,c
->argv
[2]);
4028 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
4030 addReply(c
,shared
.cone
);
4034 static void zrangeGenericCommand(redisClient
*c
, int reverse
) {
4036 int start
= atoi(c
->argv
[2]->ptr
);
4037 int end
= atoi(c
->argv
[3]->ptr
);
4039 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
4041 addReply(c
,shared
.nullmultibulk
);
4043 if (o
->type
!= REDIS_ZSET
) {
4044 addReply(c
,shared
.wrongtypeerr
);
4046 zset
*zsetobj
= o
->ptr
;
4047 zskiplist
*zsl
= zsetobj
->zsl
;
4050 int llen
= zsl
->length
;
4054 /* convert negative indexes */
4055 if (start
< 0) start
= llen
+start
;
4056 if (end
< 0) end
= llen
+end
;
4057 if (start
< 0) start
= 0;
4058 if (end
< 0) end
= 0;
4060 /* indexes sanity checks */
4061 if (start
> end
|| start
>= llen
) {
4062 /* Out of range start or start > end result in empty list */
4063 addReply(c
,shared
.emptymultibulk
);
4066 if (end
>= llen
) end
= llen
-1;
4067 rangelen
= (end
-start
)+1;
4069 /* Return the result in form of a multi-bulk reply */
4075 ln
= zsl
->header
->forward
[0];
4077 ln
= ln
->forward
[0];
4080 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",rangelen
));
4081 for (j
= 0; j
< rangelen
; j
++) {
4083 addReplyBulkLen(c
,ele
);
4085 addReply(c
,shared
.crlf
);
4086 ln
= reverse
? ln
->backward
: ln
->forward
[0];
4092 static void zrangeCommand(redisClient
*c
) {
4093 zrangeGenericCommand(c
,0);
4096 static void zrevrangeCommand(redisClient
*c
) {
4097 zrangeGenericCommand(c
,1);
4100 static void zrangebyscoreCommand(redisClient
*c
) {
4102 double min
= strtod(c
->argv
[2]->ptr
,NULL
);
4103 double max
= strtod(c
->argv
[3]->ptr
,NULL
);
4105 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
4107 addReply(c
,shared
.nullmultibulk
);
4109 if (o
->type
!= REDIS_ZSET
) {
4110 addReply(c
,shared
.wrongtypeerr
);
4112 zset
*zsetobj
= o
->ptr
;
4113 zskiplist
*zsl
= zsetobj
->zsl
;
4116 unsigned int rangelen
= 0;
4118 /* Get the first node with the score >= min */
4119 ln
= zslFirstWithScore(zsl
,min
);
4121 /* No element matching the speciifed interval */
4122 addReply(c
,shared
.emptymultibulk
);
4126 /* We don't know in advance how many matching elements there
4127 * are in the list, so we push this object that will represent
4128 * the multi-bulk length in the output buffer, and will "fix"
4130 lenobj
= createObject(REDIS_STRING
,NULL
);
4133 while(ln
&& ln
->score
<= max
) {
4135 addReplyBulkLen(c
,ele
);
4137 addReply(c
,shared
.crlf
);
4138 ln
= ln
->forward
[0];
4141 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%d\r\n",rangelen
);
4146 static void zlenCommand(redisClient
*c
) {
4150 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
4152 addReply(c
,shared
.czero
);
4155 if (o
->type
!= REDIS_ZSET
) {
4156 addReply(c
,shared
.wrongtypeerr
);
4159 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",zs
->zsl
->length
));
4164 static void zscoreCommand(redisClient
*c
) {
4168 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
4170 addReply(c
,shared
.czero
);
4173 if (o
->type
!= REDIS_ZSET
) {
4174 addReply(c
,shared
.wrongtypeerr
);
4179 de
= dictFind(zs
->dict
,c
->argv
[2]);
4181 addReply(c
,shared
.nullbulk
);
4184 double *score
= dictGetEntryVal(de
);
4186 snprintf(buf
,sizeof(buf
),"%.16g",*score
);
4187 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n%s\r\n",
4194 /* ========================= Non type-specific commands ==================== */
4196 static void flushdbCommand(redisClient
*c
) {
4197 server
.dirty
+= dictSize(c
->db
->dict
);
4198 dictEmpty(c
->db
->dict
);
4199 dictEmpty(c
->db
->expires
);
4200 addReply(c
,shared
.ok
);
4203 static void flushallCommand(redisClient
*c
) {
4204 server
.dirty
+= emptyDb();
4205 addReply(c
,shared
.ok
);
4206 rdbSave(server
.dbfilename
);
4210 static redisSortOperation
*createSortOperation(int type
, robj
*pattern
) {
4211 redisSortOperation
*so
= zmalloc(sizeof(*so
));
4213 so
->pattern
= pattern
;
4217 /* Return the value associated to the key with a name obtained
4218 * substituting the first occurence of '*' in 'pattern' with 'subst' */
4219 static robj
*lookupKeyByPattern(redisDb
*db
, robj
*pattern
, robj
*subst
) {
4223 int prefixlen
, sublen
, postfixlen
;
4224 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
4228 char buf
[REDIS_SORTKEY_MAX
+1];
4231 if (subst
->encoding
== REDIS_ENCODING_RAW
)
4232 incrRefCount(subst
);
4234 subst
= getDecodedObject(subst
);
4237 spat
= pattern
->ptr
;
4239 if (sdslen(spat
)+sdslen(ssub
)-1 > REDIS_SORTKEY_MAX
) return NULL
;
4240 p
= strchr(spat
,'*');
4241 if (!p
) return NULL
;
4244 sublen
= sdslen(ssub
);
4245 postfixlen
= sdslen(spat
)-(prefixlen
+1);
4246 memcpy(keyname
.buf
,spat
,prefixlen
);
4247 memcpy(keyname
.buf
+prefixlen
,ssub
,sublen
);
4248 memcpy(keyname
.buf
+prefixlen
+sublen
,p
+1,postfixlen
);
4249 keyname
.buf
[prefixlen
+sublen
+postfixlen
] = '\0';
4250 keyname
.len
= prefixlen
+sublen
+postfixlen
;
4252 keyobj
.refcount
= 1;
4253 keyobj
.type
= REDIS_STRING
;
4254 keyobj
.ptr
= ((char*)&keyname
)+(sizeof(long)*2);
4256 decrRefCount(subst
);
4258 /* printf("lookup '%s' => %p\n", keyname.buf,de); */
4259 return lookupKeyRead(db
,&keyobj
);
4262 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
4263 * the additional parameter is not standard but a BSD-specific we have to
4264 * pass sorting parameters via the global 'server' structure */
4265 static int sortCompare(const void *s1
, const void *s2
) {
4266 const redisSortObject
*so1
= s1
, *so2
= s2
;
4269 if (!server
.sort_alpha
) {
4270 /* Numeric sorting. Here it's trivial as we precomputed scores */
4271 if (so1
->u
.score
> so2
->u
.score
) {
4273 } else if (so1
->u
.score
< so2
->u
.score
) {
4279 /* Alphanumeric sorting */
4280 if (server
.sort_bypattern
) {
4281 if (!so1
->u
.cmpobj
|| !so2
->u
.cmpobj
) {
4282 /* At least one compare object is NULL */
4283 if (so1
->u
.cmpobj
== so2
->u
.cmpobj
)
4285 else if (so1
->u
.cmpobj
== NULL
)
4290 /* We have both the objects, use strcoll */
4291 cmp
= strcoll(so1
->u
.cmpobj
->ptr
,so2
->u
.cmpobj
->ptr
);
4294 /* Compare elements directly */
4295 if (so1
->obj
->encoding
== REDIS_ENCODING_RAW
&&
4296 so2
->obj
->encoding
== REDIS_ENCODING_RAW
) {
4297 cmp
= strcoll(so1
->obj
->ptr
,so2
->obj
->ptr
);
4301 dec1
= so1
->obj
->encoding
== REDIS_ENCODING_RAW
?
4302 so1
->obj
: getDecodedObject(so1
->obj
);
4303 dec2
= so2
->obj
->encoding
== REDIS_ENCODING_RAW
?
4304 so2
->obj
: getDecodedObject(so2
->obj
);
4305 cmp
= strcoll(dec1
->ptr
,dec2
->ptr
);
4306 if (dec1
!= so1
->obj
) decrRefCount(dec1
);
4307 if (dec2
!= so2
->obj
) decrRefCount(dec2
);
4311 return server
.sort_desc
? -cmp
: cmp
;
4314 /* The SORT command is the most complex command in Redis. Warning: this code
4315 * is optimized for speed and a bit less for readability */
4316 static void sortCommand(redisClient
*c
) {
4319 int desc
= 0, alpha
= 0;
4320 int limit_start
= 0, limit_count
= -1, start
, end
;
4321 int j
, dontsort
= 0, vectorlen
;
4322 int getop
= 0; /* GET operation counter */
4323 robj
*sortval
, *sortby
= NULL
;
4324 redisSortObject
*vector
; /* Resulting vector to sort */
4326 /* Lookup the key to sort. It must be of the right types */
4327 sortval
= lookupKeyRead(c
->db
,c
->argv
[1]);
4328 if (sortval
== NULL
) {
4329 addReply(c
,shared
.nokeyerr
);
4332 if (sortval
->type
!= REDIS_SET
&& sortval
->type
!= REDIS_LIST
) {
4333 addReply(c
,shared
.wrongtypeerr
);
4337 /* Create a list of operations to perform for every sorted element.
4338 * Operations can be GET/DEL/INCR/DECR */
4339 operations
= listCreate();
4340 listSetFreeMethod(operations
,zfree
);
4343 /* Now we need to protect sortval incrementing its count, in the future
4344 * SORT may have options able to overwrite/delete keys during the sorting
4345 * and the sorted key itself may get destroied */
4346 incrRefCount(sortval
);
4348 /* The SORT command has an SQL-alike syntax, parse it */
4349 while(j
< c
->argc
) {
4350 int leftargs
= c
->argc
-j
-1;
4351 if (!strcasecmp(c
->argv
[j
]->ptr
,"asc")) {
4353 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"desc")) {
4355 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"alpha")) {
4357 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"limit") && leftargs
>= 2) {
4358 limit_start
= atoi(c
->argv
[j
+1]->ptr
);
4359 limit_count
= atoi(c
->argv
[j
+2]->ptr
);
4361 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"by") && leftargs
>= 1) {
4362 sortby
= c
->argv
[j
+1];
4363 /* If the BY pattern does not contain '*', i.e. it is constant,
4364 * we don't need to sort nor to lookup the weight keys. */
4365 if (strchr(c
->argv
[j
+1]->ptr
,'*') == NULL
) dontsort
= 1;
4367 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
4368 listAddNodeTail(operations
,createSortOperation(
4369 REDIS_SORT_GET
,c
->argv
[j
+1]));
4372 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"del") && leftargs
>= 1) {
4373 listAddNodeTail(operations
,createSortOperation(
4374 REDIS_SORT_DEL
,c
->argv
[j
+1]));
4376 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"incr") && leftargs
>= 1) {
4377 listAddNodeTail(operations
,createSortOperation(
4378 REDIS_SORT_INCR
,c
->argv
[j
+1]));
4380 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
4381 listAddNodeTail(operations
,createSortOperation(
4382 REDIS_SORT_DECR
,c
->argv
[j
+1]));
4385 decrRefCount(sortval
);
4386 listRelease(operations
);
4387 addReply(c
,shared
.syntaxerr
);
4393 /* Load the sorting vector with all the objects to sort */
4394 vectorlen
= (sortval
->type
== REDIS_LIST
) ?
4395 listLength((list
*)sortval
->ptr
) :
4396 dictSize((dict
*)sortval
->ptr
);
4397 vector
= zmalloc(sizeof(redisSortObject
)*vectorlen
);
4399 if (sortval
->type
== REDIS_LIST
) {
4400 list
*list
= sortval
->ptr
;
4404 while((ln
= listYield(list
))) {
4405 robj
*ele
= ln
->value
;
4406 vector
[j
].obj
= ele
;
4407 vector
[j
].u
.score
= 0;
4408 vector
[j
].u
.cmpobj
= NULL
;
4412 dict
*set
= sortval
->ptr
;
4416 di
= dictGetIterator(set
);
4417 while((setele
= dictNext(di
)) != NULL
) {
4418 vector
[j
].obj
= dictGetEntryKey(setele
);
4419 vector
[j
].u
.score
= 0;
4420 vector
[j
].u
.cmpobj
= NULL
;
4423 dictReleaseIterator(di
);
4425 assert(j
== vectorlen
);
4427 /* Now it's time to load the right scores in the sorting vector */
4428 if (dontsort
== 0) {
4429 for (j
= 0; j
< vectorlen
; j
++) {
4433 byval
= lookupKeyByPattern(c
->db
,sortby
,vector
[j
].obj
);
4434 if (!byval
|| byval
->type
!= REDIS_STRING
) continue;
4436 if (byval
->encoding
== REDIS_ENCODING_RAW
) {
4437 vector
[j
].u
.cmpobj
= byval
;
4438 incrRefCount(byval
);
4440 vector
[j
].u
.cmpobj
= getDecodedObject(byval
);
4443 if (byval
->encoding
== REDIS_ENCODING_RAW
) {
4444 vector
[j
].u
.score
= strtod(byval
->ptr
,NULL
);
4446 if (byval
->encoding
== REDIS_ENCODING_INT
) {
4447 vector
[j
].u
.score
= (long)byval
->ptr
;
4454 if (vector
[j
].obj
->encoding
== REDIS_ENCODING_RAW
)
4455 vector
[j
].u
.score
= strtod(vector
[j
].obj
->ptr
,NULL
);
4457 if (vector
[j
].obj
->encoding
== REDIS_ENCODING_INT
)
4458 vector
[j
].u
.score
= (long) vector
[j
].obj
->ptr
;
4467 /* We are ready to sort the vector... perform a bit of sanity check
4468 * on the LIMIT option too. We'll use a partial version of quicksort. */
4469 start
= (limit_start
< 0) ? 0 : limit_start
;
4470 end
= (limit_count
< 0) ? vectorlen
-1 : start
+limit_count
-1;
4471 if (start
>= vectorlen
) {
4472 start
= vectorlen
-1;
4475 if (end
>= vectorlen
) end
= vectorlen
-1;
4477 if (dontsort
== 0) {
4478 server
.sort_desc
= desc
;
4479 server
.sort_alpha
= alpha
;
4480 server
.sort_bypattern
= sortby
? 1 : 0;
4481 if (sortby
&& (start
!= 0 || end
!= vectorlen
-1))
4482 pqsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
, start
,end
);
4484 qsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
);
4487 /* Send command output to the output buffer, performing the specified
4488 * GET/DEL/INCR/DECR operations if any. */
4489 outputlen
= getop
? getop
*(end
-start
+1) : end
-start
+1;
4490 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",outputlen
));
4491 for (j
= start
; j
<= end
; j
++) {
4494 addReplyBulkLen(c
,vector
[j
].obj
);
4495 addReply(c
,vector
[j
].obj
);
4496 addReply(c
,shared
.crlf
);
4498 listRewind(operations
);
4499 while((ln
= listYield(operations
))) {
4500 redisSortOperation
*sop
= ln
->value
;
4501 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
4504 if (sop
->type
== REDIS_SORT_GET
) {
4505 if (!val
|| val
->type
!= REDIS_STRING
) {
4506 addReply(c
,shared
.nullbulk
);
4508 addReplyBulkLen(c
,val
);
4510 addReply(c
,shared
.crlf
);
4512 } else if (sop
->type
== REDIS_SORT_DEL
) {
4519 decrRefCount(sortval
);
4520 listRelease(operations
);
4521 for (j
= 0; j
< vectorlen
; j
++) {
4522 if (sortby
&& alpha
&& vector
[j
].u
.cmpobj
)
4523 decrRefCount(vector
[j
].u
.cmpobj
);
4528 static void infoCommand(redisClient
*c
) {
4530 time_t uptime
= time(NULL
)-server
.stat_starttime
;
4533 info
= sdscatprintf(sdsempty(),
4534 "redis_version:%s\r\n"
4536 "uptime_in_seconds:%d\r\n"
4537 "uptime_in_days:%d\r\n"
4538 "connected_clients:%d\r\n"
4539 "connected_slaves:%d\r\n"
4540 "used_memory:%zu\r\n"
4541 "changes_since_last_save:%lld\r\n"
4542 "bgsave_in_progress:%d\r\n"
4543 "last_save_time:%d\r\n"
4544 "total_connections_received:%lld\r\n"
4545 "total_commands_processed:%lld\r\n"
4548 (sizeof(long) == 8) ? "64" : "32",
4551 listLength(server
.clients
)-listLength(server
.slaves
),
4552 listLength(server
.slaves
),
4555 server
.bgsaveinprogress
,
4557 server
.stat_numconnections
,
4558 server
.stat_numcommands
,
4559 server
.masterhost
== NULL
? "master" : "slave"
4561 if (server
.masterhost
) {
4562 info
= sdscatprintf(info
,
4563 "master_host:%s\r\n"
4564 "master_port:%d\r\n"
4565 "master_link_status:%s\r\n"
4566 "master_last_io_seconds_ago:%d\r\n"
4569 (server
.replstate
== REDIS_REPL_CONNECTED
) ?
4571 (int)(time(NULL
)-server
.master
->lastinteraction
)
4574 for (j
= 0; j
< server
.dbnum
; j
++) {
4575 long long keys
, vkeys
;
4577 keys
= dictSize(server
.db
[j
].dict
);
4578 vkeys
= dictSize(server
.db
[j
].expires
);
4579 if (keys
|| vkeys
) {
4580 info
= sdscatprintf(info
, "db%d: keys=%lld,expires=%lld\r\n",
4584 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info
)));
4585 addReplySds(c
,info
);
4586 addReply(c
,shared
.crlf
);
4589 static void monitorCommand(redisClient
*c
) {
4590 /* ignore MONITOR if aleady slave or in monitor mode */
4591 if (c
->flags
& REDIS_SLAVE
) return;
4593 c
->flags
|= (REDIS_SLAVE
|REDIS_MONITOR
);
4595 listAddNodeTail(server
.monitors
,c
);
4596 addReply(c
,shared
.ok
);
4599 /* ================================= Expire ================================= */
4600 static int removeExpire(redisDb
*db
, robj
*key
) {
4601 if (dictDelete(db
->expires
,key
) == DICT_OK
) {
4608 static int setExpire(redisDb
*db
, robj
*key
, time_t when
) {
4609 if (dictAdd(db
->expires
,key
,(void*)when
) == DICT_ERR
) {
4617 /* Return the expire time of the specified key, or -1 if no expire
4618 * is associated with this key (i.e. the key is non volatile) */
4619 static time_t getExpire(redisDb
*db
, robj
*key
) {
4622 /* No expire? return ASAP */
4623 if (dictSize(db
->expires
) == 0 ||
4624 (de
= dictFind(db
->expires
,key
)) == NULL
) return -1;
4626 return (time_t) dictGetEntryVal(de
);
4629 static int expireIfNeeded(redisDb
*db
, robj
*key
) {
4633 /* No expire? return ASAP */
4634 if (dictSize(db
->expires
) == 0 ||
4635 (de
= dictFind(db
->expires
,key
)) == NULL
) return 0;
4637 /* Lookup the expire */
4638 when
= (time_t) dictGetEntryVal(de
);
4639 if (time(NULL
) <= when
) return 0;
4641 /* Delete the key */
4642 dictDelete(db
->expires
,key
);
4643 return dictDelete(db
->dict
,key
) == DICT_OK
;
4646 static int deleteIfVolatile(redisDb
*db
, robj
*key
) {
4649 /* No expire? return ASAP */
4650 if (dictSize(db
->expires
) == 0 ||
4651 (de
= dictFind(db
->expires
,key
)) == NULL
) return 0;
4653 /* Delete the key */
4655 dictDelete(db
->expires
,key
);
4656 return dictDelete(db
->dict
,key
) == DICT_OK
;
4659 static void expireCommand(redisClient
*c
) {
4661 int seconds
= atoi(c
->argv
[2]->ptr
);
4663 de
= dictFind(c
->db
->dict
,c
->argv
[1]);
4665 addReply(c
,shared
.czero
);
4669 addReply(c
, shared
.czero
);
4672 time_t when
= time(NULL
)+seconds
;
4673 if (setExpire(c
->db
,c
->argv
[1],when
)) {
4674 addReply(c
,shared
.cone
);
4677 addReply(c
,shared
.czero
);
4683 static void ttlCommand(redisClient
*c
) {
4687 expire
= getExpire(c
->db
,c
->argv
[1]);
4689 ttl
= (int) (expire
-time(NULL
));
4690 if (ttl
< 0) ttl
= -1;
4692 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",ttl
));
4695 static void msetGenericCommand(redisClient
*c
, int nx
) {
4698 if ((c
->argc
% 2) == 0) {
4699 addReplySds(c
,sdsnew("-ERR wrong number of arguments\r\n"));
4702 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4703 * set nothing at all if at least one already key exists. */
4705 for (j
= 1; j
< c
->argc
; j
+= 2) {
4706 if (dictFind(c
->db
->dict
,c
->argv
[j
]) != NULL
) {
4707 addReply(c
, shared
.czero
);
4713 for (j
= 1; j
< c
->argc
; j
+= 2) {
4716 retval
= dictAdd(c
->db
->dict
,c
->argv
[j
],c
->argv
[j
+1]);
4717 if (retval
== DICT_ERR
) {
4718 dictReplace(c
->db
->dict
,c
->argv
[j
],c
->argv
[j
+1]);
4719 incrRefCount(c
->argv
[j
+1]);
4721 incrRefCount(c
->argv
[j
]);
4722 incrRefCount(c
->argv
[j
+1]);
4724 removeExpire(c
->db
,c
->argv
[j
]);
4726 server
.dirty
+= (c
->argc
-1)/2;
4727 addReply(c
, nx
? shared
.cone
: shared
.ok
);
4730 static void msetCommand(redisClient
*c
) {
4731 msetGenericCommand(c
,0);
4734 static void msetnxCommand(redisClient
*c
) {
4735 msetGenericCommand(c
,1);
4738 /* =============================== Replication ============================= */
4740 static int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
) {
4741 ssize_t nwritten
, ret
= size
;
4742 time_t start
= time(NULL
);
4746 if (aeWait(fd
,AE_WRITABLE
,1000) & AE_WRITABLE
) {
4747 nwritten
= write(fd
,ptr
,size
);
4748 if (nwritten
== -1) return -1;
4752 if ((time(NULL
)-start
) > timeout
) {
4760 static int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
) {
4761 ssize_t nread
, totread
= 0;
4762 time_t start
= time(NULL
);
4766 if (aeWait(fd
,AE_READABLE
,1000) & AE_READABLE
) {
4767 nread
= read(fd
,ptr
,size
);
4768 if (nread
== -1) return -1;
4773 if ((time(NULL
)-start
) > timeout
) {
4781 static int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
) {
4788 if (syncRead(fd
,&c
,1,timeout
) == -1) return -1;
4791 if (nread
&& *(ptr
-1) == '\r') *(ptr
-1) = '\0';
4802 static void syncCommand(redisClient
*c
) {
4803 /* ignore SYNC if aleady slave or in monitor mode */
4804 if (c
->flags
& REDIS_SLAVE
) return;
4806 /* SYNC can't be issued when the server has pending data to send to
4807 * the client about already issued commands. We need a fresh reply
4808 * buffer registering the differences between the BGSAVE and the current
4809 * dataset, so that we can copy to other slaves if needed. */
4810 if (listLength(c
->reply
) != 0) {
4811 addReplySds(c
,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
4815 redisLog(REDIS_NOTICE
,"Slave ask for synchronization");
4816 /* Here we need to check if there is a background saving operation
4817 * in progress, or if it is required to start one */
4818 if (server
.bgsaveinprogress
) {
4819 /* Ok a background save is in progress. Let's check if it is a good
4820 * one for replication, i.e. if there is another slave that is
4821 * registering differences since the server forked to save */
4825 listRewind(server
.slaves
);
4826 while((ln
= listYield(server
.slaves
))) {
4828 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_END
) break;
4831 /* Perfect, the server is already registering differences for
4832 * another slave. Set the right state, and copy the buffer. */
4833 listRelease(c
->reply
);
4834 c
->reply
= listDup(slave
->reply
);
4835 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
4836 redisLog(REDIS_NOTICE
,"Waiting for end of BGSAVE for SYNC");
4838 /* No way, we need to wait for the next BGSAVE in order to
4839 * register differences */
4840 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_START
;
4841 redisLog(REDIS_NOTICE
,"Waiting for next BGSAVE for SYNC");
4844 /* Ok we don't have a BGSAVE in progress, let's start one */
4845 redisLog(REDIS_NOTICE
,"Starting BGSAVE for SYNC");
4846 if (rdbSaveBackground(server
.dbfilename
) != REDIS_OK
) {
4847 redisLog(REDIS_NOTICE
,"Replication failed, can't BGSAVE");
4848 addReplySds(c
,sdsnew("-ERR Unalbe to perform background save\r\n"));
4851 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
4854 c
->flags
|= REDIS_SLAVE
;
4856 listAddNodeTail(server
.slaves
,c
);
4860 static void sendBulkToSlave(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
4861 redisClient
*slave
= privdata
;
4863 REDIS_NOTUSED(mask
);
4864 char buf
[REDIS_IOBUF_LEN
];
4865 ssize_t nwritten
, buflen
;
4867 if (slave
->repldboff
== 0) {
4868 /* Write the bulk write count before to transfer the DB. In theory here
4869 * we don't know how much room there is in the output buffer of the
4870 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
4871 * operations) will never be smaller than the few bytes we need. */
4874 bulkcount
= sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
4876 if (write(fd
,bulkcount
,sdslen(bulkcount
)) != (signed)sdslen(bulkcount
))
4884 lseek(slave
->repldbfd
,slave
->repldboff
,SEEK_SET
);
4885 buflen
= read(slave
->repldbfd
,buf
,REDIS_IOBUF_LEN
);
4887 redisLog(REDIS_WARNING
,"Read error sending DB to slave: %s",
4888 (buflen
== 0) ? "premature EOF" : strerror(errno
));
4892 if ((nwritten
= write(fd
,buf
,buflen
)) == -1) {
4893 redisLog(REDIS_DEBUG
,"Write error sending DB to slave: %s",
4898 slave
->repldboff
+= nwritten
;
4899 if (slave
->repldboff
== slave
->repldbsize
) {
4900 close(slave
->repldbfd
);
4901 slave
->repldbfd
= -1;
4902 aeDeleteFileEvent(server
.el
,slave
->fd
,AE_WRITABLE
);
4903 slave
->replstate
= REDIS_REPL_ONLINE
;
4904 if (aeCreateFileEvent(server
.el
, slave
->fd
, AE_WRITABLE
,
4905 sendReplyToClient
, slave
, NULL
) == AE_ERR
) {
4909 addReplySds(slave
,sdsempty());
4910 redisLog(REDIS_NOTICE
,"Synchronization with slave succeeded");
4914 /* This function is called at the end of every backgrond saving.
4915 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
4916 * otherwise REDIS_ERR is passed to the function.
4918 * The goal of this function is to handle slaves waiting for a successful
4919 * background saving in order to perform non-blocking synchronization. */
4920 static void updateSlavesWaitingBgsave(int bgsaveerr
) {
4922 int startbgsave
= 0;
4924 listRewind(server
.slaves
);
4925 while((ln
= listYield(server
.slaves
))) {
4926 redisClient
*slave
= ln
->value
;
4928 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
) {
4930 slave
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
4931 } else if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_END
) {
4932 struct redis_stat buf
;
4934 if (bgsaveerr
!= REDIS_OK
) {
4936 redisLog(REDIS_WARNING
,"SYNC failed. BGSAVE child returned an error");
4939 if ((slave
->repldbfd
= open(server
.dbfilename
,O_RDONLY
)) == -1 ||
4940 redis_fstat(slave
->repldbfd
,&buf
) == -1) {
4942 redisLog(REDIS_WARNING
,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno
));
4945 slave
->repldboff
= 0;
4946 slave
->repldbsize
= buf
.st_size
;
4947 slave
->replstate
= REDIS_REPL_SEND_BULK
;
4948 aeDeleteFileEvent(server
.el
,slave
->fd
,AE_WRITABLE
);
4949 if (aeCreateFileEvent(server
.el
, slave
->fd
, AE_WRITABLE
, sendBulkToSlave
, slave
, NULL
) == AE_ERR
) {
4956 if (rdbSaveBackground(server
.dbfilename
) != REDIS_OK
) {
4957 listRewind(server
.slaves
);
4958 redisLog(REDIS_WARNING
,"SYNC failed. BGSAVE failed");
4959 while((ln
= listYield(server
.slaves
))) {
4960 redisClient
*slave
= ln
->value
;
4962 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
)
4969 static int syncWithMaster(void) {
4970 char buf
[1024], tmpfile
[256];
4972 int fd
= anetTcpConnect(NULL
,server
.masterhost
,server
.masterport
);
4976 redisLog(REDIS_WARNING
,"Unable to connect to MASTER: %s",
4980 /* Issue the SYNC command */
4981 if (syncWrite(fd
,"SYNC \r\n",7,5) == -1) {
4983 redisLog(REDIS_WARNING
,"I/O error writing to MASTER: %s",
4987 /* Read the bulk write count */
4988 if (syncReadLine(fd
,buf
,1024,3600) == -1) {
4990 redisLog(REDIS_WARNING
,"I/O error reading bulk count from MASTER: %s",
4994 dumpsize
= atoi(buf
+1);
4995 redisLog(REDIS_NOTICE
,"Receiving %d bytes data dump from MASTER",dumpsize
);
4996 /* Read the bulk write data on a temp file */
4997 snprintf(tmpfile
,256,"temp-%d.%ld.rdb",(int)time(NULL
),(long int)random());
4998 dfd
= open(tmpfile
,O_CREAT
|O_WRONLY
,0644);
5001 redisLog(REDIS_WARNING
,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno
));
5005 int nread
, nwritten
;
5007 nread
= read(fd
,buf
,(dumpsize
< 1024)?dumpsize
:1024);
5009 redisLog(REDIS_WARNING
,"I/O error trying to sync with MASTER: %s",
5015 nwritten
= write(dfd
,buf
,nread
);
5016 if (nwritten
== -1) {
5017 redisLog(REDIS_WARNING
,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno
));
5025 if (rename(tmpfile
,server
.dbfilename
) == -1) {
5026 redisLog(REDIS_WARNING
,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno
));
5032 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
5033 redisLog(REDIS_WARNING
,"Failed trying to load the MASTER synchronization DB from disk");
5037 server
.master
= createClient(fd
);
5038 server
.master
->flags
|= REDIS_MASTER
;
5039 server
.replstate
= REDIS_REPL_CONNECTED
;
5043 static void slaveofCommand(redisClient
*c
) {
5044 if (!strcasecmp(c
->argv
[1]->ptr
,"no") &&
5045 !strcasecmp(c
->argv
[2]->ptr
,"one")) {
5046 if (server
.masterhost
) {
5047 sdsfree(server
.masterhost
);
5048 server
.masterhost
= NULL
;
5049 if (server
.master
) freeClient(server
.master
);
5050 server
.replstate
= REDIS_REPL_NONE
;
5051 redisLog(REDIS_NOTICE
,"MASTER MODE enabled (user request)");
5054 sdsfree(server
.masterhost
);
5055 server
.masterhost
= sdsdup(c
->argv
[1]->ptr
);
5056 server
.masterport
= atoi(c
->argv
[2]->ptr
);
5057 if (server
.master
) freeClient(server
.master
);
5058 server
.replstate
= REDIS_REPL_CONNECT
;
5059 redisLog(REDIS_NOTICE
,"SLAVE OF %s:%d enabled (user request)",
5060 server
.masterhost
, server
.masterport
);
5062 addReply(c
,shared
.ok
);
5065 /* ============================ Maxmemory directive ======================== */
5067 /* This function gets called when 'maxmemory' is set on the config file to limit
5068 * the max memory used by the server, and we are out of memory.
5069 * This function will try to, in order:
5071 * - Free objects from the free list
5072 * - Try to remove keys with an EXPIRE set
5074 * It is not possible to free enough memory to reach used-memory < maxmemory
5075 * the server will start refusing commands that will enlarge even more the
5078 static void freeMemoryIfNeeded(void) {
5079 while (server
.maxmemory
&& zmalloc_used_memory() > server
.maxmemory
) {
5080 if (listLength(server
.objfreelist
)) {
5083 listNode
*head
= listFirst(server
.objfreelist
);
5084 o
= listNodeValue(head
);
5085 listDelNode(server
.objfreelist
,head
);
5088 int j
, k
, freed
= 0;
5090 for (j
= 0; j
< server
.dbnum
; j
++) {
5092 robj
*minkey
= NULL
;
5093 struct dictEntry
*de
;
5095 if (dictSize(server
.db
[j
].expires
)) {
5097 /* From a sample of three keys drop the one nearest to
5098 * the natural expire */
5099 for (k
= 0; k
< 3; k
++) {
5102 de
= dictGetRandomKey(server
.db
[j
].expires
);
5103 t
= (time_t) dictGetEntryVal(de
);
5104 if (minttl
== -1 || t
< minttl
) {
5105 minkey
= dictGetEntryKey(de
);
5109 deleteKey(server
.db
+j
,minkey
);
5112 if (!freed
) return; /* nothing to free... */
5117 /* ================================= Debugging ============================== */
5119 static void debugCommand(redisClient
*c
) {
5120 if (!strcasecmp(c
->argv
[1]->ptr
,"segfault")) {
5122 } else if (!strcasecmp(c
->argv
[1]->ptr
,"object") && c
->argc
== 3) {
5123 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]);
5127 addReply(c
,shared
.nokeyerr
);
5130 key
= dictGetEntryKey(de
);
5131 val
= dictGetEntryVal(de
);
5132 addReplySds(c
,sdscatprintf(sdsempty(),
5133 "+Key at:%p refcount:%d, value at:%p refcount:%d encoding:%d\r\n",
5134 key
, key
->refcount
, val
, val
->refcount
, val
->encoding
));
5136 addReplySds(c
,sdsnew(
5137 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>]\r\n"));
5141 #ifdef HAVE_BACKTRACE
5142 static struct redisFunctionSym symsTable
[] = {
5143 {"compareStringObjects", (unsigned long)compareStringObjects
},
5144 {"isStringRepresentableAsLong", (unsigned long)isStringRepresentableAsLong
},
5145 {"dictEncObjKeyCompare", (unsigned long)dictEncObjKeyCompare
},
5146 {"dictEncObjHash", (unsigned long)dictEncObjHash
},
5147 {"incrDecrCommand", (unsigned long)incrDecrCommand
},
5148 {"freeStringObject", (unsigned long)freeStringObject
},
5149 {"freeListObject", (unsigned long)freeListObject
},
5150 {"freeSetObject", (unsigned long)freeSetObject
},
5151 {"decrRefCount", (unsigned long)decrRefCount
},
5152 {"createObject", (unsigned long)createObject
},
5153 {"freeClient", (unsigned long)freeClient
},
5154 {"rdbLoad", (unsigned long)rdbLoad
},
5155 {"rdbSaveStringObject", (unsigned long)rdbSaveStringObject
},
5156 {"rdbSaveStringObjectRaw", (unsigned long)rdbSaveStringObjectRaw
},
5157 {"addReply", (unsigned long)addReply
},
5158 {"addReplySds", (unsigned long)addReplySds
},
5159 {"incrRefCount", (unsigned long)incrRefCount
},
5160 {"rdbSaveBackground", (unsigned long)rdbSaveBackground
},
5161 {"createStringObject", (unsigned long)createStringObject
},
5162 {"replicationFeedSlaves", (unsigned long)replicationFeedSlaves
},
5163 {"syncWithMaster", (unsigned long)syncWithMaster
},
5164 {"tryObjectSharing", (unsigned long)tryObjectSharing
},
5165 {"tryObjectEncoding", (unsigned long)tryObjectEncoding
},
5166 {"getDecodedObject", (unsigned long)getDecodedObject
},
5167 {"removeExpire", (unsigned long)removeExpire
},
5168 {"expireIfNeeded", (unsigned long)expireIfNeeded
},
5169 {"deleteIfVolatile", (unsigned long)deleteIfVolatile
},
5170 {"deleteKey", (unsigned long)deleteKey
},
5171 {"getExpire", (unsigned long)getExpire
},
5172 {"setExpire", (unsigned long)setExpire
},
5173 {"updateSlavesWaitingBgsave", (unsigned long)updateSlavesWaitingBgsave
},
5174 {"freeMemoryIfNeeded", (unsigned long)freeMemoryIfNeeded
},
5175 {"authCommand", (unsigned long)authCommand
},
5176 {"pingCommand", (unsigned long)pingCommand
},
5177 {"echoCommand", (unsigned long)echoCommand
},
5178 {"setCommand", (unsigned long)setCommand
},
5179 {"setnxCommand", (unsigned long)setnxCommand
},
5180 {"getCommand", (unsigned long)getCommand
},
5181 {"delCommand", (unsigned long)delCommand
},
5182 {"existsCommand", (unsigned long)existsCommand
},
5183 {"incrCommand", (unsigned long)incrCommand
},
5184 {"decrCommand", (unsigned long)decrCommand
},
5185 {"incrbyCommand", (unsigned long)incrbyCommand
},
5186 {"decrbyCommand", (unsigned long)decrbyCommand
},
5187 {"selectCommand", (unsigned long)selectCommand
},
5188 {"randomkeyCommand", (unsigned long)randomkeyCommand
},
5189 {"keysCommand", (unsigned long)keysCommand
},
5190 {"dbsizeCommand", (unsigned long)dbsizeCommand
},
5191 {"lastsaveCommand", (unsigned long)lastsaveCommand
},
5192 {"saveCommand", (unsigned long)saveCommand
},
5193 {"bgsaveCommand", (unsigned long)bgsaveCommand
},
5194 {"shutdownCommand", (unsigned long)shutdownCommand
},
5195 {"moveCommand", (unsigned long)moveCommand
},
5196 {"renameCommand", (unsigned long)renameCommand
},
5197 {"renamenxCommand", (unsigned long)renamenxCommand
},
5198 {"lpushCommand", (unsigned long)lpushCommand
},
5199 {"rpushCommand", (unsigned long)rpushCommand
},
5200 {"lpopCommand", (unsigned long)lpopCommand
},
5201 {"rpopCommand", (unsigned long)rpopCommand
},
5202 {"llenCommand", (unsigned long)llenCommand
},
5203 {"lindexCommand", (unsigned long)lindexCommand
},
5204 {"lrangeCommand", (unsigned long)lrangeCommand
},
5205 {"ltrimCommand", (unsigned long)ltrimCommand
},
5206 {"typeCommand", (unsigned long)typeCommand
},
5207 {"lsetCommand", (unsigned long)lsetCommand
},
5208 {"saddCommand", (unsigned long)saddCommand
},
5209 {"sremCommand", (unsigned long)sremCommand
},
5210 {"smoveCommand", (unsigned long)smoveCommand
},
5211 {"sismemberCommand", (unsigned long)sismemberCommand
},
5212 {"scardCommand", (unsigned long)scardCommand
},
5213 {"spopCommand", (unsigned long)spopCommand
},
5214 {"srandmemberCommand", (unsigned long)srandmemberCommand
},
5215 {"sinterCommand", (unsigned long)sinterCommand
},
5216 {"sinterstoreCommand", (unsigned long)sinterstoreCommand
},
5217 {"sunionCommand", (unsigned long)sunionCommand
},
5218 {"sunionstoreCommand", (unsigned long)sunionstoreCommand
},
5219 {"sdiffCommand", (unsigned long)sdiffCommand
},
5220 {"sdiffstoreCommand", (unsigned long)sdiffstoreCommand
},
5221 {"syncCommand", (unsigned long)syncCommand
},
5222 {"flushdbCommand", (unsigned long)flushdbCommand
},
5223 {"flushallCommand", (unsigned long)flushallCommand
},
5224 {"sortCommand", (unsigned long)sortCommand
},
5225 {"lremCommand", (unsigned long)lremCommand
},
5226 {"infoCommand", (unsigned long)infoCommand
},
5227 {"mgetCommand", (unsigned long)mgetCommand
},
5228 {"monitorCommand", (unsigned long)monitorCommand
},
5229 {"expireCommand", (unsigned long)expireCommand
},
5230 {"getsetCommand", (unsigned long)getsetCommand
},
5231 {"ttlCommand", (unsigned long)ttlCommand
},
5232 {"slaveofCommand", (unsigned long)slaveofCommand
},
5233 {"debugCommand", (unsigned long)debugCommand
},
5234 {"processCommand", (unsigned long)processCommand
},
5235 {"setupSigSegvAction", (unsigned long)setupSigSegvAction
},
5236 {"readQueryFromClient", (unsigned long)readQueryFromClient
},
5237 {"rdbRemoveTempFile", (unsigned long)rdbRemoveTempFile
},
5238 {"msetGenericCommand", (unsigned long)msetGenericCommand
},
5239 {"msetCommand", (unsigned long)msetCommand
},
5240 {"msetnxCommand", (unsigned long)msetnxCommand
},
5241 {"zslCreateNode", (unsigned long)zslCreateNode
},
5242 {"zslCreate", (unsigned long)zslCreate
},
5243 {"zslFreeNode",(unsigned long)zslFreeNode
},
5244 {"zslFree",(unsigned long)zslFree
},
5245 {"zslRandomLevel",(unsigned long)zslRandomLevel
},
5246 {"zslInsert",(unsigned long)zslInsert
},
5247 {"zslDelete",(unsigned long)zslDelete
},
5248 {"createZsetObject",(unsigned long)createZsetObject
},
5249 {"zaddCommand",(unsigned long)zaddCommand
},
5250 {"zrangeGenericCommand",(unsigned long)zrangeGenericCommand
},
5251 {"zrangeCommand",(unsigned long)zrangeCommand
},
5252 {"zrevrangeCommand",(unsigned long)zrevrangeCommand
},
5253 {"zremCommand",(unsigned long)zremCommand
},
5254 {"rdbSaveDoubleValue",(unsigned long)rdbSaveDoubleValue
},
5255 {"rdbLoadDoubleValue",(unsigned long)rdbLoadDoubleValue
},
5259 /* This function try to convert a pointer into a function name. It's used in
5260 * oreder to provide a backtrace under segmentation fault that's able to
5261 * display functions declared as static (otherwise the backtrace is useless). */
5262 static char *findFuncName(void *pointer
, unsigned long *offset
){
5264 unsigned long off
, minoff
= 0;
5266 /* Try to match against the Symbol with the smallest offset */
5267 for (i
=0; symsTable
[i
].pointer
; i
++) {
5268 unsigned long lp
= (unsigned long) pointer
;
5270 if (lp
!= (unsigned long)-1 && lp
>= symsTable
[i
].pointer
) {
5271 off
=lp
-symsTable
[i
].pointer
;
5272 if (ret
< 0 || off
< minoff
) {
5278 if (ret
== -1) return NULL
;
5280 return symsTable
[ret
].name
;
5283 static void *getMcontextEip(ucontext_t
*uc
) {
5284 #if defined(__FreeBSD__)
5285 return (void*) uc
->uc_mcontext
.mc_eip
;
5286 #elif defined(__dietlibc__)
5287 return (void*) uc
->uc_mcontext
.eip
;
5288 #elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
5289 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
5290 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
5291 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
5292 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
5294 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
5296 #elif defined(__i386__) || defined(__X86_64__) /* Linux x86 */
5297 return (void*) uc
->uc_mcontext
.gregs
[REG_EIP
];
5298 #elif defined(__ia64__) /* Linux IA64 */
5299 return (void*) uc
->uc_mcontext
.sc_ip
;
5305 static void segvHandler(int sig
, siginfo_t
*info
, void *secret
) {
5307 char **messages
= NULL
;
5308 int i
, trace_size
= 0;
5309 unsigned long offset
=0;
5310 time_t uptime
= time(NULL
)-server
.stat_starttime
;
5311 ucontext_t
*uc
= (ucontext_t
*) secret
;
5312 REDIS_NOTUSED(info
);
5314 redisLog(REDIS_WARNING
,
5315 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION
, sig
);
5316 redisLog(REDIS_WARNING
, "%s", sdscatprintf(sdsempty(),
5317 "redis_version:%s; "
5318 "uptime_in_seconds:%d; "
5319 "connected_clients:%d; "
5320 "connected_slaves:%d; "
5322 "changes_since_last_save:%lld; "
5323 "bgsave_in_progress:%d; "
5324 "last_save_time:%d; "
5325 "total_connections_received:%lld; "
5326 "total_commands_processed:%lld; "
5330 listLength(server
.clients
)-listLength(server
.slaves
),
5331 listLength(server
.slaves
),
5334 server
.bgsaveinprogress
,
5336 server
.stat_numconnections
,
5337 server
.stat_numcommands
,
5338 server
.masterhost
== NULL
? "master" : "slave"
5341 trace_size
= backtrace(trace
, 100);
5342 /* overwrite sigaction with caller's address */
5343 if (getMcontextEip(uc
) != NULL
) {
5344 trace
[1] = getMcontextEip(uc
);
5346 messages
= backtrace_symbols(trace
, trace_size
);
5348 for (i
=1; i
<trace_size
; ++i
) {
5349 char *fn
= findFuncName(trace
[i
], &offset
), *p
;
5351 p
= strchr(messages
[i
],'+');
5352 if (!fn
|| (p
&& ((unsigned long)strtol(p
+1,NULL
,10)) < offset
)) {
5353 redisLog(REDIS_WARNING
,"%s", messages
[i
]);
5355 redisLog(REDIS_WARNING
,"%d redis-server %p %s + %d", i
, trace
[i
], fn
, (unsigned int)offset
);
5362 static void setupSigSegvAction(void) {
5363 struct sigaction act
;
5365 sigemptyset (&act
.sa_mask
);
5366 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
5367 * is used. Otherwise, sa_handler is used */
5368 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
5369 act
.sa_sigaction
= segvHandler
;
5370 sigaction (SIGSEGV
, &act
, NULL
);
5371 sigaction (SIGBUS
, &act
, NULL
);
5372 sigaction (SIGFPE
, &act
, NULL
);
5373 sigaction (SIGILL
, &act
, NULL
);
5374 sigaction (SIGBUS
, &act
, NULL
);
5377 #else /* HAVE_BACKTRACE */
5378 static void setupSigSegvAction(void) {
5380 #endif /* HAVE_BACKTRACE */
5382 /* =================================== Main! ================================ */
5385 int linuxOvercommitMemoryValue(void) {
5386 FILE *fp
= fopen("/proc/sys/vm/overcommit_memory","r");
5390 if (fgets(buf
,64,fp
) == NULL
) {
5399 void linuxOvercommitMemoryWarning(void) {
5400 if (linuxOvercommitMemoryValue() == 0) {
5401 redisLog(REDIS_WARNING
,"WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.");
5404 #endif /* __linux__ */
5406 static void daemonize(void) {
5410 if (fork() != 0) exit(0); /* parent exits */
5411 setsid(); /* create a new session */
5413 /* Every output goes to /dev/null. If Redis is daemonized but
5414 * the 'logfile' is set to 'stdout' in the configuration file
5415 * it will not log at all. */
5416 if ((fd
= open("/dev/null", O_RDWR
, 0)) != -1) {
5417 dup2(fd
, STDIN_FILENO
);
5418 dup2(fd
, STDOUT_FILENO
);
5419 dup2(fd
, STDERR_FILENO
);
5420 if (fd
> STDERR_FILENO
) close(fd
);
5422 /* Try to write the pid file */
5423 fp
= fopen(server
.pidfile
,"w");
5425 fprintf(fp
,"%d\n",getpid());
5430 int main(int argc
, char **argv
) {
5433 ResetServerSaveParams();
5434 loadServerConfig(argv
[1]);
5435 } else if (argc
> 2) {
5436 fprintf(stderr
,"Usage: ./redis-server [/path/to/redis.conf]\n");
5439 redisLog(REDIS_WARNING
,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
5442 if (server
.daemonize
) daemonize();
5443 redisLog(REDIS_NOTICE
,"Server started, Redis version " REDIS_VERSION
);
5445 linuxOvercommitMemoryWarning();
5447 if (rdbLoad(server
.dbfilename
) == REDIS_OK
)
5448 redisLog(REDIS_NOTICE
,"DB loaded from disk");
5449 if (aeCreateFileEvent(server
.el
, server
.fd
, AE_READABLE
,
5450 acceptHandler
, NULL
, NULL
) == AE_ERR
) oom("creating file event");
5451 redisLog(REDIS_NOTICE
,"The server is now ready to accept connections on port %d", server
.port
);
5453 aeDeleteEventLoop(server
.el
);