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