4 robj
*createObject(int type
, void *ptr
) {
5 robj
*o
= zmalloc(sizeof(*o
));
7 o
->encoding
= REDIS_ENCODING_RAW
;
11 /* Set the LRU to the current lruclock (minutes resolution).
12 * We do this regardless of the fact VM is active as LRU is also
13 * used for the maxmemory directive when Redis is used as cache.
15 * Note that this code may run in the context of an I/O thread
16 * and accessing server.lruclock in theory is an error
17 * (no locks). But in practice this is safe, and even if we read
18 * garbage Redis will not fail. */
19 o
->lru
= server
.lruclock
;
20 /* The following is only needed if VM is active, but since the conditional
21 * is probably more costly than initializing the field it's better to
22 * have every field properly initialized anyway. */
26 robj
*createStringObject(char *ptr
, size_t len
) {
27 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
30 robj
*createStringObjectFromLongLong(long long value
) {
32 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
33 incrRefCount(shared
.integers
[value
]);
34 o
= shared
.integers
[value
];
36 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
37 o
= createObject(REDIS_STRING
, NULL
);
38 o
->encoding
= REDIS_ENCODING_INT
;
39 o
->ptr
= (void*)((long)value
);
41 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
47 /* Note: this function is defined into object.c since here it is where it
48 * belongs but it is actually designed to be used just for INCRBYFLOAT */
49 robj
*createStringObjectFromLongDouble(long double value
) {
53 /* We use 17 digits precision since with 128 bit floats that precision
54 * after rouding is able to represent most small decimal numbers in a way
55 * that is "non surprising" for the user (that is, most small decimal
56 * numbers will be represented in a way that when converted back into
57 * a string are exactly the same as what the user typed.) */
58 len
= snprintf(buf
,sizeof(buf
),"%.17Lg", value
);
59 return createStringObject(buf
,len
);
62 robj
*dupStringObject(robj
*o
) {
63 redisAssertWithInfo(NULL
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
64 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
67 robj
*createListObject(void) {
68 list
*l
= listCreate();
69 robj
*o
= createObject(REDIS_LIST
,l
);
70 listSetFreeMethod(l
,decrRefCount
);
71 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
75 robj
*createZiplistObject(void) {
76 unsigned char *zl
= ziplistNew();
77 robj
*o
= createObject(REDIS_LIST
,zl
);
78 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
82 robj
*createSetObject(void) {
83 dict
*d
= dictCreate(&setDictType
,NULL
);
84 robj
*o
= createObject(REDIS_SET
,d
);
85 o
->encoding
= REDIS_ENCODING_HT
;
89 robj
*createIntsetObject(void) {
90 intset
*is
= intsetNew();
91 robj
*o
= createObject(REDIS_SET
,is
);
92 o
->encoding
= REDIS_ENCODING_INTSET
;
96 robj
*createHashObject(void) {
97 /* All the Hashes start as zipmaps. Will be automatically converted
98 * into hash tables if there are enough elements or big elements
100 unsigned char *zm
= zipmapNew();
101 robj
*o
= createObject(REDIS_HASH
,zm
);
102 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
106 robj
*createZsetObject(void) {
107 zset
*zs
= zmalloc(sizeof(*zs
));
110 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
111 zs
->zsl
= zslCreate();
112 o
= createObject(REDIS_ZSET
,zs
);
113 o
->encoding
= REDIS_ENCODING_SKIPLIST
;
117 robj
*createZsetZiplistObject(void) {
118 unsigned char *zl
= ziplistNew();
119 robj
*o
= createObject(REDIS_ZSET
,zl
);
120 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
124 void freeStringObject(robj
*o
) {
125 if (o
->encoding
== REDIS_ENCODING_RAW
) {
130 void freeListObject(robj
*o
) {
131 switch (o
->encoding
) {
132 case REDIS_ENCODING_LINKEDLIST
:
133 listRelease((list
*) o
->ptr
);
135 case REDIS_ENCODING_ZIPLIST
:
139 redisPanic("Unknown list encoding type");
143 void freeSetObject(robj
*o
) {
144 switch (o
->encoding
) {
145 case REDIS_ENCODING_HT
:
146 dictRelease((dict
*) o
->ptr
);
148 case REDIS_ENCODING_INTSET
:
152 redisPanic("Unknown set encoding type");
156 void freeZsetObject(robj
*o
) {
158 switch (o
->encoding
) {
159 case REDIS_ENCODING_SKIPLIST
:
161 dictRelease(zs
->dict
);
165 case REDIS_ENCODING_ZIPLIST
:
169 redisPanic("Unknown sorted set encoding");
173 void freeHashObject(robj
*o
) {
174 switch (o
->encoding
) {
175 case REDIS_ENCODING_HT
:
176 dictRelease((dict
*) o
->ptr
);
178 case REDIS_ENCODING_ZIPMAP
:
182 redisPanic("Unknown hash encoding type");
187 void incrRefCount(robj
*o
) {
191 void decrRefCount(void *obj
) {
194 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
195 if (o
->refcount
== 1) {
197 case REDIS_STRING
: freeStringObject(o
); break;
198 case REDIS_LIST
: freeListObject(o
); break;
199 case REDIS_SET
: freeSetObject(o
); break;
200 case REDIS_ZSET
: freeZsetObject(o
); break;
201 case REDIS_HASH
: freeHashObject(o
); break;
202 default: redisPanic("Unknown object type"); break;
210 /* This function set the ref count to zero without freeing the object.
211 * It is useful in order to pass a new object to functions incrementing
212 * the ref count of the received object. Example:
214 * functionThatWillIncrementRefCount(resetRefCount(CreateObject(...)));
216 * Otherwise you need to resort to the less elegant pattern:
218 * *obj = createObject(...);
219 * functionThatWillIncrementRefCount(obj);
222 robj
*resetRefCount(robj
*obj
) {
227 int checkType(redisClient
*c
, robj
*o
, int type
) {
228 if (o
->type
!= type
) {
229 addReply(c
,shared
.wrongtypeerr
);
235 int isObjectRepresentableAsLongLong(robj
*o
, long long *llval
) {
236 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
237 if (o
->encoding
== REDIS_ENCODING_INT
) {
238 if (llval
) *llval
= (long) o
->ptr
;
241 return string2ll(o
->ptr
,sdslen(o
->ptr
),llval
) ? REDIS_OK
: REDIS_ERR
;
245 /* Try to encode a string object in order to save space */
246 robj
*tryObjectEncoding(robj
*o
) {
250 if (o
->encoding
!= REDIS_ENCODING_RAW
)
251 return o
; /* Already encoded */
253 /* It's not safe to encode shared objects: shared objects can be shared
254 * everywhere in the "object space" of Redis. Encoded objects can only
255 * appear as "values" (and not, for instance, as keys) */
256 if (o
->refcount
> 1) return o
;
258 /* Currently we try to encode only strings */
259 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
261 /* Check if we can represent this string as a long integer */
262 if (!string2l(s
,sdslen(s
),&value
)) return o
;
264 /* Ok, this object can be encoded...
266 * Can I use a shared object? Only if the object is inside a given
267 * range and if the back end in use is in-memory. For disk store every
268 * object in memory used as value should be independent.
270 * Note that we also avoid using shared integers when maxmemory is used
271 * because every object needs to have a private LRU field for the LRU
272 * algorithm to work well. */
273 if (server
.maxmemory
== 0 && value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
275 incrRefCount(shared
.integers
[value
]);
276 return shared
.integers
[value
];
278 o
->encoding
= REDIS_ENCODING_INT
;
280 o
->ptr
= (void*) value
;
285 /* Get a decoded version of an encoded object (returned as a new object).
286 * If the object is already raw-encoded just increment the ref count. */
287 robj
*getDecodedObject(robj
*o
) {
290 if (o
->encoding
== REDIS_ENCODING_RAW
) {
294 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
297 ll2string(buf
,32,(long)o
->ptr
);
298 dec
= createStringObject(buf
,strlen(buf
));
301 redisPanic("Unknown encoding type");
305 /* Compare two string objects via strcmp() or alike.
306 * Note that the objects may be integer-encoded. In such a case we
307 * use ll2string() to get a string representation of the numbers on the stack
308 * and compare the strings, it's much faster than calling getDecodedObject().
310 * Important note: if objects are not integer encoded, but binary-safe strings,
311 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
313 int compareStringObjects(robj
*a
, robj
*b
) {
314 redisAssertWithInfo(NULL
,a
,a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
315 char bufa
[128], bufb
[128], *astr
, *bstr
;
318 if (a
== b
) return 0;
319 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
320 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
326 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
327 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
333 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
336 /* Equal string objects return 1 if the two objects are the same from the
337 * point of view of a string comparison, otherwise 0 is returned. Note that
338 * this function is faster then checking for (compareStringObject(a,b) == 0)
339 * because it can perform some more optimization. */
340 int equalStringObjects(robj
*a
, robj
*b
) {
341 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
342 return a
->ptr
== b
->ptr
;
344 return compareStringObjects(a
,b
) == 0;
348 size_t stringObjectLen(robj
*o
) {
349 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
350 if (o
->encoding
== REDIS_ENCODING_RAW
) {
351 return sdslen(o
->ptr
);
355 return ll2string(buf
,32,(long)o
->ptr
);
359 int getDoubleFromObject(robj
*o
, double *target
) {
366 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
367 if (o
->encoding
== REDIS_ENCODING_RAW
) {
369 value
= strtod(o
->ptr
, &eptr
);
370 if (eptr
[0] != '\0' || errno
== ERANGE
|| isnan(value
))
372 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
373 value
= (long)o
->ptr
;
375 redisPanic("Unknown string encoding");
383 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
385 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
387 addReplyError(c
,(char*)msg
);
389 addReplyError(c
,"value is not a valid float");
398 int getLongDoubleFromObject(robj
*o
, long double *target
) {
405 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
406 if (o
->encoding
== REDIS_ENCODING_RAW
) {
408 value
= strtold(o
->ptr
, &eptr
);
409 if (eptr
[0] != '\0' || errno
== ERANGE
|| isnan(value
))
411 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
412 value
= (long)o
->ptr
;
414 redisPanic("Unknown string encoding");
421 int getLongDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, long double *target
, const char *msg
) {
423 if (getLongDoubleFromObject(o
, &value
) != REDIS_OK
) {
425 addReplyError(c
,(char*)msg
);
427 addReplyError(c
,"value is not a valid float");
436 int getLongLongFromObject(robj
*o
, long long *target
) {
443 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
444 if (o
->encoding
== REDIS_ENCODING_RAW
) {
445 value
= strtoll(o
->ptr
, &eptr
, 10);
446 if (eptr
[0] != '\0') return REDIS_ERR
;
447 if (errno
== ERANGE
&& (value
== LLONG_MIN
|| value
== LLONG_MAX
))
449 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
450 value
= (long)o
->ptr
;
452 redisPanic("Unknown string encoding");
456 if (target
) *target
= value
;
460 int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
462 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
464 addReplyError(c
,(char*)msg
);
466 addReplyError(c
,"value is not an integer or out of range");
475 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
478 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
479 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
481 addReplyError(c
,(char*)msg
);
483 addReplyError(c
,"value is out of range");
492 char *strEncoding(int encoding
) {
494 case REDIS_ENCODING_RAW
: return "raw";
495 case REDIS_ENCODING_INT
: return "int";
496 case REDIS_ENCODING_HT
: return "hashtable";
497 case REDIS_ENCODING_ZIPMAP
: return "zipmap";
498 case REDIS_ENCODING_LINKEDLIST
: return "linkedlist";
499 case REDIS_ENCODING_ZIPLIST
: return "ziplist";
500 case REDIS_ENCODING_INTSET
: return "intset";
501 case REDIS_ENCODING_SKIPLIST
: return "skiplist";
502 default: return "unknown";
506 /* Given an object returns the min number of seconds the object was never
507 * requested, using an approximated LRU algorithm. */
508 unsigned long estimateObjectIdleTime(robj
*o
) {
509 if (server
.lruclock
>= o
->lru
) {
510 return (server
.lruclock
- o
->lru
) * REDIS_LRU_CLOCK_RESOLUTION
;
512 return ((REDIS_LRU_CLOCK_MAX
- o
->lru
) + server
.lruclock
) *
513 REDIS_LRU_CLOCK_RESOLUTION
;
517 /* This is an helper function for the DEBUG command. We need to lookup keys
518 * without any modification of LRU or other parameters. */
519 robj
*objectCommandLookup(redisClient
*c
, robj
*key
) {
522 if ((de
= dictFind(c
->db
->dict
,key
->ptr
)) == NULL
) return NULL
;
523 return (robj
*) dictGetVal(de
);
526 robj
*objectCommandLookupOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
527 robj
*o
= objectCommandLookup(c
,key
);
529 if (!o
) addReply(c
, reply
);
533 /* Object command allows to inspect the internals of an Redis Object.
534 * Usage: OBJECT <verb> ... arguments ... */
535 void objectCommand(redisClient
*c
) {
538 if (!strcasecmp(c
->argv
[1]->ptr
,"refcount") && c
->argc
== 3) {
539 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
541 addReplyLongLong(c
,o
->refcount
);
542 } else if (!strcasecmp(c
->argv
[1]->ptr
,"encoding") && c
->argc
== 3) {
543 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
545 addReplyBulkCString(c
,strEncoding(o
->encoding
));
546 } else if (!strcasecmp(c
->argv
[1]->ptr
,"idletime") && c
->argc
== 3) {
547 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
549 addReplyLongLong(c
,estimateObjectIdleTime(o
));
551 addReplyError(c
,"Syntax error. Try OBJECT (refcount|encoding|idletime)");