5 robj
*createObject(int type
, void *ptr
) {
6 robj
*o
= zmalloc(sizeof(*o
));
8 o
->encoding
= REDIS_ENCODING_RAW
;
12 /* Set the LRU to the current lruclock (minutes resolution).
13 * We do this regardless of the fact VM is active as LRU is also
14 * used for the maxmemory directive when Redis is used as cache.
16 * Note that this code may run in the context of an I/O thread
17 * and accessing server.lruclock in theory is an error
18 * (no locks). But in practice this is safe, and even if we read
19 * garbage Redis will not fail. */
20 o
->lru
= server
.lruclock
;
21 /* The following is only needed if VM is active, but since the conditional
22 * is probably more costly than initializing the field it's better to
23 * have every field properly initialized anyway. */
27 robj
*createStringObject(char *ptr
, size_t len
) {
28 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
31 robj
*createStringObjectFromLongLong(long long value
) {
33 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
34 incrRefCount(shared
.integers
[value
]);
35 o
= shared
.integers
[value
];
37 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
38 o
= createObject(REDIS_STRING
, NULL
);
39 o
->encoding
= REDIS_ENCODING_INT
;
40 o
->ptr
= (void*)((long)value
);
42 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
48 /* Note: this function is defined into object.c since here it is where it
49 * belongs but it is actually designed to be used just for INCRBYFLOAT */
50 robj
*createStringObjectFromLongDouble(long double value
) {
54 /* We use 17 digits precision since with 128 bit floats that precision
55 * after rouding is able to represent most small decimal numbers in a way
56 * that is "non surprising" for the user (that is, most small decimal
57 * numbers will be represented in a way that when converted back into
58 * a string are exactly the same as what the user typed.) */
59 len
= snprintf(buf
,sizeof(buf
),"%.17Lg", value
);
60 return createStringObject(buf
,len
);
63 robj
*dupStringObject(robj
*o
) {
64 redisAssertWithInfo(NULL
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
65 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
68 robj
*createListObject(void) {
69 list
*l
= listCreate();
70 robj
*o
= createObject(REDIS_LIST
,l
);
71 listSetFreeMethod(l
,decrRefCount
);
72 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
76 robj
*createZiplistObject(void) {
77 unsigned char *zl
= ziplistNew();
78 robj
*o
= createObject(REDIS_LIST
,zl
);
79 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
83 robj
*createSetObject(void) {
84 dict
*d
= dictCreate(&setDictType
,NULL
);
85 robj
*o
= createObject(REDIS_SET
,d
);
86 o
->encoding
= REDIS_ENCODING_HT
;
90 robj
*createIntsetObject(void) {
91 intset
*is
= intsetNew();
92 robj
*o
= createObject(REDIS_SET
,is
);
93 o
->encoding
= REDIS_ENCODING_INTSET
;
97 robj
*createHashObject(void) {
98 /* All the Hashes start as zipmaps. Will be automatically converted
99 * into hash tables if there are enough elements or big elements
101 unsigned char *zm
= zipmapNew();
102 robj
*o
= createObject(REDIS_HASH
,zm
);
103 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
107 robj
*createZsetObject(void) {
108 zset
*zs
= zmalloc(sizeof(*zs
));
111 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
112 zs
->zsl
= zslCreate();
113 o
= createObject(REDIS_ZSET
,zs
);
114 o
->encoding
= REDIS_ENCODING_SKIPLIST
;
118 robj
*createZsetZiplistObject(void) {
119 unsigned char *zl
= ziplistNew();
120 robj
*o
= createObject(REDIS_ZSET
,zl
);
121 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
125 void freeStringObject(robj
*o
) {
126 if (o
->encoding
== REDIS_ENCODING_RAW
) {
131 void freeListObject(robj
*o
) {
132 switch (o
->encoding
) {
133 case REDIS_ENCODING_LINKEDLIST
:
134 listRelease((list
*) o
->ptr
);
136 case REDIS_ENCODING_ZIPLIST
:
140 redisPanic("Unknown list encoding type");
144 void freeSetObject(robj
*o
) {
145 switch (o
->encoding
) {
146 case REDIS_ENCODING_HT
:
147 dictRelease((dict
*) o
->ptr
);
149 case REDIS_ENCODING_INTSET
:
153 redisPanic("Unknown set encoding type");
157 void freeZsetObject(robj
*o
) {
159 switch (o
->encoding
) {
160 case REDIS_ENCODING_SKIPLIST
:
162 dictRelease(zs
->dict
);
166 case REDIS_ENCODING_ZIPLIST
:
170 redisPanic("Unknown sorted set encoding");
174 void freeHashObject(robj
*o
) {
175 switch (o
->encoding
) {
176 case REDIS_ENCODING_HT
:
177 dictRelease((dict
*) o
->ptr
);
179 case REDIS_ENCODING_ZIPMAP
:
183 redisPanic("Unknown hash encoding type");
188 void incrRefCount(robj
*o
) {
192 void decrRefCount(void *obj
) {
195 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
196 if (o
->refcount
== 1) {
198 case REDIS_STRING
: freeStringObject(o
); break;
199 case REDIS_LIST
: freeListObject(o
); break;
200 case REDIS_SET
: freeSetObject(o
); break;
201 case REDIS_ZSET
: freeZsetObject(o
); break;
202 case REDIS_HASH
: freeHashObject(o
); break;
203 default: redisPanic("Unknown object type"); break;
211 /* This function set the ref count to zero without freeing the object.
212 * It is useful in order to pass a new object to functions incrementing
213 * the ref count of the received object. Example:
215 * functionThatWillIncrementRefCount(resetRefCount(CreateObject(...)));
217 * Otherwise you need to resort to the less elegant pattern:
219 * *obj = createObject(...);
220 * functionThatWillIncrementRefCount(obj);
223 robj
*resetRefCount(robj
*obj
) {
228 int checkType(redisClient
*c
, robj
*o
, int type
) {
229 if (o
->type
!= type
) {
230 addReply(c
,shared
.wrongtypeerr
);
236 int isObjectRepresentableAsLongLong(robj
*o
, long long *llval
) {
237 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
238 if (o
->encoding
== REDIS_ENCODING_INT
) {
239 if (llval
) *llval
= (long) o
->ptr
;
242 return string2ll(o
->ptr
,sdslen(o
->ptr
),llval
) ? REDIS_OK
: REDIS_ERR
;
246 /* Try to encode a string object in order to save space */
247 robj
*tryObjectEncoding(robj
*o
) {
251 if (o
->encoding
!= REDIS_ENCODING_RAW
)
252 return o
; /* Already encoded */
254 /* It's not safe to encode shared objects: shared objects can be shared
255 * everywhere in the "object space" of Redis. Encoded objects can only
256 * appear as "values" (and not, for instance, as keys) */
257 if (o
->refcount
> 1) return o
;
259 /* Currently we try to encode only strings */
260 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
262 /* Check if we can represent this string as a long integer */
263 if (!string2l(s
,sdslen(s
),&value
)) return o
;
265 /* Ok, this object can be encoded...
267 * Can I use a shared object? Only if the object is inside a given
268 * range and if the back end in use is in-memory. For disk store every
269 * object in memory used as value should be independent.
271 * Note that we also avoid using shared integers when maxmemory is used
272 * because every object needs to have a private LRU field for the LRU
273 * algorithm to work well. */
274 if (server
.maxmemory
== 0 && value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
276 incrRefCount(shared
.integers
[value
]);
277 return shared
.integers
[value
];
279 o
->encoding
= REDIS_ENCODING_INT
;
281 o
->ptr
= (void*) value
;
286 /* Get a decoded version of an encoded object (returned as a new object).
287 * If the object is already raw-encoded just increment the ref count. */
288 robj
*getDecodedObject(robj
*o
) {
291 if (o
->encoding
== REDIS_ENCODING_RAW
) {
295 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
298 ll2string(buf
,32,(long)o
->ptr
);
299 dec
= createStringObject(buf
,strlen(buf
));
302 redisPanic("Unknown encoding type");
306 /* Compare two string objects via strcmp() or alike.
307 * Note that the objects may be integer-encoded. In such a case we
308 * use ll2string() to get a string representation of the numbers on the stack
309 * and compare the strings, it's much faster than calling getDecodedObject().
311 * Important note: if objects are not integer encoded, but binary-safe strings,
312 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
314 int compareStringObjects(robj
*a
, robj
*b
) {
315 redisAssertWithInfo(NULL
,a
,a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
316 char bufa
[128], bufb
[128], *astr
, *bstr
;
319 if (a
== b
) return 0;
320 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
321 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
327 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
328 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
334 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
337 /* Equal string objects return 1 if the two objects are the same from the
338 * point of view of a string comparison, otherwise 0 is returned. Note that
339 * this function is faster then checking for (compareStringObject(a,b) == 0)
340 * because it can perform some more optimization. */
341 int equalStringObjects(robj
*a
, robj
*b
) {
342 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
343 return a
->ptr
== b
->ptr
;
345 return compareStringObjects(a
,b
) == 0;
349 size_t stringObjectLen(robj
*o
) {
350 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
351 if (o
->encoding
== REDIS_ENCODING_RAW
) {
352 return sdslen(o
->ptr
);
356 return ll2string(buf
,32,(long)o
->ptr
);
360 int getDoubleFromObject(robj
*o
, double *target
) {
367 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
368 if (o
->encoding
== REDIS_ENCODING_RAW
) {
370 value
= strtod(o
->ptr
, &eptr
);
371 if (isspace(((char*)o
->ptr
)[0]) || eptr
[0] != '\0' ||
372 errno
== ERANGE
|| isnan(value
))
374 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
375 value
= (long)o
->ptr
;
377 redisPanic("Unknown string encoding");
384 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
386 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
388 addReplyError(c
,(char*)msg
);
390 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 (isspace(((char*)o
->ptr
)[0]) || eptr
[0] != '\0' ||
410 errno
== ERANGE
|| isnan(value
))
412 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
413 value
= (long)o
->ptr
;
415 redisPanic("Unknown string encoding");
422 int getLongDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, long double *target
, const char *msg
) {
424 if (getLongDoubleFromObject(o
, &value
) != REDIS_OK
) {
426 addReplyError(c
,(char*)msg
);
428 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
) {
446 value
= strtoll(o
->ptr
, &eptr
, 10);
447 if (isspace(((char*)o
->ptr
)[0]) || eptr
[0] != '\0' ||
450 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
451 value
= (long)o
->ptr
;
453 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");
474 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
477 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
478 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
480 addReplyError(c
,(char*)msg
);
482 addReplyError(c
,"value is out of range");
490 char *strEncoding(int encoding
) {
492 case REDIS_ENCODING_RAW
: return "raw";
493 case REDIS_ENCODING_INT
: return "int";
494 case REDIS_ENCODING_HT
: return "hashtable";
495 case REDIS_ENCODING_ZIPMAP
: return "zipmap";
496 case REDIS_ENCODING_LINKEDLIST
: return "linkedlist";
497 case REDIS_ENCODING_ZIPLIST
: return "ziplist";
498 case REDIS_ENCODING_INTSET
: return "intset";
499 case REDIS_ENCODING_SKIPLIST
: return "skiplist";
500 default: return "unknown";
504 /* Given an object returns the min number of seconds the object was never
505 * requested, using an approximated LRU algorithm. */
506 unsigned long estimateObjectIdleTime(robj
*o
) {
507 if (server
.lruclock
>= o
->lru
) {
508 return (server
.lruclock
- o
->lru
) * REDIS_LRU_CLOCK_RESOLUTION
;
510 return ((REDIS_LRU_CLOCK_MAX
- o
->lru
) + server
.lruclock
) *
511 REDIS_LRU_CLOCK_RESOLUTION
;
515 /* This is an helper function for the DEBUG command. We need to lookup keys
516 * without any modification of LRU or other parameters. */
517 robj
*objectCommandLookup(redisClient
*c
, robj
*key
) {
520 if ((de
= dictFind(c
->db
->dict
,key
->ptr
)) == NULL
) return NULL
;
521 return (robj
*) dictGetVal(de
);
524 robj
*objectCommandLookupOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
525 robj
*o
= objectCommandLookup(c
,key
);
527 if (!o
) addReply(c
, reply
);
531 /* Object command allows to inspect the internals of an Redis Object.
532 * Usage: OBJECT <verb> ... arguments ... */
533 void objectCommand(redisClient
*c
) {
536 if (!strcasecmp(c
->argv
[1]->ptr
,"refcount") && c
->argc
== 3) {
537 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
539 addReplyLongLong(c
,o
->refcount
);
540 } else if (!strcasecmp(c
->argv
[1]->ptr
,"encoding") && c
->argc
== 3) {
541 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
543 addReplyBulkCString(c
,strEncoding(o
->encoding
));
544 } else if (!strcasecmp(c
->argv
[1]->ptr
,"idletime") && c
->argc
== 3) {
545 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
547 addReplyLongLong(c
,estimateObjectIdleTime(o
));
549 addReplyError(c
,"Syntax error. Try OBJECT (refcount|encoding|idletime)");