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 "0.09"
46 #include <arpa/inet.h>
50 #include <sys/resource.h>
53 #include "ae.h" /* Event driven programming library */
54 #include "sds.h" /* Dynamic safe strings */
55 #include "anet.h" /* Networking the easy way */
56 #include "dict.h" /* Hash tables */
57 #include "adlist.h" /* Linked lists */
58 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
64 /* Static server configuration */
65 #define REDIS_SERVERPORT 6379 /* TCP port */
66 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
67 #define REDIS_QUERYBUF_LEN 1024
68 #define REDIS_LOADBUF_LEN 1024
69 #define REDIS_MAX_ARGS 16
70 #define REDIS_DEFAULT_DBNUM 16
71 #define REDIS_CONFIGLINE_MAX 1024
72 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
73 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
75 /* Hash table parameters */
76 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
77 #define REDIS_HT_MINSLOTS 16384 /* Never resize the HT under this */
80 #define REDIS_CMD_BULK 1
81 #define REDIS_CMD_INLINE 2
84 #define REDIS_STRING 0
89 /* Object types only used for dumping to disk */
90 #define REDIS_SELECTDB 254
93 /* Defines related to the dump file format. To store 32 bits lengths for short
94 * keys requires a lot of space, so we check the most significant 2 bits of
95 * the first byte to interpreter the length:
97 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
98 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
99 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
100 * 11|000000 this means: specially encoded object will follow. The six bits
101 * number specify the kind of object that follows.
102 * See the REDIS_RDB_ENC_* defines.
104 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
105 * values, will fit inside. */
106 #define REDIS_RDB_6BITLEN 0
107 #define REDIS_RDB_14BITLEN 1
108 #define REDIS_RDB_32BITLEN 2
109 #define REDIS_RDB_ENCVAL 3
110 #define REDIS_RDB_LENERR UINT_MAX
112 /* When a length of a string object stored on disk has the first two bits
113 * set, the remaining two bits specify a special encoding for the object
114 * accordingly to the following defines: */
115 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
116 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
117 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
118 #define REDIS_RDB_ENC_FLZ 3 /* string compressed with FASTLZ */
121 #define REDIS_CLOSE 1 /* This client connection should be closed ASAP */
122 #define REDIS_SLAVE 2 /* This client is a slave server */
123 #define REDIS_MASTER 4 /* This client is a master server */
124 #define REDIS_MONITOR 8 /* This client is a slave monitor, see MONITOR */
126 /* Server replication state */
127 #define REDIS_REPL_NONE 0 /* No active replication */
128 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
129 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
131 /* List related stuff */
135 /* Sort operations */
136 #define REDIS_SORT_GET 0
137 #define REDIS_SORT_DEL 1
138 #define REDIS_SORT_INCR 2
139 #define REDIS_SORT_DECR 3
140 #define REDIS_SORT_ASC 4
141 #define REDIS_SORT_DESC 5
142 #define REDIS_SORTKEY_MAX 1024
145 #define REDIS_DEBUG 0
146 #define REDIS_NOTICE 1
147 #define REDIS_WARNING 2
149 /* Anti-warning macro... */
150 #define REDIS_NOTUSED(V) ((void) V)
152 /*================================= Data types ============================== */
154 /* A redis object, that is a type able to hold a string / list / set */
155 typedef struct redisObject
{
161 /* With multiplexing we need to take per-clinet state.
162 * Clients are taken in a liked list. */
163 typedef struct redisClient
{
168 robj
*argv
[REDIS_MAX_ARGS
];
170 int bulklen
; /* bulk read len. -1 if not in bulk read mode */
173 time_t lastinteraction
; /* time of the last interaction, used for timeout */
174 int flags
; /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
175 int slaveseldb
; /* slave selected db, if this client is a slave */
176 int authenticated
; /* when requirepass is non-NULL */
184 /* Global server state structure */
190 unsigned int sharingpoolsize
;
191 long long dirty
; /* changes to DB from the last save */
193 list
*slaves
, *monitors
;
194 char neterr
[ANET_ERR_LEN
];
196 int cronloops
; /* number of times the cron function run */
197 list
*objfreelist
; /* A list of freed objects to avoid malloc() */
198 time_t lastsave
; /* Unix time of last save succeeede */
199 int usedmemory
; /* Used memory in megabytes */
200 /* Fields used only for stats */
201 time_t stat_starttime
; /* server start time */
202 long long stat_numcommands
; /* number of processed commands */
203 long long stat_numconnections
; /* number of connections received */
211 int bgsaveinprogress
;
212 struct saveparam
*saveparams
;
219 /* Replication related */
225 /* Sort parameters - qsort_r() is only available under BSD so we
226 * have to take this state global, in order to pass it to sortCompare() */
232 typedef void redisCommandProc(redisClient
*c
);
233 struct redisCommand
{
235 redisCommandProc
*proc
;
240 typedef struct _redisSortObject
{
248 typedef struct _redisSortOperation
{
251 } redisSortOperation
;
253 struct sharedObjectsStruct
{
254 robj
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *pong
, *space
,
255 *colon
, *nullbulk
, *nullmultibulk
,
256 *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
,
257 *outofrangeerr
, *plus
,
258 *select0
, *select1
, *select2
, *select3
, *select4
,
259 *select5
, *select6
, *select7
, *select8
, *select9
;
262 /*================================ Prototypes =============================== */
264 static void freeStringObject(robj
*o
);
265 static void freeListObject(robj
*o
);
266 static void freeSetObject(robj
*o
);
267 static void decrRefCount(void *o
);
268 static robj
*createObject(int type
, void *ptr
);
269 static void freeClient(redisClient
*c
);
270 static int rdbLoad(char *filename
);
271 static void addReply(redisClient
*c
, robj
*obj
);
272 static void addReplySds(redisClient
*c
, sds s
);
273 static void incrRefCount(robj
*o
);
274 static int rdbSaveBackground(char *filename
);
275 static robj
*createStringObject(char *ptr
, size_t len
);
276 static void replicationFeedSlaves(list
*slaves
, struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
);
277 static int syncWithMaster(void);
278 static robj
*tryObjectSharing(robj
*o
);
280 static void authCommand(redisClient
*c
);
281 static void pingCommand(redisClient
*c
);
282 static void echoCommand(redisClient
*c
);
283 static void setCommand(redisClient
*c
);
284 static void setnxCommand(redisClient
*c
);
285 static void getCommand(redisClient
*c
);
286 static void delCommand(redisClient
*c
);
287 static void existsCommand(redisClient
*c
);
288 static void incrCommand(redisClient
*c
);
289 static void decrCommand(redisClient
*c
);
290 static void incrbyCommand(redisClient
*c
);
291 static void decrbyCommand(redisClient
*c
);
292 static void selectCommand(redisClient
*c
);
293 static void randomkeyCommand(redisClient
*c
);
294 static void keysCommand(redisClient
*c
);
295 static void dbsizeCommand(redisClient
*c
);
296 static void lastsaveCommand(redisClient
*c
);
297 static void saveCommand(redisClient
*c
);
298 static void bgsaveCommand(redisClient
*c
);
299 static void shutdownCommand(redisClient
*c
);
300 static void moveCommand(redisClient
*c
);
301 static void renameCommand(redisClient
*c
);
302 static void renamenxCommand(redisClient
*c
);
303 static void lpushCommand(redisClient
*c
);
304 static void rpushCommand(redisClient
*c
);
305 static void lpopCommand(redisClient
*c
);
306 static void rpopCommand(redisClient
*c
);
307 static void llenCommand(redisClient
*c
);
308 static void lindexCommand(redisClient
*c
);
309 static void lrangeCommand(redisClient
*c
);
310 static void ltrimCommand(redisClient
*c
);
311 static void typeCommand(redisClient
*c
);
312 static void lsetCommand(redisClient
*c
);
313 static void saddCommand(redisClient
*c
);
314 static void sremCommand(redisClient
*c
);
315 static void sismemberCommand(redisClient
*c
);
316 static void scardCommand(redisClient
*c
);
317 static void sinterCommand(redisClient
*c
);
318 static void sinterstoreCommand(redisClient
*c
);
319 static void syncCommand(redisClient
*c
);
320 static void flushdbCommand(redisClient
*c
);
321 static void flushallCommand(redisClient
*c
);
322 static void sortCommand(redisClient
*c
);
323 static void lremCommand(redisClient
*c
);
324 static void infoCommand(redisClient
*c
);
325 static void mgetCommand(redisClient
*c
);
326 static void monitorCommand(redisClient
*c
);
328 /*================================= Globals ================================= */
331 static struct redisServer server
; /* server global state */
332 static struct redisCommand cmdTable
[] = {
333 {"get",getCommand
,2,REDIS_CMD_INLINE
},
334 {"set",setCommand
,3,REDIS_CMD_BULK
},
335 {"setnx",setnxCommand
,3,REDIS_CMD_BULK
},
336 {"del",delCommand
,2,REDIS_CMD_INLINE
},
337 {"exists",existsCommand
,2,REDIS_CMD_INLINE
},
338 {"incr",incrCommand
,2,REDIS_CMD_INLINE
},
339 {"decr",decrCommand
,2,REDIS_CMD_INLINE
},
340 {"mget",mgetCommand
,-2,REDIS_CMD_INLINE
},
341 {"rpush",rpushCommand
,3,REDIS_CMD_BULK
},
342 {"lpush",lpushCommand
,3,REDIS_CMD_BULK
},
343 {"rpop",rpopCommand
,2,REDIS_CMD_INLINE
},
344 {"lpop",lpopCommand
,2,REDIS_CMD_INLINE
},
345 {"llen",llenCommand
,2,REDIS_CMD_INLINE
},
346 {"lindex",lindexCommand
,3,REDIS_CMD_INLINE
},
347 {"lset",lsetCommand
,4,REDIS_CMD_BULK
},
348 {"lrange",lrangeCommand
,4,REDIS_CMD_INLINE
},
349 {"ltrim",ltrimCommand
,4,REDIS_CMD_INLINE
},
350 {"lrem",lremCommand
,4,REDIS_CMD_BULK
},
351 {"sadd",saddCommand
,3,REDIS_CMD_BULK
},
352 {"srem",sremCommand
,3,REDIS_CMD_BULK
},
353 {"sismember",sismemberCommand
,3,REDIS_CMD_BULK
},
354 {"scard",scardCommand
,2,REDIS_CMD_INLINE
},
355 {"sinter",sinterCommand
,-2,REDIS_CMD_INLINE
},
356 {"sinterstore",sinterstoreCommand
,-3,REDIS_CMD_INLINE
},
357 {"smembers",sinterCommand
,2,REDIS_CMD_INLINE
},
358 {"incrby",incrbyCommand
,3,REDIS_CMD_INLINE
},
359 {"decrby",decrbyCommand
,3,REDIS_CMD_INLINE
},
360 {"randomkey",randomkeyCommand
,1,REDIS_CMD_INLINE
},
361 {"select",selectCommand
,2,REDIS_CMD_INLINE
},
362 {"move",moveCommand
,3,REDIS_CMD_INLINE
},
363 {"rename",renameCommand
,3,REDIS_CMD_INLINE
},
364 {"renamenx",renamenxCommand
,3,REDIS_CMD_INLINE
},
365 {"keys",keysCommand
,2,REDIS_CMD_INLINE
},
366 {"dbsize",dbsizeCommand
,1,REDIS_CMD_INLINE
},
367 {"auth",authCommand
,2,REDIS_CMD_INLINE
},
368 {"ping",pingCommand
,1,REDIS_CMD_INLINE
},
369 {"echo",echoCommand
,2,REDIS_CMD_BULK
},
370 {"save",saveCommand
,1,REDIS_CMD_INLINE
},
371 {"bgsave",bgsaveCommand
,1,REDIS_CMD_INLINE
},
372 {"shutdown",shutdownCommand
,1,REDIS_CMD_INLINE
},
373 {"lastsave",lastsaveCommand
,1,REDIS_CMD_INLINE
},
374 {"type",typeCommand
,2,REDIS_CMD_INLINE
},
375 {"sync",syncCommand
,1,REDIS_CMD_INLINE
},
376 {"flushdb",flushdbCommand
,1,REDIS_CMD_INLINE
},
377 {"flushall",flushallCommand
,1,REDIS_CMD_INLINE
},
378 {"sort",sortCommand
,-2,REDIS_CMD_INLINE
},
379 {"info",infoCommand
,1,REDIS_CMD_INLINE
},
380 {"monitor",monitorCommand
,1,REDIS_CMD_INLINE
},
384 /*============================ Utility functions ============================ */
386 /* Glob-style pattern matching. */
387 int stringmatchlen(const char *pattern
, int patternLen
,
388 const char *string
, int stringLen
, int nocase
)
393 while (pattern
[1] == '*') {
398 return 1; /* match */
400 if (stringmatchlen(pattern
+1, patternLen
-1,
401 string
, stringLen
, nocase
))
402 return 1; /* match */
406 return 0; /* no match */
410 return 0; /* no match */
420 not = pattern
[0] == '^';
427 if (pattern
[0] == '\\') {
430 if (pattern
[0] == string
[0])
432 } else if (pattern
[0] == ']') {
434 } else if (patternLen
== 0) {
438 } else if (pattern
[1] == '-' && patternLen
>= 3) {
439 int start
= pattern
[0];
440 int end
= pattern
[2];
448 start
= tolower(start
);
454 if (c
>= start
&& c
<= end
)
458 if (pattern
[0] == string
[0])
461 if (tolower((int)pattern
[0]) == tolower((int)string
[0]))
471 return 0; /* no match */
477 if (patternLen
>= 2) {
484 if (pattern
[0] != string
[0])
485 return 0; /* no match */
487 if (tolower((int)pattern
[0]) != tolower((int)string
[0]))
488 return 0; /* no match */
496 if (stringLen
== 0) {
497 while(*pattern
== '*') {
504 if (patternLen
== 0 && stringLen
== 0)
509 void redisLog(int level
, const char *fmt
, ...)
514 fp
= (server
.logfile
== NULL
) ? stdout
: fopen(server
.logfile
,"a");
518 if (level
>= server
.verbosity
) {
520 fprintf(fp
,"%c ",c
[level
]);
521 vfprintf(fp
, fmt
, ap
);
527 if (server
.logfile
) fclose(fp
);
530 /*====================== Hash table type implementation ==================== */
532 /* This is an hash table type that uses the SDS dynamic strings libary as
533 * keys and radis objects as values (objects can hold SDS strings,
536 static int sdsDictKeyCompare(void *privdata
, const void *key1
,
540 DICT_NOTUSED(privdata
);
542 l1
= sdslen((sds
)key1
);
543 l2
= sdslen((sds
)key2
);
544 if (l1
!= l2
) return 0;
545 return memcmp(key1
, key2
, l1
) == 0;
548 static void dictRedisObjectDestructor(void *privdata
, void *val
)
550 DICT_NOTUSED(privdata
);
555 static int dictSdsKeyCompare(void *privdata
, const void *key1
,
558 const robj
*o1
= key1
, *o2
= key2
;
559 return sdsDictKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
562 static unsigned int dictSdsHash(const void *key
) {
564 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
567 static dictType setDictType
= {
568 dictSdsHash
, /* hash function */
571 dictSdsKeyCompare
, /* key compare */
572 dictRedisObjectDestructor
, /* key destructor */
573 NULL
/* val destructor */
576 static dictType hashDictType
= {
577 dictSdsHash
, /* hash function */
580 dictSdsKeyCompare
, /* key compare */
581 dictRedisObjectDestructor
, /* key destructor */
582 dictRedisObjectDestructor
/* val destructor */
585 /* ========================= Random utility functions ======================= */
587 /* Redis generally does not try to recover from out of memory conditions
588 * when allocating objects or strings, it is not clear if it will be possible
589 * to report this condition to the client since the networking layer itself
590 * is based on heap allocation for send buffers, so we simply abort.
591 * At least the code will be simpler to read... */
592 static void oom(const char *msg
) {
593 fprintf(stderr
, "%s: Out of memory\n",msg
);
599 /* ====================== Redis server networking stuff ===================== */
600 void closeTimedoutClients(void) {
604 time_t now
= time(NULL
);
606 li
= listGetIterator(server
.clients
,AL_START_HEAD
);
608 while ((ln
= listNextElement(li
)) != NULL
) {
609 c
= listNodeValue(ln
);
610 if (!(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
611 (now
- c
->lastinteraction
> server
.maxidletime
)) {
612 redisLog(REDIS_DEBUG
,"Closing idle client");
616 listReleaseIterator(li
);
619 int serverCron(struct aeEventLoop
*eventLoop
, long long id
, void *clientData
) {
620 int j
, size
, used
, loops
= server
.cronloops
++;
621 REDIS_NOTUSED(eventLoop
);
623 REDIS_NOTUSED(clientData
);
625 /* Update the global state with the amount of used memory */
626 server
.usedmemory
= zmalloc_used_memory();
628 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
629 * we resize the hash table to save memory */
630 for (j
= 0; j
< server
.dbnum
; j
++) {
631 size
= dictGetHashTableSize(server
.dict
[j
]);
632 used
= dictGetHashTableUsed(server
.dict
[j
]);
633 if (!(loops
% 5) && used
> 0) {
634 redisLog(REDIS_DEBUG
,"DB %d: %d keys in %d slots HT.",j
,used
,size
);
635 /* dictPrintStats(server.dict); */
637 if (size
&& used
&& size
> REDIS_HT_MINSLOTS
&&
638 (used
*100/size
< REDIS_HT_MINFILL
)) {
639 redisLog(REDIS_NOTICE
,"The hash table %d is too sparse, resize it...",j
);
640 dictResize(server
.dict
[j
]);
641 redisLog(REDIS_NOTICE
,"Hash table %d resized.",j
);
645 /* Show information about connected clients */
647 redisLog(REDIS_DEBUG
,"%d clients connected (%d slaves), %d bytes in use",
648 listLength(server
.clients
)-listLength(server
.slaves
),
649 listLength(server
.slaves
),
651 dictGetHashTableUsed(server
.sharingpool
));
654 /* Close connections of timedout clients */
656 closeTimedoutClients();
658 /* Check if a background saving in progress terminated */
659 if (server
.bgsaveinprogress
) {
661 if (wait4(-1,&statloc
,WNOHANG
,NULL
)) {
662 int exitcode
= WEXITSTATUS(statloc
);
664 redisLog(REDIS_NOTICE
,
665 "Background saving terminated with success");
667 server
.lastsave
= time(NULL
);
669 redisLog(REDIS_WARNING
,
670 "Background saving error");
672 server
.bgsaveinprogress
= 0;
675 /* If there is not a background saving in progress check if
676 * we have to save now */
677 time_t now
= time(NULL
);
678 for (j
= 0; j
< server
.saveparamslen
; j
++) {
679 struct saveparam
*sp
= server
.saveparams
+j
;
681 if (server
.dirty
>= sp
->changes
&&
682 now
-server
.lastsave
> sp
->seconds
) {
683 redisLog(REDIS_NOTICE
,"%d changes in %d seconds. Saving...",
684 sp
->changes
, sp
->seconds
);
685 rdbSaveBackground(server
.dbfilename
);
690 /* Check if we should connect to a MASTER */
691 if (server
.replstate
== REDIS_REPL_CONNECT
) {
692 redisLog(REDIS_NOTICE
,"Connecting to MASTER...");
693 if (syncWithMaster() == REDIS_OK
) {
694 redisLog(REDIS_NOTICE
,"MASTER <-> SLAVE sync succeeded");
700 static void createSharedObjects(void) {
701 shared
.crlf
= createObject(REDIS_STRING
,sdsnew("\r\n"));
702 shared
.ok
= createObject(REDIS_STRING
,sdsnew("+OK\r\n"));
703 shared
.err
= createObject(REDIS_STRING
,sdsnew("-ERR\r\n"));
704 shared
.emptybulk
= createObject(REDIS_STRING
,sdsnew("$0\r\n\r\n"));
705 shared
.czero
= createObject(REDIS_STRING
,sdsnew(":0\r\n"));
706 shared
.cone
= createObject(REDIS_STRING
,sdsnew(":1\r\n"));
707 shared
.nullbulk
= createObject(REDIS_STRING
,sdsnew("$-1\r\n"));
708 shared
.nullmultibulk
= createObject(REDIS_STRING
,sdsnew("*-1\r\n"));
709 shared
.emptymultibulk
= createObject(REDIS_STRING
,sdsnew("*0\r\n"));
711 shared
.pong
= createObject(REDIS_STRING
,sdsnew("+PONG\r\n"));
712 shared
.wrongtypeerr
= createObject(REDIS_STRING
,sdsnew(
713 "-ERR Operation against a key holding the wrong kind of value\r\n"));
714 shared
.nokeyerr
= createObject(REDIS_STRING
,sdsnew(
715 "-ERR no such key\r\n"));
716 shared
.syntaxerr
= createObject(REDIS_STRING
,sdsnew(
717 "-ERR syntax error\r\n"));
718 shared
.sameobjecterr
= createObject(REDIS_STRING
,sdsnew(
719 "-ERR source and destination objects are the same\r\n"));
720 shared
.outofrangeerr
= createObject(REDIS_STRING
,sdsnew(
721 "-ERR index out of range\r\n"));
722 shared
.space
= createObject(REDIS_STRING
,sdsnew(" "));
723 shared
.colon
= createObject(REDIS_STRING
,sdsnew(":"));
724 shared
.plus
= createObject(REDIS_STRING
,sdsnew("+"));
725 shared
.select0
= createStringObject("select 0\r\n",10);
726 shared
.select1
= createStringObject("select 1\r\n",10);
727 shared
.select2
= createStringObject("select 2\r\n",10);
728 shared
.select3
= createStringObject("select 3\r\n",10);
729 shared
.select4
= createStringObject("select 4\r\n",10);
730 shared
.select5
= createStringObject("select 5\r\n",10);
731 shared
.select6
= createStringObject("select 6\r\n",10);
732 shared
.select7
= createStringObject("select 7\r\n",10);
733 shared
.select8
= createStringObject("select 8\r\n",10);
734 shared
.select9
= createStringObject("select 9\r\n",10);
737 static void appendServerSaveParams(time_t seconds
, int changes
) {
738 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
739 if (server
.saveparams
== NULL
) oom("appendServerSaveParams");
740 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
741 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
742 server
.saveparamslen
++;
745 static void ResetServerSaveParams() {
746 zfree(server
.saveparams
);
747 server
.saveparams
= NULL
;
748 server
.saveparamslen
= 0;
751 static void initServerConfig() {
752 server
.dbnum
= REDIS_DEFAULT_DBNUM
;
753 server
.port
= REDIS_SERVERPORT
;
754 server
.verbosity
= REDIS_DEBUG
;
755 server
.maxidletime
= REDIS_MAXIDLETIME
;
756 server
.saveparams
= NULL
;
757 server
.logfile
= NULL
; /* NULL = log on standard output */
758 server
.bindaddr
= NULL
;
759 server
.glueoutputbuf
= 1;
760 server
.daemonize
= 0;
761 server
.pidfile
= "/var/run/redis.pid";
762 server
.dbfilename
= "dump.rdb";
763 server
.requirepass
= NULL
;
764 server
.shareobjects
= 0;
765 ResetServerSaveParams();
767 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
768 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
769 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
770 /* Replication related */
772 server
.masterhost
= NULL
;
773 server
.masterport
= 6379;
774 server
.master
= NULL
;
775 server
.replstate
= REDIS_REPL_NONE
;
778 static void initServer() {
781 signal(SIGHUP
, SIG_IGN
);
782 signal(SIGPIPE
, SIG_IGN
);
784 server
.clients
= listCreate();
785 server
.slaves
= listCreate();
786 server
.monitors
= listCreate();
787 server
.objfreelist
= listCreate();
788 createSharedObjects();
789 server
.el
= aeCreateEventLoop();
790 server
.dict
= zmalloc(sizeof(dict
*)*server
.dbnum
);
791 server
.sharingpool
= dictCreate(&setDictType
,NULL
);
792 server
.sharingpoolsize
= 1024;
793 if (!server
.dict
|| !server
.clients
|| !server
.slaves
|| !server
.monitors
|| !server
.el
|| !server
.objfreelist
)
794 oom("server initialization"); /* Fatal OOM */
795 server
.fd
= anetTcpServer(server
.neterr
, server
.port
, server
.bindaddr
);
796 if (server
.fd
== -1) {
797 redisLog(REDIS_WARNING
, "Opening TCP port: %s", server
.neterr
);
800 for (j
= 0; j
< server
.dbnum
; j
++)
801 server
.dict
[j
] = dictCreate(&hashDictType
,NULL
);
802 server
.cronloops
= 0;
803 server
.bgsaveinprogress
= 0;
804 server
.lastsave
= time(NULL
);
806 server
.usedmemory
= 0;
807 server
.stat_numcommands
= 0;
808 server
.stat_numconnections
= 0;
809 server
.stat_starttime
= time(NULL
);
810 aeCreateTimeEvent(server
.el
, 1000, serverCron
, NULL
, NULL
);
813 /* Empty the whole database */
814 static void emptyDb() {
817 for (j
= 0; j
< server
.dbnum
; j
++)
818 dictEmpty(server
.dict
[j
]);
821 /* I agree, this is a very rudimental way to load a configuration...
822 will improve later if the config gets more complex */
823 static void loadServerConfig(char *filename
) {
824 FILE *fp
= fopen(filename
,"r");
825 char buf
[REDIS_CONFIGLINE_MAX
+1], *err
= NULL
;
830 redisLog(REDIS_WARNING
,"Fatal error, can't open config file");
833 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
) {
839 line
= sdstrim(line
," \t\r\n");
841 /* Skip comments and blank lines*/
842 if (line
[0] == '#' || line
[0] == '\0') {
847 /* Split into arguments */
848 argv
= sdssplitlen(line
,sdslen(line
)," ",1,&argc
);
851 /* Execute config directives */
852 if (!strcmp(argv
[0],"timeout") && argc
== 2) {
853 server
.maxidletime
= atoi(argv
[1]);
854 if (server
.maxidletime
< 1) {
855 err
= "Invalid timeout value"; goto loaderr
;
857 } else if (!strcmp(argv
[0],"port") && argc
== 2) {
858 server
.port
= atoi(argv
[1]);
859 if (server
.port
< 1 || server
.port
> 65535) {
860 err
= "Invalid port"; goto loaderr
;
862 } else if (!strcmp(argv
[0],"bind") && argc
== 2) {
863 server
.bindaddr
= zstrdup(argv
[1]);
864 } else if (!strcmp(argv
[0],"save") && argc
== 3) {
865 int seconds
= atoi(argv
[1]);
866 int changes
= atoi(argv
[2]);
867 if (seconds
< 1 || changes
< 0) {
868 err
= "Invalid save parameters"; goto loaderr
;
870 appendServerSaveParams(seconds
,changes
);
871 } else if (!strcmp(argv
[0],"dir") && argc
== 2) {
872 if (chdir(argv
[1]) == -1) {
873 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
874 argv
[1], strerror(errno
));
877 } else if (!strcmp(argv
[0],"loglevel") && argc
== 2) {
878 if (!strcmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
879 else if (!strcmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
880 else if (!strcmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
882 err
= "Invalid log level. Must be one of debug, notice, warning";
885 } else if (!strcmp(argv
[0],"logfile") && argc
== 2) {
888 server
.logfile
= zstrdup(argv
[1]);
889 if (!strcmp(server
.logfile
,"stdout")) {
890 zfree(server
.logfile
);
891 server
.logfile
= NULL
;
893 if (server
.logfile
) {
894 /* Test if we are able to open the file. The server will not
895 * be able to abort just for this problem later... */
896 fp
= fopen(server
.logfile
,"a");
898 err
= sdscatprintf(sdsempty(),
899 "Can't open the log file: %s", strerror(errno
));
904 } else if (!strcmp(argv
[0],"databases") && argc
== 2) {
905 server
.dbnum
= atoi(argv
[1]);
906 if (server
.dbnum
< 1) {
907 err
= "Invalid number of databases"; goto loaderr
;
909 } else if (!strcmp(argv
[0],"slaveof") && argc
== 3) {
910 server
.masterhost
= sdsnew(argv
[1]);
911 server
.masterport
= atoi(argv
[2]);
912 server
.replstate
= REDIS_REPL_CONNECT
;
913 } else if (!strcmp(argv
[0],"glueoutputbuf") && argc
== 2) {
915 if (!strcmp(argv
[1],"yes")) server
.glueoutputbuf
= 1;
916 else if (!strcmp(argv
[1],"no")) server
.glueoutputbuf
= 0;
918 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
920 } else if (!strcmp(argv
[0],"shareobjects") && argc
== 2) {
922 if (!strcmp(argv
[1],"yes")) server
.shareobjects
= 1;
923 else if (!strcmp(argv
[1],"no")) server
.shareobjects
= 0;
925 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
927 } else if (!strcmp(argv
[0],"daemonize") && argc
== 2) {
929 if (!strcmp(argv
[1],"yes")) server
.daemonize
= 1;
930 else if (!strcmp(argv
[1],"no")) server
.daemonize
= 0;
932 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
934 } else if (!strcmp(argv
[0],"requirepass") && argc
== 2) {
935 server
.requirepass
= zstrdup(argv
[1]);
936 } else if (!strcmp(argv
[0],"pidfile") && argc
== 2) {
937 server
.pidfile
= zstrdup(argv
[1]);
939 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
941 for (j
= 0; j
< argc
; j
++)
950 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
951 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
952 fprintf(stderr
, ">>> '%s'\n", line
);
953 fprintf(stderr
, "%s\n", err
);
957 static void freeClientArgv(redisClient
*c
) {
960 for (j
= 0; j
< c
->argc
; j
++)
961 decrRefCount(c
->argv
[j
]);
965 static void freeClient(redisClient
*c
) {
968 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
969 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
970 sdsfree(c
->querybuf
);
971 listRelease(c
->reply
);
974 ln
= listSearchKey(server
.clients
,c
);
976 listDelNode(server
.clients
,ln
);
977 if (c
->flags
& REDIS_SLAVE
) {
978 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
979 ln
= listSearchKey(l
,c
);
983 if (c
->flags
& REDIS_MASTER
) {
984 server
.master
= NULL
;
985 server
.replstate
= REDIS_REPL_CONNECT
;
990 static void glueReplyBuffersIfNeeded(redisClient
*c
) {
992 listNode
*ln
= c
->reply
->head
, *next
;
997 totlen
+= sdslen(o
->ptr
);
999 /* This optimization makes more sense if we don't have to copy
1001 if (totlen
> 1024) return;
1007 ln
= c
->reply
->head
;
1011 memcpy(buf
+copylen
,o
->ptr
,sdslen(o
->ptr
));
1012 copylen
+= sdslen(o
->ptr
);
1013 listDelNode(c
->reply
,ln
);
1016 /* Now the output buffer is empty, add the new single element */
1017 addReplySds(c
,sdsnewlen(buf
,totlen
));
1021 static void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1022 redisClient
*c
= privdata
;
1023 int nwritten
= 0, totwritten
= 0, objlen
;
1026 REDIS_NOTUSED(mask
);
1028 if (server
.glueoutputbuf
&& listLength(c
->reply
) > 1)
1029 glueReplyBuffersIfNeeded(c
);
1030 while(listLength(c
->reply
)) {
1031 o
= listNodeValue(listFirst(c
->reply
));
1032 objlen
= sdslen(o
->ptr
);
1035 listDelNode(c
->reply
,listFirst(c
->reply
));
1039 if (c
->flags
& REDIS_MASTER
) {
1040 nwritten
= objlen
- c
->sentlen
;
1042 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
, objlen
- c
->sentlen
);
1043 if (nwritten
<= 0) break;
1045 c
->sentlen
+= nwritten
;
1046 totwritten
+= nwritten
;
1047 /* If we fully sent the object on head go to the next one */
1048 if (c
->sentlen
== objlen
) {
1049 listDelNode(c
->reply
,listFirst(c
->reply
));
1053 if (nwritten
== -1) {
1054 if (errno
== EAGAIN
) {
1057 redisLog(REDIS_DEBUG
,
1058 "Error writing to client: %s", strerror(errno
));
1063 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
1064 if (listLength(c
->reply
) == 0) {
1066 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
1070 static struct redisCommand
*lookupCommand(char *name
) {
1072 while(cmdTable
[j
].name
!= NULL
) {
1073 if (!strcmp(name
,cmdTable
[j
].name
)) return &cmdTable
[j
];
1079 /* resetClient prepare the client to process the next command */
1080 static void resetClient(redisClient
*c
) {
1085 /* If this function gets called we already read a whole
1086 * command, argments are in the client argv/argc fields.
1087 * processCommand() execute the command or prepare the
1088 * server for a bulk read from the client.
1090 * If 1 is returned the client is still alive and valid and
1091 * and other operations can be performed by the caller. Otherwise
1092 * if 0 is returned the client was destroied (i.e. after QUIT). */
1093 static int processCommand(redisClient
*c
) {
1094 struct redisCommand
*cmd
;
1097 sdstolower(c
->argv
[0]->ptr
);
1098 /* The QUIT command is handled as a special case. Normal command
1099 * procs are unable to close the client connection safely */
1100 if (!strcmp(c
->argv
[0]->ptr
,"quit")) {
1104 cmd
= lookupCommand(c
->argv
[0]->ptr
);
1106 addReplySds(c
,sdsnew("-ERR unknown command\r\n"));
1109 } else if ((cmd
->arity
> 0 && cmd
->arity
!= c
->argc
) ||
1110 (c
->argc
< -cmd
->arity
)) {
1111 addReplySds(c
,sdsnew("-ERR wrong number of arguments\r\n"));
1114 } else if (cmd
->flags
& REDIS_CMD_BULK
&& c
->bulklen
== -1) {
1115 int bulklen
= atoi(c
->argv
[c
->argc
-1]->ptr
);
1117 decrRefCount(c
->argv
[c
->argc
-1]);
1118 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
1120 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
1125 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
1126 /* It is possible that the bulk read is already in the
1127 * buffer. Check this condition and handle it accordingly */
1128 if ((signed)sdslen(c
->querybuf
) >= c
->bulklen
) {
1129 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
1131 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
1136 /* Let's try to share objects on the command arguments vector */
1137 if (server
.shareobjects
) {
1139 for(j
= 1; j
< c
->argc
; j
++)
1140 c
->argv
[j
] = tryObjectSharing(c
->argv
[j
]);
1142 /* Check if the user is authenticated */
1143 if (server
.requirepass
&& !c
->authenticated
&& cmd
->proc
!= authCommand
) {
1144 addReplySds(c
,sdsnew("-ERR operation not permitted\r\n"));
1149 /* Exec the command */
1150 dirty
= server
.dirty
;
1152 if (server
.dirty
-dirty
!= 0 && listLength(server
.slaves
))
1153 replicationFeedSlaves(server
.slaves
,cmd
,c
->dictid
,c
->argv
,c
->argc
);
1154 if (listLength(server
.monitors
))
1155 replicationFeedSlaves(server
.monitors
,cmd
,c
->dictid
,c
->argv
,c
->argc
);
1156 server
.stat_numcommands
++;
1158 /* Prepare the client for the next command */
1159 if (c
->flags
& REDIS_CLOSE
) {
1167 static void replicationFeedSlaves(list
*slaves
, struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
) {
1168 listNode
*ln
= slaves
->head
;
1169 robj
*outv
[REDIS_MAX_ARGS
*4]; /* enough room for args, spaces, newlines */
1172 for (j
= 0; j
< argc
; j
++) {
1173 if (j
!= 0) outv
[outc
++] = shared
.space
;
1174 if ((cmd
->flags
& REDIS_CMD_BULK
) && j
== argc
-1) {
1177 lenobj
= createObject(REDIS_STRING
,
1178 sdscatprintf(sdsempty(),"%d\r\n",sdslen(argv
[j
]->ptr
)));
1179 lenobj
->refcount
= 0;
1180 outv
[outc
++] = lenobj
;
1182 outv
[outc
++] = argv
[j
];
1184 outv
[outc
++] = shared
.crlf
;
1187 redisClient
*slave
= ln
->value
;
1188 if (slave
->slaveseldb
!= dictid
) {
1192 case 0: selectcmd
= shared
.select0
; break;
1193 case 1: selectcmd
= shared
.select1
; break;
1194 case 2: selectcmd
= shared
.select2
; break;
1195 case 3: selectcmd
= shared
.select3
; break;
1196 case 4: selectcmd
= shared
.select4
; break;
1197 case 5: selectcmd
= shared
.select5
; break;
1198 case 6: selectcmd
= shared
.select6
; break;
1199 case 7: selectcmd
= shared
.select7
; break;
1200 case 8: selectcmd
= shared
.select8
; break;
1201 case 9: selectcmd
= shared
.select9
; break;
1203 selectcmd
= createObject(REDIS_STRING
,
1204 sdscatprintf(sdsempty(),"select %d\r\n",dictid
));
1205 selectcmd
->refcount
= 0;
1208 addReply(slave
,selectcmd
);
1209 slave
->slaveseldb
= dictid
;
1211 for (j
= 0; j
< outc
; j
++) addReply(slave
,outv
[j
]);
1216 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1217 redisClient
*c
= (redisClient
*) privdata
;
1218 char buf
[REDIS_QUERYBUF_LEN
];
1221 REDIS_NOTUSED(mask
);
1223 nread
= read(fd
, buf
, REDIS_QUERYBUF_LEN
);
1225 if (errno
== EAGAIN
) {
1228 redisLog(REDIS_DEBUG
, "Reading from client: %s",strerror(errno
));
1232 } else if (nread
== 0) {
1233 redisLog(REDIS_DEBUG
, "Client closed connection");
1238 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
1239 c
->lastinteraction
= time(NULL
);
1245 if (c
->bulklen
== -1) {
1246 /* Read the first line of the query */
1247 char *p
= strchr(c
->querybuf
,'\n');
1253 query
= c
->querybuf
;
1254 c
->querybuf
= sdsempty();
1255 querylen
= 1+(p
-(query
));
1256 if (sdslen(query
) > querylen
) {
1257 /* leave data after the first line of the query in the buffer */
1258 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
1260 *p
= '\0'; /* remove "\n" */
1261 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
1262 sdsupdatelen(query
);
1264 /* Now we can split the query in arguments */
1265 if (sdslen(query
) == 0) {
1266 /* Ignore empty query */
1270 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
1272 if (argv
== NULL
) oom("sdssplitlen");
1273 for (j
= 0; j
< argc
&& j
< REDIS_MAX_ARGS
; j
++) {
1274 if (sdslen(argv
[j
])) {
1275 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
1282 /* Execute the command. If the client is still valid
1283 * after processCommand() return and there is something
1284 * on the query buffer try to process the next command. */
1285 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
1287 } else if (sdslen(c
->querybuf
) >= 1024) {
1288 redisLog(REDIS_DEBUG
, "Client protocol error");
1293 /* Bulk read handling. Note that if we are at this point
1294 the client already sent a command terminated with a newline,
1295 we are reading the bulk data that is actually the last
1296 argument of the command. */
1297 int qbl
= sdslen(c
->querybuf
);
1299 if (c
->bulklen
<= qbl
) {
1300 /* Copy everything but the final CRLF as final argument */
1301 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
1303 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
1310 static int selectDb(redisClient
*c
, int id
) {
1311 if (id
< 0 || id
>= server
.dbnum
)
1313 c
->dict
= server
.dict
[id
];
1318 static redisClient
*createClient(int fd
) {
1319 redisClient
*c
= zmalloc(sizeof(*c
));
1321 anetNonBlock(NULL
,fd
);
1322 anetTcpNoDelay(NULL
,fd
);
1323 if (!c
) return NULL
;
1326 c
->querybuf
= sdsempty();
1331 c
->lastinteraction
= time(NULL
);
1332 c
->authenticated
= 0;
1333 if ((c
->reply
= listCreate()) == NULL
) oom("listCreate");
1334 listSetFreeMethod(c
->reply
,decrRefCount
);
1335 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
1336 readQueryFromClient
, c
, NULL
) == AE_ERR
) {
1340 if (!listAddNodeTail(server
.clients
,c
)) oom("listAddNodeTail");
1344 static void addReply(redisClient
*c
, robj
*obj
) {
1345 if (listLength(c
->reply
) == 0 &&
1346 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
1347 sendReplyToClient
, c
, NULL
) == AE_ERR
) return;
1348 if (!listAddNodeTail(c
->reply
,obj
)) oom("listAddNodeTail");
1352 static void addReplySds(redisClient
*c
, sds s
) {
1353 robj
*o
= createObject(REDIS_STRING
,s
);
1358 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
1362 REDIS_NOTUSED(mask
);
1363 REDIS_NOTUSED(privdata
);
1365 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
1366 if (cfd
== AE_ERR
) {
1367 redisLog(REDIS_DEBUG
,"Accepting client connection: %s", server
.neterr
);
1370 redisLog(REDIS_DEBUG
,"Accepted %s:%d", cip
, cport
);
1371 if (createClient(cfd
) == NULL
) {
1372 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
1373 close(cfd
); /* May be already closed, just ingore errors */
1376 server
.stat_numconnections
++;
1379 /* ======================= Redis objects implementation ===================== */
1381 static robj
*createObject(int type
, void *ptr
) {
1384 if (listLength(server
.objfreelist
)) {
1385 listNode
*head
= listFirst(server
.objfreelist
);
1386 o
= listNodeValue(head
);
1387 listDelNode(server
.objfreelist
,head
);
1389 o
= zmalloc(sizeof(*o
));
1391 if (!o
) oom("createObject");
1398 static robj
*createStringObject(char *ptr
, size_t len
) {
1399 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
1402 static robj
*createListObject(void) {
1403 list
*l
= listCreate();
1405 if (!l
) oom("listCreate");
1406 listSetFreeMethod(l
,decrRefCount
);
1407 return createObject(REDIS_LIST
,l
);
1410 static robj
*createSetObject(void) {
1411 dict
*d
= dictCreate(&setDictType
,NULL
);
1412 if (!d
) oom("dictCreate");
1413 return createObject(REDIS_SET
,d
);
1417 static robj
*createHashObject(void) {
1418 dict
*d
= dictCreate(&hashDictType
,NULL
);
1419 if (!d
) oom("dictCreate");
1420 return createObject(REDIS_SET
,d
);
1424 static void freeStringObject(robj
*o
) {
1428 static void freeListObject(robj
*o
) {
1429 listRelease((list
*) o
->ptr
);
1432 static void freeSetObject(robj
*o
) {
1433 dictRelease((dict
*) o
->ptr
);
1436 static void freeHashObject(robj
*o
) {
1437 dictRelease((dict
*) o
->ptr
);
1440 static void incrRefCount(robj
*o
) {
1444 static void decrRefCount(void *obj
) {
1446 if (--(o
->refcount
) == 0) {
1448 case REDIS_STRING
: freeStringObject(o
); break;
1449 case REDIS_LIST
: freeListObject(o
); break;
1450 case REDIS_SET
: freeSetObject(o
); break;
1451 case REDIS_HASH
: freeHashObject(o
); break;
1452 default: assert(0 != 0); break;
1454 if (listLength(server
.objfreelist
) > REDIS_OBJFREELIST_MAX
||
1455 !listAddNodeHead(server
.objfreelist
,o
))
1460 /* Try to share an object against the shared objects pool */
1461 static robj
*tryObjectSharing(robj
*o
) {
1462 struct dictEntry
*de
;
1465 if (server
.shareobjects
== 0) return o
;
1467 assert(o
->type
== REDIS_STRING
);
1468 de
= dictFind(server
.sharingpool
,o
);
1470 robj
*shared
= dictGetEntryKey(de
);
1472 c
= ((unsigned long) dictGetEntryVal(de
))+1;
1473 dictGetEntryVal(de
) = (void*) c
;
1474 incrRefCount(shared
);
1478 /* Here we are using a stream algorihtm: Every time an object is
1479 * shared we increment its count, everytime there is a miss we
1480 * recrement the counter of a random object. If this object reaches
1481 * zero we remove the object and put the current object instead. */
1482 if (dictGetHashTableUsed(server
.sharingpool
) >=
1483 server
.sharingpoolsize
) {
1484 de
= dictGetRandomKey(server
.sharingpool
);
1486 c
= ((unsigned long) dictGetEntryVal(de
))-1;
1487 dictGetEntryVal(de
) = (void*) c
;
1489 dictDelete(server
.sharingpool
,de
->key
);
1492 c
= 0; /* If the pool is empty we want to add this object */
1497 retval
= dictAdd(server
.sharingpool
,o
,(void*)1);
1498 assert(retval
== DICT_OK
);
1505 /*============================ DB saving/loading ============================ */
1507 static int rdbSaveType(FILE *fp
, unsigned char type
) {
1508 if (fwrite(&type
,1,1,fp
) == 0) return -1;
1512 /* check rdbLoadLen() comments for more info */
1513 static int rdbSaveLen(FILE *fp
, uint32_t len
) {
1514 unsigned char buf
[2];
1517 /* Save a 6 bit len */
1518 buf
[0] = (len
&0xFF)|(REDIS_RDB_6BITLEN
<<6);
1519 if (fwrite(buf
,1,1,fp
) == 0) return -1;
1520 } else if (len
< (1<<14)) {
1521 /* Save a 14 bit len */
1522 buf
[0] = ((len
>>8)&0xFF)|(REDIS_RDB_14BITLEN
<<6);
1524 if (fwrite(buf
,2,1,fp
) == 0) return -1;
1526 /* Save a 32 bit len */
1527 buf
[0] = (REDIS_RDB_32BITLEN
<<6);
1528 if (fwrite(buf
,1,1,fp
) == 0) return -1;
1530 if (fwrite(&len
,4,1,fp
) == 0) return -1;
1535 /* String objects in the form "2391" "-100" without any space and with a
1536 * range of values that can fit in an 8, 16 or 32 bit signed value can be
1537 * encoded as integers to save space */
1538 int rdbTryIntegerEncoding(sds s
, unsigned char *enc
) {
1540 char *endptr
, buf
[32];
1542 /* Check if it's possible to encode this value as a number */
1543 value
= strtoll(s
, &endptr
, 10);
1544 if (endptr
[0] != '\0') return 0;
1545 snprintf(buf
,32,"%lld",value
);
1547 /* If the number converted back into a string is not identical
1548 * then it's not possible to encode the string as integer */
1549 if (strlen(buf
) != sdslen(s
) || memcmp(buf
,s
,sdslen(s
))) return 0;
1551 /* Finally check if it fits in our ranges */
1552 if (value
>= -(1<<7) && value
<= (1<<7)-1) {
1553 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT8
;
1554 enc
[1] = value
&0xFF;
1556 } else if (value
>= -(1<<15) && value
<= (1<<15)-1) {
1557 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT16
;
1558 enc
[1] = value
&0xFF;
1559 enc
[2] = (value
>>8)&0xFF;
1561 } else if (value
>= -((long long)1<<31) && value
<= ((long long)1<<31)-1) {
1562 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT32
;
1563 enc
[1] = value
&0xFF;
1564 enc
[2] = (value
>>8)&0xFF;
1565 enc
[3] = (value
>>16)&0xFF;
1566 enc
[4] = (value
>>24)&0xFF;
1573 /* Save a string objet as [len][data] on disk. If the object is a string
1574 * representation of an integer value we try to safe it in a special form */
1575 static int rdbSaveStringObject(FILE *fp
, robj
*obj
) {
1576 size_t len
= sdslen(obj
->ptr
);
1580 unsigned char buf
[5];
1581 if ((enclen
= rdbTryIntegerEncoding(obj
->ptr
,buf
)) > 0) {
1582 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
1586 if (rdbSaveLen(fp
,len
) == -1) return -1;
1587 if (len
&& fwrite(obj
->ptr
,len
,1,fp
) == 0) return -1;
1591 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
1592 static int rdbSave(char *filename
) {
1593 dictIterator
*di
= NULL
;
1599 snprintf(tmpfile
,256,"temp-%d.%ld.rdb",(int)time(NULL
),(long int)random());
1600 fp
= fopen(tmpfile
,"w");
1602 redisLog(REDIS_WARNING
, "Failed saving the DB: %s", strerror(errno
));
1605 if (fwrite("REDIS0001",9,1,fp
) == 0) goto werr
;
1606 for (j
= 0; j
< server
.dbnum
; j
++) {
1607 dict
*d
= server
.dict
[j
];
1608 if (dictGetHashTableUsed(d
) == 0) continue;
1609 di
= dictGetIterator(d
);
1615 /* Write the SELECT DB opcode */
1616 if (rdbSaveType(fp
,REDIS_SELECTDB
) == -1) goto werr
;
1617 if (rdbSaveLen(fp
,j
) == -1) goto werr
;
1619 /* Iterate this DB writing every entry */
1620 while((de
= dictNext(di
)) != NULL
) {
1621 robj
*key
= dictGetEntryKey(de
);
1622 robj
*o
= dictGetEntryVal(de
);
1624 if (rdbSaveType(fp
,o
->type
) == -1) goto werr
;
1625 if (rdbSaveStringObject(fp
,key
) == -1) goto werr
;
1626 if (o
->type
== REDIS_STRING
) {
1627 /* Save a string value */
1628 if (rdbSaveStringObject(fp
,o
) == -1) goto werr
;
1629 } else if (o
->type
== REDIS_LIST
) {
1630 /* Save a list value */
1631 list
*list
= o
->ptr
;
1632 listNode
*ln
= list
->head
;
1634 if (rdbSaveLen(fp
,listLength(list
)) == -1) goto werr
;
1636 robj
*eleobj
= listNodeValue(ln
);
1638 if (rdbSaveStringObject(fp
,eleobj
) == -1) goto werr
;
1641 } else if (o
->type
== REDIS_SET
) {
1642 /* Save a set value */
1644 dictIterator
*di
= dictGetIterator(set
);
1647 if (!set
) oom("dictGetIteraotr");
1648 if (rdbSaveLen(fp
,dictGetHashTableUsed(set
)) == -1) goto werr
;
1649 while((de
= dictNext(di
)) != NULL
) {
1650 robj
*eleobj
= dictGetEntryKey(de
);
1652 if (rdbSaveStringObject(fp
,eleobj
) == -1) goto werr
;
1654 dictReleaseIterator(di
);
1659 dictReleaseIterator(di
);
1662 if (rdbSaveType(fp
,REDIS_EOF
) == -1) goto werr
;
1664 /* Make sure data will not remain on the OS's output buffers */
1669 /* Use RENAME to make sure the DB file is changed atomically only
1670 * if the generate DB file is ok. */
1671 if (rename(tmpfile
,filename
) == -1) {
1672 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destionation: %s", strerror(errno
));
1676 redisLog(REDIS_NOTICE
,"DB saved on disk");
1678 server
.lastsave
= time(NULL
);
1684 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
1685 if (di
) dictReleaseIterator(di
);
1689 static int rdbSaveBackground(char *filename
) {
1692 if (server
.bgsaveinprogress
) return REDIS_ERR
;
1693 if ((childpid
= fork()) == 0) {
1696 if (rdbSave(filename
) == REDIS_OK
) {
1703 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
1704 server
.bgsaveinprogress
= 1;
1707 return REDIS_OK
; /* unreached */
1710 static int rdbLoadType(FILE *fp
) {
1712 if (fread(&type
,1,1,fp
) == 0) return -1;
1716 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
1717 * of this file for a description of how this are stored on disk.
1719 * isencoded is set to 1 if the readed length is not actually a length but
1720 * an "encoding type", check the above comments for more info */
1721 static uint32_t rdbLoadLen(FILE *fp
, int rdbver
, int *isencoded
) {
1722 unsigned char buf
[2];
1725 if (isencoded
) *isencoded
= 0;
1727 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
1732 if (fread(buf
,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
1733 type
= (buf
[0]&0xC0)>>6;
1734 if (type
== REDIS_RDB_6BITLEN
) {
1735 /* Read a 6 bit len */
1737 } else if (type
== REDIS_RDB_ENCVAL
) {
1738 /* Read a 6 bit len encoding type */
1739 if (isencoded
) *isencoded
= 1;
1741 } else if (type
== REDIS_RDB_14BITLEN
) {
1742 /* Read a 14 bit len */
1743 if (fread(buf
+1,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
1744 return ((buf
[0]&0x3F)<<8)|buf
[1];
1746 /* Read a 32 bit len */
1747 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
1753 static robj
*rdbLoadIntegerObject(FILE *fp
, int enctype
) {
1754 unsigned char enc
[4];
1757 if (enctype
== REDIS_RDB_ENC_INT8
) {
1758 if (fread(enc
,1,1,fp
) == 0) return NULL
;
1759 val
= (signed char)enc
[0];
1760 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
1762 if (fread(enc
,2,1,fp
) == 0) return NULL
;
1763 v
= enc
[0]|(enc
[1]<<8);
1765 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
1767 if (fread(enc
,4,1,fp
) == 0) return NULL
;
1768 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
1771 val
= 0; /* anti-warning */
1774 return createObject(REDIS_STRING
,sdscatprintf(sdsempty(),"%lld",val
));
1777 static robj
*rdbLoadStringObject(FILE*fp
, int rdbver
) {
1782 len
= rdbLoadLen(fp
,rdbver
,&isencoded
);
1785 case REDIS_RDB_ENC_INT8
:
1786 case REDIS_RDB_ENC_INT16
:
1787 case REDIS_RDB_ENC_INT32
:
1788 return rdbLoadIntegerObject(fp
,len
);
1794 if (len
== REDIS_RDB_LENERR
) return NULL
;
1795 val
= sdsnewlen(NULL
,len
);
1796 if (len
&& fread(val
,len
,1,fp
) == 0) {
1800 return tryObjectSharing(createObject(REDIS_STRING
,val
));
1803 static int rdbLoad(char *filename
) {
1805 robj
*keyobj
= NULL
;
1809 dict
*d
= server
.dict
[0];
1812 fp
= fopen(filename
,"r");
1813 if (!fp
) return REDIS_ERR
;
1814 if (fread(buf
,9,1,fp
) == 0) goto eoferr
;
1816 if (memcmp(buf
,"REDIS",5) != 0) {
1818 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
1821 rdbver
= atoi(buf
+5);
1824 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
1831 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
1832 if (type
== REDIS_EOF
) break;
1833 /* Handle SELECT DB opcode as a special case */
1834 if (type
== REDIS_SELECTDB
) {
1835 if ((dbid
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
)
1837 if (dbid
>= (unsigned)server
.dbnum
) {
1838 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
1841 d
= server
.dict
[dbid
];
1845 if ((keyobj
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
1847 if (type
== REDIS_STRING
) {
1848 /* Read string value */
1849 if ((o
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
1850 } else if (type
== REDIS_LIST
|| type
== REDIS_SET
) {
1851 /* Read list/set value */
1854 if ((listlen
= rdbLoadLen(fp
,rdbver
,NULL
)) == REDIS_RDB_LENERR
)
1856 o
= (type
== REDIS_LIST
) ? createListObject() : createSetObject();
1857 /* Load every single element of the list/set */
1861 if ((ele
= rdbLoadStringObject(fp
,rdbver
)) == NULL
) goto eoferr
;
1862 if (type
== REDIS_LIST
) {
1863 if (!listAddNodeTail((list
*)o
->ptr
,ele
))
1864 oom("listAddNodeTail");
1866 if (dictAdd((dict
*)o
->ptr
,ele
,NULL
) == DICT_ERR
)
1873 /* Add the new object in the hash table */
1874 retval
= dictAdd(d
,keyobj
,o
);
1875 if (retval
== DICT_ERR
) {
1876 redisLog(REDIS_WARNING
,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", keyobj
->ptr
);
1884 eoferr
: /* unexpected end of file is handled here with a fatal exit */
1885 if (keyobj
) decrRefCount(keyobj
);
1886 redisLog(REDIS_WARNING
,"Short read or OOM loading DB. Unrecoverable error, exiting now.");
1888 return REDIS_ERR
; /* Just to avoid warning */
1891 /*================================== Commands =============================== */
1893 static void authCommand(redisClient
*c
) {
1894 if (!strcmp(c
->argv
[1]->ptr
, server
.requirepass
)) {
1895 c
->authenticated
= 1;
1896 addReply(c
,shared
.ok
);
1898 c
->authenticated
= 0;
1899 addReply(c
,shared
.err
);
1903 static void pingCommand(redisClient
*c
) {
1904 addReply(c
,shared
.pong
);
1907 static void echoCommand(redisClient
*c
) {
1908 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",
1909 (int)sdslen(c
->argv
[1]->ptr
)));
1910 addReply(c
,c
->argv
[1]);
1911 addReply(c
,shared
.crlf
);
1914 /*=================================== Strings =============================== */
1916 static void setGenericCommand(redisClient
*c
, int nx
) {
1919 retval
= dictAdd(c
->dict
,c
->argv
[1],c
->argv
[2]);
1920 if (retval
== DICT_ERR
) {
1922 dictReplace(c
->dict
,c
->argv
[1],c
->argv
[2]);
1923 incrRefCount(c
->argv
[2]);
1925 addReply(c
,shared
.czero
);
1929 incrRefCount(c
->argv
[1]);
1930 incrRefCount(c
->argv
[2]);
1933 addReply(c
, nx
? shared
.cone
: shared
.ok
);
1936 static void setCommand(redisClient
*c
) {
1937 setGenericCommand(c
,0);
1940 static void setnxCommand(redisClient
*c
) {
1941 setGenericCommand(c
,1);
1944 static void getCommand(redisClient
*c
) {
1947 de
= dictFind(c
->dict
,c
->argv
[1]);
1949 addReply(c
,shared
.nullbulk
);
1951 robj
*o
= dictGetEntryVal(de
);
1953 if (o
->type
!= REDIS_STRING
) {
1954 addReply(c
,shared
.wrongtypeerr
);
1956 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o
->ptr
)));
1958 addReply(c
,shared
.crlf
);
1963 static void mgetCommand(redisClient
*c
) {
1967 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-1));
1968 for (j
= 1; j
< c
->argc
; j
++) {
1969 de
= dictFind(c
->dict
,c
->argv
[j
]);
1971 addReply(c
,shared
.nullbulk
);
1973 robj
*o
= dictGetEntryVal(de
);
1975 if (o
->type
!= REDIS_STRING
) {
1976 addReply(c
,shared
.nullbulk
);
1978 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o
->ptr
)));
1980 addReply(c
,shared
.crlf
);
1986 static void incrDecrCommand(redisClient
*c
, int incr
) {
1992 de
= dictFind(c
->dict
,c
->argv
[1]);
1996 robj
*o
= dictGetEntryVal(de
);
1998 if (o
->type
!= REDIS_STRING
) {
2003 value
= strtoll(o
->ptr
, &eptr
, 10);
2008 o
= createObject(REDIS_STRING
,sdscatprintf(sdsempty(),"%lld",value
));
2009 retval
= dictAdd(c
->dict
,c
->argv
[1],o
);
2010 if (retval
== DICT_ERR
) {
2011 dictReplace(c
->dict
,c
->argv
[1],o
);
2013 incrRefCount(c
->argv
[1]);
2016 addReply(c
,shared
.colon
);
2018 addReply(c
,shared
.crlf
);
2021 static void incrCommand(redisClient
*c
) {
2022 incrDecrCommand(c
,1);
2025 static void decrCommand(redisClient
*c
) {
2026 incrDecrCommand(c
,-1);
2029 static void incrbyCommand(redisClient
*c
) {
2030 int incr
= atoi(c
->argv
[2]->ptr
);
2031 incrDecrCommand(c
,incr
);
2034 static void decrbyCommand(redisClient
*c
) {
2035 int incr
= atoi(c
->argv
[2]->ptr
);
2036 incrDecrCommand(c
,-incr
);
2039 /* ========================= Type agnostic commands ========================= */
2041 static void delCommand(redisClient
*c
) {
2042 if (dictDelete(c
->dict
,c
->argv
[1]) == DICT_OK
) {
2044 addReply(c
,shared
.cone
);
2046 addReply(c
,shared
.czero
);
2050 static void existsCommand(redisClient
*c
) {
2053 de
= dictFind(c
->dict
,c
->argv
[1]);
2055 addReply(c
,shared
.czero
);
2057 addReply(c
,shared
.cone
);
2060 static void selectCommand(redisClient
*c
) {
2061 int id
= atoi(c
->argv
[1]->ptr
);
2063 if (selectDb(c
,id
) == REDIS_ERR
) {
2064 addReplySds(c
,"-ERR invalid DB index\r\n");
2066 addReply(c
,shared
.ok
);
2070 static void randomkeyCommand(redisClient
*c
) {
2073 de
= dictGetRandomKey(c
->dict
);
2075 addReply(c
,shared
.crlf
);
2077 addReply(c
,shared
.plus
);
2078 addReply(c
,dictGetEntryKey(de
));
2079 addReply(c
,shared
.crlf
);
2083 static void keysCommand(redisClient
*c
) {
2086 sds pattern
= c
->argv
[1]->ptr
;
2087 int plen
= sdslen(pattern
);
2088 int numkeys
= 0, keyslen
= 0;
2089 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
2091 di
= dictGetIterator(c
->dict
);
2092 if (!di
) oom("dictGetIterator");
2094 decrRefCount(lenobj
);
2095 while((de
= dictNext(di
)) != NULL
) {
2096 robj
*keyobj
= dictGetEntryKey(de
);
2097 sds key
= keyobj
->ptr
;
2098 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
2099 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
2101 addReply(c
,shared
.space
);
2104 keyslen
+= sdslen(key
);
2107 dictReleaseIterator(di
);
2108 lenobj
->ptr
= sdscatprintf(sdsempty(),"$%lu\r\n",keyslen
+(numkeys
? (numkeys
-1) : 0));
2109 addReply(c
,shared
.crlf
);
2112 static void dbsizeCommand(redisClient
*c
) {
2114 sdscatprintf(sdsempty(),":%lu\r\n",dictGetHashTableUsed(c
->dict
)));
2117 static void lastsaveCommand(redisClient
*c
) {
2119 sdscatprintf(sdsempty(),":%lu\r\n",server
.lastsave
));
2122 static void typeCommand(redisClient
*c
) {
2126 de
= dictFind(c
->dict
,c
->argv
[1]);
2130 robj
*o
= dictGetEntryVal(de
);
2133 case REDIS_STRING
: type
= "+string"; break;
2134 case REDIS_LIST
: type
= "+list"; break;
2135 case REDIS_SET
: type
= "+set"; break;
2136 default: type
= "unknown"; break;
2139 addReplySds(c
,sdsnew(type
));
2140 addReply(c
,shared
.crlf
);
2143 static void saveCommand(redisClient
*c
) {
2144 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
2145 addReply(c
,shared
.ok
);
2147 addReply(c
,shared
.err
);
2151 static void bgsaveCommand(redisClient
*c
) {
2152 if (server
.bgsaveinprogress
) {
2153 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
2156 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
2157 addReply(c
,shared
.ok
);
2159 addReply(c
,shared
.err
);
2163 static void shutdownCommand(redisClient
*c
) {
2164 redisLog(REDIS_WARNING
,"User requested shutdown, saving DB...");
2165 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
2166 if (server
.daemonize
) {
2167 unlink(server
.pidfile
);
2169 redisLog(REDIS_WARNING
,"Server exit now, bye bye...");
2172 redisLog(REDIS_WARNING
,"Error trying to save the DB, can't exit");
2173 addReplySds(c
,sdsnew("-ERR can't quit, problems saving the DB\r\n"));
2177 static void renameGenericCommand(redisClient
*c
, int nx
) {
2181 /* To use the same key as src and dst is probably an error */
2182 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
2183 addReply(c
,shared
.sameobjecterr
);
2187 de
= dictFind(c
->dict
,c
->argv
[1]);
2189 addReply(c
,shared
.nokeyerr
);
2192 o
= dictGetEntryVal(de
);
2194 if (dictAdd(c
->dict
,c
->argv
[2],o
) == DICT_ERR
) {
2197 addReply(c
,shared
.czero
);
2200 dictReplace(c
->dict
,c
->argv
[2],o
);
2202 incrRefCount(c
->argv
[2]);
2204 dictDelete(c
->dict
,c
->argv
[1]);
2206 addReply(c
,nx
? shared
.cone
: shared
.ok
);
2209 static void renameCommand(redisClient
*c
) {
2210 renameGenericCommand(c
,0);
2213 static void renamenxCommand(redisClient
*c
) {
2214 renameGenericCommand(c
,1);
2217 static void moveCommand(redisClient
*c
) {
2223 /* Obtain source and target DB pointers */
2226 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
2227 addReply(c
,shared
.outofrangeerr
);
2234 /* If the user is moving using as target the same
2235 * DB as the source DB it is probably an error. */
2237 addReply(c
,shared
.sameobjecterr
);
2241 /* Check if the element exists and get a reference */
2242 de
= dictFind(c
->dict
,c
->argv
[1]);
2244 addReply(c
,shared
.czero
);
2248 /* Try to add the element to the target DB */
2249 key
= dictGetEntryKey(de
);
2250 o
= dictGetEntryVal(de
);
2251 if (dictAdd(dst
,key
,o
) == DICT_ERR
) {
2252 addReply(c
,shared
.czero
);
2258 /* OK! key moved, free the entry in the source DB */
2259 dictDelete(src
,c
->argv
[1]);
2261 addReply(c
,shared
.cone
);
2264 /* =================================== Lists ================================ */
2265 static void pushGenericCommand(redisClient
*c
, int where
) {
2270 de
= dictFind(c
->dict
,c
->argv
[1]);
2272 lobj
= createListObject();
2274 if (where
== REDIS_HEAD
) {
2275 if (!listAddNodeHead(list
,c
->argv
[2])) oom("listAddNodeHead");
2277 if (!listAddNodeTail(list
,c
->argv
[2])) oom("listAddNodeTail");
2279 dictAdd(c
->dict
,c
->argv
[1],lobj
);
2280 incrRefCount(c
->argv
[1]);
2281 incrRefCount(c
->argv
[2]);
2283 lobj
= dictGetEntryVal(de
);
2284 if (lobj
->type
!= REDIS_LIST
) {
2285 addReply(c
,shared
.wrongtypeerr
);
2289 if (where
== REDIS_HEAD
) {
2290 if (!listAddNodeHead(list
,c
->argv
[2])) oom("listAddNodeHead");
2292 if (!listAddNodeTail(list
,c
->argv
[2])) oom("listAddNodeTail");
2294 incrRefCount(c
->argv
[2]);
2297 addReply(c
,shared
.ok
);
2300 static void lpushCommand(redisClient
*c
) {
2301 pushGenericCommand(c
,REDIS_HEAD
);
2304 static void rpushCommand(redisClient
*c
) {
2305 pushGenericCommand(c
,REDIS_TAIL
);
2308 static void llenCommand(redisClient
*c
) {
2312 de
= dictFind(c
->dict
,c
->argv
[1]);
2314 addReply(c
,shared
.czero
);
2317 robj
*o
= dictGetEntryVal(de
);
2318 if (o
->type
!= REDIS_LIST
) {
2319 addReply(c
,shared
.wrongtypeerr
);
2322 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",listLength(l
)));
2327 static void lindexCommand(redisClient
*c
) {
2329 int index
= atoi(c
->argv
[2]->ptr
);
2331 de
= dictFind(c
->dict
,c
->argv
[1]);
2333 addReply(c
,shared
.nullbulk
);
2335 robj
*o
= dictGetEntryVal(de
);
2337 if (o
->type
!= REDIS_LIST
) {
2338 addReply(c
,shared
.wrongtypeerr
);
2340 list
*list
= o
->ptr
;
2343 ln
= listIndex(list
, index
);
2345 addReply(c
,shared
.nullbulk
);
2347 robj
*ele
= listNodeValue(ln
);
2348 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele
->ptr
)));
2350 addReply(c
,shared
.crlf
);
2356 static void lsetCommand(redisClient
*c
) {
2358 int index
= atoi(c
->argv
[2]->ptr
);
2360 de
= dictFind(c
->dict
,c
->argv
[1]);
2362 addReply(c
,shared
.nokeyerr
);
2364 robj
*o
= dictGetEntryVal(de
);
2366 if (o
->type
!= REDIS_LIST
) {
2367 addReply(c
,shared
.wrongtypeerr
);
2369 list
*list
= o
->ptr
;
2372 ln
= listIndex(list
, index
);
2374 addReply(c
,shared
.outofrangeerr
);
2376 robj
*ele
= listNodeValue(ln
);
2379 listNodeValue(ln
) = c
->argv
[3];
2380 incrRefCount(c
->argv
[3]);
2381 addReply(c
,shared
.ok
);
2388 static void popGenericCommand(redisClient
*c
, int where
) {
2391 de
= dictFind(c
->dict
,c
->argv
[1]);
2393 addReply(c
,shared
.nullbulk
);
2395 robj
*o
= dictGetEntryVal(de
);
2397 if (o
->type
!= REDIS_LIST
) {
2398 addReply(c
,shared
.wrongtypeerr
);
2400 list
*list
= o
->ptr
;
2403 if (where
== REDIS_HEAD
)
2404 ln
= listFirst(list
);
2406 ln
= listLast(list
);
2409 addReply(c
,shared
.nullbulk
);
2411 robj
*ele
= listNodeValue(ln
);
2412 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele
->ptr
)));
2414 addReply(c
,shared
.crlf
);
2415 listDelNode(list
,ln
);
2422 static void lpopCommand(redisClient
*c
) {
2423 popGenericCommand(c
,REDIS_HEAD
);
2426 static void rpopCommand(redisClient
*c
) {
2427 popGenericCommand(c
,REDIS_TAIL
);
2430 static void lrangeCommand(redisClient
*c
) {
2432 int start
= atoi(c
->argv
[2]->ptr
);
2433 int end
= atoi(c
->argv
[3]->ptr
);
2435 de
= dictFind(c
->dict
,c
->argv
[1]);
2437 addReply(c
,shared
.nullmultibulk
);
2439 robj
*o
= dictGetEntryVal(de
);
2441 if (o
->type
!= REDIS_LIST
) {
2442 addReply(c
,shared
.wrongtypeerr
);
2444 list
*list
= o
->ptr
;
2446 int llen
= listLength(list
);
2450 /* convert negative indexes */
2451 if (start
< 0) start
= llen
+start
;
2452 if (end
< 0) end
= llen
+end
;
2453 if (start
< 0) start
= 0;
2454 if (end
< 0) end
= 0;
2456 /* indexes sanity checks */
2457 if (start
> end
|| start
>= llen
) {
2458 /* Out of range start or start > end result in empty list */
2459 addReply(c
,shared
.emptymultibulk
);
2462 if (end
>= llen
) end
= llen
-1;
2463 rangelen
= (end
-start
)+1;
2465 /* Return the result in form of a multi-bulk reply */
2466 ln
= listIndex(list
, start
);
2467 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",rangelen
));
2468 for (j
= 0; j
< rangelen
; j
++) {
2469 ele
= listNodeValue(ln
);
2470 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele
->ptr
)));
2472 addReply(c
,shared
.crlf
);
2479 static void ltrimCommand(redisClient
*c
) {
2481 int start
= atoi(c
->argv
[2]->ptr
);
2482 int end
= atoi(c
->argv
[3]->ptr
);
2484 de
= dictFind(c
->dict
,c
->argv
[1]);
2486 addReply(c
,shared
.nokeyerr
);
2488 robj
*o
= dictGetEntryVal(de
);
2490 if (o
->type
!= REDIS_LIST
) {
2491 addReply(c
,shared
.wrongtypeerr
);
2493 list
*list
= o
->ptr
;
2495 int llen
= listLength(list
);
2496 int j
, ltrim
, rtrim
;
2498 /* convert negative indexes */
2499 if (start
< 0) start
= llen
+start
;
2500 if (end
< 0) end
= llen
+end
;
2501 if (start
< 0) start
= 0;
2502 if (end
< 0) end
= 0;
2504 /* indexes sanity checks */
2505 if (start
> end
|| start
>= llen
) {
2506 /* Out of range start or start > end result in empty list */
2510 if (end
>= llen
) end
= llen
-1;
2515 /* Remove list elements to perform the trim */
2516 for (j
= 0; j
< ltrim
; j
++) {
2517 ln
= listFirst(list
);
2518 listDelNode(list
,ln
);
2520 for (j
= 0; j
< rtrim
; j
++) {
2521 ln
= listLast(list
);
2522 listDelNode(list
,ln
);
2524 addReply(c
,shared
.ok
);
2530 static void lremCommand(redisClient
*c
) {
2533 de
= dictFind(c
->dict
,c
->argv
[1]);
2535 addReply(c
,shared
.nokeyerr
);
2537 robj
*o
= dictGetEntryVal(de
);
2539 if (o
->type
!= REDIS_LIST
) {
2540 addReply(c
,shared
.wrongtypeerr
);
2542 list
*list
= o
->ptr
;
2543 listNode
*ln
, *next
;
2544 int toremove
= atoi(c
->argv
[2]->ptr
);
2549 toremove
= -toremove
;
2552 ln
= fromtail
? list
->tail
: list
->head
;
2554 robj
*ele
= listNodeValue(ln
);
2556 next
= fromtail
? ln
->prev
: ln
->next
;
2557 if (sdscmp(ele
->ptr
,c
->argv
[3]->ptr
) == 0) {
2558 listDelNode(list
,ln
);
2561 if (toremove
&& removed
== toremove
) break;
2565 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",removed
));
2570 /* ==================================== Sets ================================ */
2572 static void saddCommand(redisClient
*c
) {
2576 de
= dictFind(c
->dict
,c
->argv
[1]);
2578 set
= createSetObject();
2579 dictAdd(c
->dict
,c
->argv
[1],set
);
2580 incrRefCount(c
->argv
[1]);
2582 set
= dictGetEntryVal(de
);
2583 if (set
->type
!= REDIS_SET
) {
2584 addReply(c
,shared
.wrongtypeerr
);
2588 if (dictAdd(set
->ptr
,c
->argv
[2],NULL
) == DICT_OK
) {
2589 incrRefCount(c
->argv
[2]);
2591 addReply(c
,shared
.cone
);
2593 addReply(c
,shared
.czero
);
2597 static void sremCommand(redisClient
*c
) {
2600 de
= dictFind(c
->dict
,c
->argv
[1]);
2602 addReply(c
,shared
.czero
);
2606 set
= dictGetEntryVal(de
);
2607 if (set
->type
!= REDIS_SET
) {
2608 addReply(c
,shared
.wrongtypeerr
);
2611 if (dictDelete(set
->ptr
,c
->argv
[2]) == DICT_OK
) {
2613 addReply(c
,shared
.cone
);
2615 addReply(c
,shared
.czero
);
2620 static void sismemberCommand(redisClient
*c
) {
2623 de
= dictFind(c
->dict
,c
->argv
[1]);
2625 addReply(c
,shared
.czero
);
2629 set
= dictGetEntryVal(de
);
2630 if (set
->type
!= REDIS_SET
) {
2631 addReply(c
,shared
.wrongtypeerr
);
2634 if (dictFind(set
->ptr
,c
->argv
[2]))
2635 addReply(c
,shared
.cone
);
2637 addReply(c
,shared
.czero
);
2641 static void scardCommand(redisClient
*c
) {
2645 de
= dictFind(c
->dict
,c
->argv
[1]);
2647 addReply(c
,shared
.czero
);
2650 robj
*o
= dictGetEntryVal(de
);
2651 if (o
->type
!= REDIS_SET
) {
2652 addReply(c
,shared
.wrongtypeerr
);
2655 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",
2656 dictGetHashTableUsed(s
)));
2661 static int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
2662 dict
**d1
= (void*) s1
, **d2
= (void*) s2
;
2664 return dictGetHashTableUsed(*d1
)-dictGetHashTableUsed(*d2
);
2667 static void sinterGenericCommand(redisClient
*c
, robj
**setskeys
, int setsnum
, robj
*dstkey
) {
2668 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
2671 robj
*lenobj
= NULL
, *dstset
= NULL
;
2672 int j
, cardinality
= 0;
2674 if (!dv
) oom("sinterCommand");
2675 for (j
= 0; j
< setsnum
; j
++) {
2679 de
= dictFind(c
->dict
,setskeys
[j
]);
2682 addReply(c
,shared
.nokeyerr
);
2685 setobj
= dictGetEntryVal(de
);
2686 if (setobj
->type
!= REDIS_SET
) {
2688 addReply(c
,shared
.wrongtypeerr
);
2691 dv
[j
] = setobj
->ptr
;
2693 /* Sort sets from the smallest to largest, this will improve our
2694 * algorithm's performace */
2695 qsort(dv
,setsnum
,sizeof(dict
*),qsortCompareSetsByCardinality
);
2697 /* The first thing we should output is the total number of elements...
2698 * since this is a multi-bulk write, but at this stage we don't know
2699 * the intersection set size, so we use a trick, append an empty object
2700 * to the output list and save the pointer to later modify it with the
2703 lenobj
= createObject(REDIS_STRING
,NULL
);
2705 decrRefCount(lenobj
);
2707 /* If we have a target key where to store the resulting set
2708 * create this key with an empty set inside */
2709 dstset
= createSetObject();
2710 dictDelete(c
->dict
,dstkey
);
2711 dictAdd(c
->dict
,dstkey
,dstset
);
2712 incrRefCount(dstkey
);
2715 /* Iterate all the elements of the first (smallest) set, and test
2716 * the element against all the other sets, if at least one set does
2717 * not include the element it is discarded */
2718 di
= dictGetIterator(dv
[0]);
2719 if (!di
) oom("dictGetIterator");
2721 while((de
= dictNext(di
)) != NULL
) {
2724 for (j
= 1; j
< setsnum
; j
++)
2725 if (dictFind(dv
[j
],dictGetEntryKey(de
)) == NULL
) break;
2727 continue; /* at least one set does not contain the member */
2728 ele
= dictGetEntryKey(de
);
2730 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(ele
->ptr
)));
2732 addReply(c
,shared
.crlf
);
2735 dictAdd(dstset
->ptr
,ele
,NULL
);
2739 dictReleaseIterator(di
);
2742 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%d\r\n",cardinality
);
2744 addReply(c
,shared
.ok
);
2748 static void sinterCommand(redisClient
*c
) {
2749 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
2752 static void sinterstoreCommand(redisClient
*c
) {
2753 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
2756 static void flushdbCommand(redisClient
*c
) {
2758 addReply(c
,shared
.ok
);
2759 rdbSave(server
.dbfilename
);
2762 static void flushallCommand(redisClient
*c
) {
2764 addReply(c
,shared
.ok
);
2765 rdbSave(server
.dbfilename
);
2768 redisSortOperation
*createSortOperation(int type
, robj
*pattern
) {
2769 redisSortOperation
*so
= zmalloc(sizeof(*so
));
2770 if (!so
) oom("createSortOperation");
2772 so
->pattern
= pattern
;
2776 /* Return the value associated to the key with a name obtained
2777 * substituting the first occurence of '*' in 'pattern' with 'subst' */
2778 robj
*lookupKeyByPattern(dict
*dict
, robj
*pattern
, robj
*subst
) {
2782 int prefixlen
, sublen
, postfixlen
;
2784 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
2788 char buf
[REDIS_SORTKEY_MAX
+1];
2792 spat
= pattern
->ptr
;
2794 if (sdslen(spat
)+sdslen(ssub
)-1 > REDIS_SORTKEY_MAX
) return NULL
;
2795 p
= strchr(spat
,'*');
2796 if (!p
) return NULL
;
2799 sublen
= sdslen(ssub
);
2800 postfixlen
= sdslen(spat
)-(prefixlen
+1);
2801 memcpy(keyname
.buf
,spat
,prefixlen
);
2802 memcpy(keyname
.buf
+prefixlen
,ssub
,sublen
);
2803 memcpy(keyname
.buf
+prefixlen
+sublen
,p
+1,postfixlen
);
2804 keyname
.buf
[prefixlen
+sublen
+postfixlen
] = '\0';
2805 keyname
.len
= prefixlen
+sublen
+postfixlen
;
2807 keyobj
.refcount
= 1;
2808 keyobj
.type
= REDIS_STRING
;
2809 keyobj
.ptr
= ((char*)&keyname
)+(sizeof(long)*2);
2811 de
= dictFind(dict
,&keyobj
);
2812 /* printf("lookup '%s' => %p\n", keyname.buf,de); */
2813 if (!de
) return NULL
;
2814 return dictGetEntryVal(de
);
2817 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
2818 * the additional parameter is not standard but a BSD-specific we have to
2819 * pass sorting parameters via the global 'server' structure */
2820 static int sortCompare(const void *s1
, const void *s2
) {
2821 const redisSortObject
*so1
= s1
, *so2
= s2
;
2824 if (!server
.sort_alpha
) {
2825 /* Numeric sorting. Here it's trivial as we precomputed scores */
2826 if (so1
->u
.score
> so2
->u
.score
) {
2828 } else if (so1
->u
.score
< so2
->u
.score
) {
2834 /* Alphanumeric sorting */
2835 if (server
.sort_bypattern
) {
2836 if (!so1
->u
.cmpobj
|| !so2
->u
.cmpobj
) {
2837 /* At least one compare object is NULL */
2838 if (so1
->u
.cmpobj
== so2
->u
.cmpobj
)
2840 else if (so1
->u
.cmpobj
== NULL
)
2845 /* We have both the objects, use strcoll */
2846 cmp
= strcoll(so1
->u
.cmpobj
->ptr
,so2
->u
.cmpobj
->ptr
);
2849 /* Compare elements directly */
2850 cmp
= strcoll(so1
->obj
->ptr
,so2
->obj
->ptr
);
2853 return server
.sort_desc
? -cmp
: cmp
;
2856 /* The SORT command is the most complex command in Redis. Warning: this code
2857 * is optimized for speed and a bit less for readability */
2858 static void sortCommand(redisClient
*c
) {
2862 int desc
= 0, alpha
= 0;
2863 int limit_start
= 0, limit_count
= -1, start
, end
;
2864 int j
, dontsort
= 0, vectorlen
;
2865 int getop
= 0; /* GET operation counter */
2866 robj
*sortval
, *sortby
= NULL
;
2867 redisSortObject
*vector
; /* Resulting vector to sort */
2869 /* Lookup the key to sort. It must be of the right types */
2870 de
= dictFind(c
->dict
,c
->argv
[1]);
2872 addReply(c
,shared
.nokeyerr
);
2875 sortval
= dictGetEntryVal(de
);
2876 if (sortval
->type
!= REDIS_SET
&& sortval
->type
!= REDIS_LIST
) {
2877 addReply(c
,shared
.wrongtypeerr
);
2881 /* Create a list of operations to perform for every sorted element.
2882 * Operations can be GET/DEL/INCR/DECR */
2883 operations
= listCreate();
2884 listSetFreeMethod(operations
,zfree
);
2887 /* Now we need to protect sortval incrementing its count, in the future
2888 * SORT may have options able to overwrite/delete keys during the sorting
2889 * and the sorted key itself may get destroied */
2890 incrRefCount(sortval
);
2892 /* The SORT command has an SQL-alike syntax, parse it */
2893 while(j
< c
->argc
) {
2894 int leftargs
= c
->argc
-j
-1;
2895 if (!strcasecmp(c
->argv
[j
]->ptr
,"asc")) {
2897 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"desc")) {
2899 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"alpha")) {
2901 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"limit") && leftargs
>= 2) {
2902 limit_start
= atoi(c
->argv
[j
+1]->ptr
);
2903 limit_count
= atoi(c
->argv
[j
+2]->ptr
);
2905 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"by") && leftargs
>= 1) {
2906 sortby
= c
->argv
[j
+1];
2907 /* If the BY pattern does not contain '*', i.e. it is constant,
2908 * we don't need to sort nor to lookup the weight keys. */
2909 if (strchr(c
->argv
[j
+1]->ptr
,'*') == NULL
) dontsort
= 1;
2911 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
2912 listAddNodeTail(operations
,createSortOperation(
2913 REDIS_SORT_GET
,c
->argv
[j
+1]));
2916 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"del") && leftargs
>= 1) {
2917 listAddNodeTail(operations
,createSortOperation(
2918 REDIS_SORT_DEL
,c
->argv
[j
+1]));
2920 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"incr") && leftargs
>= 1) {
2921 listAddNodeTail(operations
,createSortOperation(
2922 REDIS_SORT_INCR
,c
->argv
[j
+1]));
2924 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
2925 listAddNodeTail(operations
,createSortOperation(
2926 REDIS_SORT_DECR
,c
->argv
[j
+1]));
2929 decrRefCount(sortval
);
2930 listRelease(operations
);
2931 addReply(c
,shared
.syntaxerr
);
2937 /* Load the sorting vector with all the objects to sort */
2938 vectorlen
= (sortval
->type
== REDIS_LIST
) ?
2939 listLength((list
*)sortval
->ptr
) :
2940 dictGetHashTableUsed((dict
*)sortval
->ptr
);
2941 vector
= zmalloc(sizeof(redisSortObject
)*vectorlen
);
2942 if (!vector
) oom("allocating objects vector for SORT");
2944 if (sortval
->type
== REDIS_LIST
) {
2945 list
*list
= sortval
->ptr
;
2946 listNode
*ln
= list
->head
;
2948 robj
*ele
= ln
->value
;
2949 vector
[j
].obj
= ele
;
2950 vector
[j
].u
.score
= 0;
2951 vector
[j
].u
.cmpobj
= NULL
;
2956 dict
*set
= sortval
->ptr
;
2960 di
= dictGetIterator(set
);
2961 if (!di
) oom("dictGetIterator");
2962 while((setele
= dictNext(di
)) != NULL
) {
2963 vector
[j
].obj
= dictGetEntryKey(setele
);
2964 vector
[j
].u
.score
= 0;
2965 vector
[j
].u
.cmpobj
= NULL
;
2968 dictReleaseIterator(di
);
2970 assert(j
== vectorlen
);
2972 /* Now it's time to load the right scores in the sorting vector */
2973 if (dontsort
== 0) {
2974 for (j
= 0; j
< vectorlen
; j
++) {
2978 byval
= lookupKeyByPattern(c
->dict
,sortby
,vector
[j
].obj
);
2979 if (!byval
|| byval
->type
!= REDIS_STRING
) continue;
2981 vector
[j
].u
.cmpobj
= byval
;
2982 incrRefCount(byval
);
2984 vector
[j
].u
.score
= strtod(byval
->ptr
,NULL
);
2987 if (!alpha
) vector
[j
].u
.score
= strtod(vector
[j
].obj
->ptr
,NULL
);
2992 /* We are ready to sort the vector... perform a bit of sanity check
2993 * on the LIMIT option too. We'll use a partial version of quicksort. */
2994 start
= (limit_start
< 0) ? 0 : limit_start
;
2995 end
= (limit_count
< 0) ? vectorlen
-1 : start
+limit_count
-1;
2996 if (start
>= vectorlen
) {
2997 start
= vectorlen
-1;
3000 if (end
>= vectorlen
) end
= vectorlen
-1;
3002 if (dontsort
== 0) {
3003 server
.sort_desc
= desc
;
3004 server
.sort_alpha
= alpha
;
3005 server
.sort_bypattern
= sortby
? 1 : 0;
3006 qsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
);
3009 /* Send command output to the output buffer, performing the specified
3010 * GET/DEL/INCR/DECR operations if any. */
3011 outputlen
= getop
? getop
*(end
-start
+1) : end
-start
+1;
3012 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",outputlen
));
3013 for (j
= start
; j
<= end
; j
++) {
3014 listNode
*ln
= operations
->head
;
3016 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",
3017 sdslen(vector
[j
].obj
->ptr
)));
3018 addReply(c
,vector
[j
].obj
);
3019 addReply(c
,shared
.crlf
);
3022 redisSortOperation
*sop
= ln
->value
;
3023 robj
*val
= lookupKeyByPattern(c
->dict
,sop
->pattern
,
3026 if (sop
->type
== REDIS_SORT_GET
) {
3027 if (!val
|| val
->type
!= REDIS_STRING
) {
3028 addReply(c
,shared
.nullbulk
);
3030 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",
3033 addReply(c
,shared
.crlf
);
3035 } else if (sop
->type
== REDIS_SORT_DEL
) {
3043 decrRefCount(sortval
);
3044 listRelease(operations
);
3045 for (j
= 0; j
< vectorlen
; j
++) {
3046 if (sortby
&& alpha
&& vector
[j
].u
.cmpobj
)
3047 decrRefCount(vector
[j
].u
.cmpobj
);
3052 static void infoCommand(redisClient
*c
) {
3054 time_t uptime
= time(NULL
)-server
.stat_starttime
;
3056 info
= sdscatprintf(sdsempty(),
3057 "redis_version:%s\r\n"
3058 "connected_clients:%d\r\n"
3059 "connected_slaves:%d\r\n"
3060 "used_memory:%d\r\n"
3061 "changes_since_last_save:%lld\r\n"
3062 "last_save_time:%d\r\n"
3063 "total_connections_received:%lld\r\n"
3064 "total_commands_processed:%lld\r\n"
3065 "uptime_in_seconds:%d\r\n"
3066 "uptime_in_days:%d\r\n"
3068 listLength(server
.clients
)-listLength(server
.slaves
),
3069 listLength(server
.slaves
),
3073 server
.stat_numconnections
,
3074 server
.stat_numcommands
,
3078 addReplySds(c
,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info
)));
3079 addReplySds(c
,info
);
3080 addReply(c
,shared
.crlf
);
3083 /* =============================== Replication ============================= */
3085 /* Send the whole output buffer syncronously to the slave. This a general operation in theory, but it is actually useful only for replication. */
3086 static int flushClientOutput(redisClient
*c
) {
3088 time_t start
= time(NULL
);
3090 while(listLength(c
->reply
)) {
3091 if (time(NULL
)-start
> 5) return REDIS_ERR
; /* 5 seconds timeout */
3092 retval
= aeWait(c
->fd
,AE_WRITABLE
,1000);
3095 } else if (retval
& AE_WRITABLE
) {
3096 sendReplyToClient(NULL
, c
->fd
, c
, AE_WRITABLE
);
3102 static int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
) {
3103 ssize_t nwritten
, ret
= size
;
3104 time_t start
= time(NULL
);
3108 if (aeWait(fd
,AE_WRITABLE
,1000) & AE_WRITABLE
) {
3109 nwritten
= write(fd
,ptr
,size
);
3110 if (nwritten
== -1) return -1;
3114 if ((time(NULL
)-start
) > timeout
) {
3122 static int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
) {
3123 ssize_t nread
, totread
= 0;
3124 time_t start
= time(NULL
);
3128 if (aeWait(fd
,AE_READABLE
,1000) & AE_READABLE
) {
3129 nread
= read(fd
,ptr
,size
);
3130 if (nread
== -1) return -1;
3135 if ((time(NULL
)-start
) > timeout
) {
3143 static int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
) {
3150 if (syncRead(fd
,&c
,1,timeout
) == -1) return -1;
3153 if (nread
&& *(ptr
-1) == '\r') *(ptr
-1) = '\0';
3164 static void syncCommand(redisClient
*c
) {
3167 time_t start
= time(NULL
);
3170 /* ignore SYNC if aleady slave or in monitor mode */
3171 if (c
->flags
& REDIS_SLAVE
) return;
3173 redisLog(REDIS_NOTICE
,"Slave ask for syncronization");
3174 if (flushClientOutput(c
) == REDIS_ERR
||
3175 rdbSave(server
.dbfilename
) != REDIS_OK
)
3178 fd
= open(server
.dbfilename
, O_RDONLY
);
3179 if (fd
== -1 || fstat(fd
,&sb
) == -1) goto closeconn
;
3182 snprintf(sizebuf
,32,"$%d\r\n",len
);
3183 if (syncWrite(c
->fd
,sizebuf
,strlen(sizebuf
),5) == -1) goto closeconn
;
3188 if (time(NULL
)-start
> REDIS_MAX_SYNC_TIME
) goto closeconn
;
3189 nread
= read(fd
,buf
,1024);
3190 if (nread
== -1) goto closeconn
;
3192 if (syncWrite(c
->fd
,buf
,nread
,5) == -1) goto closeconn
;
3194 if (syncWrite(c
->fd
,"\r\n",2,5) == -1) goto closeconn
;
3196 c
->flags
|= REDIS_SLAVE
;
3198 if (!listAddNodeTail(server
.slaves
,c
)) oom("listAddNodeTail");
3199 redisLog(REDIS_NOTICE
,"Syncronization with slave succeeded");
3203 if (fd
!= -1) close(fd
);
3204 c
->flags
|= REDIS_CLOSE
;
3205 redisLog(REDIS_WARNING
,"Syncronization with slave failed");
3209 static int syncWithMaster(void) {
3210 char buf
[1024], tmpfile
[256];
3212 int fd
= anetTcpConnect(NULL
,server
.masterhost
,server
.masterport
);
3216 redisLog(REDIS_WARNING
,"Unable to connect to MASTER: %s",
3220 /* Issue the SYNC command */
3221 if (syncWrite(fd
,"SYNC \r\n",7,5) == -1) {
3223 redisLog(REDIS_WARNING
,"I/O error writing to MASTER: %s",
3227 /* Read the bulk write count */
3228 if (syncReadLine(fd
,buf
,1024,5) == -1) {
3230 redisLog(REDIS_WARNING
,"I/O error reading bulk count from MASTER: %s",
3234 dumpsize
= atoi(buf
+1);
3235 redisLog(REDIS_NOTICE
,"Receiving %d bytes data dump from MASTER",dumpsize
);
3236 /* Read the bulk write data on a temp file */
3237 snprintf(tmpfile
,256,"temp-%d.%ld.rdb",(int)time(NULL
),(long int)random());
3238 dfd
= open(tmpfile
,O_CREAT
|O_WRONLY
,0644);
3241 redisLog(REDIS_WARNING
,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno
));
3245 int nread
, nwritten
;
3247 nread
= read(fd
,buf
,(dumpsize
< 1024)?dumpsize
:1024);
3249 redisLog(REDIS_WARNING
,"I/O error trying to sync with MASTER: %s",
3255 nwritten
= write(dfd
,buf
,nread
);
3256 if (nwritten
== -1) {
3257 redisLog(REDIS_WARNING
,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno
));
3265 if (rename(tmpfile
,server
.dbfilename
) == -1) {
3266 redisLog(REDIS_WARNING
,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno
));
3272 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
3273 redisLog(REDIS_WARNING
,"Failed trying to load the MASTER synchronization DB from disk");
3277 server
.master
= createClient(fd
);
3278 server
.master
->flags
|= REDIS_MASTER
;
3279 server
.replstate
= REDIS_REPL_CONNECTED
;
3283 static void monitorCommand(redisClient
*c
) {
3284 /* ignore MONITOR if aleady slave or in monitor mode */
3285 if (c
->flags
& REDIS_SLAVE
) return;
3287 c
->flags
|= (REDIS_SLAVE
|REDIS_MONITOR
);
3289 if (!listAddNodeTail(server
.monitors
,c
)) oom("listAddNodeTail");
3290 addReply(c
,shared
.ok
);
3293 /* =================================== Main! ================================ */
3295 static void daemonize(void) {
3299 if (fork() != 0) exit(0); /* parent exits */
3300 setsid(); /* create a new session */
3302 /* Every output goes to /dev/null. If Redis is daemonized but
3303 * the 'logfile' is set to 'stdout' in the configuration file
3304 * it will not log at all. */
3305 if ((fd
= open("/dev/null", O_RDWR
, 0)) != -1) {
3306 dup2(fd
, STDIN_FILENO
);
3307 dup2(fd
, STDOUT_FILENO
);
3308 dup2(fd
, STDERR_FILENO
);
3309 if (fd
> STDERR_FILENO
) close(fd
);
3311 /* Try to write the pid file */
3312 fp
= fopen(server
.pidfile
,"w");
3314 fprintf(fp
,"%d\n",getpid());
3319 int main(int argc
, char **argv
) {
3322 ResetServerSaveParams();
3323 loadServerConfig(argv
[1]);
3324 } else if (argc
> 2) {
3325 fprintf(stderr
,"Usage: ./redis-server [/path/to/redis.conf]\n");
3329 if (server
.daemonize
) daemonize();
3330 redisLog(REDIS_NOTICE
,"Server started, Redis version " REDIS_VERSION
);
3331 if (rdbLoad(server
.dbfilename
) == REDIS_OK
)
3332 redisLog(REDIS_NOTICE
,"DB loaded from disk");
3333 if (aeCreateFileEvent(server
.el
, server
.fd
, AE_READABLE
,
3334 acceptHandler
, NULL
, NULL
) == AE_ERR
) oom("creating file event");
3335 redisLog(REDIS_NOTICE
,"The server is now ready to accept connections on port %d", server
.port
);
3337 aeDeleteEventLoop(server
.el
);