1 /* Redis Object implementation.
3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
35 robj
*createObject(int type
, void *ptr
) {
36 robj
*o
= zmalloc(sizeof(*o
));
39 o
->encoding
= REDIS_ENCODING_RAW
;
43 /* Set the LRU to the current lruclock (minutes resolution). */
44 o
->lru
= server
.lruclock
;
48 robj
*createStringObject(char *ptr
, size_t len
) {
49 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
52 robj
*createStringObjectFromLongLong(long long value
) {
54 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
55 incrRefCount(shared
.integers
[value
]);
56 o
= shared
.integers
[value
];
58 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
59 o
= createObject(REDIS_STRING
, NULL
);
60 o
->encoding
= REDIS_ENCODING_INT
;
61 o
->ptr
= (void*)((long)value
);
63 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
69 /* Note: this function is defined into object.c since here it is where it
70 * belongs but it is actually designed to be used just for INCRBYFLOAT */
71 robj
*createStringObjectFromLongDouble(long double value
) {
75 /* We use 17 digits precision since with 128 bit floats that precision
76 * after rouding is able to represent most small decimal numbers in a way
77 * that is "non surprising" for the user (that is, most small decimal
78 * numbers will be represented in a way that when converted back into
79 * a string are exactly the same as what the user typed.) */
80 len
= snprintf(buf
,sizeof(buf
),"%.17Lf", value
);
81 /* Now remove trailing zeroes after the '.' */
82 if (strchr(buf
,'.') != NULL
) {
90 return createStringObject(buf
,len
);
93 robj
*dupStringObject(robj
*o
) {
94 redisAssertWithInfo(NULL
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
95 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
98 robj
*createListObject(void) {
99 list
*l
= listCreate();
100 robj
*o
= createObject(REDIS_LIST
,l
);
101 listSetFreeMethod(l
,decrRefCount
);
102 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
106 robj
*createZiplistObject(void) {
107 unsigned char *zl
= ziplistNew();
108 robj
*o
= createObject(REDIS_LIST
,zl
);
109 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
113 robj
*createSetObject(void) {
114 dict
*d
= dictCreate(&setDictType
,NULL
);
115 robj
*o
= createObject(REDIS_SET
,d
);
116 o
->encoding
= REDIS_ENCODING_HT
;
120 robj
*createIntsetObject(void) {
121 intset
*is
= intsetNew();
122 robj
*o
= createObject(REDIS_SET
,is
);
123 o
->encoding
= REDIS_ENCODING_INTSET
;
127 robj
*createHashObject(void) {
128 unsigned char *zl
= ziplistNew();
129 robj
*o
= createObject(REDIS_HASH
, zl
);
130 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
134 robj
*createZsetObject(void) {
135 zset
*zs
= zmalloc(sizeof(*zs
));
138 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
139 zs
->zsl
= zslCreate();
140 o
= createObject(REDIS_ZSET
,zs
);
141 o
->encoding
= REDIS_ENCODING_SKIPLIST
;
145 robj
*createZsetZiplistObject(void) {
146 unsigned char *zl
= ziplistNew();
147 robj
*o
= createObject(REDIS_ZSET
,zl
);
148 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
152 void freeStringObject(robj
*o
) {
153 if (o
->encoding
== REDIS_ENCODING_RAW
) {
158 void freeListObject(robj
*o
) {
159 switch (o
->encoding
) {
160 case REDIS_ENCODING_LINKEDLIST
:
161 listRelease((list
*) o
->ptr
);
163 case REDIS_ENCODING_ZIPLIST
:
167 redisPanic("Unknown list encoding type");
171 void freeSetObject(robj
*o
) {
172 switch (o
->encoding
) {
173 case REDIS_ENCODING_HT
:
174 dictRelease((dict
*) o
->ptr
);
176 case REDIS_ENCODING_INTSET
:
180 redisPanic("Unknown set encoding type");
184 void freeZsetObject(robj
*o
) {
186 switch (o
->encoding
) {
187 case REDIS_ENCODING_SKIPLIST
:
189 dictRelease(zs
->dict
);
193 case REDIS_ENCODING_ZIPLIST
:
197 redisPanic("Unknown sorted set encoding");
201 void freeHashObject(robj
*o
) {
202 switch (o
->encoding
) {
203 case REDIS_ENCODING_HT
:
204 dictRelease((dict
*) o
->ptr
);
206 case REDIS_ENCODING_ZIPLIST
:
210 redisPanic("Unknown hash encoding type");
215 void incrRefCount(robj
*o
) {
219 void decrRefCount(void *obj
) {
222 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
223 if (o
->refcount
== 1) {
225 case REDIS_STRING
: freeStringObject(o
); break;
226 case REDIS_LIST
: freeListObject(o
); break;
227 case REDIS_SET
: freeSetObject(o
); break;
228 case REDIS_ZSET
: freeZsetObject(o
); break;
229 case REDIS_HASH
: freeHashObject(o
); break;
230 default: redisPanic("Unknown object type"); break;
238 /* This function set the ref count to zero without freeing the object.
239 * It is useful in order to pass a new object to functions incrementing
240 * the ref count of the received object. Example:
242 * functionThatWillIncrementRefCount(resetRefCount(CreateObject(...)));
244 * Otherwise you need to resort to the less elegant pattern:
246 * *obj = createObject(...);
247 * functionThatWillIncrementRefCount(obj);
250 robj
*resetRefCount(robj
*obj
) {
255 int checkType(redisClient
*c
, robj
*o
, int type
) {
256 if (o
->type
!= type
) {
257 addReply(c
,shared
.wrongtypeerr
);
263 int isObjectRepresentableAsLongLong(robj
*o
, long long *llval
) {
264 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
265 if (o
->encoding
== REDIS_ENCODING_INT
) {
266 if (llval
) *llval
= (long) o
->ptr
;
269 return string2ll(o
->ptr
,sdslen(o
->ptr
),llval
) ? REDIS_OK
: REDIS_ERR
;
273 /* Try to encode a string object in order to save space */
274 robj
*tryObjectEncoding(robj
*o
) {
278 if (o
->encoding
!= REDIS_ENCODING_RAW
)
279 return o
; /* Already encoded */
281 /* It's not safe to encode shared objects: shared objects can be shared
282 * everywhere in the "object space" of Redis. Encoded objects can only
283 * appear as "values" (and not, for instance, as keys) */
284 if (o
->refcount
> 1) return o
;
286 /* Currently we try to encode only strings */
287 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
289 /* Check if we can represent this string as a long integer */
290 if (!string2l(s
,sdslen(s
),&value
)) return o
;
292 /* Ok, this object can be encoded...
294 * Can I use a shared object? Only if the object is inside a given range
296 * Note that we also avoid using shared integers when maxmemory is used
297 * because every object needs to have a private LRU field for the LRU
298 * algorithm to work well. */
299 if (server
.maxmemory
== 0 && value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
301 incrRefCount(shared
.integers
[value
]);
302 return shared
.integers
[value
];
304 o
->encoding
= REDIS_ENCODING_INT
;
306 o
->ptr
= (void*) value
;
311 /* Get a decoded version of an encoded object (returned as a new object).
312 * If the object is already raw-encoded just increment the ref count. */
313 robj
*getDecodedObject(robj
*o
) {
316 if (o
->encoding
== REDIS_ENCODING_RAW
) {
320 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
323 ll2string(buf
,32,(long)o
->ptr
);
324 dec
= createStringObject(buf
,strlen(buf
));
327 redisPanic("Unknown encoding type");
331 /* Compare two string objects via strcmp() or alike.
332 * Note that the objects may be integer-encoded. In such a case we
333 * use ll2string() to get a string representation of the numbers on the stack
334 * and compare the strings, it's much faster than calling getDecodedObject().
336 * Important note: if objects are not integer encoded, but binary-safe strings,
337 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
339 int compareStringObjects(robj
*a
, robj
*b
) {
340 redisAssertWithInfo(NULL
,a
,a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
341 char bufa
[128], bufb
[128], *astr
, *bstr
;
344 if (a
== b
) return 0;
345 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
346 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
352 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
353 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
359 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
362 /* Equal string objects return 1 if the two objects are the same from the
363 * point of view of a string comparison, otherwise 0 is returned. Note that
364 * this function is faster then checking for (compareStringObject(a,b) == 0)
365 * because it can perform some more optimization. */
366 int equalStringObjects(robj
*a
, robj
*b
) {
367 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
368 return a
->ptr
== b
->ptr
;
370 return compareStringObjects(a
,b
) == 0;
374 size_t stringObjectLen(robj
*o
) {
375 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
376 if (o
->encoding
== REDIS_ENCODING_RAW
) {
377 return sdslen(o
->ptr
);
381 return ll2string(buf
,32,(long)o
->ptr
);
385 int getDoubleFromObject(robj
*o
, double *target
) {
392 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
393 if (o
->encoding
== REDIS_ENCODING_RAW
) {
395 value
= strtod(o
->ptr
, &eptr
);
396 if (isspace(((char*)o
->ptr
)[0]) || eptr
[0] != '\0' ||
397 errno
== ERANGE
|| isnan(value
))
399 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
400 value
= (long)o
->ptr
;
402 redisPanic("Unknown string encoding");
409 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
411 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
413 addReplyError(c
,(char*)msg
);
415 addReplyError(c
,"value is not a valid float");
423 int getLongDoubleFromObject(robj
*o
, long double *target
) {
430 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
431 if (o
->encoding
== REDIS_ENCODING_RAW
) {
433 value
= strtold(o
->ptr
, &eptr
);
434 if (isspace(((char*)o
->ptr
)[0]) || eptr
[0] != '\0' ||
435 errno
== ERANGE
|| isnan(value
))
437 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
438 value
= (long)o
->ptr
;
440 redisPanic("Unknown string encoding");
447 int getLongDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, long double *target
, const char *msg
) {
449 if (getLongDoubleFromObject(o
, &value
) != REDIS_OK
) {
451 addReplyError(c
,(char*)msg
);
453 addReplyError(c
,"value is not a valid float");
461 int getLongLongFromObject(robj
*o
, long long *target
) {
468 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
469 if (o
->encoding
== REDIS_ENCODING_RAW
) {
471 value
= strtoll(o
->ptr
, &eptr
, 10);
472 if (isspace(((char*)o
->ptr
)[0]) || eptr
[0] != '\0' ||
475 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
476 value
= (long)o
->ptr
;
478 redisPanic("Unknown string encoding");
481 if (target
) *target
= value
;
485 int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
487 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
489 addReplyError(c
,(char*)msg
);
491 addReplyError(c
,"value is not an integer or out of range");
499 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
502 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
503 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
505 addReplyError(c
,(char*)msg
);
507 addReplyError(c
,"value is out of range");
515 char *strEncoding(int encoding
) {
517 case REDIS_ENCODING_RAW
: return "raw";
518 case REDIS_ENCODING_INT
: return "int";
519 case REDIS_ENCODING_HT
: return "hashtable";
520 case REDIS_ENCODING_LINKEDLIST
: return "linkedlist";
521 case REDIS_ENCODING_ZIPLIST
: return "ziplist";
522 case REDIS_ENCODING_INTSET
: return "intset";
523 case REDIS_ENCODING_SKIPLIST
: return "skiplist";
524 default: return "unknown";
528 /* Given an object returns the min number of seconds the object was never
529 * requested, using an approximated LRU algorithm. */
530 unsigned long estimateObjectIdleTime(robj
*o
) {
531 if (server
.lruclock
>= o
->lru
) {
532 return (server
.lruclock
- o
->lru
) * REDIS_LRU_CLOCK_RESOLUTION
;
534 return ((REDIS_LRU_CLOCK_MAX
- o
->lru
) + server
.lruclock
) *
535 REDIS_LRU_CLOCK_RESOLUTION
;
539 /* This is an helper function for the DEBUG command. We need to lookup keys
540 * without any modification of LRU or other parameters. */
541 robj
*objectCommandLookup(redisClient
*c
, robj
*key
) {
544 if ((de
= dictFind(c
->db
->dict
,key
->ptr
)) == NULL
) return NULL
;
545 return (robj
*) dictGetVal(de
);
548 robj
*objectCommandLookupOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
549 robj
*o
= objectCommandLookup(c
,key
);
551 if (!o
) addReply(c
, reply
);
555 /* Object command allows to inspect the internals of an Redis Object.
556 * Usage: OBJECT <verb> ... arguments ... */
557 void objectCommand(redisClient
*c
) {
560 if (!strcasecmp(c
->argv
[1]->ptr
,"refcount") && c
->argc
== 3) {
561 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
563 addReplyLongLong(c
,o
->refcount
);
564 } else if (!strcasecmp(c
->argv
[1]->ptr
,"encoding") && c
->argc
== 3) {
565 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
567 addReplyBulkCString(c
,strEncoding(o
->encoding
));
568 } else if (!strcasecmp(c
->argv
[1]->ptr
,"idletime") && c
->argc
== 3) {
569 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
571 addReplyLongLong(c
,estimateObjectIdleTime(o
));
573 addReplyError(c
,"Syntax error. Try OBJECT (refcount|encoding|idletime)");