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