]> git.saurik.com Git - redis.git/blame - redis.c
rubyredis more compatible with Redis-rb
[redis.git] / redis.c
CommitLineData
ed9b544e 1/*
2 * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
16 *
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.
28 */
29
dec423d9 30#define REDIS_VERSION "0.100"
23d4709d 31
32#include "fmacros.h"
ed9b544e 33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <time.h>
38#include <unistd.h>
39#include <signal.h>
40#include <sys/wait.h>
41#include <errno.h>
42#include <assert.h>
43#include <ctype.h>
44#include <stdarg.h>
45#include <inttypes.h>
46#include <arpa/inet.h>
47#include <sys/stat.h>
48#include <fcntl.h>
49#include <sys/time.h>
50#include <sys/resource.h>
f78fd11b 51#include <limits.h>
ed9b544e 52
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 */
5f5b9840 59#include "lzf.h" /* LZF compression library */
60#include "pqsort.h" /* Partial qsort for SORT+LIMIT */
ed9b544e 61
62/* Error codes */
63#define REDIS_OK 0
64#define REDIS_ERR -1
65
66/* Static server configuration */
67#define REDIS_SERVERPORT 6379 /* TCP port */
68#define REDIS_MAXIDLETIME (60*5) /* default client timeout */
6208b3a7 69#define REDIS_IOBUF_LEN 1024
ed9b544e 70#define REDIS_LOADBUF_LEN 1024
93ea3759 71#define REDIS_STATIC_ARGS 4
ed9b544e 72#define REDIS_DEFAULT_DBNUM 16
73#define REDIS_CONFIGLINE_MAX 1024
74#define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
75#define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
94754ccc 76#define REDIS_EXPIRELOOKUPS_PER_CRON 100 /* try to expire 100 keys/second */
ed9b544e 77
78/* Hash table parameters */
79#define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
80#define REDIS_HT_MINSLOTS 16384 /* Never resize the HT under this */
81
82/* Command flags */
83#define REDIS_CMD_BULK 1
84#define REDIS_CMD_INLINE 2
85
86/* Object types */
87#define REDIS_STRING 0
88#define REDIS_LIST 1
89#define REDIS_SET 2
90#define REDIS_HASH 3
f78fd11b 91
92/* Object types only used for dumping to disk */
bb32ede5 93#define REDIS_EXPIRETIME 253
ed9b544e 94#define REDIS_SELECTDB 254
95#define REDIS_EOF 255
96
f78fd11b 97/* Defines related to the dump file format. To store 32 bits lengths for short
98 * keys requires a lot of space, so we check the most significant 2 bits of
99 * the first byte to interpreter the length:
100 *
101 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
102 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
103 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
a4d1ba9a 104 * 11|000000 this means: specially encoded object will follow. The six bits
105 * number specify the kind of object that follows.
106 * See the REDIS_RDB_ENC_* defines.
f78fd11b 107 *
10c43610 108 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
109 * values, will fit inside. */
f78fd11b 110#define REDIS_RDB_6BITLEN 0
111#define REDIS_RDB_14BITLEN 1
112#define REDIS_RDB_32BITLEN 2
17be1a4a 113#define REDIS_RDB_ENCVAL 3
f78fd11b 114#define REDIS_RDB_LENERR UINT_MAX
115
a4d1ba9a 116/* When a length of a string object stored on disk has the first two bits
117 * set, the remaining two bits specify a special encoding for the object
118 * accordingly to the following defines: */
119#define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
120#define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
121#define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
774e3047 122#define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
a4d1ba9a 123
ed9b544e 124/* Client flags */
125#define REDIS_CLOSE 1 /* This client connection should be closed ASAP */
126#define REDIS_SLAVE 2 /* This client is a slave server */
127#define REDIS_MASTER 4 /* This client is a master server */
87eca727 128#define REDIS_MONITOR 8 /* This client is a slave monitor, see MONITOR */
ed9b544e 129
40d224a9 130/* Slave replication state - slave side */
ed9b544e 131#define REDIS_REPL_NONE 0 /* No active replication */
132#define REDIS_REPL_CONNECT 1 /* Must connect to master */
133#define REDIS_REPL_CONNECTED 2 /* Connected to master */
134
40d224a9 135/* Slave replication state - from the point of view of master
136 * Note that in SEND_BULK and ONLINE state the slave receives new updates
137 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
138 * to start the next background saving in order to send updates to it. */
139#define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
140#define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
141#define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
142#define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
143
ed9b544e 144/* List related stuff */
145#define REDIS_HEAD 0
146#define REDIS_TAIL 1
147
148/* Sort operations */
149#define REDIS_SORT_GET 0
150#define REDIS_SORT_DEL 1
151#define REDIS_SORT_INCR 2
152#define REDIS_SORT_DECR 3
153#define REDIS_SORT_ASC 4
154#define REDIS_SORT_DESC 5
155#define REDIS_SORTKEY_MAX 1024
156
157/* Log levels */
158#define REDIS_DEBUG 0
159#define REDIS_NOTICE 1
160#define REDIS_WARNING 2
161
162/* Anti-warning macro... */
163#define REDIS_NOTUSED(V) ((void) V)
164
165/*================================= Data types ============================== */
166
167/* A redis object, that is a type able to hold a string / list / set */
168typedef struct redisObject {
ed9b544e 169 void *ptr;
5a6e8b1d 170 int type;
ed9b544e 171 int refcount;
172} robj;
173
3305306f 174typedef struct redisDb {
175 dict *dict;
176 dict *expires;
177 int id;
178} redisDb;
179
ed9b544e 180/* With multiplexing we need to take per-clinet state.
181 * Clients are taken in a liked list. */
182typedef struct redisClient {
183 int fd;
3305306f 184 redisDb *db;
ed9b544e 185 int dictid;
186 sds querybuf;
93ea3759 187 robj **argv;
ed9b544e 188 int argc;
40d224a9 189 int bulklen; /* bulk read len. -1 if not in bulk read mode */
ed9b544e 190 list *reply;
191 int sentlen;
192 time_t lastinteraction; /* time of the last interaction, used for timeout */
40d224a9 193 int flags; /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
194 int slaveseldb; /* slave selected db, if this client is a slave */
195 int authenticated; /* when requirepass is non-NULL */
196 int replstate; /* replication state if this is a slave */
197 int repldbfd; /* replication DB file descriptor */
6208b3a7 198 long repldboff; /* replication DB file offset */
40d224a9 199 off_t repldbsize; /* replication DB file size */
ed9b544e 200} redisClient;
201
202struct saveparam {
203 time_t seconds;
204 int changes;
205};
206
207/* Global server state structure */
208struct redisServer {
209 int port;
210 int fd;
3305306f 211 redisDb *db;
10c43610 212 dict *sharingpool;
213 unsigned int sharingpoolsize;
ed9b544e 214 long long dirty; /* changes to DB from the last save */
215 list *clients;
87eca727 216 list *slaves, *monitors;
ed9b544e 217 char neterr[ANET_ERR_LEN];
218 aeEventLoop *el;
219 int cronloops; /* number of times the cron function run */
220 list *objfreelist; /* A list of freed objects to avoid malloc() */
221 time_t lastsave; /* Unix time of last save succeeede */
5fba9f71 222 size_t usedmemory; /* Used memory in megabytes */
ed9b544e 223 /* Fields used only for stats */
224 time_t stat_starttime; /* server start time */
225 long long stat_numcommands; /* number of processed commands */
226 long long stat_numconnections; /* number of connections received */
227 /* Configuration */
228 int verbosity;
229 int glueoutputbuf;
230 int maxidletime;
231 int dbnum;
232 int daemonize;
ed329fcf 233 char *pidfile;
ed9b544e 234 int bgsaveinprogress;
235 struct saveparam *saveparams;
236 int saveparamslen;
237 char *logfile;
238 char *bindaddr;
239 char *dbfilename;
abcb223e 240 char *requirepass;
10c43610 241 int shareobjects;
ed9b544e 242 /* Replication related */
243 int isslave;
244 char *masterhost;
245 int masterport;
40d224a9 246 redisClient *master; /* client that is master for this slave */
ed9b544e 247 int replstate;
248 /* Sort parameters - qsort_r() is only available under BSD so we
249 * have to take this state global, in order to pass it to sortCompare() */
250 int sort_desc;
251 int sort_alpha;
252 int sort_bypattern;
253};
254
255typedef void redisCommandProc(redisClient *c);
256struct redisCommand {
257 char *name;
258 redisCommandProc *proc;
259 int arity;
260 int flags;
261};
262
263typedef struct _redisSortObject {
264 robj *obj;
265 union {
266 double score;
267 robj *cmpobj;
268 } u;
269} redisSortObject;
270
271typedef struct _redisSortOperation {
272 int type;
273 robj *pattern;
274} redisSortOperation;
275
276struct sharedObjectsStruct {
c937aa89 277 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
7b45bfb2 278 *colon, *nullbulk, *nullmultibulk,
c937aa89 279 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
280 *outofrangeerr, *plus,
ed9b544e 281 *select0, *select1, *select2, *select3, *select4,
282 *select5, *select6, *select7, *select8, *select9;
283} shared;
284
285/*================================ Prototypes =============================== */
286
287static void freeStringObject(robj *o);
288static void freeListObject(robj *o);
289static void freeSetObject(robj *o);
290static void decrRefCount(void *o);
291static robj *createObject(int type, void *ptr);
292static void freeClient(redisClient *c);
f78fd11b 293static int rdbLoad(char *filename);
ed9b544e 294static void addReply(redisClient *c, robj *obj);
295static void addReplySds(redisClient *c, sds s);
296static void incrRefCount(robj *o);
f78fd11b 297static int rdbSaveBackground(char *filename);
ed9b544e 298static robj *createStringObject(char *ptr, size_t len);
87eca727 299static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int dictid, robj **argv, int argc);
ed9b544e 300static int syncWithMaster(void);
10c43610 301static robj *tryObjectSharing(robj *o);
3305306f 302static int removeExpire(redisDb *db, robj *key);
303static int expireIfNeeded(redisDb *db, robj *key);
304static int deleteIfVolatile(redisDb *db, robj *key);
94754ccc 305static int deleteKey(redisDb *db, robj *key);
bb32ede5 306static time_t getExpire(redisDb *db, robj *key);
307static int setExpire(redisDb *db, robj *key, time_t when);
6208b3a7 308static void updateSalvesWaitingBgsave(int bgsaveerr);
ed9b544e 309
abcb223e 310static void authCommand(redisClient *c);
ed9b544e 311static void pingCommand(redisClient *c);
312static void echoCommand(redisClient *c);
313static void setCommand(redisClient *c);
314static void setnxCommand(redisClient *c);
315static void getCommand(redisClient *c);
316static void delCommand(redisClient *c);
317static void existsCommand(redisClient *c);
318static void incrCommand(redisClient *c);
319static void decrCommand(redisClient *c);
320static void incrbyCommand(redisClient *c);
321static void decrbyCommand(redisClient *c);
322static void selectCommand(redisClient *c);
323static void randomkeyCommand(redisClient *c);
324static void keysCommand(redisClient *c);
325static void dbsizeCommand(redisClient *c);
326static void lastsaveCommand(redisClient *c);
327static void saveCommand(redisClient *c);
328static void bgsaveCommand(redisClient *c);
329static void shutdownCommand(redisClient *c);
330static void moveCommand(redisClient *c);
331static void renameCommand(redisClient *c);
332static void renamenxCommand(redisClient *c);
333static void lpushCommand(redisClient *c);
334static void rpushCommand(redisClient *c);
335static void lpopCommand(redisClient *c);
336static void rpopCommand(redisClient *c);
337static void llenCommand(redisClient *c);
338static void lindexCommand(redisClient *c);
339static void lrangeCommand(redisClient *c);
340static void ltrimCommand(redisClient *c);
341static void typeCommand(redisClient *c);
342static void lsetCommand(redisClient *c);
343static void saddCommand(redisClient *c);
344static void sremCommand(redisClient *c);
a4460ef4 345static void smoveCommand(redisClient *c);
ed9b544e 346static void sismemberCommand(redisClient *c);
347static void scardCommand(redisClient *c);
348static void sinterCommand(redisClient *c);
349static void sinterstoreCommand(redisClient *c);
40d224a9 350static void sunionCommand(redisClient *c);
351static void sunionstoreCommand(redisClient *c);
f4f56e1d 352static void sdiffCommand(redisClient *c);
353static void sdiffstoreCommand(redisClient *c);
ed9b544e 354static void syncCommand(redisClient *c);
355static void flushdbCommand(redisClient *c);
356static void flushallCommand(redisClient *c);
357static void sortCommand(redisClient *c);
358static void lremCommand(redisClient *c);
359static void infoCommand(redisClient *c);
70003d28 360static void mgetCommand(redisClient *c);
87eca727 361static void monitorCommand(redisClient *c);
3305306f 362static void expireCommand(redisClient *c);
a431eb74 363static void getSetCommand(redisClient *c);
ed9b544e 364
365/*================================= Globals ================================= */
366
367/* Global vars */
368static struct redisServer server; /* server global state */
369static struct redisCommand cmdTable[] = {
370 {"get",getCommand,2,REDIS_CMD_INLINE},
371 {"set",setCommand,3,REDIS_CMD_BULK},
372 {"setnx",setnxCommand,3,REDIS_CMD_BULK},
5109cdff 373 {"del",delCommand,-2,REDIS_CMD_INLINE},
ed9b544e 374 {"exists",existsCommand,2,REDIS_CMD_INLINE},
375 {"incr",incrCommand,2,REDIS_CMD_INLINE},
376 {"decr",decrCommand,2,REDIS_CMD_INLINE},
70003d28 377 {"mget",mgetCommand,-2,REDIS_CMD_INLINE},
ed9b544e 378 {"rpush",rpushCommand,3,REDIS_CMD_BULK},
379 {"lpush",lpushCommand,3,REDIS_CMD_BULK},
380 {"rpop",rpopCommand,2,REDIS_CMD_INLINE},
381 {"lpop",lpopCommand,2,REDIS_CMD_INLINE},
382 {"llen",llenCommand,2,REDIS_CMD_INLINE},
383 {"lindex",lindexCommand,3,REDIS_CMD_INLINE},
384 {"lset",lsetCommand,4,REDIS_CMD_BULK},
385 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE},
386 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE},
387 {"lrem",lremCommand,4,REDIS_CMD_BULK},
388 {"sadd",saddCommand,3,REDIS_CMD_BULK},
389 {"srem",sremCommand,3,REDIS_CMD_BULK},
a4460ef4 390 {"smove",smoveCommand,4,REDIS_CMD_BULK},
ed9b544e 391 {"sismember",sismemberCommand,3,REDIS_CMD_BULK},
392 {"scard",scardCommand,2,REDIS_CMD_INLINE},
393 {"sinter",sinterCommand,-2,REDIS_CMD_INLINE},
394 {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE},
40d224a9 395 {"sunion",sunionCommand,-2,REDIS_CMD_INLINE},
396 {"sunionstore",sunionstoreCommand,-3,REDIS_CMD_INLINE},
f4f56e1d 397 {"sdiff",sdiffCommand,-2,REDIS_CMD_INLINE},
398 {"sdiffstore",sdiffstoreCommand,-3,REDIS_CMD_INLINE},
ed9b544e 399 {"smembers",sinterCommand,2,REDIS_CMD_INLINE},
400 {"incrby",incrbyCommand,3,REDIS_CMD_INLINE},
401 {"decrby",decrbyCommand,3,REDIS_CMD_INLINE},
a431eb74 402 {"getset",getSetCommand,3,REDIS_CMD_BULK},
ed9b544e 403 {"randomkey",randomkeyCommand,1,REDIS_CMD_INLINE},
404 {"select",selectCommand,2,REDIS_CMD_INLINE},
405 {"move",moveCommand,3,REDIS_CMD_INLINE},
406 {"rename",renameCommand,3,REDIS_CMD_INLINE},
407 {"renamenx",renamenxCommand,3,REDIS_CMD_INLINE},
408 {"keys",keysCommand,2,REDIS_CMD_INLINE},
409 {"dbsize",dbsizeCommand,1,REDIS_CMD_INLINE},
abcb223e 410 {"auth",authCommand,2,REDIS_CMD_INLINE},
ed9b544e 411 {"ping",pingCommand,1,REDIS_CMD_INLINE},
412 {"echo",echoCommand,2,REDIS_CMD_BULK},
413 {"save",saveCommand,1,REDIS_CMD_INLINE},
414 {"bgsave",bgsaveCommand,1,REDIS_CMD_INLINE},
415 {"shutdown",shutdownCommand,1,REDIS_CMD_INLINE},
416 {"lastsave",lastsaveCommand,1,REDIS_CMD_INLINE},
417 {"type",typeCommand,2,REDIS_CMD_INLINE},
418 {"sync",syncCommand,1,REDIS_CMD_INLINE},
419 {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE},
420 {"flushall",flushallCommand,1,REDIS_CMD_INLINE},
421 {"sort",sortCommand,-2,REDIS_CMD_INLINE},
422 {"info",infoCommand,1,REDIS_CMD_INLINE},
87eca727 423 {"monitor",monitorCommand,1,REDIS_CMD_INLINE},
3305306f 424 {"expire",expireCommand,3,REDIS_CMD_INLINE},
ed9b544e 425 {NULL,NULL,0,0}
426};
427
428/*============================ Utility functions ============================ */
429
430/* Glob-style pattern matching. */
431int stringmatchlen(const char *pattern, int patternLen,
432 const char *string, int stringLen, int nocase)
433{
434 while(patternLen) {
435 switch(pattern[0]) {
436 case '*':
437 while (pattern[1] == '*') {
438 pattern++;
439 patternLen--;
440 }
441 if (patternLen == 1)
442 return 1; /* match */
443 while(stringLen) {
444 if (stringmatchlen(pattern+1, patternLen-1,
445 string, stringLen, nocase))
446 return 1; /* match */
447 string++;
448 stringLen--;
449 }
450 return 0; /* no match */
451 break;
452 case '?':
453 if (stringLen == 0)
454 return 0; /* no match */
455 string++;
456 stringLen--;
457 break;
458 case '[':
459 {
460 int not, match;
461
462 pattern++;
463 patternLen--;
464 not = pattern[0] == '^';
465 if (not) {
466 pattern++;
467 patternLen--;
468 }
469 match = 0;
470 while(1) {
471 if (pattern[0] == '\\') {
472 pattern++;
473 patternLen--;
474 if (pattern[0] == string[0])
475 match = 1;
476 } else if (pattern[0] == ']') {
477 break;
478 } else if (patternLen == 0) {
479 pattern--;
480 patternLen++;
481 break;
482 } else if (pattern[1] == '-' && patternLen >= 3) {
483 int start = pattern[0];
484 int end = pattern[2];
485 int c = string[0];
486 if (start > end) {
487 int t = start;
488 start = end;
489 end = t;
490 }
491 if (nocase) {
492 start = tolower(start);
493 end = tolower(end);
494 c = tolower(c);
495 }
496 pattern += 2;
497 patternLen -= 2;
498 if (c >= start && c <= end)
499 match = 1;
500 } else {
501 if (!nocase) {
502 if (pattern[0] == string[0])
503 match = 1;
504 } else {
505 if (tolower((int)pattern[0]) == tolower((int)string[0]))
506 match = 1;
507 }
508 }
509 pattern++;
510 patternLen--;
511 }
512 if (not)
513 match = !match;
514 if (!match)
515 return 0; /* no match */
516 string++;
517 stringLen--;
518 break;
519 }
520 case '\\':
521 if (patternLen >= 2) {
522 pattern++;
523 patternLen--;
524 }
525 /* fall through */
526 default:
527 if (!nocase) {
528 if (pattern[0] != string[0])
529 return 0; /* no match */
530 } else {
531 if (tolower((int)pattern[0]) != tolower((int)string[0]))
532 return 0; /* no match */
533 }
534 string++;
535 stringLen--;
536 break;
537 }
538 pattern++;
539 patternLen--;
540 if (stringLen == 0) {
541 while(*pattern == '*') {
542 pattern++;
543 patternLen--;
544 }
545 break;
546 }
547 }
548 if (patternLen == 0 && stringLen == 0)
549 return 1;
550 return 0;
551}
552
553void redisLog(int level, const char *fmt, ...)
554{
555 va_list ap;
556 FILE *fp;
557
558 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
559 if (!fp) return;
560
561 va_start(ap, fmt);
562 if (level >= server.verbosity) {
563 char *c = ".-*";
1904ecc1 564 char buf[64];
565 time_t now;
566
567 now = time(NULL);
568 strftime(buf,64,"%d %b %H:%M:%S",gmtime(&now));
569 fprintf(fp,"%s %c ",buf,c[level]);
ed9b544e 570 vfprintf(fp, fmt, ap);
571 fprintf(fp,"\n");
572 fflush(fp);
573 }
574 va_end(ap);
575
576 if (server.logfile) fclose(fp);
577}
578
579/*====================== Hash table type implementation ==================== */
580
581/* This is an hash table type that uses the SDS dynamic strings libary as
582 * keys and radis objects as values (objects can hold SDS strings,
583 * lists, sets). */
584
585static int sdsDictKeyCompare(void *privdata, const void *key1,
586 const void *key2)
587{
588 int l1,l2;
589 DICT_NOTUSED(privdata);
590
591 l1 = sdslen((sds)key1);
592 l2 = sdslen((sds)key2);
593 if (l1 != l2) return 0;
594 return memcmp(key1, key2, l1) == 0;
595}
596
597static void dictRedisObjectDestructor(void *privdata, void *val)
598{
599 DICT_NOTUSED(privdata);
600
601 decrRefCount(val);
602}
603
604static int dictSdsKeyCompare(void *privdata, const void *key1,
605 const void *key2)
606{
607 const robj *o1 = key1, *o2 = key2;
608 return sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
609}
610
611static unsigned int dictSdsHash(const void *key) {
612 const robj *o = key;
613 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
614}
615
616static dictType setDictType = {
617 dictSdsHash, /* hash function */
618 NULL, /* key dup */
619 NULL, /* val dup */
620 dictSdsKeyCompare, /* key compare */
621 dictRedisObjectDestructor, /* key destructor */
622 NULL /* val destructor */
623};
624
625static dictType hashDictType = {
626 dictSdsHash, /* hash function */
627 NULL, /* key dup */
628 NULL, /* val dup */
629 dictSdsKeyCompare, /* key compare */
630 dictRedisObjectDestructor, /* key destructor */
631 dictRedisObjectDestructor /* val destructor */
632};
633
634/* ========================= Random utility functions ======================= */
635
636/* Redis generally does not try to recover from out of memory conditions
637 * when allocating objects or strings, it is not clear if it will be possible
638 * to report this condition to the client since the networking layer itself
639 * is based on heap allocation for send buffers, so we simply abort.
640 * At least the code will be simpler to read... */
641static void oom(const char *msg) {
642 fprintf(stderr, "%s: Out of memory\n",msg);
643 fflush(stderr);
644 sleep(1);
645 abort();
646}
647
648/* ====================== Redis server networking stuff ===================== */
649void closeTimedoutClients(void) {
650 redisClient *c;
ed9b544e 651 listNode *ln;
652 time_t now = time(NULL);
653
6208b3a7 654 listRewind(server.clients);
655 while ((ln = listYield(server.clients)) != NULL) {
ed9b544e 656 c = listNodeValue(ln);
657 if (!(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
658 (now - c->lastinteraction > server.maxidletime)) {
659 redisLog(REDIS_DEBUG,"Closing idle client");
660 freeClient(c);
661 }
662 }
ed9b544e 663}
664
0bc03378 665/* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
666 * we resize the hash table to save memory */
667void tryResizeHashTables(void) {
668 int j;
669
670 for (j = 0; j < server.dbnum; j++) {
671 long long size, used;
672
673 size = dictSlots(server.db[j].dict);
674 used = dictSize(server.db[j].dict);
675 if (size && used && size > REDIS_HT_MINSLOTS &&
676 (used*100/size < REDIS_HT_MINFILL)) {
677 redisLog(REDIS_NOTICE,"The hash table %d is too sparse, resize it...",j);
678 dictResize(server.db[j].dict);
679 redisLog(REDIS_NOTICE,"Hash table %d resized.",j);
680 }
681 }
682}
683
ed9b544e 684int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
94754ccc 685 int j, loops = server.cronloops++;
ed9b544e 686 REDIS_NOTUSED(eventLoop);
687 REDIS_NOTUSED(id);
688 REDIS_NOTUSED(clientData);
689
690 /* Update the global state with the amount of used memory */
691 server.usedmemory = zmalloc_used_memory();
692
0bc03378 693 /* Show some info about non-empty databases */
ed9b544e 694 for (j = 0; j < server.dbnum; j++) {
dec423d9 695 long long size, used, vkeys;
94754ccc 696
3305306f 697 size = dictSlots(server.db[j].dict);
698 used = dictSize(server.db[j].dict);
94754ccc 699 vkeys = dictSize(server.db[j].expires);
ed9b544e 700 if (!(loops % 5) && used > 0) {
94754ccc 701 redisLog(REDIS_DEBUG,"DB %d: %d keys (%d volatile) in %d slots HT.",j,used,vkeys,size);
a4d1ba9a 702 /* dictPrintStats(server.dict); */
ed9b544e 703 }
ed9b544e 704 }
705
0bc03378 706 /* We don't want to resize the hash tables while a bacground saving
707 * is in progress: the saving child is created using fork() that is
708 * implemented with a copy-on-write semantic in most modern systems, so
709 * if we resize the HT while there is the saving child at work actually
710 * a lot of memory movements in the parent will cause a lot of pages
711 * copied. */
712 if (!server.bgsaveinprogress) tryResizeHashTables();
713
ed9b544e 714 /* Show information about connected clients */
715 if (!(loops % 5)) {
5fba9f71 716 redisLog(REDIS_DEBUG,"%d clients connected (%d slaves), %zu bytes in use",
ed9b544e 717 listLength(server.clients)-listLength(server.slaves),
718 listLength(server.slaves),
10c43610 719 server.usedmemory,
3305306f 720 dictSize(server.sharingpool));
ed9b544e 721 }
722
723 /* Close connections of timedout clients */
0150db36 724 if (server.maxidletime && !(loops % 10))
ed9b544e 725 closeTimedoutClients();
726
727 /* Check if a background saving in progress terminated */
728 if (server.bgsaveinprogress) {
729 int statloc;
6208b3a7 730 /* XXX: TODO handle the case of the saving child killed */
ed9b544e 731 if (wait4(-1,&statloc,WNOHANG,NULL)) {
732 int exitcode = WEXITSTATUS(statloc);
733 if (exitcode == 0) {
734 redisLog(REDIS_NOTICE,
735 "Background saving terminated with success");
736 server.dirty = 0;
737 server.lastsave = time(NULL);
738 } else {
739 redisLog(REDIS_WARNING,
740 "Background saving error");
741 }
742 server.bgsaveinprogress = 0;
6208b3a7 743 updateSalvesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
ed9b544e 744 }
745 } else {
746 /* If there is not a background saving in progress check if
747 * we have to save now */
748 time_t now = time(NULL);
749 for (j = 0; j < server.saveparamslen; j++) {
750 struct saveparam *sp = server.saveparams+j;
751
752 if (server.dirty >= sp->changes &&
753 now-server.lastsave > sp->seconds) {
754 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
755 sp->changes, sp->seconds);
f78fd11b 756 rdbSaveBackground(server.dbfilename);
ed9b544e 757 break;
758 }
759 }
760 }
94754ccc 761
762 /* Try to expire a few timed out keys */
763 for (j = 0; j < server.dbnum; j++) {
764 redisDb *db = server.db+j;
765 int num = dictSize(db->expires);
766
767 if (num) {
768 time_t now = time(NULL);
769
770 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
771 num = REDIS_EXPIRELOOKUPS_PER_CRON;
772 while (num--) {
773 dictEntry *de;
774 time_t t;
775
776 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
777 t = (time_t) dictGetEntryVal(de);
778 if (now > t) {
779 deleteKey(db,dictGetEntryKey(de));
780 }
781 }
782 }
783 }
784
ed9b544e 785 /* Check if we should connect to a MASTER */
786 if (server.replstate == REDIS_REPL_CONNECT) {
787 redisLog(REDIS_NOTICE,"Connecting to MASTER...");
788 if (syncWithMaster() == REDIS_OK) {
789 redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync succeeded");
790 }
791 }
792 return 1000;
793}
794
795static void createSharedObjects(void) {
796 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
797 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
798 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
c937aa89 799 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
800 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
801 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
802 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
803 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
804 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
ed9b544e 805 /* no such key */
ed9b544e 806 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
807 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
808 "-ERR Operation against a key holding the wrong kind of value\r\n"));
ed9b544e 809 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
810 "-ERR no such key\r\n"));
ed9b544e 811 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
812 "-ERR syntax error\r\n"));
c937aa89 813 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
814 "-ERR source and destination objects are the same\r\n"));
815 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
816 "-ERR index out of range\r\n"));
ed9b544e 817 shared.space = createObject(REDIS_STRING,sdsnew(" "));
c937aa89 818 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
819 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
ed9b544e 820 shared.select0 = createStringObject("select 0\r\n",10);
821 shared.select1 = createStringObject("select 1\r\n",10);
822 shared.select2 = createStringObject("select 2\r\n",10);
823 shared.select3 = createStringObject("select 3\r\n",10);
824 shared.select4 = createStringObject("select 4\r\n",10);
825 shared.select5 = createStringObject("select 5\r\n",10);
826 shared.select6 = createStringObject("select 6\r\n",10);
827 shared.select7 = createStringObject("select 7\r\n",10);
828 shared.select8 = createStringObject("select 8\r\n",10);
829 shared.select9 = createStringObject("select 9\r\n",10);
830}
831
832static void appendServerSaveParams(time_t seconds, int changes) {
833 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
834 if (server.saveparams == NULL) oom("appendServerSaveParams");
835 server.saveparams[server.saveparamslen].seconds = seconds;
836 server.saveparams[server.saveparamslen].changes = changes;
837 server.saveparamslen++;
838}
839
840static void ResetServerSaveParams() {
841 zfree(server.saveparams);
842 server.saveparams = NULL;
843 server.saveparamslen = 0;
844}
845
846static void initServerConfig() {
847 server.dbnum = REDIS_DEFAULT_DBNUM;
848 server.port = REDIS_SERVERPORT;
849 server.verbosity = REDIS_DEBUG;
850 server.maxidletime = REDIS_MAXIDLETIME;
851 server.saveparams = NULL;
852 server.logfile = NULL; /* NULL = log on standard output */
853 server.bindaddr = NULL;
854 server.glueoutputbuf = 1;
855 server.daemonize = 0;
ed329fcf 856 server.pidfile = "/var/run/redis.pid";
ed9b544e 857 server.dbfilename = "dump.rdb";
abcb223e 858 server.requirepass = NULL;
10c43610 859 server.shareobjects = 0;
ed9b544e 860 ResetServerSaveParams();
861
862 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
863 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
864 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
865 /* Replication related */
866 server.isslave = 0;
867 server.masterhost = NULL;
868 server.masterport = 6379;
869 server.master = NULL;
870 server.replstate = REDIS_REPL_NONE;
871}
872
873static void initServer() {
874 int j;
875
876 signal(SIGHUP, SIG_IGN);
877 signal(SIGPIPE, SIG_IGN);
878
879 server.clients = listCreate();
880 server.slaves = listCreate();
87eca727 881 server.monitors = listCreate();
ed9b544e 882 server.objfreelist = listCreate();
883 createSharedObjects();
884 server.el = aeCreateEventLoop();
3305306f 885 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
10c43610 886 server.sharingpool = dictCreate(&setDictType,NULL);
887 server.sharingpoolsize = 1024;
3305306f 888 if (!server.db || !server.clients || !server.slaves || !server.monitors || !server.el || !server.objfreelist)
ed9b544e 889 oom("server initialization"); /* Fatal OOM */
890 server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
891 if (server.fd == -1) {
892 redisLog(REDIS_WARNING, "Opening TCP port: %s", server.neterr);
893 exit(1);
894 }
3305306f 895 for (j = 0; j < server.dbnum; j++) {
896 server.db[j].dict = dictCreate(&hashDictType,NULL);
897 server.db[j].expires = dictCreate(&setDictType,NULL);
898 server.db[j].id = j;
899 }
ed9b544e 900 server.cronloops = 0;
901 server.bgsaveinprogress = 0;
902 server.lastsave = time(NULL);
903 server.dirty = 0;
904 server.usedmemory = 0;
905 server.stat_numcommands = 0;
906 server.stat_numconnections = 0;
907 server.stat_starttime = time(NULL);
908 aeCreateTimeEvent(server.el, 1000, serverCron, NULL, NULL);
909}
910
911/* Empty the whole database */
ca37e9cd 912static long long emptyDb() {
ed9b544e 913 int j;
ca37e9cd 914 long long removed = 0;
ed9b544e 915
3305306f 916 for (j = 0; j < server.dbnum; j++) {
ca37e9cd 917 removed += dictSize(server.db[j].dict);
3305306f 918 dictEmpty(server.db[j].dict);
919 dictEmpty(server.db[j].expires);
920 }
ca37e9cd 921 return removed;
ed9b544e 922}
923
85dd2f3a 924static int yesnotoi(char *s) {
925 if (!strcasecmp(s,"yes")) return 1;
926 else if (!strcasecmp(s,"no")) return 0;
927 else return -1;
928}
929
ed9b544e 930/* I agree, this is a very rudimental way to load a configuration...
931 will improve later if the config gets more complex */
932static void loadServerConfig(char *filename) {
933 FILE *fp = fopen(filename,"r");
934 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
935 int linenum = 0;
936 sds line = NULL;
937
938 if (!fp) {
939 redisLog(REDIS_WARNING,"Fatal error, can't open config file");
940 exit(1);
941 }
942 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
943 sds *argv;
944 int argc, j;
945
946 linenum++;
947 line = sdsnew(buf);
948 line = sdstrim(line," \t\r\n");
949
950 /* Skip comments and blank lines*/
951 if (line[0] == '#' || line[0] == '\0') {
952 sdsfree(line);
953 continue;
954 }
955
956 /* Split into arguments */
957 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
958 sdstolower(argv[0]);
959
960 /* Execute config directives */
bb0b03a3 961 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
ed9b544e 962 server.maxidletime = atoi(argv[1]);
0150db36 963 if (server.maxidletime < 0) {
ed9b544e 964 err = "Invalid timeout value"; goto loaderr;
965 }
bb0b03a3 966 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
ed9b544e 967 server.port = atoi(argv[1]);
968 if (server.port < 1 || server.port > 65535) {
969 err = "Invalid port"; goto loaderr;
970 }
bb0b03a3 971 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
ed9b544e 972 server.bindaddr = zstrdup(argv[1]);
bb0b03a3 973 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
ed9b544e 974 int seconds = atoi(argv[1]);
975 int changes = atoi(argv[2]);
976 if (seconds < 1 || changes < 0) {
977 err = "Invalid save parameters"; goto loaderr;
978 }
979 appendServerSaveParams(seconds,changes);
bb0b03a3 980 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
ed9b544e 981 if (chdir(argv[1]) == -1) {
982 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
983 argv[1], strerror(errno));
984 exit(1);
985 }
bb0b03a3 986 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
987 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
988 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
989 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
ed9b544e 990 else {
991 err = "Invalid log level. Must be one of debug, notice, warning";
992 goto loaderr;
993 }
bb0b03a3 994 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
ed9b544e 995 FILE *fp;
996
997 server.logfile = zstrdup(argv[1]);
bb0b03a3 998 if (!strcasecmp(server.logfile,"stdout")) {
ed9b544e 999 zfree(server.logfile);
1000 server.logfile = NULL;
1001 }
1002 if (server.logfile) {
1003 /* Test if we are able to open the file. The server will not
1004 * be able to abort just for this problem later... */
1005 fp = fopen(server.logfile,"a");
1006 if (fp == NULL) {
1007 err = sdscatprintf(sdsempty(),
1008 "Can't open the log file: %s", strerror(errno));
1009 goto loaderr;
1010 }
1011 fclose(fp);
1012 }
bb0b03a3 1013 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
ed9b544e 1014 server.dbnum = atoi(argv[1]);
1015 if (server.dbnum < 1) {
1016 err = "Invalid number of databases"; goto loaderr;
1017 }
bb0b03a3 1018 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
ed9b544e 1019 server.masterhost = sdsnew(argv[1]);
1020 server.masterport = atoi(argv[2]);
1021 server.replstate = REDIS_REPL_CONNECT;
bb0b03a3 1022 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
85dd2f3a 1023 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
ed9b544e 1024 err = "argument must be 'yes' or 'no'"; goto loaderr;
1025 }
bb0b03a3 1026 } else if (!strcasecmp(argv[0],"shareobjects") && argc == 2) {
85dd2f3a 1027 if ((server.shareobjects = yesnotoi(argv[1])) == -1) {
10c43610 1028 err = "argument must be 'yes' or 'no'"; goto loaderr;
1029 }
bb0b03a3 1030 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
85dd2f3a 1031 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
ed9b544e 1032 err = "argument must be 'yes' or 'no'"; goto loaderr;
1033 }
bb0b03a3 1034 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
abcb223e 1035 server.requirepass = zstrdup(argv[1]);
bb0b03a3 1036 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
ed329fcf 1037 server.pidfile = zstrdup(argv[1]);
bb0b03a3 1038 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
b8b553c8 1039 server.dbfilename = zstrdup(argv[1]);
ed9b544e 1040 } else {
1041 err = "Bad directive or wrong number of arguments"; goto loaderr;
1042 }
1043 for (j = 0; j < argc; j++)
1044 sdsfree(argv[j]);
1045 zfree(argv);
1046 sdsfree(line);
1047 }
1048 fclose(fp);
1049 return;
1050
1051loaderr:
1052 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
1053 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
1054 fprintf(stderr, ">>> '%s'\n", line);
1055 fprintf(stderr, "%s\n", err);
1056 exit(1);
1057}
1058
1059static void freeClientArgv(redisClient *c) {
1060 int j;
1061
1062 for (j = 0; j < c->argc; j++)
1063 decrRefCount(c->argv[j]);
1064 c->argc = 0;
1065}
1066
1067static void freeClient(redisClient *c) {
1068 listNode *ln;
1069
1070 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
1071 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1072 sdsfree(c->querybuf);
1073 listRelease(c->reply);
1074 freeClientArgv(c);
1075 close(c->fd);
1076 ln = listSearchKey(server.clients,c);
1077 assert(ln != NULL);
1078 listDelNode(server.clients,ln);
1079 if (c->flags & REDIS_SLAVE) {
6208b3a7 1080 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
1081 close(c->repldbfd);
87eca727 1082 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
1083 ln = listSearchKey(l,c);
ed9b544e 1084 assert(ln != NULL);
87eca727 1085 listDelNode(l,ln);
ed9b544e 1086 }
1087 if (c->flags & REDIS_MASTER) {
1088 server.master = NULL;
1089 server.replstate = REDIS_REPL_CONNECT;
1090 }
93ea3759 1091 zfree(c->argv);
ed9b544e 1092 zfree(c);
1093}
1094
1095static void glueReplyBuffersIfNeeded(redisClient *c) {
1096 int totlen = 0;
6208b3a7 1097 listNode *ln;
ed9b544e 1098 robj *o;
1099
6208b3a7 1100 listRewind(c->reply);
1101 while((ln = listYield(c->reply))) {
ed9b544e 1102 o = ln->value;
1103 totlen += sdslen(o->ptr);
ed9b544e 1104 /* This optimization makes more sense if we don't have to copy
1105 * too much data */
1106 if (totlen > 1024) return;
1107 }
1108 if (totlen > 0) {
1109 char buf[1024];
1110 int copylen = 0;
1111
6208b3a7 1112 listRewind(c->reply);
1113 while((ln = listYield(c->reply))) {
ed9b544e 1114 o = ln->value;
1115 memcpy(buf+copylen,o->ptr,sdslen(o->ptr));
1116 copylen += sdslen(o->ptr);
1117 listDelNode(c->reply,ln);
ed9b544e 1118 }
1119 /* Now the output buffer is empty, add the new single element */
1120 addReplySds(c,sdsnewlen(buf,totlen));
1121 }
1122}
1123
1124static void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1125 redisClient *c = privdata;
1126 int nwritten = 0, totwritten = 0, objlen;
1127 robj *o;
1128 REDIS_NOTUSED(el);
1129 REDIS_NOTUSED(mask);
1130
1131 if (server.glueoutputbuf && listLength(c->reply) > 1)
1132 glueReplyBuffersIfNeeded(c);
1133 while(listLength(c->reply)) {
1134 o = listNodeValue(listFirst(c->reply));
1135 objlen = sdslen(o->ptr);
1136
1137 if (objlen == 0) {
1138 listDelNode(c->reply,listFirst(c->reply));
1139 continue;
1140 }
1141
1142 if (c->flags & REDIS_MASTER) {
1143 nwritten = objlen - c->sentlen;
1144 } else {
a4d1ba9a 1145 nwritten = write(fd, ((char*)o->ptr)+c->sentlen, objlen - c->sentlen);
ed9b544e 1146 if (nwritten <= 0) break;
1147 }
1148 c->sentlen += nwritten;
1149 totwritten += nwritten;
1150 /* If we fully sent the object on head go to the next one */
1151 if (c->sentlen == objlen) {
1152 listDelNode(c->reply,listFirst(c->reply));
1153 c->sentlen = 0;
1154 }
1155 }
1156 if (nwritten == -1) {
1157 if (errno == EAGAIN) {
1158 nwritten = 0;
1159 } else {
1160 redisLog(REDIS_DEBUG,
1161 "Error writing to client: %s", strerror(errno));
1162 freeClient(c);
1163 return;
1164 }
1165 }
1166 if (totwritten > 0) c->lastinteraction = time(NULL);
1167 if (listLength(c->reply) == 0) {
1168 c->sentlen = 0;
1169 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1170 }
1171}
1172
1173static struct redisCommand *lookupCommand(char *name) {
1174 int j = 0;
1175 while(cmdTable[j].name != NULL) {
bb0b03a3 1176 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
ed9b544e 1177 j++;
1178 }
1179 return NULL;
1180}
1181
1182/* resetClient prepare the client to process the next command */
1183static void resetClient(redisClient *c) {
1184 freeClientArgv(c);
1185 c->bulklen = -1;
1186}
1187
1188/* If this function gets called we already read a whole
1189 * command, argments are in the client argv/argc fields.
1190 * processCommand() execute the command or prepare the
1191 * server for a bulk read from the client.
1192 *
1193 * If 1 is returned the client is still alive and valid and
1194 * and other operations can be performed by the caller. Otherwise
1195 * if 0 is returned the client was destroied (i.e. after QUIT). */
1196static int processCommand(redisClient *c) {
1197 struct redisCommand *cmd;
1198 long long dirty;
1199
ed9b544e 1200 /* The QUIT command is handled as a special case. Normal command
1201 * procs are unable to close the client connection safely */
bb0b03a3 1202 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
ed9b544e 1203 freeClient(c);
1204 return 0;
1205 }
1206 cmd = lookupCommand(c->argv[0]->ptr);
1207 if (!cmd) {
1208 addReplySds(c,sdsnew("-ERR unknown command\r\n"));
1209 resetClient(c);
1210 return 1;
1211 } else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
1212 (c->argc < -cmd->arity)) {
1213 addReplySds(c,sdsnew("-ERR wrong number of arguments\r\n"));
1214 resetClient(c);
1215 return 1;
1216 } else if (cmd->flags & REDIS_CMD_BULK && c->bulklen == -1) {
1217 int bulklen = atoi(c->argv[c->argc-1]->ptr);
1218
1219 decrRefCount(c->argv[c->argc-1]);
1220 if (bulklen < 0 || bulklen > 1024*1024*1024) {
1221 c->argc--;
1222 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
1223 resetClient(c);
1224 return 1;
1225 }
1226 c->argc--;
1227 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
1228 /* It is possible that the bulk read is already in the
1229 * buffer. Check this condition and handle it accordingly */
1230 if ((signed)sdslen(c->querybuf) >= c->bulklen) {
1231 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
1232 c->argc++;
1233 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
1234 } else {
1235 return 1;
1236 }
1237 }
10c43610 1238 /* Let's try to share objects on the command arguments vector */
1239 if (server.shareobjects) {
1240 int j;
1241 for(j = 1; j < c->argc; j++)
1242 c->argv[j] = tryObjectSharing(c->argv[j]);
1243 }
e63943a4 1244 /* Check if the user is authenticated */
1245 if (server.requirepass && !c->authenticated && cmd->proc != authCommand) {
1246 addReplySds(c,sdsnew("-ERR operation not permitted\r\n"));
1247 resetClient(c);
1248 return 1;
1249 }
1250
ed9b544e 1251 /* Exec the command */
1252 dirty = server.dirty;
1253 cmd->proc(c);
1254 if (server.dirty-dirty != 0 && listLength(server.slaves))
3305306f 1255 replicationFeedSlaves(server.slaves,cmd,c->db->id,c->argv,c->argc);
87eca727 1256 if (listLength(server.monitors))
3305306f 1257 replicationFeedSlaves(server.monitors,cmd,c->db->id,c->argv,c->argc);
ed9b544e 1258 server.stat_numcommands++;
1259
1260 /* Prepare the client for the next command */
1261 if (c->flags & REDIS_CLOSE) {
1262 freeClient(c);
1263 return 0;
1264 }
1265 resetClient(c);
1266 return 1;
1267}
1268
87eca727 1269static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int dictid, robj **argv, int argc) {
6208b3a7 1270 listNode *ln;
ed9b544e 1271 int outc = 0, j;
93ea3759 1272 robj **outv;
1273 /* (args*2)+1 is enough room for args, spaces, newlines */
1274 robj *static_outv[REDIS_STATIC_ARGS*2+1];
1275
1276 if (argc <= REDIS_STATIC_ARGS) {
1277 outv = static_outv;
1278 } else {
1279 outv = zmalloc(sizeof(robj*)*(argc*2+1));
1280 if (!outv) oom("replicationFeedSlaves");
1281 }
ed9b544e 1282
1283 for (j = 0; j < argc; j++) {
1284 if (j != 0) outv[outc++] = shared.space;
1285 if ((cmd->flags & REDIS_CMD_BULK) && j == argc-1) {
1286 robj *lenobj;
1287
1288 lenobj = createObject(REDIS_STRING,
1289 sdscatprintf(sdsempty(),"%d\r\n",sdslen(argv[j]->ptr)));
1290 lenobj->refcount = 0;
1291 outv[outc++] = lenobj;
1292 }
1293 outv[outc++] = argv[j];
1294 }
1295 outv[outc++] = shared.crlf;
1296
40d224a9 1297 /* Increment all the refcounts at start and decrement at end in order to
1298 * be sure to free objects if there is no slave in a replication state
1299 * able to be feed with commands */
1300 for (j = 0; j < outc; j++) incrRefCount(outv[j]);
6208b3a7 1301 listRewind(slaves);
1302 while((ln = listYield(slaves))) {
ed9b544e 1303 redisClient *slave = ln->value;
40d224a9 1304
1305 /* Don't feed slaves that are still waiting for BGSAVE to start */
6208b3a7 1306 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;
40d224a9 1307
1308 /* Feed all the other slaves, MONITORs and so on */
ed9b544e 1309 if (slave->slaveseldb != dictid) {
1310 robj *selectcmd;
1311
1312 switch(dictid) {
1313 case 0: selectcmd = shared.select0; break;
1314 case 1: selectcmd = shared.select1; break;
1315 case 2: selectcmd = shared.select2; break;
1316 case 3: selectcmd = shared.select3; break;
1317 case 4: selectcmd = shared.select4; break;
1318 case 5: selectcmd = shared.select5; break;
1319 case 6: selectcmd = shared.select6; break;
1320 case 7: selectcmd = shared.select7; break;
1321 case 8: selectcmd = shared.select8; break;
1322 case 9: selectcmd = shared.select9; break;
1323 default:
1324 selectcmd = createObject(REDIS_STRING,
1325 sdscatprintf(sdsempty(),"select %d\r\n",dictid));
1326 selectcmd->refcount = 0;
1327 break;
1328 }
1329 addReply(slave,selectcmd);
1330 slave->slaveseldb = dictid;
1331 }
1332 for (j = 0; j < outc; j++) addReply(slave,outv[j]);
ed9b544e 1333 }
40d224a9 1334 for (j = 0; j < outc; j++) decrRefCount(outv[j]);
93ea3759 1335 if (outv != static_outv) zfree(outv);
ed9b544e 1336}
1337
1338static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1339 redisClient *c = (redisClient*) privdata;
6208b3a7 1340 char buf[REDIS_IOBUF_LEN];
ed9b544e 1341 int nread;
1342 REDIS_NOTUSED(el);
1343 REDIS_NOTUSED(mask);
1344
6208b3a7 1345 nread = read(fd, buf, REDIS_IOBUF_LEN);
ed9b544e 1346 if (nread == -1) {
1347 if (errno == EAGAIN) {
1348 nread = 0;
1349 } else {
1350 redisLog(REDIS_DEBUG, "Reading from client: %s",strerror(errno));
1351 freeClient(c);
1352 return;
1353 }
1354 } else if (nread == 0) {
1355 redisLog(REDIS_DEBUG, "Client closed connection");
1356 freeClient(c);
1357 return;
1358 }
1359 if (nread) {
1360 c->querybuf = sdscatlen(c->querybuf, buf, nread);
1361 c->lastinteraction = time(NULL);
1362 } else {
1363 return;
1364 }
1365
1366again:
1367 if (c->bulklen == -1) {
1368 /* Read the first line of the query */
1369 char *p = strchr(c->querybuf,'\n');
1370 size_t querylen;
1371 if (p) {
1372 sds query, *argv;
1373 int argc, j;
1374
1375 query = c->querybuf;
1376 c->querybuf = sdsempty();
1377 querylen = 1+(p-(query));
1378 if (sdslen(query) > querylen) {
1379 /* leave data after the first line of the query in the buffer */
1380 c->querybuf = sdscatlen(c->querybuf,query+querylen,sdslen(query)-querylen);
1381 }
1382 *p = '\0'; /* remove "\n" */
1383 if (*(p-1) == '\r') *(p-1) = '\0'; /* and "\r" if any */
1384 sdsupdatelen(query);
1385
1386 /* Now we can split the query in arguments */
1387 if (sdslen(query) == 0) {
1388 /* Ignore empty query */
1389 sdsfree(query);
1390 return;
1391 }
1392 argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
ed9b544e 1393 if (argv == NULL) oom("sdssplitlen");
93ea3759 1394 sdsfree(query);
1395
1396 if (c->argv) zfree(c->argv);
1397 c->argv = zmalloc(sizeof(robj*)*argc);
1398 if (c->argv == NULL) oom("allocating arguments list for client");
1399
1400 for (j = 0; j < argc; j++) {
ed9b544e 1401 if (sdslen(argv[j])) {
1402 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
1403 c->argc++;
1404 } else {
1405 sdsfree(argv[j]);
1406 }
1407 }
1408 zfree(argv);
1409 /* Execute the command. If the client is still valid
1410 * after processCommand() return and there is something
1411 * on the query buffer try to process the next command. */
1412 if (processCommand(c) && sdslen(c->querybuf)) goto again;
1413 return;
1414 } else if (sdslen(c->querybuf) >= 1024) {
1415 redisLog(REDIS_DEBUG, "Client protocol error");
1416 freeClient(c);
1417 return;
1418 }
1419 } else {
1420 /* Bulk read handling. Note that if we are at this point
1421 the client already sent a command terminated with a newline,
1422 we are reading the bulk data that is actually the last
1423 argument of the command. */
1424 int qbl = sdslen(c->querybuf);
1425
1426 if (c->bulklen <= qbl) {
1427 /* Copy everything but the final CRLF as final argument */
1428 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
1429 c->argc++;
1430 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
1431 processCommand(c);
1432 return;
1433 }
1434 }
1435}
1436
1437static int selectDb(redisClient *c, int id) {
1438 if (id < 0 || id >= server.dbnum)
1439 return REDIS_ERR;
3305306f 1440 c->db = &server.db[id];
ed9b544e 1441 return REDIS_OK;
1442}
1443
40d224a9 1444static void *dupClientReplyValue(void *o) {
1445 incrRefCount((robj*)o);
1446 return 0;
1447}
1448
ed9b544e 1449static redisClient *createClient(int fd) {
1450 redisClient *c = zmalloc(sizeof(*c));
1451
1452 anetNonBlock(NULL,fd);
1453 anetTcpNoDelay(NULL,fd);
1454 if (!c) return NULL;
1455 selectDb(c,0);
1456 c->fd = fd;
1457 c->querybuf = sdsempty();
1458 c->argc = 0;
93ea3759 1459 c->argv = NULL;
ed9b544e 1460 c->bulklen = -1;
1461 c->sentlen = 0;
1462 c->flags = 0;
1463 c->lastinteraction = time(NULL);
abcb223e 1464 c->authenticated = 0;
40d224a9 1465 c->replstate = REDIS_REPL_NONE;
ed9b544e 1466 if ((c->reply = listCreate()) == NULL) oom("listCreate");
1467 listSetFreeMethod(c->reply,decrRefCount);
40d224a9 1468 listSetDupMethod(c->reply,dupClientReplyValue);
ed9b544e 1469 if (aeCreateFileEvent(server.el, c->fd, AE_READABLE,
1470 readQueryFromClient, c, NULL) == AE_ERR) {
1471 freeClient(c);
1472 return NULL;
1473 }
1474 if (!listAddNodeTail(server.clients,c)) oom("listAddNodeTail");
1475 return c;
1476}
1477
1478static void addReply(redisClient *c, robj *obj) {
1479 if (listLength(c->reply) == 0 &&
6208b3a7 1480 (c->replstate == REDIS_REPL_NONE ||
1481 c->replstate == REDIS_REPL_ONLINE) &&
ed9b544e 1482 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
1483 sendReplyToClient, c, NULL) == AE_ERR) return;
1484 if (!listAddNodeTail(c->reply,obj)) oom("listAddNodeTail");
1485 incrRefCount(obj);
1486}
1487
1488static void addReplySds(redisClient *c, sds s) {
1489 robj *o = createObject(REDIS_STRING,s);
1490 addReply(c,o);
1491 decrRefCount(o);
1492}
1493
1494static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
1495 int cport, cfd;
1496 char cip[128];
1497 REDIS_NOTUSED(el);
1498 REDIS_NOTUSED(mask);
1499 REDIS_NOTUSED(privdata);
1500
1501 cfd = anetAccept(server.neterr, fd, cip, &cport);
1502 if (cfd == AE_ERR) {
1503 redisLog(REDIS_DEBUG,"Accepting client connection: %s", server.neterr);
1504 return;
1505 }
1506 redisLog(REDIS_DEBUG,"Accepted %s:%d", cip, cport);
1507 if (createClient(cfd) == NULL) {
1508 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
1509 close(cfd); /* May be already closed, just ingore errors */
1510 return;
1511 }
1512 server.stat_numconnections++;
1513}
1514
1515/* ======================= Redis objects implementation ===================== */
1516
1517static robj *createObject(int type, void *ptr) {
1518 robj *o;
1519
1520 if (listLength(server.objfreelist)) {
1521 listNode *head = listFirst(server.objfreelist);
1522 o = listNodeValue(head);
1523 listDelNode(server.objfreelist,head);
1524 } else {
1525 o = zmalloc(sizeof(*o));
1526 }
1527 if (!o) oom("createObject");
1528 o->type = type;
1529 o->ptr = ptr;
1530 o->refcount = 1;
1531 return o;
1532}
1533
1534static robj *createStringObject(char *ptr, size_t len) {
1535 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
1536}
1537
1538static robj *createListObject(void) {
1539 list *l = listCreate();
1540
1541 if (!l) oom("listCreate");
1542 listSetFreeMethod(l,decrRefCount);
1543 return createObject(REDIS_LIST,l);
1544}
1545
1546static robj *createSetObject(void) {
1547 dict *d = dictCreate(&setDictType,NULL);
1548 if (!d) oom("dictCreate");
1549 return createObject(REDIS_SET,d);
1550}
1551
ed9b544e 1552static void freeStringObject(robj *o) {
1553 sdsfree(o->ptr);
1554}
1555
1556static void freeListObject(robj *o) {
1557 listRelease((list*) o->ptr);
1558}
1559
1560static void freeSetObject(robj *o) {
1561 dictRelease((dict*) o->ptr);
1562}
1563
1564static void freeHashObject(robj *o) {
1565 dictRelease((dict*) o->ptr);
1566}
1567
1568static void incrRefCount(robj *o) {
1569 o->refcount++;
94754ccc 1570#ifdef DEBUG_REFCOUNT
1571 if (o->type == REDIS_STRING)
1572 printf("Increment '%s'(%p), now is: %d\n",o->ptr,o,o->refcount);
1573#endif
ed9b544e 1574}
1575
1576static void decrRefCount(void *obj) {
1577 robj *o = obj;
94754ccc 1578
1579#ifdef DEBUG_REFCOUNT
1580 if (o->type == REDIS_STRING)
1581 printf("Decrement '%s'(%p), now is: %d\n",o->ptr,o,o->refcount-1);
1582#endif
ed9b544e 1583 if (--(o->refcount) == 0) {
1584 switch(o->type) {
1585 case REDIS_STRING: freeStringObject(o); break;
1586 case REDIS_LIST: freeListObject(o); break;
1587 case REDIS_SET: freeSetObject(o); break;
1588 case REDIS_HASH: freeHashObject(o); break;
1589 default: assert(0 != 0); break;
1590 }
1591 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
1592 !listAddNodeHead(server.objfreelist,o))
1593 zfree(o);
1594 }
1595}
1596
10c43610 1597/* Try to share an object against the shared objects pool */
1598static robj *tryObjectSharing(robj *o) {
1599 struct dictEntry *de;
1600 unsigned long c;
1601
3305306f 1602 if (o == NULL || server.shareobjects == 0) return o;
10c43610 1603
1604 assert(o->type == REDIS_STRING);
1605 de = dictFind(server.sharingpool,o);
1606 if (de) {
1607 robj *shared = dictGetEntryKey(de);
1608
1609 c = ((unsigned long) dictGetEntryVal(de))+1;
1610 dictGetEntryVal(de) = (void*) c;
1611 incrRefCount(shared);
1612 decrRefCount(o);
1613 return shared;
1614 } else {
1615 /* Here we are using a stream algorihtm: Every time an object is
1616 * shared we increment its count, everytime there is a miss we
1617 * recrement the counter of a random object. If this object reaches
1618 * zero we remove the object and put the current object instead. */
3305306f 1619 if (dictSize(server.sharingpool) >=
10c43610 1620 server.sharingpoolsize) {
1621 de = dictGetRandomKey(server.sharingpool);
1622 assert(de != NULL);
1623 c = ((unsigned long) dictGetEntryVal(de))-1;
1624 dictGetEntryVal(de) = (void*) c;
1625 if (c == 0) {
1626 dictDelete(server.sharingpool,de->key);
1627 }
1628 } else {
1629 c = 0; /* If the pool is empty we want to add this object */
1630 }
1631 if (c == 0) {
1632 int retval;
1633
1634 retval = dictAdd(server.sharingpool,o,(void*)1);
1635 assert(retval == DICT_OK);
1636 incrRefCount(o);
1637 }
1638 return o;
1639 }
1640}
1641
3305306f 1642static robj *lookupKey(redisDb *db, robj *key) {
1643 dictEntry *de = dictFind(db->dict,key);
1644 return de ? dictGetEntryVal(de) : NULL;
1645}
1646
1647static robj *lookupKeyRead(redisDb *db, robj *key) {
1648 expireIfNeeded(db,key);
1649 return lookupKey(db,key);
1650}
1651
1652static robj *lookupKeyWrite(redisDb *db, robj *key) {
1653 deleteIfVolatile(db,key);
1654 return lookupKey(db,key);
1655}
1656
1657static int deleteKey(redisDb *db, robj *key) {
94754ccc 1658 int retval;
1659
1660 /* We need to protect key from destruction: after the first dictDelete()
1661 * it may happen that 'key' is no longer valid if we don't increment
1662 * it's count. This may happen when we get the object reference directly
1663 * from the hash table with dictRandomKey() or dict iterators */
1664 incrRefCount(key);
3305306f 1665 if (dictSize(db->expires)) dictDelete(db->expires,key);
94754ccc 1666 retval = dictDelete(db->dict,key);
1667 decrRefCount(key);
1668
1669 return retval == DICT_OK;
3305306f 1670}
1671
ed9b544e 1672/*============================ DB saving/loading ============================ */
1673
f78fd11b 1674static int rdbSaveType(FILE *fp, unsigned char type) {
1675 if (fwrite(&type,1,1,fp) == 0) return -1;
1676 return 0;
1677}
1678
bb32ede5 1679static int rdbSaveTime(FILE *fp, time_t t) {
1680 int32_t t32 = (int32_t) t;
1681 if (fwrite(&t32,4,1,fp) == 0) return -1;
1682 return 0;
1683}
1684
e3566d4b 1685/* check rdbLoadLen() comments for more info */
f78fd11b 1686static int rdbSaveLen(FILE *fp, uint32_t len) {
1687 unsigned char buf[2];
1688
1689 if (len < (1<<6)) {
1690 /* Save a 6 bit len */
10c43610 1691 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
f78fd11b 1692 if (fwrite(buf,1,1,fp) == 0) return -1;
1693 } else if (len < (1<<14)) {
1694 /* Save a 14 bit len */
10c43610 1695 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
f78fd11b 1696 buf[1] = len&0xFF;
17be1a4a 1697 if (fwrite(buf,2,1,fp) == 0) return -1;
f78fd11b 1698 } else {
1699 /* Save a 32 bit len */
10c43610 1700 buf[0] = (REDIS_RDB_32BITLEN<<6);
f78fd11b 1701 if (fwrite(buf,1,1,fp) == 0) return -1;
1702 len = htonl(len);
1703 if (fwrite(&len,4,1,fp) == 0) return -1;
1704 }
1705 return 0;
1706}
1707
e3566d4b 1708/* String objects in the form "2391" "-100" without any space and with a
1709 * range of values that can fit in an 8, 16 or 32 bit signed value can be
1710 * encoded as integers to save space */
1711int rdbTryIntegerEncoding(sds s, unsigned char *enc) {
1712 long long value;
1713 char *endptr, buf[32];
1714
1715 /* Check if it's possible to encode this value as a number */
1716 value = strtoll(s, &endptr, 10);
1717 if (endptr[0] != '\0') return 0;
1718 snprintf(buf,32,"%lld",value);
1719
1720 /* If the number converted back into a string is not identical
1721 * then it's not possible to encode the string as integer */
1722 if (strlen(buf) != sdslen(s) || memcmp(buf,s,sdslen(s))) return 0;
1723
1724 /* Finally check if it fits in our ranges */
1725 if (value >= -(1<<7) && value <= (1<<7)-1) {
1726 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
1727 enc[1] = value&0xFF;
1728 return 2;
1729 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
1730 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
1731 enc[1] = value&0xFF;
1732 enc[2] = (value>>8)&0xFF;
1733 return 3;
1734 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
1735 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
1736 enc[1] = value&0xFF;
1737 enc[2] = (value>>8)&0xFF;
1738 enc[3] = (value>>16)&0xFF;
1739 enc[4] = (value>>24)&0xFF;
1740 return 5;
1741 } else {
1742 return 0;
1743 }
1744}
1745
774e3047 1746static int rdbSaveLzfStringObject(FILE *fp, robj *obj) {
1747 unsigned int comprlen, outlen;
1748 unsigned char byte;
1749 void *out;
1750
1751 /* We require at least four bytes compression for this to be worth it */
1752 outlen = sdslen(obj->ptr)-4;
1753 if (outlen <= 0) return 0;
3a2694c4 1754 if ((out = zmalloc(outlen+1)) == NULL) return 0;
774e3047 1755 comprlen = lzf_compress(obj->ptr, sdslen(obj->ptr), out, outlen);
1756 if (comprlen == 0) {
88e85998 1757 zfree(out);
774e3047 1758 return 0;
1759 }
1760 /* Data compressed! Let's save it on disk */
1761 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
1762 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
1763 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
1764 if (rdbSaveLen(fp,sdslen(obj->ptr)) == -1) goto writeerr;
1765 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
88e85998 1766 zfree(out);
774e3047 1767 return comprlen;
1768
1769writeerr:
88e85998 1770 zfree(out);
774e3047 1771 return -1;
1772}
1773
e3566d4b 1774/* Save a string objet as [len][data] on disk. If the object is a string
1775 * representation of an integer value we try to safe it in a special form */
10c43610 1776static int rdbSaveStringObject(FILE *fp, robj *obj) {
1777 size_t len = sdslen(obj->ptr);
e3566d4b 1778 int enclen;
10c43610 1779
774e3047 1780 /* Try integer encoding */
e3566d4b 1781 if (len <= 11) {
1782 unsigned char buf[5];
1783 if ((enclen = rdbTryIntegerEncoding(obj->ptr,buf)) > 0) {
1784 if (fwrite(buf,enclen,1,fp) == 0) return -1;
1785 return 0;
1786 }
1787 }
774e3047 1788
1789 /* Try LZF compression - under 20 bytes it's unable to compress even
88e85998 1790 * aaaaaaaaaaaaaaaaaa so skip it */
3a2694c4 1791 if (1 && len > 20) {
774e3047 1792 int retval;
1793
1794 retval = rdbSaveLzfStringObject(fp,obj);
1795 if (retval == -1) return -1;
1796 if (retval > 0) return 0;
1797 /* retval == 0 means data can't be compressed, save the old way */
1798 }
1799
1800 /* Store verbatim */
10c43610 1801 if (rdbSaveLen(fp,len) == -1) return -1;
1802 if (len && fwrite(obj->ptr,len,1,fp) == 0) return -1;
1803 return 0;
1804}
1805
ed9b544e 1806/* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
f78fd11b 1807static int rdbSave(char *filename) {
ed9b544e 1808 dictIterator *di = NULL;
1809 dictEntry *de;
ed9b544e 1810 FILE *fp;
1811 char tmpfile[256];
1812 int j;
bb32ede5 1813 time_t now = time(NULL);
ed9b544e 1814
1815 snprintf(tmpfile,256,"temp-%d.%ld.rdb",(int)time(NULL),(long int)random());
1816 fp = fopen(tmpfile,"w");
1817 if (!fp) {
1818 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
1819 return REDIS_ERR;
1820 }
f78fd11b 1821 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
ed9b544e 1822 for (j = 0; j < server.dbnum; j++) {
bb32ede5 1823 redisDb *db = server.db+j;
1824 dict *d = db->dict;
3305306f 1825 if (dictSize(d) == 0) continue;
ed9b544e 1826 di = dictGetIterator(d);
1827 if (!di) {
1828 fclose(fp);
1829 return REDIS_ERR;
1830 }
1831
1832 /* Write the SELECT DB opcode */
f78fd11b 1833 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
1834 if (rdbSaveLen(fp,j) == -1) goto werr;
ed9b544e 1835
1836 /* Iterate this DB writing every entry */
1837 while((de = dictNext(di)) != NULL) {
1838 robj *key = dictGetEntryKey(de);
1839 robj *o = dictGetEntryVal(de);
bb32ede5 1840 time_t expiretime = getExpire(db,key);
1841
1842 /* Save the expire time */
1843 if (expiretime != -1) {
1844 /* If this key is already expired skip it */
1845 if (expiretime < now) continue;
1846 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
1847 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
1848 }
1849 /* Save the key and associated value */
f78fd11b 1850 if (rdbSaveType(fp,o->type) == -1) goto werr;
10c43610 1851 if (rdbSaveStringObject(fp,key) == -1) goto werr;
f78fd11b 1852 if (o->type == REDIS_STRING) {
ed9b544e 1853 /* Save a string value */
10c43610 1854 if (rdbSaveStringObject(fp,o) == -1) goto werr;
f78fd11b 1855 } else if (o->type == REDIS_LIST) {
ed9b544e 1856 /* Save a list value */
1857 list *list = o->ptr;
6208b3a7 1858 listNode *ln;
ed9b544e 1859
6208b3a7 1860 listRewind(list);
f78fd11b 1861 if (rdbSaveLen(fp,listLength(list)) == -1) goto werr;
6208b3a7 1862 while((ln = listYield(list))) {
ed9b544e 1863 robj *eleobj = listNodeValue(ln);
f78fd11b 1864
10c43610 1865 if (rdbSaveStringObject(fp,eleobj) == -1) goto werr;
ed9b544e 1866 }
f78fd11b 1867 } else if (o->type == REDIS_SET) {
ed9b544e 1868 /* Save a set value */
1869 dict *set = o->ptr;
1870 dictIterator *di = dictGetIterator(set);
1871 dictEntry *de;
1872
1873 if (!set) oom("dictGetIteraotr");
3305306f 1874 if (rdbSaveLen(fp,dictSize(set)) == -1) goto werr;
ed9b544e 1875 while((de = dictNext(di)) != NULL) {
10c43610 1876 robj *eleobj = dictGetEntryKey(de);
ed9b544e 1877
10c43610 1878 if (rdbSaveStringObject(fp,eleobj) == -1) goto werr;
ed9b544e 1879 }
1880 dictReleaseIterator(di);
1881 } else {
1882 assert(0 != 0);
1883 }
1884 }
1885 dictReleaseIterator(di);
1886 }
1887 /* EOF opcode */
f78fd11b 1888 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
1889
1890 /* Make sure data will not remain on the OS's output buffers */
ed9b544e 1891 fflush(fp);
1892 fsync(fileno(fp));
1893 fclose(fp);
1894
1895 /* Use RENAME to make sure the DB file is changed atomically only
1896 * if the generate DB file is ok. */
1897 if (rename(tmpfile,filename) == -1) {
1898 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destionation: %s", strerror(errno));
1899 unlink(tmpfile);
1900 return REDIS_ERR;
1901 }
1902 redisLog(REDIS_NOTICE,"DB saved on disk");
1903 server.dirty = 0;
1904 server.lastsave = time(NULL);
1905 return REDIS_OK;
1906
1907werr:
1908 fclose(fp);
1909 unlink(tmpfile);
1910 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
1911 if (di) dictReleaseIterator(di);
1912 return REDIS_ERR;
1913}
1914
f78fd11b 1915static int rdbSaveBackground(char *filename) {
ed9b544e 1916 pid_t childpid;
1917
1918 if (server.bgsaveinprogress) return REDIS_ERR;
1919 if ((childpid = fork()) == 0) {
1920 /* Child */
1921 close(server.fd);
f78fd11b 1922 if (rdbSave(filename) == REDIS_OK) {
ed9b544e 1923 exit(0);
1924 } else {
1925 exit(1);
1926 }
1927 } else {
1928 /* Parent */
5a7c647e 1929 if (childpid == -1) {
1930 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
1931 strerror(errno));
1932 return REDIS_ERR;
1933 }
ed9b544e 1934 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
1935 server.bgsaveinprogress = 1;
1936 return REDIS_OK;
1937 }
1938 return REDIS_OK; /* unreached */
1939}
1940
f78fd11b 1941static int rdbLoadType(FILE *fp) {
1942 unsigned char type;
7b45bfb2 1943 if (fread(&type,1,1,fp) == 0) return -1;
1944 return type;
1945}
1946
bb32ede5 1947static time_t rdbLoadTime(FILE *fp) {
1948 int32_t t32;
1949 if (fread(&t32,4,1,fp) == 0) return -1;
1950 return (time_t) t32;
1951}
1952
e3566d4b 1953/* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
1954 * of this file for a description of how this are stored on disk.
1955 *
1956 * isencoded is set to 1 if the readed length is not actually a length but
1957 * an "encoding type", check the above comments for more info */
1958static uint32_t rdbLoadLen(FILE *fp, int rdbver, int *isencoded) {
f78fd11b 1959 unsigned char buf[2];
1960 uint32_t len;
1961
e3566d4b 1962 if (isencoded) *isencoded = 0;
f78fd11b 1963 if (rdbver == 0) {
1964 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
1965 return ntohl(len);
1966 } else {
17be1a4a 1967 int type;
1968
f78fd11b 1969 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
17be1a4a 1970 type = (buf[0]&0xC0)>>6;
1971 if (type == REDIS_RDB_6BITLEN) {
f78fd11b 1972 /* Read a 6 bit len */
e3566d4b 1973 return buf[0]&0x3F;
1974 } else if (type == REDIS_RDB_ENCVAL) {
1975 /* Read a 6 bit len encoding type */
1976 if (isencoded) *isencoded = 1;
1977 return buf[0]&0x3F;
17be1a4a 1978 } else if (type == REDIS_RDB_14BITLEN) {
f78fd11b 1979 /* Read a 14 bit len */
1980 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
1981 return ((buf[0]&0x3F)<<8)|buf[1];
1982 } else {
1983 /* Read a 32 bit len */
1984 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
1985 return ntohl(len);
1986 }
1987 }
f78fd11b 1988}
1989
e3566d4b 1990static robj *rdbLoadIntegerObject(FILE *fp, int enctype) {
1991 unsigned char enc[4];
1992 long long val;
1993
1994 if (enctype == REDIS_RDB_ENC_INT8) {
1995 if (fread(enc,1,1,fp) == 0) return NULL;
1996 val = (signed char)enc[0];
1997 } else if (enctype == REDIS_RDB_ENC_INT16) {
1998 uint16_t v;
1999 if (fread(enc,2,1,fp) == 0) return NULL;
2000 v = enc[0]|(enc[1]<<8);
2001 val = (int16_t)v;
2002 } else if (enctype == REDIS_RDB_ENC_INT32) {
2003 uint32_t v;
2004 if (fread(enc,4,1,fp) == 0) return NULL;
2005 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
2006 val = (int32_t)v;
2007 } else {
2008 val = 0; /* anti-warning */
2009 assert(0!=0);
2010 }
2011 return createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",val));
2012}
2013
88e85998 2014static robj *rdbLoadLzfStringObject(FILE*fp, int rdbver) {
2015 unsigned int len, clen;
2016 unsigned char *c = NULL;
2017 sds val = NULL;
2018
2019 if ((clen = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR) return NULL;
2020 if ((len = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR) return NULL;
2021 if ((c = zmalloc(clen)) == NULL) goto err;
2022 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
2023 if (fread(c,clen,1,fp) == 0) goto err;
2024 if (lzf_decompress(c,clen,val,len) == 0) goto err;
5109cdff 2025 zfree(c);
88e85998 2026 return createObject(REDIS_STRING,val);
2027err:
2028 zfree(c);
2029 sdsfree(val);
2030 return NULL;
2031}
2032
e3566d4b 2033static robj *rdbLoadStringObject(FILE*fp, int rdbver) {
2034 int isencoded;
2035 uint32_t len;
f78fd11b 2036 sds val;
2037
e3566d4b 2038 len = rdbLoadLen(fp,rdbver,&isencoded);
2039 if (isencoded) {
2040 switch(len) {
2041 case REDIS_RDB_ENC_INT8:
2042 case REDIS_RDB_ENC_INT16:
2043 case REDIS_RDB_ENC_INT32:
3305306f 2044 return tryObjectSharing(rdbLoadIntegerObject(fp,len));
88e85998 2045 case REDIS_RDB_ENC_LZF:
2046 return tryObjectSharing(rdbLoadLzfStringObject(fp,rdbver));
e3566d4b 2047 default:
2048 assert(0!=0);
2049 }
2050 }
2051
f78fd11b 2052 if (len == REDIS_RDB_LENERR) return NULL;
2053 val = sdsnewlen(NULL,len);
2054 if (len && fread(val,len,1,fp) == 0) {
2055 sdsfree(val);
2056 return NULL;
2057 }
10c43610 2058 return tryObjectSharing(createObject(REDIS_STRING,val));
f78fd11b 2059}
2060
2061static int rdbLoad(char *filename) {
ed9b544e 2062 FILE *fp;
f78fd11b 2063 robj *keyobj = NULL;
2064 uint32_t dbid;
bb32ede5 2065 int type, retval, rdbver;
3305306f 2066 dict *d = server.db[0].dict;
bb32ede5 2067 redisDb *db = server.db+0;
f78fd11b 2068 char buf[1024];
bb32ede5 2069 time_t expiretime = -1, now = time(NULL);
2070
ed9b544e 2071 fp = fopen(filename,"r");
2072 if (!fp) return REDIS_ERR;
2073 if (fread(buf,9,1,fp) == 0) goto eoferr;
f78fd11b 2074 buf[9] = '\0';
2075 if (memcmp(buf,"REDIS",5) != 0) {
ed9b544e 2076 fclose(fp);
2077 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
2078 return REDIS_ERR;
2079 }
f78fd11b 2080 rdbver = atoi(buf+5);
2081 if (rdbver > 1) {
2082 fclose(fp);
2083 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
2084 return REDIS_ERR;
2085 }
ed9b544e 2086 while(1) {
2087 robj *o;
2088
2089 /* Read type. */
f78fd11b 2090 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
bb32ede5 2091 if (type == REDIS_EXPIRETIME) {
2092 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
2093 /* We read the time so we need to read the object type again */
2094 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
2095 }
ed9b544e 2096 if (type == REDIS_EOF) break;
2097 /* Handle SELECT DB opcode as a special case */
2098 if (type == REDIS_SELECTDB) {
e3566d4b 2099 if ((dbid = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR)
2100 goto eoferr;
ed9b544e 2101 if (dbid >= (unsigned)server.dbnum) {
f78fd11b 2102 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
ed9b544e 2103 exit(1);
2104 }
bb32ede5 2105 db = server.db+dbid;
2106 d = db->dict;
ed9b544e 2107 continue;
2108 }
2109 /* Read key */
f78fd11b 2110 if ((keyobj = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
ed9b544e 2111
2112 if (type == REDIS_STRING) {
2113 /* Read string value */
f78fd11b 2114 if ((o = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
ed9b544e 2115 } else if (type == REDIS_LIST || type == REDIS_SET) {
2116 /* Read list/set value */
2117 uint32_t listlen;
f78fd11b 2118
e3566d4b 2119 if ((listlen = rdbLoadLen(fp,rdbver,NULL)) == REDIS_RDB_LENERR)
f78fd11b 2120 goto eoferr;
ed9b544e 2121 o = (type == REDIS_LIST) ? createListObject() : createSetObject();
2122 /* Load every single element of the list/set */
2123 while(listlen--) {
2124 robj *ele;
2125
f78fd11b 2126 if ((ele = rdbLoadStringObject(fp,rdbver)) == NULL) goto eoferr;
ed9b544e 2127 if (type == REDIS_LIST) {
2128 if (!listAddNodeTail((list*)o->ptr,ele))
2129 oom("listAddNodeTail");
2130 } else {
2131 if (dictAdd((dict*)o->ptr,ele,NULL) == DICT_ERR)
2132 oom("dictAdd");
2133 }
ed9b544e 2134 }
2135 } else {
2136 assert(0 != 0);
2137 }
2138 /* Add the new object in the hash table */
f78fd11b 2139 retval = dictAdd(d,keyobj,o);
ed9b544e 2140 if (retval == DICT_ERR) {
f78fd11b 2141 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", keyobj->ptr);
ed9b544e 2142 exit(1);
2143 }
bb32ede5 2144 /* Set the expire time if needed */
2145 if (expiretime != -1) {
2146 setExpire(db,keyobj,expiretime);
2147 /* Delete this key if already expired */
2148 if (expiretime < now) deleteKey(db,keyobj);
2149 expiretime = -1;
2150 }
f78fd11b 2151 keyobj = o = NULL;
ed9b544e 2152 }
2153 fclose(fp);
2154 return REDIS_OK;
2155
2156eoferr: /* unexpected end of file is handled here with a fatal exit */
e3566d4b 2157 if (keyobj) decrRefCount(keyobj);
2158 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, exiting now.");
ed9b544e 2159 exit(1);
2160 return REDIS_ERR; /* Just to avoid warning */
2161}
2162
2163/*================================== Commands =============================== */
2164
abcb223e 2165static void authCommand(redisClient *c) {
2e77c2ee 2166 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
abcb223e
BH
2167 c->authenticated = 1;
2168 addReply(c,shared.ok);
2169 } else {
2170 c->authenticated = 0;
2171 addReply(c,shared.err);
2172 }
2173}
2174
ed9b544e 2175static void pingCommand(redisClient *c) {
2176 addReply(c,shared.pong);
2177}
2178
2179static void echoCommand(redisClient *c) {
c937aa89 2180 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",
ed9b544e 2181 (int)sdslen(c->argv[1]->ptr)));
2182 addReply(c,c->argv[1]);
2183 addReply(c,shared.crlf);
2184}
2185
2186/*=================================== Strings =============================== */
2187
2188static void setGenericCommand(redisClient *c, int nx) {
2189 int retval;
2190
3305306f 2191 retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
ed9b544e 2192 if (retval == DICT_ERR) {
2193 if (!nx) {
3305306f 2194 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
ed9b544e 2195 incrRefCount(c->argv[2]);
2196 } else {
c937aa89 2197 addReply(c,shared.czero);
ed9b544e 2198 return;
2199 }
2200 } else {
2201 incrRefCount(c->argv[1]);
2202 incrRefCount(c->argv[2]);
2203 }
2204 server.dirty++;
3305306f 2205 removeExpire(c->db,c->argv[1]);
c937aa89 2206 addReply(c, nx ? shared.cone : shared.ok);
ed9b544e 2207}
2208
2209static void setCommand(redisClient *c) {
a4d1ba9a 2210 setGenericCommand(c,0);
ed9b544e 2211}
2212
2213static void setnxCommand(redisClient *c) {
a4d1ba9a 2214 setGenericCommand(c,1);
ed9b544e 2215}
2216
2217static void getCommand(redisClient *c) {
3305306f 2218 robj *o = lookupKeyRead(c->db,c->argv[1]);
2219
2220 if (o == NULL) {
c937aa89 2221 addReply(c,shared.nullbulk);
ed9b544e 2222 } else {
ed9b544e 2223 if (o->type != REDIS_STRING) {
c937aa89 2224 addReply(c,shared.wrongtypeerr);
ed9b544e 2225 } else {
c937aa89 2226 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o->ptr)));
ed9b544e 2227 addReply(c,o);
2228 addReply(c,shared.crlf);
2229 }
2230 }
2231}
2232
a431eb74 2233static void getSetCommand(redisClient *c) {
2234 getCommand(c);
2235 if (dictAdd(c->db->dict,c->argv[1],c->argv[2]) == DICT_ERR) {
2236 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
2237 } else {
2238 incrRefCount(c->argv[1]);
2239 }
2240 incrRefCount(c->argv[2]);
2241 server.dirty++;
2242 removeExpire(c->db,c->argv[1]);
2243}
2244
70003d28 2245static void mgetCommand(redisClient *c) {
70003d28 2246 int j;
2247
c937aa89 2248 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
70003d28 2249 for (j = 1; j < c->argc; j++) {
3305306f 2250 robj *o = lookupKeyRead(c->db,c->argv[j]);
2251 if (o == NULL) {
c937aa89 2252 addReply(c,shared.nullbulk);
70003d28 2253 } else {
70003d28 2254 if (o->type != REDIS_STRING) {
c937aa89 2255 addReply(c,shared.nullbulk);
70003d28 2256 } else {
c937aa89 2257 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(o->ptr)));
70003d28 2258 addReply(c,o);
2259 addReply(c,shared.crlf);
2260 }
2261 }
2262 }
2263}
2264
d68ed120 2265static void incrDecrCommand(redisClient *c, long long incr) {
ed9b544e 2266 long long value;
2267 int retval;
2268 robj *o;
2269
3305306f 2270 o = lookupKeyWrite(c->db,c->argv[1]);
2271 if (o == NULL) {
ed9b544e 2272 value = 0;
2273 } else {
ed9b544e 2274 if (o->type != REDIS_STRING) {
2275 value = 0;
2276 } else {
2277 char *eptr;
2278
2279 value = strtoll(o->ptr, &eptr, 10);
2280 }
2281 }
2282
2283 value += incr;
2284 o = createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",value));
3305306f 2285 retval = dictAdd(c->db->dict,c->argv[1],o);
ed9b544e 2286 if (retval == DICT_ERR) {
3305306f 2287 dictReplace(c->db->dict,c->argv[1],o);
2288 removeExpire(c->db,c->argv[1]);
ed9b544e 2289 } else {
2290 incrRefCount(c->argv[1]);
2291 }
2292 server.dirty++;
c937aa89 2293 addReply(c,shared.colon);
ed9b544e 2294 addReply(c,o);
2295 addReply(c,shared.crlf);
2296}
2297
2298static void incrCommand(redisClient *c) {
a4d1ba9a 2299 incrDecrCommand(c,1);
ed9b544e 2300}
2301
2302static void decrCommand(redisClient *c) {
a4d1ba9a 2303 incrDecrCommand(c,-1);
ed9b544e 2304}
2305
2306static void incrbyCommand(redisClient *c) {
d68ed120 2307 long long incr = strtoll(c->argv[2]->ptr, NULL, 10);
a4d1ba9a 2308 incrDecrCommand(c,incr);
ed9b544e 2309}
2310
2311static void decrbyCommand(redisClient *c) {
d68ed120 2312 long long incr = strtoll(c->argv[2]->ptr, NULL, 10);
a4d1ba9a 2313 incrDecrCommand(c,-incr);
ed9b544e 2314}
2315
2316/* ========================= Type agnostic commands ========================= */
2317
2318static void delCommand(redisClient *c) {
5109cdff 2319 int deleted = 0, j;
2320
2321 for (j = 1; j < c->argc; j++) {
2322 if (deleteKey(c->db,c->argv[j])) {
2323 server.dirty++;
2324 deleted++;
2325 }
2326 }
2327 switch(deleted) {
2328 case 0:
c937aa89 2329 addReply(c,shared.czero);
5109cdff 2330 break;
2331 case 1:
2332 addReply(c,shared.cone);
2333 break;
2334 default:
2335 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",deleted));
2336 break;
ed9b544e 2337 }
2338}
2339
2340static void existsCommand(redisClient *c) {
3305306f 2341 addReply(c,lookupKeyRead(c->db,c->argv[1]) ? shared.cone : shared.czero);
ed9b544e 2342}
2343
2344static void selectCommand(redisClient *c) {
2345 int id = atoi(c->argv[1]->ptr);
2346
2347 if (selectDb(c,id) == REDIS_ERR) {
774e3047 2348 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
ed9b544e 2349 } else {
2350 addReply(c,shared.ok);
2351 }
2352}
2353
2354static void randomkeyCommand(redisClient *c) {
2355 dictEntry *de;
3305306f 2356
2357 while(1) {
2358 de = dictGetRandomKey(c->db->dict);
ce7bef07 2359 if (!de || expireIfNeeded(c->db,dictGetEntryKey(de)) == 0) break;
3305306f 2360 }
ed9b544e 2361 if (de == NULL) {
ce7bef07 2362 addReply(c,shared.plus);
ed9b544e 2363 addReply(c,shared.crlf);
2364 } else {
c937aa89 2365 addReply(c,shared.plus);
ed9b544e 2366 addReply(c,dictGetEntryKey(de));
2367 addReply(c,shared.crlf);
2368 }
2369}
2370
2371static void keysCommand(redisClient *c) {
2372 dictIterator *di;
2373 dictEntry *de;
2374 sds pattern = c->argv[1]->ptr;
2375 int plen = sdslen(pattern);
2376 int numkeys = 0, keyslen = 0;
2377 robj *lenobj = createObject(REDIS_STRING,NULL);
2378
3305306f 2379 di = dictGetIterator(c->db->dict);
ed9b544e 2380 if (!di) oom("dictGetIterator");
2381 addReply(c,lenobj);
2382 decrRefCount(lenobj);
2383 while((de = dictNext(di)) != NULL) {
2384 robj *keyobj = dictGetEntryKey(de);
3305306f 2385
ed9b544e 2386 sds key = keyobj->ptr;
2387 if ((pattern[0] == '*' && pattern[1] == '\0') ||
2388 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
3305306f 2389 if (expireIfNeeded(c->db,keyobj) == 0) {
2390 if (numkeys != 0)
2391 addReply(c,shared.space);
2392 addReply(c,keyobj);
2393 numkeys++;
2394 keyslen += sdslen(key);
2395 }
ed9b544e 2396 }
2397 }
2398 dictReleaseIterator(di);
c937aa89 2399 lenobj->ptr = sdscatprintf(sdsempty(),"$%lu\r\n",keyslen+(numkeys ? (numkeys-1) : 0));
ed9b544e 2400 addReply(c,shared.crlf);
2401}
2402
2403static void dbsizeCommand(redisClient *c) {
2404 addReplySds(c,
3305306f 2405 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
ed9b544e 2406}
2407
2408static void lastsaveCommand(redisClient *c) {
2409 addReplySds(c,
c937aa89 2410 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
ed9b544e 2411}
2412
2413static void typeCommand(redisClient *c) {
3305306f 2414 robj *o;
ed9b544e 2415 char *type;
3305306f 2416
2417 o = lookupKeyRead(c->db,c->argv[1]);
2418 if (o == NULL) {
c937aa89 2419 type = "+none";
ed9b544e 2420 } else {
ed9b544e 2421 switch(o->type) {
c937aa89 2422 case REDIS_STRING: type = "+string"; break;
2423 case REDIS_LIST: type = "+list"; break;
2424 case REDIS_SET: type = "+set"; break;
ed9b544e 2425 default: type = "unknown"; break;
2426 }
2427 }
2428 addReplySds(c,sdsnew(type));
2429 addReply(c,shared.crlf);
2430}
2431
2432static void saveCommand(redisClient *c) {
05557f6d 2433 if (server.bgsaveinprogress) {
2434 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
2435 return;
2436 }
f78fd11b 2437 if (rdbSave(server.dbfilename) == REDIS_OK) {
ed9b544e 2438 addReply(c,shared.ok);
2439 } else {
2440 addReply(c,shared.err);
2441 }
2442}
2443
2444static void bgsaveCommand(redisClient *c) {
2445 if (server.bgsaveinprogress) {
2446 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
2447 return;
2448 }
f78fd11b 2449 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
ed9b544e 2450 addReply(c,shared.ok);
2451 } else {
2452 addReply(c,shared.err);
2453 }
2454}
2455
2456static void shutdownCommand(redisClient *c) {
2457 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
6208b3a7 2458 /* XXX: TODO kill the child if there is a bgsave in progress */
f78fd11b 2459 if (rdbSave(server.dbfilename) == REDIS_OK) {
ed329fcf 2460 if (server.daemonize) {
b284af55 2461 unlink(server.pidfile);
ed329fcf 2462 }
b284af55 2463 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
ed9b544e 2464 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
2465 exit(1);
2466 } else {
2467 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
2468 addReplySds(c,sdsnew("-ERR can't quit, problems saving the DB\r\n"));
2469 }
2470}
2471
2472static void renameGenericCommand(redisClient *c, int nx) {
ed9b544e 2473 robj *o;
2474
2475 /* To use the same key as src and dst is probably an error */
2476 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
c937aa89 2477 addReply(c,shared.sameobjecterr);
ed9b544e 2478 return;
2479 }
2480
3305306f 2481 o = lookupKeyWrite(c->db,c->argv[1]);
2482 if (o == NULL) {
c937aa89 2483 addReply(c,shared.nokeyerr);
ed9b544e 2484 return;
2485 }
ed9b544e 2486 incrRefCount(o);
3305306f 2487 deleteIfVolatile(c->db,c->argv[2]);
2488 if (dictAdd(c->db->dict,c->argv[2],o) == DICT_ERR) {
ed9b544e 2489 if (nx) {
2490 decrRefCount(o);
c937aa89 2491 addReply(c,shared.czero);
ed9b544e 2492 return;
2493 }
3305306f 2494 dictReplace(c->db->dict,c->argv[2],o);
ed9b544e 2495 } else {
2496 incrRefCount(c->argv[2]);
2497 }
3305306f 2498 deleteKey(c->db,c->argv[1]);
ed9b544e 2499 server.dirty++;
c937aa89 2500 addReply(c,nx ? shared.cone : shared.ok);
ed9b544e 2501}
2502
2503static void renameCommand(redisClient *c) {
2504 renameGenericCommand(c,0);
2505}
2506
2507static void renamenxCommand(redisClient *c) {
2508 renameGenericCommand(c,1);
2509}
2510
2511static void moveCommand(redisClient *c) {
3305306f 2512 robj *o;
2513 redisDb *src, *dst;
ed9b544e 2514 int srcid;
2515
2516 /* Obtain source and target DB pointers */
3305306f 2517 src = c->db;
2518 srcid = c->db->id;
ed9b544e 2519 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
c937aa89 2520 addReply(c,shared.outofrangeerr);
ed9b544e 2521 return;
2522 }
3305306f 2523 dst = c->db;
2524 selectDb(c,srcid); /* Back to the source DB */
ed9b544e 2525
2526 /* If the user is moving using as target the same
2527 * DB as the source DB it is probably an error. */
2528 if (src == dst) {
c937aa89 2529 addReply(c,shared.sameobjecterr);
ed9b544e 2530 return;
2531 }
2532
2533 /* Check if the element exists and get a reference */
3305306f 2534 o = lookupKeyWrite(c->db,c->argv[1]);
2535 if (!o) {
c937aa89 2536 addReply(c,shared.czero);
ed9b544e 2537 return;
2538 }
2539
2540 /* Try to add the element to the target DB */
3305306f 2541 deleteIfVolatile(dst,c->argv[1]);
2542 if (dictAdd(dst->dict,c->argv[1],o) == DICT_ERR) {
c937aa89 2543 addReply(c,shared.czero);
ed9b544e 2544 return;
2545 }
3305306f 2546 incrRefCount(c->argv[1]);
ed9b544e 2547 incrRefCount(o);
2548
2549 /* OK! key moved, free the entry in the source DB */
3305306f 2550 deleteKey(src,c->argv[1]);
ed9b544e 2551 server.dirty++;
c937aa89 2552 addReply(c,shared.cone);
ed9b544e 2553}
2554
2555/* =================================== Lists ================================ */
2556static void pushGenericCommand(redisClient *c, int where) {
2557 robj *lobj;
ed9b544e 2558 list *list;
3305306f 2559
2560 lobj = lookupKeyWrite(c->db,c->argv[1]);
2561 if (lobj == NULL) {
ed9b544e 2562 lobj = createListObject();
2563 list = lobj->ptr;
2564 if (where == REDIS_HEAD) {
2565 if (!listAddNodeHead(list,c->argv[2])) oom("listAddNodeHead");
2566 } else {
2567 if (!listAddNodeTail(list,c->argv[2])) oom("listAddNodeTail");
2568 }
3305306f 2569 dictAdd(c->db->dict,c->argv[1],lobj);
ed9b544e 2570 incrRefCount(c->argv[1]);
2571 incrRefCount(c->argv[2]);
2572 } else {
ed9b544e 2573 if (lobj->type != REDIS_LIST) {
2574 addReply(c,shared.wrongtypeerr);
2575 return;
2576 }
2577 list = lobj->ptr;
2578 if (where == REDIS_HEAD) {
2579 if (!listAddNodeHead(list,c->argv[2])) oom("listAddNodeHead");
2580 } else {
2581 if (!listAddNodeTail(list,c->argv[2])) oom("listAddNodeTail");
2582 }
2583 incrRefCount(c->argv[2]);
2584 }
2585 server.dirty++;
2586 addReply(c,shared.ok);
2587}
2588
2589static void lpushCommand(redisClient *c) {
2590 pushGenericCommand(c,REDIS_HEAD);
2591}
2592
2593static void rpushCommand(redisClient *c) {
2594 pushGenericCommand(c,REDIS_TAIL);
2595}
2596
2597static void llenCommand(redisClient *c) {
3305306f 2598 robj *o;
ed9b544e 2599 list *l;
2600
3305306f 2601 o = lookupKeyRead(c->db,c->argv[1]);
2602 if (o == NULL) {
c937aa89 2603 addReply(c,shared.czero);
ed9b544e 2604 return;
2605 } else {
ed9b544e 2606 if (o->type != REDIS_LIST) {
c937aa89 2607 addReply(c,shared.wrongtypeerr);
ed9b544e 2608 } else {
2609 l = o->ptr;
c937aa89 2610 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",listLength(l)));
ed9b544e 2611 }
2612 }
2613}
2614
2615static void lindexCommand(redisClient *c) {
3305306f 2616 robj *o;
ed9b544e 2617 int index = atoi(c->argv[2]->ptr);
2618
3305306f 2619 o = lookupKeyRead(c->db,c->argv[1]);
2620 if (o == NULL) {
c937aa89 2621 addReply(c,shared.nullbulk);
ed9b544e 2622 } else {
ed9b544e 2623 if (o->type != REDIS_LIST) {
c937aa89 2624 addReply(c,shared.wrongtypeerr);
ed9b544e 2625 } else {
2626 list *list = o->ptr;
2627 listNode *ln;
2628
2629 ln = listIndex(list, index);
2630 if (ln == NULL) {
c937aa89 2631 addReply(c,shared.nullbulk);
ed9b544e 2632 } else {
2633 robj *ele = listNodeValue(ln);
c937aa89 2634 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele->ptr)));
ed9b544e 2635 addReply(c,ele);
2636 addReply(c,shared.crlf);
2637 }
2638 }
2639 }
2640}
2641
2642static void lsetCommand(redisClient *c) {
3305306f 2643 robj *o;
ed9b544e 2644 int index = atoi(c->argv[2]->ptr);
2645
3305306f 2646 o = lookupKeyWrite(c->db,c->argv[1]);
2647 if (o == NULL) {
ed9b544e 2648 addReply(c,shared.nokeyerr);
2649 } else {
ed9b544e 2650 if (o->type != REDIS_LIST) {
2651 addReply(c,shared.wrongtypeerr);
2652 } else {
2653 list *list = o->ptr;
2654 listNode *ln;
2655
2656 ln = listIndex(list, index);
2657 if (ln == NULL) {
c937aa89 2658 addReply(c,shared.outofrangeerr);
ed9b544e 2659 } else {
2660 robj *ele = listNodeValue(ln);
2661
2662 decrRefCount(ele);
2663 listNodeValue(ln) = c->argv[3];
2664 incrRefCount(c->argv[3]);
2665 addReply(c,shared.ok);
2666 server.dirty++;
2667 }
2668 }
2669 }
2670}
2671
2672static void popGenericCommand(redisClient *c, int where) {
3305306f 2673 robj *o;
2674
2675 o = lookupKeyWrite(c->db,c->argv[1]);
2676 if (o == NULL) {
c937aa89 2677 addReply(c,shared.nullbulk);
ed9b544e 2678 } else {
ed9b544e 2679 if (o->type != REDIS_LIST) {
c937aa89 2680 addReply(c,shared.wrongtypeerr);
ed9b544e 2681 } else {
2682 list *list = o->ptr;
2683 listNode *ln;
2684
2685 if (where == REDIS_HEAD)
2686 ln = listFirst(list);
2687 else
2688 ln = listLast(list);
2689
2690 if (ln == NULL) {
c937aa89 2691 addReply(c,shared.nullbulk);
ed9b544e 2692 } else {
2693 robj *ele = listNodeValue(ln);
c937aa89 2694 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele->ptr)));
ed9b544e 2695 addReply(c,ele);
2696 addReply(c,shared.crlf);
2697 listDelNode(list,ln);
2698 server.dirty++;
2699 }
2700 }
2701 }
2702}
2703
2704static void lpopCommand(redisClient *c) {
2705 popGenericCommand(c,REDIS_HEAD);
2706}
2707
2708static void rpopCommand(redisClient *c) {
2709 popGenericCommand(c,REDIS_TAIL);
2710}
2711
2712static void lrangeCommand(redisClient *c) {
3305306f 2713 robj *o;
ed9b544e 2714 int start = atoi(c->argv[2]->ptr);
2715 int end = atoi(c->argv[3]->ptr);
3305306f 2716
2717 o = lookupKeyRead(c->db,c->argv[1]);
2718 if (o == NULL) {
c937aa89 2719 addReply(c,shared.nullmultibulk);
ed9b544e 2720 } else {
ed9b544e 2721 if (o->type != REDIS_LIST) {
c937aa89 2722 addReply(c,shared.wrongtypeerr);
ed9b544e 2723 } else {
2724 list *list = o->ptr;
2725 listNode *ln;
2726 int llen = listLength(list);
2727 int rangelen, j;
2728 robj *ele;
2729
2730 /* convert negative indexes */
2731 if (start < 0) start = llen+start;
2732 if (end < 0) end = llen+end;
2733 if (start < 0) start = 0;
2734 if (end < 0) end = 0;
2735
2736 /* indexes sanity checks */
2737 if (start > end || start >= llen) {
2738 /* Out of range start or start > end result in empty list */
c937aa89 2739 addReply(c,shared.emptymultibulk);
ed9b544e 2740 return;
2741 }
2742 if (end >= llen) end = llen-1;
2743 rangelen = (end-start)+1;
2744
2745 /* Return the result in form of a multi-bulk reply */
2746 ln = listIndex(list, start);
c937aa89 2747 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
ed9b544e 2748 for (j = 0; j < rangelen; j++) {
2749 ele = listNodeValue(ln);
c937aa89 2750 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",(int)sdslen(ele->ptr)));
ed9b544e 2751 addReply(c,ele);
2752 addReply(c,shared.crlf);
2753 ln = ln->next;
2754 }
2755 }
2756 }
2757}
2758
2759static void ltrimCommand(redisClient *c) {
3305306f 2760 robj *o;
ed9b544e 2761 int start = atoi(c->argv[2]->ptr);
2762 int end = atoi(c->argv[3]->ptr);
2763
3305306f 2764 o = lookupKeyWrite(c->db,c->argv[1]);
2765 if (o == NULL) {
ed9b544e 2766 addReply(c,shared.nokeyerr);
2767 } else {
ed9b544e 2768 if (o->type != REDIS_LIST) {
2769 addReply(c,shared.wrongtypeerr);
2770 } else {
2771 list *list = o->ptr;
2772 listNode *ln;
2773 int llen = listLength(list);
2774 int j, ltrim, rtrim;
2775
2776 /* convert negative indexes */
2777 if (start < 0) start = llen+start;
2778 if (end < 0) end = llen+end;
2779 if (start < 0) start = 0;
2780 if (end < 0) end = 0;
2781
2782 /* indexes sanity checks */
2783 if (start > end || start >= llen) {
2784 /* Out of range start or start > end result in empty list */
2785 ltrim = llen;
2786 rtrim = 0;
2787 } else {
2788 if (end >= llen) end = llen-1;
2789 ltrim = start;
2790 rtrim = llen-end-1;
2791 }
2792
2793 /* Remove list elements to perform the trim */
2794 for (j = 0; j < ltrim; j++) {
2795 ln = listFirst(list);
2796 listDelNode(list,ln);
2797 }
2798 for (j = 0; j < rtrim; j++) {
2799 ln = listLast(list);
2800 listDelNode(list,ln);
2801 }
2802 addReply(c,shared.ok);
2803 server.dirty++;
2804 }
2805 }
2806}
2807
2808static void lremCommand(redisClient *c) {
3305306f 2809 robj *o;
ed9b544e 2810
3305306f 2811 o = lookupKeyWrite(c->db,c->argv[1]);
2812 if (o == NULL) {
7b45bfb2 2813 addReply(c,shared.nokeyerr);
ed9b544e 2814 } else {
ed9b544e 2815 if (o->type != REDIS_LIST) {
c937aa89 2816 addReply(c,shared.wrongtypeerr);
ed9b544e 2817 } else {
2818 list *list = o->ptr;
2819 listNode *ln, *next;
2820 int toremove = atoi(c->argv[2]->ptr);
2821 int removed = 0;
2822 int fromtail = 0;
2823
2824 if (toremove < 0) {
2825 toremove = -toremove;
2826 fromtail = 1;
2827 }
2828 ln = fromtail ? list->tail : list->head;
2829 while (ln) {
ed9b544e 2830 robj *ele = listNodeValue(ln);
a4d1ba9a 2831
2832 next = fromtail ? ln->prev : ln->next;
ed9b544e 2833 if (sdscmp(ele->ptr,c->argv[3]->ptr) == 0) {
2834 listDelNode(list,ln);
2835 server.dirty++;
2836 removed++;
2837 if (toremove && removed == toremove) break;
2838 }
2839 ln = next;
2840 }
c937aa89 2841 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
ed9b544e 2842 }
2843 }
2844}
2845
2846/* ==================================== Sets ================================ */
2847
2848static void saddCommand(redisClient *c) {
ed9b544e 2849 robj *set;
2850
3305306f 2851 set = lookupKeyWrite(c->db,c->argv[1]);
2852 if (set == NULL) {
ed9b544e 2853 set = createSetObject();
3305306f 2854 dictAdd(c->db->dict,c->argv[1],set);
ed9b544e 2855 incrRefCount(c->argv[1]);
2856 } else {
ed9b544e 2857 if (set->type != REDIS_SET) {
c937aa89 2858 addReply(c,shared.wrongtypeerr);
ed9b544e 2859 return;
2860 }
2861 }
2862 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
2863 incrRefCount(c->argv[2]);
2864 server.dirty++;
c937aa89 2865 addReply(c,shared.cone);
ed9b544e 2866 } else {
c937aa89 2867 addReply(c,shared.czero);
ed9b544e 2868 }
2869}
2870
2871static void sremCommand(redisClient *c) {
3305306f 2872 robj *set;
ed9b544e 2873
3305306f 2874 set = lookupKeyWrite(c->db,c->argv[1]);
2875 if (set == NULL) {
c937aa89 2876 addReply(c,shared.czero);
ed9b544e 2877 } else {
ed9b544e 2878 if (set->type != REDIS_SET) {
c937aa89 2879 addReply(c,shared.wrongtypeerr);
ed9b544e 2880 return;
2881 }
2882 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
2883 server.dirty++;
c937aa89 2884 addReply(c,shared.cone);
ed9b544e 2885 } else {
c937aa89 2886 addReply(c,shared.czero);
ed9b544e 2887 }
2888 }
2889}
2890
a4460ef4 2891static void smoveCommand(redisClient *c) {
2892 robj *srcset, *dstset;
2893
2894 srcset = lookupKeyWrite(c->db,c->argv[1]);
2895 dstset = lookupKeyWrite(c->db,c->argv[2]);
2896
2897 /* If the source key does not exist return 0, if it's of the wrong type
2898 * raise an error */
2899 if (srcset == NULL || srcset->type != REDIS_SET) {
2900 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
2901 return;
2902 }
2903 /* Error if the destination key is not a set as well */
2904 if (dstset && dstset->type != REDIS_SET) {
2905 addReply(c,shared.wrongtypeerr);
2906 return;
2907 }
2908 /* Remove the element from the source set */
2909 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
2910 /* Key not found in the src set! return zero */
2911 addReply(c,shared.czero);
2912 return;
2913 }
2914 server.dirty++;
2915 /* Add the element to the destination set */
2916 if (!dstset) {
2917 dstset = createSetObject();
2918 dictAdd(c->db->dict,c->argv[2],dstset);
2919 incrRefCount(c->argv[2]);
2920 }
2921 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
2922 incrRefCount(c->argv[3]);
2923 addReply(c,shared.cone);
2924}
2925
ed9b544e 2926static void sismemberCommand(redisClient *c) {
3305306f 2927 robj *set;
ed9b544e 2928
3305306f 2929 set = lookupKeyRead(c->db,c->argv[1]);
2930 if (set == NULL) {
c937aa89 2931 addReply(c,shared.czero);
ed9b544e 2932 } else {
ed9b544e 2933 if (set->type != REDIS_SET) {
c937aa89 2934 addReply(c,shared.wrongtypeerr);
ed9b544e 2935 return;
2936 }
2937 if (dictFind(set->ptr,c->argv[2]))
c937aa89 2938 addReply(c,shared.cone);
ed9b544e 2939 else
c937aa89 2940 addReply(c,shared.czero);
ed9b544e 2941 }
2942}
2943
2944static void scardCommand(redisClient *c) {
3305306f 2945 robj *o;
ed9b544e 2946 dict *s;
2947
3305306f 2948 o = lookupKeyRead(c->db,c->argv[1]);
2949 if (o == NULL) {
c937aa89 2950 addReply(c,shared.czero);
ed9b544e 2951 return;
2952 } else {
ed9b544e 2953 if (o->type != REDIS_SET) {
c937aa89 2954 addReply(c,shared.wrongtypeerr);
ed9b544e 2955 } else {
2956 s = o->ptr;
c937aa89 2957 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",
3305306f 2958 dictSize(s)));
ed9b544e 2959 }
2960 }
2961}
2962
2963static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
2964 dict **d1 = (void*) s1, **d2 = (void*) s2;
2965
3305306f 2966 return dictSize(*d1)-dictSize(*d2);
ed9b544e 2967}
2968
2969static void sinterGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey) {
2970 dict **dv = zmalloc(sizeof(dict*)*setsnum);
2971 dictIterator *di;
2972 dictEntry *de;
2973 robj *lenobj = NULL, *dstset = NULL;
2974 int j, cardinality = 0;
2975
3ba37089 2976 if (!dv) oom("sinterGenericCommand");
ed9b544e 2977 for (j = 0; j < setsnum; j++) {
2978 robj *setobj;
3305306f 2979
2980 setobj = dstkey ?
2981 lookupKeyWrite(c->db,setskeys[j]) :
2982 lookupKeyRead(c->db,setskeys[j]);
2983 if (!setobj) {
ed9b544e 2984 zfree(dv);
5faa6025 2985 if (dstkey) {
2986 deleteKey(c->db,dstkey);
2987 addReply(c,shared.ok);
2988 } else {
2989 addReply(c,shared.nullmultibulk);
2990 }
ed9b544e 2991 return;
2992 }
ed9b544e 2993 if (setobj->type != REDIS_SET) {
2994 zfree(dv);
c937aa89 2995 addReply(c,shared.wrongtypeerr);
ed9b544e 2996 return;
2997 }
2998 dv[j] = setobj->ptr;
2999 }
3000 /* Sort sets from the smallest to largest, this will improve our
3001 * algorithm's performace */
3002 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
3003
3004 /* The first thing we should output is the total number of elements...
3005 * since this is a multi-bulk write, but at this stage we don't know
3006 * the intersection set size, so we use a trick, append an empty object
3007 * to the output list and save the pointer to later modify it with the
3008 * right length */
3009 if (!dstkey) {
3010 lenobj = createObject(REDIS_STRING,NULL);
3011 addReply(c,lenobj);
3012 decrRefCount(lenobj);
3013 } else {
3014 /* If we have a target key where to store the resulting set
3015 * create this key with an empty set inside */
3016 dstset = createSetObject();
ed9b544e 3017 }
3018
3019 /* Iterate all the elements of the first (smallest) set, and test
3020 * the element against all the other sets, if at least one set does
3021 * not include the element it is discarded */
3022 di = dictGetIterator(dv[0]);
3023 if (!di) oom("dictGetIterator");
3024
3025 while((de = dictNext(di)) != NULL) {
3026 robj *ele;
3027
3028 for (j = 1; j < setsnum; j++)
3029 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
3030 if (j != setsnum)
3031 continue; /* at least one set does not contain the member */
3032 ele = dictGetEntryKey(de);
3033 if (!dstkey) {
c937aa89 3034 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(ele->ptr)));
ed9b544e 3035 addReply(c,ele);
3036 addReply(c,shared.crlf);
3037 cardinality++;
3038 } else {
3039 dictAdd(dstset->ptr,ele,NULL);
3040 incrRefCount(ele);
3041 }
3042 }
3043 dictReleaseIterator(di);
3044
83cdfe18
AG
3045 if (dstkey) {
3046 /* Store the resulting set into the target */
3047 deleteKey(c->db,dstkey);
3048 dictAdd(c->db->dict,dstkey,dstset);
3049 incrRefCount(dstkey);
3050 }
3051
40d224a9 3052 if (!dstkey) {
c937aa89 3053 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",cardinality);
40d224a9 3054 } else {
ed9b544e 3055 addReply(c,shared.ok);
40d224a9 3056 server.dirty++;
3057 }
ed9b544e 3058 zfree(dv);
3059}
3060
3061static void sinterCommand(redisClient *c) {
3062 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
3063}
3064
3065static void sinterstoreCommand(redisClient *c) {
3066 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
3067}
3068
f4f56e1d 3069#define REDIS_OP_UNION 0
3070#define REDIS_OP_DIFF 1
3071
3072static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
40d224a9 3073 dict **dv = zmalloc(sizeof(dict*)*setsnum);
3074 dictIterator *di;
3075 dictEntry *de;
f4f56e1d 3076 robj *dstset = NULL;
40d224a9 3077 int j, cardinality = 0;
3078
f4f56e1d 3079 if (!dv) oom("sunionDiffGenericCommand");
40d224a9 3080 for (j = 0; j < setsnum; j++) {
3081 robj *setobj;
3082
3083 setobj = dstkey ?
3084 lookupKeyWrite(c->db,setskeys[j]) :
3085 lookupKeyRead(c->db,setskeys[j]);
3086 if (!setobj) {
3087 dv[j] = NULL;
3088 continue;
3089 }
3090 if (setobj->type != REDIS_SET) {
3091 zfree(dv);
3092 addReply(c,shared.wrongtypeerr);
3093 return;
3094 }
3095 dv[j] = setobj->ptr;
3096 }
3097
3098 /* We need a temp set object to store our union. If the dstkey
3099 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
3100 * this set object will be the resulting object to set into the target key*/
3101 dstset = createSetObject();
3102
40d224a9 3103 /* Iterate all the elements of all the sets, add every element a single
3104 * time to the result set */
3105 for (j = 0; j < setsnum; j++) {
51829ed3 3106 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
40d224a9 3107 if (!dv[j]) continue; /* non existing keys are like empty sets */
3108
3109 di = dictGetIterator(dv[j]);
3110 if (!di) oom("dictGetIterator");
3111
3112 while((de = dictNext(di)) != NULL) {
3113 robj *ele;
3114
3115 /* dictAdd will not add the same element multiple times */
3116 ele = dictGetEntryKey(de);
f4f56e1d 3117 if (op == REDIS_OP_UNION || j == 0) {
3118 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
3119 incrRefCount(ele);
40d224a9 3120 cardinality++;
3121 }
f4f56e1d 3122 } else if (op == REDIS_OP_DIFF) {
3123 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
3124 cardinality--;
3125 }
40d224a9 3126 }
3127 }
3128 dictReleaseIterator(di);
51829ed3
AG
3129
3130 if (op == REDIS_OP_DIFF && cardinality == 0) break; /* result set is empty */
40d224a9 3131 }
3132
f4f56e1d 3133 /* Output the content of the resulting set, if not in STORE mode */
3134 if (!dstkey) {
3135 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
3136 di = dictGetIterator(dstset->ptr);
3137 if (!di) oom("dictGetIterator");
3138 while((de = dictNext(di)) != NULL) {
3139 robj *ele;
3140
3141 ele = dictGetEntryKey(de);
3142 addReplySds(c,sdscatprintf(sdsempty(),
3143 "$%d\r\n",sdslen(ele->ptr)));
3144 addReply(c,ele);
3145 addReply(c,shared.crlf);
3146 }
3147 dictReleaseIterator(di);
83cdfe18
AG
3148 } else {
3149 /* If we have a target key where to store the resulting set
3150 * create this key with the result set inside */
3151 deleteKey(c->db,dstkey);
3152 dictAdd(c->db->dict,dstkey,dstset);
3153 incrRefCount(dstkey);
f4f56e1d 3154 }
3155
3156 /* Cleanup */
40d224a9 3157 if (!dstkey) {
40d224a9 3158 decrRefCount(dstset);
3159 } else {
3160 addReply(c,shared.ok);
3161 server.dirty++;
3162 }
3163 zfree(dv);
3164}
3165
3166static void sunionCommand(redisClient *c) {
f4f56e1d 3167 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
40d224a9 3168}
3169
3170static void sunionstoreCommand(redisClient *c) {
f4f56e1d 3171 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
3172}
3173
3174static void sdiffCommand(redisClient *c) {
3175 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
3176}
3177
3178static void sdiffstoreCommand(redisClient *c) {
3179 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
40d224a9 3180}
3181
ed9b544e 3182static void flushdbCommand(redisClient *c) {
ca37e9cd 3183 server.dirty += dictSize(c->db->dict);
3305306f 3184 dictEmpty(c->db->dict);
3185 dictEmpty(c->db->expires);
ed9b544e 3186 addReply(c,shared.ok);
ed9b544e 3187}
3188
3189static void flushallCommand(redisClient *c) {
ca37e9cd 3190 server.dirty += emptyDb();
ed9b544e 3191 addReply(c,shared.ok);
f78fd11b 3192 rdbSave(server.dbfilename);
ca37e9cd 3193 server.dirty++;
ed9b544e 3194}
3195
3196redisSortOperation *createSortOperation(int type, robj *pattern) {
3197 redisSortOperation *so = zmalloc(sizeof(*so));
3198 if (!so) oom("createSortOperation");
3199 so->type = type;
3200 so->pattern = pattern;
3201 return so;
3202}
3203
3204/* Return the value associated to the key with a name obtained
3205 * substituting the first occurence of '*' in 'pattern' with 'subst' */
3305306f 3206robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
ed9b544e 3207 char *p;
3208 sds spat, ssub;
3209 robj keyobj;
3210 int prefixlen, sublen, postfixlen;
ed9b544e 3211 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
3212 struct {
3213 long len;
3214 long free;
3215 char buf[REDIS_SORTKEY_MAX+1];
3216 } keyname;
3217
ed9b544e 3218 spat = pattern->ptr;
3219 ssub = subst->ptr;
3220 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
3221 p = strchr(spat,'*');
3222 if (!p) return NULL;
3223
3224 prefixlen = p-spat;
3225 sublen = sdslen(ssub);
3226 postfixlen = sdslen(spat)-(prefixlen+1);
3227 memcpy(keyname.buf,spat,prefixlen);
3228 memcpy(keyname.buf+prefixlen,ssub,sublen);
3229 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
3230 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
3231 keyname.len = prefixlen+sublen+postfixlen;
3232
3233 keyobj.refcount = 1;
3234 keyobj.type = REDIS_STRING;
3235 keyobj.ptr = ((char*)&keyname)+(sizeof(long)*2);
3236
a4d1ba9a 3237 /* printf("lookup '%s' => %p\n", keyname.buf,de); */
3305306f 3238 return lookupKeyRead(db,&keyobj);
ed9b544e 3239}
3240
3241/* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
3242 * the additional parameter is not standard but a BSD-specific we have to
3243 * pass sorting parameters via the global 'server' structure */
3244static int sortCompare(const void *s1, const void *s2) {
3245 const redisSortObject *so1 = s1, *so2 = s2;
3246 int cmp;
3247
3248 if (!server.sort_alpha) {
3249 /* Numeric sorting. Here it's trivial as we precomputed scores */
3250 if (so1->u.score > so2->u.score) {
3251 cmp = 1;
3252 } else if (so1->u.score < so2->u.score) {
3253 cmp = -1;
3254 } else {
3255 cmp = 0;
3256 }
3257 } else {
3258 /* Alphanumeric sorting */
3259 if (server.sort_bypattern) {
3260 if (!so1->u.cmpobj || !so2->u.cmpobj) {
3261 /* At least one compare object is NULL */
3262 if (so1->u.cmpobj == so2->u.cmpobj)
3263 cmp = 0;
3264 else if (so1->u.cmpobj == NULL)
3265 cmp = -1;
3266 else
3267 cmp = 1;
3268 } else {
3269 /* We have both the objects, use strcoll */
3270 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
3271 }
3272 } else {
3273 /* Compare elements directly */
3274 cmp = strcoll(so1->obj->ptr,so2->obj->ptr);
3275 }
3276 }
3277 return server.sort_desc ? -cmp : cmp;
3278}
3279
3280/* The SORT command is the most complex command in Redis. Warning: this code
3281 * is optimized for speed and a bit less for readability */
3282static void sortCommand(redisClient *c) {
ed9b544e 3283 list *operations;
3284 int outputlen = 0;
3285 int desc = 0, alpha = 0;
3286 int limit_start = 0, limit_count = -1, start, end;
3287 int j, dontsort = 0, vectorlen;
3288 int getop = 0; /* GET operation counter */
3289 robj *sortval, *sortby = NULL;
3290 redisSortObject *vector; /* Resulting vector to sort */
3291
3292 /* Lookup the key to sort. It must be of the right types */
3305306f 3293 sortval = lookupKeyRead(c->db,c->argv[1]);
3294 if (sortval == NULL) {
c937aa89 3295 addReply(c,shared.nokeyerr);
ed9b544e 3296 return;
3297 }
ed9b544e 3298 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST) {
c937aa89 3299 addReply(c,shared.wrongtypeerr);
ed9b544e 3300 return;
3301 }
3302
3303 /* Create a list of operations to perform for every sorted element.
3304 * Operations can be GET/DEL/INCR/DECR */
3305 operations = listCreate();
092dac2a 3306 listSetFreeMethod(operations,zfree);
ed9b544e 3307 j = 2;
3308
3309 /* Now we need to protect sortval incrementing its count, in the future
3310 * SORT may have options able to overwrite/delete keys during the sorting
3311 * and the sorted key itself may get destroied */
3312 incrRefCount(sortval);
3313
3314 /* The SORT command has an SQL-alike syntax, parse it */
3315 while(j < c->argc) {
3316 int leftargs = c->argc-j-1;
3317 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
3318 desc = 0;
3319 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
3320 desc = 1;
3321 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
3322 alpha = 1;
3323 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
3324 limit_start = atoi(c->argv[j+1]->ptr);
3325 limit_count = atoi(c->argv[j+2]->ptr);
3326 j+=2;
3327 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
3328 sortby = c->argv[j+1];
3329 /* If the BY pattern does not contain '*', i.e. it is constant,
3330 * we don't need to sort nor to lookup the weight keys. */
3331 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
3332 j++;
3333 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
3334 listAddNodeTail(operations,createSortOperation(
3335 REDIS_SORT_GET,c->argv[j+1]));
3336 getop++;
3337 j++;
3338 } else if (!strcasecmp(c->argv[j]->ptr,"del") && leftargs >= 1) {
3339 listAddNodeTail(operations,createSortOperation(
3340 REDIS_SORT_DEL,c->argv[j+1]));
3341 j++;
3342 } else if (!strcasecmp(c->argv[j]->ptr,"incr") && leftargs >= 1) {
3343 listAddNodeTail(operations,createSortOperation(
3344 REDIS_SORT_INCR,c->argv[j+1]));
3345 j++;
3346 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
3347 listAddNodeTail(operations,createSortOperation(
3348 REDIS_SORT_DECR,c->argv[j+1]));
3349 j++;
3350 } else {
3351 decrRefCount(sortval);
3352 listRelease(operations);
c937aa89 3353 addReply(c,shared.syntaxerr);
ed9b544e 3354 return;
3355 }
3356 j++;
3357 }
3358
3359 /* Load the sorting vector with all the objects to sort */
3360 vectorlen = (sortval->type == REDIS_LIST) ?
3361 listLength((list*)sortval->ptr) :
3305306f 3362 dictSize((dict*)sortval->ptr);
ed9b544e 3363 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
3364 if (!vector) oom("allocating objects vector for SORT");
3365 j = 0;
3366 if (sortval->type == REDIS_LIST) {
3367 list *list = sortval->ptr;
6208b3a7 3368 listNode *ln;
3369
3370 listRewind(list);
3371 while((ln = listYield(list))) {
ed9b544e 3372 robj *ele = ln->value;
3373 vector[j].obj = ele;
3374 vector[j].u.score = 0;
3375 vector[j].u.cmpobj = NULL;
ed9b544e 3376 j++;
3377 }
3378 } else {
3379 dict *set = sortval->ptr;
3380 dictIterator *di;
3381 dictEntry *setele;
3382
3383 di = dictGetIterator(set);
3384 if (!di) oom("dictGetIterator");
3385 while((setele = dictNext(di)) != NULL) {
3386 vector[j].obj = dictGetEntryKey(setele);
3387 vector[j].u.score = 0;
3388 vector[j].u.cmpobj = NULL;
3389 j++;
3390 }
3391 dictReleaseIterator(di);
3392 }
3393 assert(j == vectorlen);
3394
3395 /* Now it's time to load the right scores in the sorting vector */
3396 if (dontsort == 0) {
3397 for (j = 0; j < vectorlen; j++) {
3398 if (sortby) {
3399 robj *byval;
3400
3305306f 3401 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
ed9b544e 3402 if (!byval || byval->type != REDIS_STRING) continue;
3403 if (alpha) {
3404 vector[j].u.cmpobj = byval;
3405 incrRefCount(byval);
3406 } else {
3407 vector[j].u.score = strtod(byval->ptr,NULL);
3408 }
3409 } else {
3410 if (!alpha) vector[j].u.score = strtod(vector[j].obj->ptr,NULL);
3411 }
3412 }
3413 }
3414
3415 /* We are ready to sort the vector... perform a bit of sanity check
3416 * on the LIMIT option too. We'll use a partial version of quicksort. */
3417 start = (limit_start < 0) ? 0 : limit_start;
3418 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
3419 if (start >= vectorlen) {
3420 start = vectorlen-1;
3421 end = vectorlen-2;
3422 }
3423 if (end >= vectorlen) end = vectorlen-1;
3424
3425 if (dontsort == 0) {
3426 server.sort_desc = desc;
3427 server.sort_alpha = alpha;
3428 server.sort_bypattern = sortby ? 1 : 0;
5f5b9840 3429 if (sortby && (start != 0 || end != vectorlen-1))
3430 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
3431 else
3432 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
ed9b544e 3433 }
3434
3435 /* Send command output to the output buffer, performing the specified
3436 * GET/DEL/INCR/DECR operations if any. */
3437 outputlen = getop ? getop*(end-start+1) : end-start+1;
c937aa89 3438 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
ed9b544e 3439 for (j = start; j <= end; j++) {
6208b3a7 3440 listNode *ln;
ed9b544e 3441 if (!getop) {
c937aa89 3442 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",
ed9b544e 3443 sdslen(vector[j].obj->ptr)));
3444 addReply(c,vector[j].obj);
3445 addReply(c,shared.crlf);
3446 }
6208b3a7 3447 listRewind(operations);
3448 while((ln = listYield(operations))) {
ed9b544e 3449 redisSortOperation *sop = ln->value;
3305306f 3450 robj *val = lookupKeyByPattern(c->db,sop->pattern,
ed9b544e 3451 vector[j].obj);
3452
3453 if (sop->type == REDIS_SORT_GET) {
3454 if (!val || val->type != REDIS_STRING) {
9eb00f21 3455 addReply(c,shared.nullbulk);
ed9b544e 3456 } else {
c937aa89 3457 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",
ed9b544e 3458 sdslen(val->ptr)));
3459 addReply(c,val);
3460 addReply(c,shared.crlf);
3461 }
3462 } else if (sop->type == REDIS_SORT_DEL) {
3463 /* TODO */
3464 }
ed9b544e 3465 }
3466 }
3467
3468 /* Cleanup */
3469 decrRefCount(sortval);
3470 listRelease(operations);
3471 for (j = 0; j < vectorlen; j++) {
3472 if (sortby && alpha && vector[j].u.cmpobj)
3473 decrRefCount(vector[j].u.cmpobj);
3474 }
3475 zfree(vector);
3476}
3477
3478static void infoCommand(redisClient *c) {
3479 sds info;
3480 time_t uptime = time(NULL)-server.stat_starttime;
3481
3482 info = sdscatprintf(sdsempty(),
3483 "redis_version:%s\r\n"
3484 "connected_clients:%d\r\n"
3485 "connected_slaves:%d\r\n"
5fba9f71 3486 "used_memory:%zu\r\n"
ed9b544e 3487 "changes_since_last_save:%lld\r\n"
be2bb6b0 3488 "bgsave_in_progress:%d\r\n"
ed9b544e 3489 "last_save_time:%d\r\n"
3490 "total_connections_received:%lld\r\n"
3491 "total_commands_processed:%lld\r\n"
3492 "uptime_in_seconds:%d\r\n"
3493 "uptime_in_days:%d\r\n"
3494 ,REDIS_VERSION,
3495 listLength(server.clients)-listLength(server.slaves),
3496 listLength(server.slaves),
3497 server.usedmemory,
3498 server.dirty,
be2bb6b0 3499 server.bgsaveinprogress,
ed9b544e 3500 server.lastsave,
3501 server.stat_numconnections,
3502 server.stat_numcommands,
3503 uptime,
3504 uptime/(3600*24)
3505 );
c937aa89 3506 addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info)));
ed9b544e 3507 addReplySds(c,info);
70003d28 3508 addReply(c,shared.crlf);
ed9b544e 3509}
3510
3305306f 3511static void monitorCommand(redisClient *c) {
3512 /* ignore MONITOR if aleady slave or in monitor mode */
3513 if (c->flags & REDIS_SLAVE) return;
3514
3515 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
3516 c->slaveseldb = 0;
3517 if (!listAddNodeTail(server.monitors,c)) oom("listAddNodeTail");
3518 addReply(c,shared.ok);
3519}
3520
3521/* ================================= Expire ================================= */
3522static int removeExpire(redisDb *db, robj *key) {
3523 if (dictDelete(db->expires,key) == DICT_OK) {
3524 return 1;
3525 } else {
3526 return 0;
3527 }
3528}
3529
3530static int setExpire(redisDb *db, robj *key, time_t when) {
3531 if (dictAdd(db->expires,key,(void*)when) == DICT_ERR) {
3532 return 0;
3533 } else {
3534 incrRefCount(key);
3535 return 1;
3536 }
3537}
3538
bb32ede5 3539/* Return the expire time of the specified key, or -1 if no expire
3540 * is associated with this key (i.e. the key is non volatile) */
3541static time_t getExpire(redisDb *db, robj *key) {
3542 dictEntry *de;
3543
3544 /* No expire? return ASAP */
3545 if (dictSize(db->expires) == 0 ||
3546 (de = dictFind(db->expires,key)) == NULL) return -1;
3547
3548 return (time_t) dictGetEntryVal(de);
3549}
3550
3305306f 3551static int expireIfNeeded(redisDb *db, robj *key) {
3552 time_t when;
3553 dictEntry *de;
3554
3555 /* No expire? return ASAP */
3556 if (dictSize(db->expires) == 0 ||
3557 (de = dictFind(db->expires,key)) == NULL) return 0;
3558
3559 /* Lookup the expire */
3560 when = (time_t) dictGetEntryVal(de);
3561 if (time(NULL) <= when) return 0;
3562
3563 /* Delete the key */
3564 dictDelete(db->expires,key);
3565 return dictDelete(db->dict,key) == DICT_OK;
3566}
3567
3568static int deleteIfVolatile(redisDb *db, robj *key) {
3569 dictEntry *de;
3570
3571 /* No expire? return ASAP */
3572 if (dictSize(db->expires) == 0 ||
3573 (de = dictFind(db->expires,key)) == NULL) return 0;
3574
3575 /* Delete the key */
0c66a471 3576 server.dirty++;
3305306f 3577 dictDelete(db->expires,key);
3578 return dictDelete(db->dict,key) == DICT_OK;
3579}
3580
3581static void expireCommand(redisClient *c) {
3582 dictEntry *de;
3583 int seconds = atoi(c->argv[2]->ptr);
3584
3585 de = dictFind(c->db->dict,c->argv[1]);
3586 if (de == NULL) {
3587 addReply(c,shared.czero);
3588 return;
3589 }
3590 if (seconds <= 0) {
3591 addReply(c, shared.czero);
3592 return;
3593 } else {
3594 time_t when = time(NULL)+seconds;
3595 if (setExpire(c->db,c->argv[1],when))
3596 addReply(c,shared.cone);
3597 else
3598 addReply(c,shared.czero);
3599 return;
3600 }
3601}
3602
ed9b544e 3603/* =============================== Replication ============================= */
3604
a4d1ba9a 3605static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
ed9b544e 3606 ssize_t nwritten, ret = size;
3607 time_t start = time(NULL);
3608
3609 timeout++;
3610 while(size) {
3611 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
3612 nwritten = write(fd,ptr,size);
3613 if (nwritten == -1) return -1;
3614 ptr += nwritten;
3615 size -= nwritten;
3616 }
3617 if ((time(NULL)-start) > timeout) {
3618 errno = ETIMEDOUT;
3619 return -1;
3620 }
3621 }
3622 return ret;
3623}
3624
a4d1ba9a 3625static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
ed9b544e 3626 ssize_t nread, totread = 0;
3627 time_t start = time(NULL);
3628
3629 timeout++;
3630 while(size) {
3631 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
3632 nread = read(fd,ptr,size);
3633 if (nread == -1) return -1;
3634 ptr += nread;
3635 size -= nread;
3636 totread += nread;
3637 }
3638 if ((time(NULL)-start) > timeout) {
3639 errno = ETIMEDOUT;
3640 return -1;
3641 }
3642 }
3643 return totread;
3644}
3645
3646static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
3647 ssize_t nread = 0;
3648
3649 size--;
3650 while(size) {
3651 char c;
3652
3653 if (syncRead(fd,&c,1,timeout) == -1) return -1;
3654 if (c == '\n') {
3655 *ptr = '\0';
3656 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
3657 return nread;
3658 } else {
3659 *ptr++ = c;
3660 *ptr = '\0';
3661 nread++;
3662 }
3663 }
3664 return nread;
3665}
3666
3667static void syncCommand(redisClient *c) {
40d224a9 3668 /* ignore SYNC if aleady slave or in monitor mode */
3669 if (c->flags & REDIS_SLAVE) return;
3670
3671 /* SYNC can't be issued when the server has pending data to send to
3672 * the client about already issued commands. We need a fresh reply
3673 * buffer registering the differences between the BGSAVE and the current
3674 * dataset, so that we can copy to other slaves if needed. */
3675 if (listLength(c->reply) != 0) {
3676 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
3677 return;
3678 }
3679
3680 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
3681 /* Here we need to check if there is a background saving operation
3682 * in progress, or if it is required to start one */
3683 if (server.bgsaveinprogress) {
3684 /* Ok a background save is in progress. Let's check if it is a good
3685 * one for replication, i.e. if there is another slave that is
3686 * registering differences since the server forked to save */
3687 redisClient *slave;
3688 listNode *ln;
3689
6208b3a7 3690 listRewind(server.slaves);
3691 while((ln = listYield(server.slaves))) {
40d224a9 3692 slave = ln->value;
3693 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
40d224a9 3694 }
3695 if (ln) {
3696 /* Perfect, the server is already registering differences for
3697 * another slave. Set the right state, and copy the buffer. */
3698 listRelease(c->reply);
3699 c->reply = listDup(slave->reply);
3700 if (!c->reply) oom("listDup copying slave reply list");
3701 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
3702 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
3703 } else {
3704 /* No way, we need to wait for the next BGSAVE in order to
3705 * register differences */
3706 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
3707 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
3708 }
3709 } else {
3710 /* Ok we don't have a BGSAVE in progress, let's start one */
3711 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
3712 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
3713 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
3714 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
3715 return;
3716 }
3717 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
3718 }
6208b3a7 3719 c->repldbfd = -1;
40d224a9 3720 c->flags |= REDIS_SLAVE;
3721 c->slaveseldb = 0;
3722 if (!listAddNodeTail(server.slaves,c)) oom("listAddNodeTail");
40d224a9 3723 return;
3724}
3725
6208b3a7 3726static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
3727 redisClient *slave = privdata;
3728 REDIS_NOTUSED(el);
3729 REDIS_NOTUSED(mask);
3730 char buf[REDIS_IOBUF_LEN];
3731 ssize_t nwritten, buflen;
3732
3733 if (slave->repldboff == 0) {
3734 /* Write the bulk write count before to transfer the DB. In theory here
3735 * we don't know how much room there is in the output buffer of the
3736 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
3737 * operations) will never be smaller than the few bytes we need. */
3738 sds bulkcount;
3739
3740 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
3741 slave->repldbsize);
3742 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
3743 {
3744 sdsfree(bulkcount);
3745 freeClient(slave);
3746 return;
3747 }
3748 sdsfree(bulkcount);
3749 }
3750 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
3751 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
3752 if (buflen <= 0) {
3753 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
3754 (buflen == 0) ? "premature EOF" : strerror(errno));
3755 freeClient(slave);
3756 return;
3757 }
3758 if ((nwritten = write(fd,buf,buflen)) == -1) {
3759 redisLog(REDIS_DEBUG,"Write error sending DB to slave: %s",
3760 strerror(errno));
3761 freeClient(slave);
3762 return;
3763 }
3764 slave->repldboff += nwritten;
3765 if (slave->repldboff == slave->repldbsize) {
3766 close(slave->repldbfd);
3767 slave->repldbfd = -1;
3768 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
3769 slave->replstate = REDIS_REPL_ONLINE;
3770 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
3771 sendReplyToClient, slave, NULL) == AE_ERR) {
3772 freeClient(slave);
3773 return;
3774 }
3775 addReplySds(slave,sdsempty());
3776 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
3777 }
3778}
ed9b544e 3779
6208b3a7 3780static void updateSalvesWaitingBgsave(int bgsaveerr) {
3781 listNode *ln;
3782 int startbgsave = 0;
ed9b544e 3783
6208b3a7 3784 listRewind(server.slaves);
3785 while((ln = listYield(server.slaves))) {
3786 redisClient *slave = ln->value;
ed9b544e 3787
6208b3a7 3788 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
3789 startbgsave = 1;
3790 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
3791 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
3792 struct stat buf;
3793
3794 if (bgsaveerr != REDIS_OK) {
3795 freeClient(slave);
3796 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
3797 continue;
3798 }
3799 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
3800 fstat(slave->repldbfd,&buf) == -1) {
3801 freeClient(slave);
3802 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
3803 continue;
3804 }
3805 slave->repldboff = 0;
3806 slave->repldbsize = buf.st_size;
3807 slave->replstate = REDIS_REPL_SEND_BULK;
3808 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
3809 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave, NULL) == AE_ERR) {
3810 freeClient(slave);
3811 continue;
3812 }
3813 }
ed9b544e 3814 }
6208b3a7 3815 if (startbgsave) {
3816 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
3817 listRewind(server.slaves);
3818 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
3819 while((ln = listYield(server.slaves))) {
3820 redisClient *slave = ln->value;
ed9b544e 3821
6208b3a7 3822 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
3823 freeClient(slave);
3824 }
3825 }
3826 }
ed9b544e 3827}
3828
3829static int syncWithMaster(void) {
3830 char buf[1024], tmpfile[256];
3831 int dumpsize;
3832 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
3833 int dfd;
3834
3835 if (fd == -1) {
3836 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
3837 strerror(errno));
3838 return REDIS_ERR;
3839 }
3840 /* Issue the SYNC command */
3841 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
3842 close(fd);
3843 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
3844 strerror(errno));
3845 return REDIS_ERR;
3846 }
3847 /* Read the bulk write count */
3848 if (syncReadLine(fd,buf,1024,5) == -1) {
3849 close(fd);
3850 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
3851 strerror(errno));
3852 return REDIS_ERR;
3853 }
c937aa89 3854 dumpsize = atoi(buf+1);
ed9b544e 3855 redisLog(REDIS_NOTICE,"Receiving %d bytes data dump from MASTER",dumpsize);
3856 /* Read the bulk write data on a temp file */
3857 snprintf(tmpfile,256,"temp-%d.%ld.rdb",(int)time(NULL),(long int)random());
3858 dfd = open(tmpfile,O_CREAT|O_WRONLY,0644);
3859 if (dfd == -1) {
3860 close(fd);
3861 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
3862 return REDIS_ERR;
3863 }
3864 while(dumpsize) {
3865 int nread, nwritten;
3866
3867 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
3868 if (nread == -1) {
3869 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
3870 strerror(errno));
3871 close(fd);
3872 close(dfd);
3873 return REDIS_ERR;
3874 }
3875 nwritten = write(dfd,buf,nread);
3876 if (nwritten == -1) {
3877 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
3878 close(fd);
3879 close(dfd);
3880 return REDIS_ERR;
3881 }
3882 dumpsize -= nread;
3883 }
3884 close(dfd);
3885 if (rename(tmpfile,server.dbfilename) == -1) {
3886 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
3887 unlink(tmpfile);
3888 close(fd);
3889 return REDIS_ERR;
3890 }
3891 emptyDb();
f78fd11b 3892 if (rdbLoad(server.dbfilename) != REDIS_OK) {
ed9b544e 3893 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
3894 close(fd);
3895 return REDIS_ERR;
3896 }
3897 server.master = createClient(fd);
3898 server.master->flags |= REDIS_MASTER;
3899 server.replstate = REDIS_REPL_CONNECTED;
3900 return REDIS_OK;
3901}
3902
3903/* =================================== Main! ================================ */
3904
0bc03378 3905#ifdef __linux__
3906int linuxOvercommitMemoryValue(void) {
3907 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
3908 char buf[64];
3909
3910 if (!fp) return -1;
3911 if (fgets(buf,64,fp) == NULL) {
3912 fclose(fp);
3913 return -1;
3914 }
3915 fclose(fp);
3916
3917 return atoi(buf);
3918}
3919
3920void linuxOvercommitMemoryWarning(void) {
3921 if (linuxOvercommitMemoryValue() == 0) {
3922 redisLog(REDIS_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. To fix this issue add 'echo 1 > /proc/sys/vm/overcommit_memory' in your init scripts.");
3923 }
3924}
3925#endif /* __linux__ */
3926
ed9b544e 3927static void daemonize(void) {
3928 int fd;
3929 FILE *fp;
3930
3931 if (fork() != 0) exit(0); /* parent exits */
3932 setsid(); /* create a new session */
3933
3934 /* Every output goes to /dev/null. If Redis is daemonized but
3935 * the 'logfile' is set to 'stdout' in the configuration file
3936 * it will not log at all. */
3937 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
3938 dup2(fd, STDIN_FILENO);
3939 dup2(fd, STDOUT_FILENO);
3940 dup2(fd, STDERR_FILENO);
3941 if (fd > STDERR_FILENO) close(fd);
3942 }
3943 /* Try to write the pid file */
ed329fcf 3944 fp = fopen(server.pidfile,"w");
ed9b544e 3945 if (fp) {
3946 fprintf(fp,"%d\n",getpid());
3947 fclose(fp);
3948 }
3949}
3950
3951int main(int argc, char **argv) {
0bc03378 3952#ifdef __linux__
3953 linuxOvercommitMemoryWarning();
3954#endif
3955
ed9b544e 3956 initServerConfig();
3957 if (argc == 2) {
3958 ResetServerSaveParams();
3959 loadServerConfig(argv[1]);
3960 } else if (argc > 2) {
3961 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
3962 exit(1);
3963 }
3964 initServer();
3965 if (server.daemonize) daemonize();
3966 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
f78fd11b 3967 if (rdbLoad(server.dbfilename) == REDIS_OK)
ed9b544e 3968 redisLog(REDIS_NOTICE,"DB loaded from disk");
3969 if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
3970 acceptHandler, NULL, NULL) == AE_ERR) oom("creating file event");
46713f83 3971 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
ed9b544e 3972 aeMain(server.el);
3973 aeDeleteEventLoop(server.el);
3974 return 0;
3975}