1 /* Redis Object implementation. 2 * 3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * * Neither the name of Redis nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without 16 * specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "redis.h" 32 #include <math.h> 33 #include <ctype.h> 34 35 #ifdef __CYGWIN__ 36 #define strtold(a,b) ((long double)strtod((a),(b))) 37 #endif 38 39 robj *createObject(int type, void *ptr) { 40 robj *o = zmalloc(sizeof(*o)); 41 o->type = type; 42 o->encoding = REDIS_ENCODING_RAW; 43 o->ptr = ptr; 44 o->refcount = 1; 45 46 /* Set the LRU to the current lruclock (minutes resolution). */ 47 o->lru = LRU_CLOCK(); 48 return o; 49 } 50 51 /* Create a string object with encoding REDIS_ENCODING_RAW, that is a plain 52 * string object where o->ptr points to a proper sds string. */ 53 robj *createRawStringObject(char *ptr, size_t len) { 54 return createObject(REDIS_STRING,sdsnewlen(ptr,len)); 55 } 56 57 /* Create a string object with encoding REDIS_ENCODING_EMBSTR, that is 58 * an object where the sds string is actually an unmodifiable string 59 * allocated in the same chunk as the object itself. */ 60 robj *createEmbeddedStringObject(char *ptr, size_t len) { 61 robj *o = zmalloc(sizeof(robj)+sizeof(struct sdshdr)+len+1); 62 struct sdshdr *sh = (void*)(o+1); 63 64 o->type = REDIS_STRING; 65 o->encoding = REDIS_ENCODING_EMBSTR; 66 o->ptr = sh+1; 67 o->refcount = 1; 68 o->lru = LRU_CLOCK(); 69 70 sh->len = len; 71 sh->free = 0; 72 if (ptr) { 73 memcpy(sh->buf,ptr,len); 74 sh->buf[len] = '\0'; 75 } else { 76 memset(sh->buf,0,len+1); 77 } 78 return o; 79 } 80 81 /* Create a string object with EMBSTR encoding if it is smaller than 82 * REIDS_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is 83 * used. 84 * 85 * The current limit of 39 is chosen so that the biggest string object 86 * we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */ 87 #define REDIS_ENCODING_EMBSTR_SIZE_LIMIT 39 88 robj *createStringObject(char *ptr, size_t len) { 89 if (len <= REDIS_ENCODING_EMBSTR_SIZE_LIMIT) 90 return createEmbeddedStringObject(ptr,len); 91 else 92 return createRawStringObject(ptr,len); 93 } 94 95 robj *createStringObjectFromLongLong(long long value) { 96 robj *o; 97 if (value >= 0 && value < REDIS_SHARED_INTEGERS) { 98 incrRefCount(shared.integers[value]); 99 o = shared.integers[value]; 100 } else { 101 if (value >= LONG_MIN && value <= LONG_MAX) { 102 o = createObject(REDIS_STRING, NULL); 103 o->encoding = REDIS_ENCODING_INT; 104 o->ptr = (void*)((long)value); 105 } else { 106 o = createObject(REDIS_STRING,sdsfromlonglong(value)); 107 } 108 } 109 return o; 110 } 111 112 /* Note: this function is defined into object.c since here it is where it 113 * belongs but it is actually designed to be used just for INCRBYFLOAT */ 114 robj *createStringObjectFromLongDouble(long double value) { 115 char buf[256]; 116 int len; 117 118 /* We use 17 digits precision since with 128 bit floats that precision 119 * after rounding is able to represent most small decimal numbers in a way 120 * that is "non surprising" for the user (that is, most small decimal 121 * numbers will be represented in a way that when converted back into 122 * a string are exactly the same as what the user typed.) */ 123 len = snprintf(buf,sizeof(buf),"%.17Lf", value); 124 /* Now remove trailing zeroes after the '.' */ 125 if (strchr(buf,'.') != NULL) { 126 char *p = buf+len-1; 127 while(*p == '0') { 128 p--; 129 len--; 130 } 131 if (*p == '.') len--; 132 } 133 return createStringObject(buf,len); 134 } 135 136 /* Duplicate a string object, with the guarantee that the returned object 137 * has the same encoding as the original one. 138 * 139 * This function also guarantees that duplicating a small integere object 140 * (or a string object that contains a representation of a small integer) 141 * will always result in a fresh object that is unshared (refcount == 1). 142 * 143 * The resulting object always has refcount set to 1. */ 144 robj *dupStringObject(robj *o) { 145 robj *d; 146 147 redisAssert(o->type == REDIS_STRING); 148 149 switch(o->encoding) { 150 case REDIS_ENCODING_RAW: 151 return createRawStringObject(o->ptr,sdslen(o->ptr)); 152 case REDIS_ENCODING_EMBSTR: 153 return createEmbeddedStringObject(o->ptr,sdslen(o->ptr)); 154 case REDIS_ENCODING_INT: 155 d = createObject(REDIS_STRING, NULL); 156 d->encoding = REDIS_ENCODING_INT; 157 d->ptr = o->ptr; 158 return d; 159 default: 160 redisPanic("Wrong encoding."); 161 break; 162 } 163 } 164 165 robj *createListObject(void) { 166 list *l = listCreate(); 167 robj *o = createObject(REDIS_LIST,l); 168 listSetFreeMethod(l,decrRefCountVoid); 169 o->encoding = REDIS_ENCODING_LINKEDLIST; 170 return o; 171 } 172 173 robj *createZiplistObject(void) { 174 unsigned char *zl = ziplistNew(); 175 robj *o = createObject(REDIS_LIST,zl); 176 o->encoding = REDIS_ENCODING_ZIPLIST; 177 return o; 178 } 179 180 robj *createSetObject(void) { 181 dict *d = dictCreate(&setDictType,NULL); 182 robj *o = createObject(REDIS_SET,d); 183 o->encoding = REDIS_ENCODING_HT; 184 return o; 185 } 186 187 robj *createIntsetObject(void) { 188 intset *is = intsetNew(); 189 robj *o = createObject(REDIS_SET,is); 190 o->encoding = REDIS_ENCODING_INTSET; 191 return o; 192 } 193 194 robj *createHashObject(void) { 195 unsigned char *zl = ziplistNew(); 196 robj *o = createObject(REDIS_HASH, zl); 197 o->encoding = REDIS_ENCODING_ZIPLIST; 198 return o; 199 } 200 201 robj *createZsetObject(void) { 202 zset *zs = zmalloc(sizeof(*zs)); 203 robj *o; 204 205 zs->dict = dictCreate(&zsetDictType,NULL); 206 zs->zsl = zslCreate(); 207 o = createObject(REDIS_ZSET,zs); 208 o->encoding = REDIS_ENCODING_SKIPLIST; 209 return o; 210 } 211 212 robj *createZsetZiplistObject(void) { 213 unsigned char *zl = ziplistNew(); 214 robj *o = createObject(REDIS_ZSET,zl); 215 o->encoding = REDIS_ENCODING_ZIPLIST; 216 return o; 217 } 218 219 void freeStringObject(robj *o) { 220 if (o->encoding == REDIS_ENCODING_RAW) { 221 sdsfree(o->ptr); 222 } 223 } 224 225 void freeListObject(robj *o) { 226 switch (o->encoding) { 227 case REDIS_ENCODING_LINKEDLIST: 228 listRelease((list*) o->ptr); 229 break; 230 case REDIS_ENCODING_ZIPLIST: 231 zfree(o->ptr); 232 break; 233 default: 234 redisPanic("Unknown list encoding type"); 235 } 236 } 237 238 void freeSetObject(robj *o) { 239 switch (o->encoding) { 240 case REDIS_ENCODING_HT: 241 dictRelease((dict*) o->ptr); 242 break; 243 case REDIS_ENCODING_INTSET: 244 zfree(o->ptr); 245 break; 246 default: 247 redisPanic("Unknown set encoding type"); 248 } 249 } 250 251 void freeZsetObject(robj *o) { 252 zset *zs; 253 switch (o->encoding) { 254 case REDIS_ENCODING_SKIPLIST: 255 zs = o->ptr; 256 dictRelease(zs->dict); 257 zslFree(zs->zsl); 258 zfree(zs); 259 break; 260 case REDIS_ENCODING_ZIPLIST: 261 zfree(o->ptr); 262 break; 263 default: 264 redisPanic("Unknown sorted set encoding"); 265 } 266 } 267 268 void freeHashObject(robj *o) { 269 switch (o->encoding) { 270 case REDIS_ENCODING_HT: 271 dictRelease((dict*) o->ptr); 272 break; 273 case REDIS_ENCODING_ZIPLIST: 274 zfree(o->ptr); 275 break; 276 default: 277 redisPanic("Unknown hash encoding type"); 278 break; 279 } 280 } 281 282 void incrRefCount(robj *o) { 283 o->refcount++; 284 } 285 286 void decrRefCount(robj *o) { 287 if (o->refcount <= 0) redisPanic("decrRefCount against refcount <= 0"); 288 if (o->refcount == 1) { 289 switch(o->type) { 290 case REDIS_STRING: freeStringObject(o); break; 291 case REDIS_LIST: freeListObject(o); break; 292 case REDIS_SET: freeSetObject(o); break; 293 case REDIS_ZSET: freeZsetObject(o); break; 294 case REDIS_HASH: freeHashObject(o); break; 295 default: redisPanic("Unknown object type"); break; 296 } 297 zfree(o); 298 } else { 299 o->refcount--; 300 } 301 } 302 303 /* This variant of decrRefCount() gets its argument as void, and is useful 304 * as free method in data structures that expect a 'void free_object(void*)' 305 * prototype for the free method. */ 306 void decrRefCountVoid(void *o) { 307 decrRefCount(o); 308 } 309 310 /* This function set the ref count to zero without freeing the object. 311 * It is useful in order to pass a new object to functions incrementing 312 * the ref count of the received object. Example: 313 * 314 * functionThatWillIncrementRefCount(resetRefCount(CreateObject(...))); 315 * 316 * Otherwise you need to resort to the less elegant pattern: 317 * 318 * *obj = createObject(...); 319 * functionThatWillIncrementRefCount(obj); 320 * decrRefCount(obj); 321 */ 322 robj *resetRefCount(robj *obj) { 323 obj->refcount = 0; 324 return obj; 325 } 326 327 int checkType(redisClient *c, robj *o, int type) { 328 if (o->type != type) { 329 addReply(c,shared.wrongtypeerr); 330 return 1; 331 } 332 return 0; 333 } 334 335 int isObjectRepresentableAsLongLong(robj *o, long long *llval) { 336 redisAssertWithInfo(NULL,o,o->type == REDIS_STRING); 337 if (o->encoding == REDIS_ENCODING_INT) { 338 if (llval) *llval = (long) o->ptr; 339 return REDIS_OK; 340 } else { 341 return string2ll(o->ptr,sdslen(o->ptr),llval) ? REDIS_OK : REDIS_ERR; 342 } 343 } 344 345 /* Try to encode a string object in order to save space */ 346 robj *tryObjectEncoding(robj *o) { 347 long value; 348 sds s = o->ptr; 349 size_t len; 350 351 /* Make sure this is a string object, the only type we encode 352 * in this function. Other types use encoded memory efficient 353 * representations but are handled by the commands implementing 354 * the type. */ 355 redisAssertWithInfo(NULL,o,o->type == REDIS_STRING); 356 357 /* We try some specialized encoding only for objects that are 358 * RAW or EMBSTR encoded, in other words objects that are still 359 * in represented by an actually array of chars. */ 360 if (!sdsEncodedObject(o)) return o; 361 362 /* It's not safe to encode shared objects: shared objects can be shared 363 * everywhere in the "object space" of Redis and may end in places where 364 * they are not handled. We handle them only as values in the keyspace. */ 365 if (o->refcount > 1) return o; 366 367 /* Check if we can represent this string as a long integer. 368 * Note that we are sure that a string larger than 21 chars is not 369 * representable as a 32 nor 64 bit integer. */ 370 len = sdslen(s); 371 if (len <= 21 && string2l(s,len,&value)) { 372 /* This object is encodable as a long. Try to use a shared object. 373 * Note that we avoid using shared integers when maxmemory is used 374 * because every object needs to have a private LRU field for the LRU 375 * algorithm to work well. */ 376 if (server.maxmemory == 0 && 377 value >= 0 && 378 value < REDIS_SHARED_INTEGERS) 379 { 380 decrRefCount(o); 381 incrRefCount(shared.integers[value]); 382 return shared.integers[value]; 383 } else { 384 if (o->encoding == REDIS_ENCODING_RAW) sdsfree(o->ptr); 385 o->encoding = REDIS_ENCODING_INT; 386 o->ptr = (void*) value; 387 return o; 388 } 389 } 390 391 /* If the string is small and is still RAW encoded, 392 * try the EMBSTR encoding which is more efficient. 393 * In this representation the object and the SDS string are allocated 394 * in the same chunk of memory to save space and cache misses. */ 395 if (len <= REDIS_ENCODING_EMBSTR_SIZE_LIMIT) { 396 robj *emb; 397 398 if (o->encoding == REDIS_ENCODING_EMBSTR) return o; 399 emb = createEmbeddedStringObject(s,sdslen(s)); 400 decrRefCount(o); 401 return emb; 402 } 403 404 /* We can't encode the object... 405 * 406 * Do the last try, and at least optimize the SDS string inside 407 * the string object to require little space, in case there 408 * is more than 10% of free space at the end of the SDS string. 409 * 410 * We do that only for relatively large strings as this branch 411 * is only entered if the length of the string is greater than 412 * REDIS_ENCODING_EMBSTR_SIZE_LIMIT. */ 413 if (o->encoding == REDIS_ENCODING_RAW && 414 sdsavail(s) > len/10) 415 { 416 o->ptr = sdsRemoveFreeSpace(o->ptr); 417 } 418 419 /* Return the original object. */ 420 return o; 421 } 422 423 /* Get a decoded version of an encoded object (returned as a new object). 424 * If the object is already raw-encoded just increment the ref count. */ 425 robj *getDecodedObject(robj *o) { 426 robj *dec; 427 428 if (sdsEncodedObject(o)) { 429 incrRefCount(o); 430 return o; 431 } 432 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) { 433 char buf[32]; 434 435 ll2string(buf,32,(long)o->ptr); 436 dec = createStringObject(buf,strlen(buf)); 437 return dec; 438 } else { 439 redisPanic("Unknown encoding type"); 440 } 441 } 442 443 /* Compare two string objects via strcmp() or strcoll() depending on flags. 444 * Note that the objects may be integer-encoded. In such a case we 445 * use ll2string() to get a string representation of the numbers on the stack 446 * and compare the strings, it's much faster than calling getDecodedObject(). 447 * 448 * Important note: when REDIS_COMPARE_BINARY is used a binary-safe comparison 449 * is used. */ 450 451 #define REDIS_COMPARE_BINARY (1<<0) 452 #define REDIS_COMPARE_COLL (1<<1) 453 454 int compareStringObjectsWithFlags(robj *a, robj *b, int flags) { 455 redisAssertWithInfo(NULL,a,a->type == REDIS_STRING && b->type == REDIS_STRING); 456 char bufa[128], bufb[128], *astr, *bstr; 457 size_t alen, blen, minlen; 458 459 if (a == b) return 0; 460 if (sdsEncodedObject(a)) { 461 astr = a->ptr; 462 alen = sdslen(astr); 463 } else { 464 alen = ll2string(bufa,sizeof(bufa),(long) a->ptr); 465 astr = bufa; 466 } 467 if (sdsEncodedObject(b)) { 468 bstr = b->ptr; 469 blen = sdslen(bstr); 470 } else { 471 blen = ll2string(bufb,sizeof(bufb),(long) b->ptr); 472 bstr = bufb; 473 } 474 if (flags & REDIS_COMPARE_COLL) { 475 return strcoll(astr,bstr); 476 } else { 477 int cmp; 478 479 minlen = (alen < blen) ? alen : blen; 480 cmp = memcmp(astr,bstr,minlen); 481 if (cmp == 0) return alen-blen; 482 return cmp; 483 } 484 } 485 486 /* Wrapper for compareStringObjectsWithFlags() using binary comparison. */ 487 int compareStringObjects(robj *a, robj *b) { 488 return compareStringObjectsWithFlags(a,b,REDIS_COMPARE_BINARY); 489 } 490 491 /* Wrapper for compareStringObjectsWithFlags() using collation. */ 492 int collateStringObjects(robj *a, robj *b) { 493 return compareStringObjectsWithFlags(a,b,REDIS_COMPARE_COLL); 494 } 495 496 /* Equal string objects return 1 if the two objects are the same from the 497 * point of view of a string comparison, otherwise 0 is returned. Note that 498 * this function is faster then checking for (compareStringObject(a,b) == 0) 499 * because it can perform some more optimization. */ 500 int equalStringObjects(robj *a, robj *b) { 501 if (a->encoding == REDIS_ENCODING_INT && 502 b->encoding == REDIS_ENCODING_INT){ 503 /* If both strings are integer encoded just check if the stored 504 * long is the same. */ 505 return a->ptr == b->ptr; 506 } else { 507 return compareStringObjects(a,b) == 0; 508 } 509 } 510 511 size_t stringObjectLen(robj *o) { 512 redisAssertWithInfo(NULL,o,o->type == REDIS_STRING); 513 if (sdsEncodedObject(o)) { 514 return sdslen(o->ptr); 515 } else { 516 char buf[32]; 517 518 return ll2string(buf,32,(long)o->ptr); 519 } 520 } 521 522 int getDoubleFromObject(robj *o, double *target) { 523 double value; 524 char *eptr; 525 526 if (o == NULL) { 527 value = 0; 528 } else { 529 redisAssertWithInfo(NULL,o,o->type == REDIS_STRING); 530 if (sdsEncodedObject(o)) { 531 errno = 0; 532 value = strtod(o->ptr, &eptr); 533 if (isspace(((char*)o->ptr)[0]) || 534 eptr[0] != '\0' || 535 (errno == ERANGE && 536 (value == HUGE_VAL || value == -HUGE_VAL || value == 0)) || 537 errno == EINVAL || 538 isnan(value)) 539 return REDIS_ERR; 540 } else if (o->encoding == REDIS_ENCODING_INT) { 541 value = (long)o->ptr; 542 } else { 543 redisPanic("Unknown string encoding"); 544 } 545 } 546 *target = value; 547 return REDIS_OK; 548 } 549 550 int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg) { 551 double value; 552 if (getDoubleFromObject(o, &value) != REDIS_OK) { 553 if (msg != NULL) { 554 addReplyError(c,(char*)msg); 555 } else { 556 addReplyError(c,"value is not a valid float"); 557 } 558 return REDIS_ERR; 559 } 560 *target = value; 561 return REDIS_OK; 562 } 563 564 int getLongDoubleFromObject(robj *o, long double *target) { 565 long double value; 566 char *eptr; 567 568 if (o == NULL) { 569 value = 0; 570 } else { 571 redisAssertWithInfo(NULL,o,o->type == REDIS_STRING); 572 if (sdsEncodedObject(o)) { 573 errno = 0; 574 value = strtold(o->ptr, &eptr); 575 if (isspace(((char*)o->ptr)[0]) || eptr[0] != '\0' || 576 errno == ERANGE || isnan(value)) 577 return REDIS_ERR; 578 } else if (o->encoding == REDIS_ENCODING_INT) { 579 value = (long)o->ptr; 580 } else { 581 redisPanic("Unknown string encoding"); 582 } 583 } 584 *target = value; 585 return REDIS_OK; 586 } 587 588 int getLongDoubleFromObjectOrReply(redisClient *c, robj *o, long double *target, const char *msg) { 589 long double value; 590 if (getLongDoubleFromObject(o, &value) != REDIS_OK) { 591 if (msg != NULL) { 592 addReplyError(c,(char*)msg); 593 } else { 594 addReplyError(c,"value is not a valid float"); 595 } 596 return REDIS_ERR; 597 } 598 *target = value; 599 return REDIS_OK; 600 } 601 602 int getLongLongFromObject(robj *o, long long *target) { 603 long long value; 604 char *eptr; 605 606 if (o == NULL) { 607 value = 0; 608 } else { 609 redisAssertWithInfo(NULL,o,o->type == REDIS_STRING); 610 if (sdsEncodedObject(o)) { 611 errno = 0; 612 value = strtoll(o->ptr, &eptr, 10); 613 if (isspace(((char*)o->ptr)[0]) || eptr[0] != '\0' || 614 errno == ERANGE) 615 return REDIS_ERR; 616 } else if (o->encoding == REDIS_ENCODING_INT) { 617 value = (long)o->ptr; 618 } else { 619 redisPanic("Unknown string encoding"); 620 } 621 } 622 if (target) *target = value; 623 return REDIS_OK; 624 } 625 626 int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg) { 627 long long value; 628 if (getLongLongFromObject(o, &value) != REDIS_OK) { 629 if (msg != NULL) { 630 addReplyError(c,(char*)msg); 631 } else { 632 addReplyError(c,"value is not an integer or out of range"); 633 } 634 return REDIS_ERR; 635 } 636 *target = value; 637 return REDIS_OK; 638 } 639 640 int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg) { 641 long long value; 642 643 if (getLongLongFromObjectOrReply(c, o, &value, msg) != REDIS_OK) return REDIS_ERR; 644 if (value < LONG_MIN || value > LONG_MAX) { 645 if (msg != NULL) { 646 addReplyError(c,(char*)msg); 647 } else { 648 addReplyError(c,"value is out of range"); 649 } 650 return REDIS_ERR; 651 } 652 *target = value; 653 return REDIS_OK; 654 } 655 656 char *strEncoding(int encoding) { 657 switch(encoding) { 658 case REDIS_ENCODING_RAW: return "raw"; 659 case REDIS_ENCODING_INT: return "int"; 660 case REDIS_ENCODING_HT: return "hashtable"; 661 case REDIS_ENCODING_LINKEDLIST: return "linkedlist"; 662 case REDIS_ENCODING_ZIPLIST: return "ziplist"; 663 case REDIS_ENCODING_INTSET: return "intset"; 664 case REDIS_ENCODING_SKIPLIST: return "skiplist"; 665 case REDIS_ENCODING_EMBSTR: return "embstr"; 666 default: return "unknown"; 667 } 668 } 669 670 /* Given an object returns the min number of milliseconds the object was never 671 * requested, using an approximated LRU algorithm. */ 672 unsigned long long estimateObjectIdleTime(robj *o) { 673 unsigned long long lruclock = LRU_CLOCK(); 674 if (lruclock >= o->lru) { 675 return (lruclock - o->lru) * REDIS_LRU_CLOCK_RESOLUTION; 676 } else { 677 return (lruclock + (REDIS_LRU_CLOCK_MAX - o->lru)) * 678 REDIS_LRU_CLOCK_RESOLUTION; 679 } 680 } 681 682 /* This is a helper function for the OBJECT command. We need to lookup keys 683 * without any modification of LRU or other parameters. */ 684 robj *objectCommandLookup(redisClient *c, robj *key) { 685 dictEntry *de; 686 687 if ((de = dictFind(c->db->dict,key->ptr)) == NULL) return NULL; 688 return (robj*) dictGetVal(de); 689 } 690 691 robj *objectCommandLookupOrReply(redisClient *c, robj *key, robj *reply) { 692 robj *o = objectCommandLookup(c,key); 693 694 if (!o) addReply(c, reply); 695 return o; 696 } 697 698 /* Object command allows to inspect the internals of an Redis Object. 699 * Usage: OBJECT <verb> ... arguments ... */ 700 void objectCommand(redisClient *c) { 701 robj *o; 702 703 if (!strcasecmp(c->argv[1]->ptr,"refcount") && c->argc == 3) { 704 if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) 705 == NULL) return; 706 addReplyLongLong(c,o->refcount); 707 } else if (!strcasecmp(c->argv[1]->ptr,"encoding") && c->argc == 3) { 708 if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) 709 == NULL) return; 710 addReplyBulkCString(c,strEncoding(o->encoding)); 711 } else if (!strcasecmp(c->argv[1]->ptr,"idletime") && c->argc == 3) { 712 if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) 713 == NULL) return; 714 addReplyLongLong(c,estimateObjectIdleTime(o)/1000); 715 } else { 716 addReplyError(c,"Syntax error. Try OBJECT (refcount|encoding|idletime)"); 717 } 718 } 719 720