xref: /redis-3.2.3/src/db.c (revision f3fd58eb)
1 /*
2  * Copyright (c) 2009-2012, 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 
30 #include "redis.h"
31 #include "cluster.h"
32 
33 #include <signal.h>
34 #include <ctype.h>
35 
36 void slotToKeyAdd(robj *key);
37 void slotToKeyDel(robj *key);
38 void slotToKeyFlush(void);
39 
40 /*-----------------------------------------------------------------------------
41  * C-level DB API
42  *----------------------------------------------------------------------------*/
43 
44 robj *lookupKey(redisDb *db, robj *key) {
45     dictEntry *de = dictFind(db->dict,key->ptr);
46     if (de) {
47         robj *val = dictGetVal(de);
48 
49         /* Update the access time for the ageing algorithm.
50          * Don't do it if we have a saving child, as this will trigger
51          * a copy on write madness. */
52         if (server.rdb_child_pid == -1 && server.aof_child_pid == -1)
53             val->lru = LRU_CLOCK();
54         return val;
55     } else {
56         return NULL;
57     }
58 }
59 
60 robj *lookupKeyRead(redisDb *db, robj *key) {
61     robj *val;
62 
63     if (expireIfNeeded(db,key) == 1) {
64         /* Key expired. If we are in the context of a master, expireIfNeeded()
65          * returns 0 only when the key does not exist at all, so it's save
66          * to return NULL ASAP. */
67         if (server.masterhost == NULL) return NULL;
68 
69         /* However if we are in the context of a slave, expireIfNeeded() will
70          * not really try to expire the key, it only returns information
71          * about the "logical" status of the key: key expiring is up to the
72          * master in order to have a consistent view of master's data set.
73          *
74          * However, if the command caller is not the master, and as additional
75          * safety measure, the command invoked is a read-only command, we can
76          * safely return NULL here, and provide a more consistent behavior
77          * to clients accessign expired values in a read-only fashion, that
78          * will say the key as non exisitng.
79          *
80          * Notably this covers GETs when slaves are used to scale reads. */
81         if (server.current_client &&
82             server.current_client != server.master &&
83             server.current_client->cmd &&
84             server.current_client->cmd->flags & REDIS_CMD_READONLY)
85         {
86             return NULL;
87         }
88     }
89     val = lookupKey(db,key);
90     if (val == NULL)
91         server.stat_keyspace_misses++;
92     else
93         server.stat_keyspace_hits++;
94     return val;
95 }
96 
97 robj *lookupKeyWrite(redisDb *db, robj *key) {
98     expireIfNeeded(db,key);
99     return lookupKey(db,key);
100 }
101 
102 robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
103     robj *o = lookupKeyRead(c->db, key);
104     if (!o) addReply(c,reply);
105     return o;
106 }
107 
108 robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
109     robj *o = lookupKeyWrite(c->db, key);
110     if (!o) addReply(c,reply);
111     return o;
112 }
113 
114 /* Add the key to the DB. It's up to the caller to increment the reference
115  * counter of the value if needed.
116  *
117  * The program is aborted if the key already exists. */
118 void dbAdd(redisDb *db, robj *key, robj *val) {
119     sds copy = sdsdup(key->ptr);
120     int retval = dictAdd(db->dict, copy, val);
121 
122     redisAssertWithInfo(NULL,key,retval == REDIS_OK);
123     if (val->type == REDIS_LIST) signalListAsReady(db, key);
124     if (server.cluster_enabled) slotToKeyAdd(key);
125  }
126 
127 /* Overwrite an existing key with a new value. Incrementing the reference
128  * count of the new value is up to the caller.
129  * This function does not modify the expire time of the existing key.
130  *
131  * The program is aborted if the key was not already present. */
132 void dbOverwrite(redisDb *db, robj *key, robj *val) {
133     dictEntry *de = dictFind(db->dict,key->ptr);
134 
135     redisAssertWithInfo(NULL,key,de != NULL);
136     dictReplace(db->dict, key->ptr, val);
137 }
138 
139 /* High level Set operation. This function can be used in order to set
140  * a key, whatever it was existing or not, to a new object.
141  *
142  * 1) The ref count of the value object is incremented.
143  * 2) clients WATCHing for the destination key notified.
144  * 3) The expire time of the key is reset (the key is made persistent). */
145 void setKey(redisDb *db, robj *key, robj *val) {
146     if (lookupKeyWrite(db,key) == NULL) {
147         dbAdd(db,key,val);
148     } else {
149         dbOverwrite(db,key,val);
150     }
151     incrRefCount(val);
152     removeExpire(db,key);
153     signalModifiedKey(db,key);
154 }
155 
156 int dbExists(redisDb *db, robj *key) {
157     return dictFind(db->dict,key->ptr) != NULL;
158 }
159 
160 /* Return a random key, in form of a Redis object.
161  * If there are no keys, NULL is returned.
162  *
163  * The function makes sure to return keys not already expired. */
164 robj *dbRandomKey(redisDb *db) {
165     dictEntry *de;
166 
167     while(1) {
168         sds key;
169         robj *keyobj;
170 
171         de = dictGetRandomKey(db->dict);
172         if (de == NULL) return NULL;
173 
174         key = dictGetKey(de);
175         keyobj = createStringObject(key,sdslen(key));
176         if (dictFind(db->expires,key)) {
177             if (expireIfNeeded(db,keyobj)) {
178                 decrRefCount(keyobj);
179                 continue; /* search for another key. This expired. */
180             }
181         }
182         return keyobj;
183     }
184 }
185 
186 /* Delete a key, value, and associated expiration entry if any, from the DB */
187 int dbDelete(redisDb *db, robj *key) {
188     /* Deleting an entry from the expires dict will not free the sds of
189      * the key, because it is shared with the main dictionary. */
190     if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);
191     if (dictDelete(db->dict,key->ptr) == DICT_OK) {
192         if (server.cluster_enabled) slotToKeyDel(key);
193         return 1;
194     } else {
195         return 0;
196     }
197 }
198 
199 /* Prepare the string object stored at 'key' to be modified destructively
200  * to implement commands like SETBIT or APPEND.
201  *
202  * An object is usually ready to be modified unless one of the two conditions
203  * are true:
204  *
205  * 1) The object 'o' is shared (refcount > 1), we don't want to affect
206  *    other users.
207  * 2) The object encoding is not "RAW".
208  *
209  * If the object is found in one of the above conditions (or both) by the
210  * function, an unshared / not-encoded copy of the string object is stored
211  * at 'key' in the specified 'db'. Otherwise the object 'o' itself is
212  * returned.
213  *
214  * USAGE:
215  *
216  * The object 'o' is what the caller already obtained by looking up 'key'
217  * in 'db', the usage pattern looks like this:
218  *
219  * o = lookupKeyWrite(db,key);
220  * if (checkType(c,o,REDIS_STRING)) return;
221  * o = dbUnshareStringValue(db,key,o);
222  *
223  * At this point the caller is ready to modify the object, for example
224  * using an sdscat() call to append some data, or anything else.
225  */
226 robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) {
227     redisAssert(o->type == REDIS_STRING);
228     if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
229         robj *decoded = getDecodedObject(o);
230         o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr));
231         decrRefCount(decoded);
232         dbOverwrite(db,key,o);
233     }
234     return o;
235 }
236 
237 long long emptyDb(void(callback)(void*)) {
238     int j;
239     long long removed = 0;
240 
241     for (j = 0; j < server.dbnum; j++) {
242         removed += dictSize(server.db[j].dict);
243         dictEmpty(server.db[j].dict,callback);
244         dictEmpty(server.db[j].expires,callback);
245     }
246     if (server.cluster_enabled) slotToKeyFlush();
247     return removed;
248 }
249 
250 int selectDb(redisClient *c, int id) {
251     if (id < 0 || id >= server.dbnum)
252         return REDIS_ERR;
253     c->db = &server.db[id];
254     return REDIS_OK;
255 }
256 
257 /*-----------------------------------------------------------------------------
258  * Hooks for key space changes.
259  *
260  * Every time a key in the database is modified the function
261  * signalModifiedKey() is called.
262  *
263  * Every time a DB is flushed the function signalFlushDb() is called.
264  *----------------------------------------------------------------------------*/
265 
266 void signalModifiedKey(redisDb *db, robj *key) {
267     touchWatchedKey(db,key);
268 }
269 
270 void signalFlushedDb(int dbid) {
271     touchWatchedKeysOnFlush(dbid);
272 }
273 
274 /*-----------------------------------------------------------------------------
275  * Type agnostic commands operating on the key space
276  *----------------------------------------------------------------------------*/
277 
278 void flushdbCommand(redisClient *c) {
279     server.dirty += dictSize(c->db->dict);
280     signalFlushedDb(c->db->id);
281     dictEmpty(c->db->dict,NULL);
282     dictEmpty(c->db->expires,NULL);
283     if (server.cluster_enabled) slotToKeyFlush();
284     addReply(c,shared.ok);
285 }
286 
287 void flushallCommand(redisClient *c) {
288     signalFlushedDb(-1);
289     server.dirty += emptyDb(NULL);
290     addReply(c,shared.ok);
291     if (server.rdb_child_pid != -1) {
292         kill(server.rdb_child_pid,SIGUSR1);
293         rdbRemoveTempFile(server.rdb_child_pid);
294     }
295     if (server.saveparamslen > 0) {
296         /* Normally rdbSave() will reset dirty, but we don't want this here
297          * as otherwise FLUSHALL will not be replicated nor put into the AOF. */
298         int saved_dirty = server.dirty;
299         rdbSave(server.rdb_filename);
300         server.dirty = saved_dirty;
301     }
302     server.dirty++;
303 }
304 
305 void delCommand(redisClient *c) {
306     int deleted = 0, j;
307 
308     for (j = 1; j < c->argc; j++) {
309         expireIfNeeded(c->db,c->argv[j]);
310         if (dbDelete(c->db,c->argv[j])) {
311             signalModifiedKey(c->db,c->argv[j]);
312             notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,
313                 "del",c->argv[j],c->db->id);
314             server.dirty++;
315             deleted++;
316         }
317     }
318     addReplyLongLong(c,deleted);
319 }
320 
321 void existsCommand(redisClient *c) {
322     expireIfNeeded(c->db,c->argv[1]);
323     if (dbExists(c->db,c->argv[1])) {
324         addReply(c, shared.cone);
325     } else {
326         addReply(c, shared.czero);
327     }
328 }
329 
330 void selectCommand(redisClient *c) {
331     long id;
332 
333     if (getLongFromObjectOrReply(c, c->argv[1], &id,
334         "invalid DB index") != REDIS_OK)
335         return;
336 
337     if (server.cluster_enabled && id != 0) {
338         addReplyError(c,"SELECT is not allowed in cluster mode");
339         return;
340     }
341     if (selectDb(c,id) == REDIS_ERR) {
342         addReplyError(c,"invalid DB index");
343     } else {
344         addReply(c,shared.ok);
345     }
346 }
347 
348 void randomkeyCommand(redisClient *c) {
349     robj *key;
350 
351     if ((key = dbRandomKey(c->db)) == NULL) {
352         addReply(c,shared.nullbulk);
353         return;
354     }
355 
356     addReplyBulk(c,key);
357     decrRefCount(key);
358 }
359 
360 void keysCommand(redisClient *c) {
361     dictIterator *di;
362     dictEntry *de;
363     sds pattern = c->argv[1]->ptr;
364     int plen = sdslen(pattern), allkeys;
365     unsigned long numkeys = 0;
366     void *replylen = addDeferredMultiBulkLength(c);
367 
368     di = dictGetSafeIterator(c->db->dict);
369     allkeys = (pattern[0] == '*' && pattern[1] == '\0');
370     while((de = dictNext(di)) != NULL) {
371         sds key = dictGetKey(de);
372         robj *keyobj;
373 
374         if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) {
375             keyobj = createStringObject(key,sdslen(key));
376             if (expireIfNeeded(c->db,keyobj) == 0) {
377                 addReplyBulk(c,keyobj);
378                 numkeys++;
379             }
380             decrRefCount(keyobj);
381         }
382     }
383     dictReleaseIterator(di);
384     setDeferredMultiBulkLength(c,replylen,numkeys);
385 }
386 
387 /* This callback is used by scanGenericCommand in order to collect elements
388  * returned by the dictionary iterator into a list. */
389 void scanCallback(void *privdata, const dictEntry *de) {
390     void **pd = (void**) privdata;
391     list *keys = pd[0];
392     robj *o = pd[1];
393     robj *key, *val = NULL;
394 
395     if (o == NULL) {
396         sds sdskey = dictGetKey(de);
397         key = createStringObject(sdskey, sdslen(sdskey));
398     } else if (o->type == REDIS_SET) {
399         key = dictGetKey(de);
400         incrRefCount(key);
401     } else if (o->type == REDIS_HASH) {
402         key = dictGetKey(de);
403         incrRefCount(key);
404         val = dictGetVal(de);
405         incrRefCount(val);
406     } else if (o->type == REDIS_ZSET) {
407         key = dictGetKey(de);
408         incrRefCount(key);
409         val = createStringObjectFromLongDouble(*(double*)dictGetVal(de),0);
410     } else {
411         redisPanic("Type not handled in SCAN callback.");
412     }
413 
414     listAddNodeTail(keys, key);
415     if (val) listAddNodeTail(keys, val);
416 }
417 
418 /* Try to parse a SCAN cursor stored at object 'o':
419  * if the cursor is valid, store it as unsigned integer into *cursor and
420  * returns REDIS_OK. Otherwise return REDIS_ERR and send an error to the
421  * client. */
422 int parseScanCursorOrReply(redisClient *c, robj *o, unsigned long *cursor) {
423     char *eptr;
424 
425     /* Use strtoul() because we need an *unsigned* long, so
426      * getLongLongFromObject() does not cover the whole cursor space. */
427     errno = 0;
428     *cursor = strtoul(o->ptr, &eptr, 10);
429     if (isspace(((char*)o->ptr)[0]) || eptr[0] != '\0' || errno == ERANGE)
430     {
431         addReplyError(c, "invalid cursor");
432         return REDIS_ERR;
433     }
434     return REDIS_OK;
435 }
436 
437 /* This command implements SCAN, HSCAN and SSCAN commands.
438  * If object 'o' is passed, then it must be a Hash or Set object, otherwise
439  * if 'o' is NULL the command will operate on the dictionary associated with
440  * the current database.
441  *
442  * When 'o' is not NULL the function assumes that the first argument in
443  * the client arguments vector is a key so it skips it before iterating
444  * in order to parse options.
445  *
446  * In the case of a Hash object the function returns both the field and value
447  * of every element on the Hash. */
448 void scanGenericCommand(redisClient *c, robj *o, unsigned long cursor) {
449     int i, j;
450     list *keys = listCreate();
451     listNode *node, *nextnode;
452     long count = 10;
453     sds pat = NULL;
454     int patlen = 0, use_pattern = 0;
455     dict *ht;
456 
457     /* Object must be NULL (to iterate keys names), or the type of the object
458      * must be Set, Sorted Set, or Hash. */
459     redisAssert(o == NULL || o->type == REDIS_SET || o->type == REDIS_HASH ||
460                 o->type == REDIS_ZSET);
461 
462     /* Set i to the first option argument. The previous one is the cursor. */
463     i = (o == NULL) ? 2 : 3; /* Skip the key argument if needed. */
464 
465     /* Step 1: Parse options. */
466     while (i < c->argc) {
467         j = c->argc - i;
468         if (!strcasecmp(c->argv[i]->ptr, "count") && j >= 2) {
469             if (getLongFromObjectOrReply(c, c->argv[i+1], &count, NULL)
470                 != REDIS_OK)
471             {
472                 goto cleanup;
473             }
474 
475             if (count < 1) {
476                 addReply(c,shared.syntaxerr);
477                 goto cleanup;
478             }
479 
480             i += 2;
481         } else if (!strcasecmp(c->argv[i]->ptr, "match") && j >= 2) {
482             pat = c->argv[i+1]->ptr;
483             patlen = sdslen(pat);
484 
485             /* The pattern always matches if it is exactly "*", so it is
486              * equivalent to disabling it. */
487             use_pattern = !(pat[0] == '*' && patlen == 1);
488 
489             i += 2;
490         } else {
491             addReply(c,shared.syntaxerr);
492             goto cleanup;
493         }
494     }
495 
496     /* Step 2: Iterate the collection.
497      *
498      * Note that if the object is encoded with a ziplist, intset, or any other
499      * representation that is not a hash table, we are sure that it is also
500      * composed of a small number of elements. So to avoid taking state we
501      * just return everything inside the object in a single call, setting the
502      * cursor to zero to signal the end of the iteration. */
503 
504     /* Handle the case of a hash table. */
505     ht = NULL;
506     if (o == NULL) {
507         ht = c->db->dict;
508     } else if (o->type == REDIS_SET && o->encoding == REDIS_ENCODING_HT) {
509         ht = o->ptr;
510     } else if (o->type == REDIS_HASH && o->encoding == REDIS_ENCODING_HT) {
511         ht = o->ptr;
512         count *= 2; /* We return key / value for this type. */
513     } else if (o->type == REDIS_ZSET && o->encoding == REDIS_ENCODING_SKIPLIST) {
514         zset *zs = o->ptr;
515         ht = zs->dict;
516         count *= 2; /* We return key / value for this type. */
517     }
518 
519     if (ht) {
520         void *privdata[2];
521         /* We set the max number of iterations to ten times the specified
522          * COUNT, so if the hash table is in a pathological state (very
523          * sparsely populated) we avoid to block too much time at the cost
524          * of returning no or very few elements. */
525         long maxiterations = count*10;
526 
527         /* We pass two pointers to the callback: the list to which it will
528          * add new elements, and the object containing the dictionary so that
529          * it is possible to fetch more data in a type-dependent way. */
530         privdata[0] = keys;
531         privdata[1] = o;
532         do {
533             cursor = dictScan(ht, cursor, scanCallback, privdata);
534         } while (cursor &&
535               maxiterations-- &&
536               listLength(keys) < (unsigned long)count);
537     } else if (o->type == REDIS_SET) {
538         int pos = 0;
539         int64_t ll;
540 
541         while(intsetGet(o->ptr,pos++,&ll))
542             listAddNodeTail(keys,createStringObjectFromLongLong(ll));
543         cursor = 0;
544     } else if (o->type == REDIS_HASH || o->type == REDIS_ZSET) {
545         unsigned char *p = ziplistIndex(o->ptr,0);
546         unsigned char *vstr;
547         unsigned int vlen;
548         long long vll;
549 
550         while(p) {
551             ziplistGet(p,&vstr,&vlen,&vll);
552             listAddNodeTail(keys,
553                 (vstr != NULL) ? createStringObject((char*)vstr,vlen) :
554                                  createStringObjectFromLongLong(vll));
555             p = ziplistNext(o->ptr,p);
556         }
557         cursor = 0;
558     } else {
559         redisPanic("Not handled encoding in SCAN.");
560     }
561 
562     /* Step 3: Filter elements. */
563     node = listFirst(keys);
564     while (node) {
565         robj *kobj = listNodeValue(node);
566         nextnode = listNextNode(node);
567         int filter = 0;
568 
569         /* Filter element if it does not match the pattern. */
570         if (!filter && use_pattern) {
571             if (sdsEncodedObject(kobj)) {
572                 if (!stringmatchlen(pat, patlen, kobj->ptr, sdslen(kobj->ptr), 0))
573                     filter = 1;
574             } else {
575                 char buf[REDIS_LONGSTR_SIZE];
576                 int len;
577 
578                 redisAssert(kobj->encoding == REDIS_ENCODING_INT);
579                 len = ll2string(buf,sizeof(buf),(long)kobj->ptr);
580                 if (!stringmatchlen(pat, patlen, buf, len, 0)) filter = 1;
581             }
582         }
583 
584         /* Filter element if it is an expired key. */
585         if (!filter && o == NULL && expireIfNeeded(c->db, kobj)) filter = 1;
586 
587         /* Remove the element and its associted value if needed. */
588         if (filter) {
589             decrRefCount(kobj);
590             listDelNode(keys, node);
591         }
592 
593         /* If this is a hash or a sorted set, we have a flat list of
594          * key-value elements, so if this element was filtered, remove the
595          * value, or skip it if it was not filtered: we only match keys. */
596         if (o && (o->type == REDIS_ZSET || o->type == REDIS_HASH)) {
597             node = nextnode;
598             nextnode = listNextNode(node);
599             if (filter) {
600                 kobj = listNodeValue(node);
601                 decrRefCount(kobj);
602                 listDelNode(keys, node);
603             }
604         }
605         node = nextnode;
606     }
607 
608     /* Step 4: Reply to the client. */
609     addReplyMultiBulkLen(c, 2);
610     addReplyBulkLongLong(c,cursor);
611 
612     addReplyMultiBulkLen(c, listLength(keys));
613     while ((node = listFirst(keys)) != NULL) {
614         robj *kobj = listNodeValue(node);
615         addReplyBulk(c, kobj);
616         decrRefCount(kobj);
617         listDelNode(keys, node);
618     }
619 
620 cleanup:
621     listSetFreeMethod(keys,decrRefCountVoid);
622     listRelease(keys);
623 }
624 
625 /* The SCAN command completely relies on scanGenericCommand. */
626 void scanCommand(redisClient *c) {
627     unsigned long cursor;
628     if (parseScanCursorOrReply(c,c->argv[1],&cursor) == REDIS_ERR) return;
629     scanGenericCommand(c,NULL,cursor);
630 }
631 
632 void dbsizeCommand(redisClient *c) {
633     addReplyLongLong(c,dictSize(c->db->dict));
634 }
635 
636 void lastsaveCommand(redisClient *c) {
637     addReplyLongLong(c,server.lastsave);
638 }
639 
640 void typeCommand(redisClient *c) {
641     robj *o;
642     char *type;
643 
644     o = lookupKeyRead(c->db,c->argv[1]);
645     if (o == NULL) {
646         type = "none";
647     } else {
648         switch(o->type) {
649         case REDIS_STRING: type = "string"; break;
650         case REDIS_LIST: type = "list"; break;
651         case REDIS_SET: type = "set"; break;
652         case REDIS_ZSET: type = "zset"; break;
653         case REDIS_HASH: type = "hash"; break;
654         default: type = "unknown"; break;
655         }
656     }
657     addReplyStatus(c,type);
658 }
659 
660 void shutdownCommand(redisClient *c) {
661     int flags = 0;
662 
663     if (c->argc > 2) {
664         addReply(c,shared.syntaxerr);
665         return;
666     } else if (c->argc == 2) {
667         if (!strcasecmp(c->argv[1]->ptr,"nosave")) {
668             flags |= REDIS_SHUTDOWN_NOSAVE;
669         } else if (!strcasecmp(c->argv[1]->ptr,"save")) {
670             flags |= REDIS_SHUTDOWN_SAVE;
671         } else {
672             addReply(c,shared.syntaxerr);
673             return;
674         }
675     }
676     /* When SHUTDOWN is called while the server is loading a dataset in
677      * memory we need to make sure no attempt is performed to save
678      * the dataset on shutdown (otherwise it could overwrite the current DB
679      * with half-read data).
680      *
681      * Also when in Sentinel mode clear the SAVE flag and force NOSAVE. */
682     if (server.loading || server.sentinel_mode)
683         flags = (flags & ~REDIS_SHUTDOWN_SAVE) | REDIS_SHUTDOWN_NOSAVE;
684     if (prepareForShutdown(flags) == REDIS_OK) exit(0);
685     addReplyError(c,"Errors trying to SHUTDOWN. Check logs.");
686 }
687 
688 void renameGenericCommand(redisClient *c, int nx) {
689     robj *o;
690     long long expire;
691 
692     /* To use the same key as src and dst is probably an error */
693     if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
694         addReply(c,shared.sameobjecterr);
695         return;
696     }
697 
698     if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
699         return;
700 
701     incrRefCount(o);
702     expire = getExpire(c->db,c->argv[1]);
703     if (lookupKeyWrite(c->db,c->argv[2]) != NULL) {
704         if (nx) {
705             decrRefCount(o);
706             addReply(c,shared.czero);
707             return;
708         }
709         /* Overwrite: delete the old key before creating the new one
710          * with the same name. */
711         dbDelete(c->db,c->argv[2]);
712     }
713     dbAdd(c->db,c->argv[2],o);
714     if (expire != -1) setExpire(c->db,c->argv[2],expire);
715     dbDelete(c->db,c->argv[1]);
716     signalModifiedKey(c->db,c->argv[1]);
717     signalModifiedKey(c->db,c->argv[2]);
718     notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"rename_from",
719         c->argv[1],c->db->id);
720     notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"rename_to",
721         c->argv[2],c->db->id);
722     server.dirty++;
723     addReply(c,nx ? shared.cone : shared.ok);
724 }
725 
726 void renameCommand(redisClient *c) {
727     renameGenericCommand(c,0);
728 }
729 
730 void renamenxCommand(redisClient *c) {
731     renameGenericCommand(c,1);
732 }
733 
734 void moveCommand(redisClient *c) {
735     robj *o;
736     redisDb *src, *dst;
737     int srcid;
738     long long dbid;
739 
740     if (server.cluster_enabled) {
741         addReplyError(c,"MOVE is not allowed in cluster mode");
742         return;
743     }
744 
745     /* Obtain source and target DB pointers */
746     src = c->db;
747     srcid = c->db->id;
748 
749     if (getLongLongFromObject(c->argv[2],&dbid) == REDIS_ERR ||
750         dbid < INT_MIN || dbid > INT_MAX ||
751         selectDb(c,dbid) == REDIS_ERR)
752     {
753         addReply(c,shared.outofrangeerr);
754         return;
755     }
756     dst = c->db;
757     selectDb(c,srcid); /* Back to the source DB */
758 
759     /* If the user is moving using as target the same
760      * DB as the source DB it is probably an error. */
761     if (src == dst) {
762         addReply(c,shared.sameobjecterr);
763         return;
764     }
765 
766     /* Check if the element exists and get a reference */
767     o = lookupKeyWrite(c->db,c->argv[1]);
768     if (!o) {
769         addReply(c,shared.czero);
770         return;
771     }
772 
773     /* Return zero if the key already exists in the target DB */
774     if (lookupKeyWrite(dst,c->argv[1]) != NULL) {
775         addReply(c,shared.czero);
776         return;
777     }
778     dbAdd(dst,c->argv[1],o);
779     incrRefCount(o);
780 
781     /* OK! key moved, free the entry in the source DB */
782     dbDelete(src,c->argv[1]);
783     server.dirty++;
784     addReply(c,shared.cone);
785 }
786 
787 /*-----------------------------------------------------------------------------
788  * Expires API
789  *----------------------------------------------------------------------------*/
790 
791 int removeExpire(redisDb *db, robj *key) {
792     /* An expire may only be removed if there is a corresponding entry in the
793      * main dict. Otherwise, the key will never be freed. */
794     redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);
795     return dictDelete(db->expires,key->ptr) == DICT_OK;
796 }
797 
798 void setExpire(redisDb *db, robj *key, long long when) {
799     dictEntry *kde, *de;
800 
801     /* Reuse the sds from the main dict in the expire dict */
802     kde = dictFind(db->dict,key->ptr);
803     redisAssertWithInfo(NULL,key,kde != NULL);
804     de = dictReplaceRaw(db->expires,dictGetKey(kde));
805     dictSetSignedIntegerVal(de,when);
806 }
807 
808 /* Return the expire time of the specified key, or -1 if no expire
809  * is associated with this key (i.e. the key is non volatile) */
810 long long getExpire(redisDb *db, robj *key) {
811     dictEntry *de;
812 
813     /* No expire? return ASAP */
814     if (dictSize(db->expires) == 0 ||
815        (de = dictFind(db->expires,key->ptr)) == NULL) return -1;
816 
817     /* The entry was found in the expire dict, this means it should also
818      * be present in the main dict (safety check). */
819     redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);
820     return dictGetSignedIntegerVal(de);
821 }
822 
823 /* Propagate expires into slaves and the AOF file.
824  * When a key expires in the master, a DEL operation for this key is sent
825  * to all the slaves and the AOF file if enabled.
826  *
827  * This way the key expiry is centralized in one place, and since both
828  * AOF and the master->slave link guarantee operation ordering, everything
829  * will be consistent even if we allow write operations against expiring
830  * keys. */
831 void propagateExpire(redisDb *db, robj *key) {
832     robj *argv[2];
833 
834     argv[0] = shared.del;
835     argv[1] = key;
836     incrRefCount(argv[0]);
837     incrRefCount(argv[1]);
838 
839     if (server.aof_state != REDIS_AOF_OFF)
840         feedAppendOnlyFile(server.delCommand,db->id,argv,2);
841     replicationFeedSlaves(server.slaves,db->id,argv,2);
842 
843     decrRefCount(argv[0]);
844     decrRefCount(argv[1]);
845 }
846 
847 int expireIfNeeded(redisDb *db, robj *key) {
848     mstime_t when = getExpire(db,key);
849     mstime_t now;
850 
851     if (when < 0) return 0; /* No expire for this key */
852 
853     /* Don't expire anything while loading. It will be done later. */
854     if (server.loading) return 0;
855 
856     /* If we are in the context of a Lua script, we claim that time is
857      * blocked to when the Lua script started. This way a key can expire
858      * only the first time it is accessed and not in the middle of the
859      * script execution, making propagation to slaves / AOF consistent.
860      * See issue #1525 on Github for more information. */
861     now = server.lua_caller ? server.lua_time_start : mstime();
862 
863     /* If we are running in the context of a slave, return ASAP:
864      * the slave key expiration is controlled by the master that will
865      * send us synthesized DEL operations for expired keys.
866      *
867      * Still we try to return the right information to the caller,
868      * that is, 0 if we think the key should be still valid, 1 if
869      * we think the key is expired at this time. */
870     if (server.masterhost != NULL) return now > when;
871 
872     /* Return when this key has not expired */
873     if (now <= when) return 0;
874 
875     /* Delete the key */
876     server.stat_expiredkeys++;
877     propagateExpire(db,key);
878     notifyKeyspaceEvent(REDIS_NOTIFY_EXPIRED,
879         "expired",key,db->id);
880     return dbDelete(db,key);
881 }
882 
883 /*-----------------------------------------------------------------------------
884  * Expires Commands
885  *----------------------------------------------------------------------------*/
886 
887 /* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT
888  * and PEXPIREAT. Because the commad second argument may be relative or absolute
889  * the "basetime" argument is used to signal what the base time is (either 0
890  * for *AT variants of the command, or the current time for relative expires).
891  *
892  * unit is either UNIT_SECONDS or UNIT_MILLISECONDS, and is only used for
893  * the argv[2] parameter. The basetime is always specified in milliseconds. */
894 void expireGenericCommand(redisClient *c, long long basetime, int unit) {
895     robj *key = c->argv[1], *param = c->argv[2];
896     long long when; /* unix time in milliseconds when the key will expire. */
897 
898     if (getLongLongFromObjectOrReply(c, param, &when, NULL) != REDIS_OK)
899         return;
900 
901     if (unit == UNIT_SECONDS) when *= 1000;
902     when += basetime;
903 
904     /* No key, return zero. */
905     if (lookupKeyWrite(c->db,key) == NULL) {
906         addReply(c,shared.czero);
907         return;
908     }
909 
910     /* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past
911      * should never be executed as a DEL when load the AOF or in the context
912      * of a slave instance.
913      *
914      * Instead we take the other branch of the IF statement setting an expire
915      * (possibly in the past) and wait for an explicit DEL from the master. */
916     if (when <= mstime() && !server.loading && !server.masterhost) {
917         robj *aux;
918 
919         redisAssertWithInfo(c,key,dbDelete(c->db,key));
920         server.dirty++;
921 
922         /* Replicate/AOF this as an explicit DEL. */
923         aux = createStringObject("DEL",3);
924         rewriteClientCommandVector(c,2,aux,key);
925         decrRefCount(aux);
926         signalModifiedKey(c->db,key);
927         notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);
928         addReply(c, shared.cone);
929         return;
930     } else {
931         setExpire(c->db,key,when);
932         addReply(c,shared.cone);
933         signalModifiedKey(c->db,key);
934         notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"expire",key,c->db->id);
935         server.dirty++;
936         return;
937     }
938 }
939 
940 void expireCommand(redisClient *c) {
941     expireGenericCommand(c,mstime(),UNIT_SECONDS);
942 }
943 
944 void expireatCommand(redisClient *c) {
945     expireGenericCommand(c,0,UNIT_SECONDS);
946 }
947 
948 void pexpireCommand(redisClient *c) {
949     expireGenericCommand(c,mstime(),UNIT_MILLISECONDS);
950 }
951 
952 void pexpireatCommand(redisClient *c) {
953     expireGenericCommand(c,0,UNIT_MILLISECONDS);
954 }
955 
956 void ttlGenericCommand(redisClient *c, int output_ms) {
957     long long expire, ttl = -1;
958 
959     /* If the key does not exist at all, return -2 */
960     if (lookupKeyRead(c->db,c->argv[1]) == NULL) {
961         addReplyLongLong(c,-2);
962         return;
963     }
964     /* The key exists. Return -1 if it has no expire, or the actual
965      * TTL value otherwise. */
966     expire = getExpire(c->db,c->argv[1]);
967     if (expire != -1) {
968         ttl = expire-mstime();
969         if (ttl < 0) ttl = 0;
970     }
971     if (ttl == -1) {
972         addReplyLongLong(c,-1);
973     } else {
974         addReplyLongLong(c,output_ms ? ttl : ((ttl+500)/1000));
975     }
976 }
977 
978 void ttlCommand(redisClient *c) {
979     ttlGenericCommand(c, 0);
980 }
981 
982 void pttlCommand(redisClient *c) {
983     ttlGenericCommand(c, 1);
984 }
985 
986 void persistCommand(redisClient *c) {
987     dictEntry *de;
988 
989     de = dictFind(c->db->dict,c->argv[1]->ptr);
990     if (de == NULL) {
991         addReply(c,shared.czero);
992     } else {
993         if (removeExpire(c->db,c->argv[1])) {
994             addReply(c,shared.cone);
995             server.dirty++;
996         } else {
997             addReply(c,shared.czero);
998         }
999     }
1000 }
1001 
1002 /* -----------------------------------------------------------------------------
1003  * API to get key arguments from commands
1004  * ---------------------------------------------------------------------------*/
1005 
1006 /* The base case is to use the keys position as given in the command table
1007  * (firstkey, lastkey, step). */
1008 int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) {
1009     int j, i = 0, last, *keys;
1010     REDIS_NOTUSED(argv);
1011 
1012     if (cmd->firstkey == 0) {
1013         *numkeys = 0;
1014         return NULL;
1015     }
1016     last = cmd->lastkey;
1017     if (last < 0) last = argc+last;
1018     keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1));
1019     for (j = cmd->firstkey; j <= last; j += cmd->keystep) {
1020         redisAssert(j < argc);
1021         keys[i++] = j;
1022     }
1023     *numkeys = i;
1024     return keys;
1025 }
1026 
1027 /* Return all the arguments that are keys in the command passed via argc / argv.
1028  *
1029  * The command returns the positions of all the key arguments inside the array,
1030  * so the actual return value is an heap allocated array of integers. The
1031  * length of the array is returned by reference into *numkeys.
1032  *
1033  * 'cmd' must be point to the corresponding entry into the redisCommand
1034  * table, according to the command name in argv[0].
1035  *
1036  * This function uses the command table if a command-specific helper function
1037  * is not required, otherwise it calls the command-specific function. */
1038 int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
1039     if (cmd->getkeys_proc) {
1040         return cmd->getkeys_proc(cmd,argv,argc,numkeys);
1041     } else {
1042         return getKeysUsingCommandTable(cmd,argv,argc,numkeys);
1043     }
1044 }
1045 
1046 /* Free the result of getKeysFromCommand. */
1047 void getKeysFreeResult(int *result) {
1048     zfree(result);
1049 }
1050 
1051 /* Helper function to extract keys from following commands:
1052  * ZUNIONSTORE <destkey> <num-keys> <key> <key> ... <key> <options>
1053  * ZINTERSTORE <destkey> <num-keys> <key> <key> ... <key> <options> */
1054 int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
1055     int i, num, *keys;
1056     REDIS_NOTUSED(cmd);
1057 
1058     num = atoi(argv[2]->ptr);
1059     /* Sanity check. Don't return any key if the command is going to
1060      * reply with syntax error. */
1061     if (num > (argc-3)) {
1062         *numkeys = 0;
1063         return NULL;
1064     }
1065 
1066     /* Keys in z{union,inter}store come from two places:
1067      * argv[1] = storage key,
1068      * argv[3...n] = keys to intersect */
1069     keys = zmalloc(sizeof(int)*(num+1));
1070 
1071     /* Add all key positions for argv[3...n] to keys[] */
1072     for (i = 0; i < num; i++) keys[i] = 3+i;
1073 
1074     /* Finally add the argv[1] key position (the storage key target). */
1075     keys[num] = 1;
1076     *numkeys = num+1;  /* Total keys = {union,inter} keys + storage key */
1077     return keys;
1078 }
1079 
1080 /* Helper function to extract keys from the following commands:
1081  * EVAL <script> <num-keys> <key> <key> ... <key> [more stuff]
1082  * EVALSHA <script> <num-keys> <key> <key> ... <key> [more stuff] */
1083 int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
1084     int i, num, *keys;
1085     REDIS_NOTUSED(cmd);
1086 
1087     num = atoi(argv[2]->ptr);
1088     /* Sanity check. Don't return any key if the command is going to
1089      * reply with syntax error. */
1090     if (num > (argc-3)) {
1091         *numkeys = 0;
1092         return NULL;
1093     }
1094 
1095     keys = zmalloc(sizeof(int)*num);
1096     *numkeys = num;
1097 
1098     /* Add all key positions for argv[3...n] to keys[] */
1099     for (i = 0; i < num; i++) keys[i] = 3+i;
1100 
1101     return keys;
1102 }
1103 
1104 /* Helper function to extract keys from the SORT command.
1105  *
1106  * SORT <sort-key> ... STORE <store-key> ...
1107  *
1108  * The first argument of SORT is always a key, however a list of options
1109  * follow in SQL-alike style. Here we parse just the minimum in order to
1110  * correctly identify keys in the "STORE" option. */
1111 int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
1112     int i, j, num, *keys, found_store = 0;
1113     REDIS_NOTUSED(cmd);
1114 
1115     num = 0;
1116     keys = zmalloc(sizeof(int)*2); /* Alloc 2 places for the worst case. */
1117 
1118     keys[num++] = 1; /* <sort-key> is always present. */
1119 
1120     /* Search for STORE option. By default we consider options to don't
1121      * have arguments, so if we find an unknown option name we scan the
1122      * next. However there are options with 1 or 2 arguments, so we
1123      * provide a list here in order to skip the right number of args. */
1124     struct {
1125         char *name;
1126         int skip;
1127     } skiplist[] = {
1128         {"limit", 2},
1129         {"get", 1},
1130         {"by", 1},
1131         {NULL, 0} /* End of elements. */
1132     };
1133 
1134     for (i = 2; i < argc; i++) {
1135         for (j = 0; skiplist[j].name != NULL; j++) {
1136             if (!strcasecmp(argv[i]->ptr,skiplist[j].name)) {
1137                 i += skiplist[j].skip;
1138                 break;
1139             } else if (!strcasecmp(argv[i]->ptr,"store") && i+1 < argc) {
1140                 /* Note: we don't increment "num" here and continue the loop
1141                  * to be sure to process the *last* "STORE" option if multiple
1142                  * ones are provided. This is same behavior as SORT. */
1143                 found_store = 1;
1144                 keys[num] = i+1; /* <store-key> */
1145                 break;
1146             }
1147         }
1148     }
1149     *numkeys = num + found_store;
1150     return keys;
1151 }
1152 
1153 /* Slot to Key API. This is used by Redis Cluster in order to obtain in
1154  * a fast way a key that belongs to a specified hash slot. This is useful
1155  * while rehashing the cluster. */
1156 void slotToKeyAdd(robj *key) {
1157     unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr));
1158 
1159     zslInsert(server.cluster->slots_to_keys,hashslot,key);
1160     incrRefCount(key);
1161 }
1162 
1163 void slotToKeyDel(robj *key) {
1164     unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr));
1165 
1166     zslDelete(server.cluster->slots_to_keys,hashslot,key);
1167 }
1168 
1169 void slotToKeyFlush(void) {
1170     zslFree(server.cluster->slots_to_keys);
1171     server.cluster->slots_to_keys = zslCreate();
1172 }
1173 
1174 unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) {
1175     zskiplistNode *n;
1176     zrangespec range;
1177     int j = 0;
1178 
1179     range.min = range.max = hashslot;
1180     range.minex = range.maxex = 0;
1181 
1182     n = zslFirstInRange(server.cluster->slots_to_keys, &range);
1183     while(n && n->score == hashslot && count--) {
1184         keys[j++] = n->obj;
1185         n = n->level[0].forward;
1186     }
1187     return j;
1188 }
1189 
1190 /* Remove all the keys in the specified hash slot.
1191  * The number of removed items is returned. */
1192 unsigned int delKeysInSlot(unsigned int hashslot) {
1193     zskiplistNode *n;
1194     zrangespec range;
1195     int j = 0;
1196 
1197     range.min = range.max = hashslot;
1198     range.minex = range.maxex = 0;
1199 
1200     n = zslFirstInRange(server.cluster->slots_to_keys, &range);
1201     while(n && n->score == hashslot) {
1202         robj *key = n->obj;
1203         n = n->level[0].forward; /* Go to the next item before freeing it. */
1204         incrRefCount(key); /* Protect the object while freeing it. */
1205         dbDelete(&server.db[0],key);
1206         decrRefCount(key);
1207         j++;
1208     }
1209     return j;
1210 }
1211 
1212 unsigned int countKeysInSlot(unsigned int hashslot) {
1213     zskiplist *zsl = server.cluster->slots_to_keys;
1214     zskiplistNode *zn;
1215     zrangespec range;
1216     int rank, count = 0;
1217 
1218     range.min = range.max = hashslot;
1219     range.minex = range.maxex = 0;
1220 
1221     /* Find first element in range */
1222     zn = zslFirstInRange(zsl, &range);
1223 
1224     /* Use rank of first element, if any, to determine preliminary count */
1225     if (zn != NULL) {
1226         rank = zslGetRank(zsl, zn->score, zn->obj);
1227         count = (zsl->length - (rank - 1));
1228 
1229         /* Find last element in range */
1230         zn = zslLastInRange(zsl, &range);
1231 
1232         /* Use rank of last element, if any, to determine the actual count */
1233         if (zn != NULL) {
1234             rank = zslGetRank(zsl, zn->score, zn->obj);
1235             count -= (zsl->length - rank);
1236         }
1237     }
1238     return count;
1239 }
1240