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 robj
*dupStringObject(robj
*o
) {
48 redisAssertWithInfo(NULL
,o
,o
->encoding
== REDIS_ENCODING_RAW
);
49 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
52 robj
*createListObject(void) {
53 list
*l
= listCreate();
54 robj
*o
= createObject(REDIS_LIST
,l
);
55 listSetFreeMethod(l
,decrRefCount
);
56 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
60 robj
*createZiplistObject(void) {
61 unsigned char *zl
= ziplistNew();
62 robj
*o
= createObject(REDIS_LIST
,zl
);
63 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
67 robj
*createSetObject(void) {
68 dict
*d
= dictCreate(&setDictType
,NULL
);
69 robj
*o
= createObject(REDIS_SET
,d
);
70 o
->encoding
= REDIS_ENCODING_HT
;
74 robj
*createIntsetObject(void) {
75 intset
*is
= intsetNew();
76 robj
*o
= createObject(REDIS_SET
,is
);
77 o
->encoding
= REDIS_ENCODING_INTSET
;
81 robj
*createHashObject(void) {
82 /* All the Hashes start as zipmaps. Will be automatically converted
83 * into hash tables if there are enough elements or big elements
85 unsigned char *zm
= zipmapNew();
86 robj
*o
= createObject(REDIS_HASH
,zm
);
87 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
91 robj
*createZsetObject(void) {
92 zset
*zs
= zmalloc(sizeof(*zs
));
95 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
96 zs
->zsl
= zslCreate();
97 o
= createObject(REDIS_ZSET
,zs
);
98 o
->encoding
= REDIS_ENCODING_SKIPLIST
;
102 robj
*createZsetZiplistObject(void) {
103 unsigned char *zl
= ziplistNew();
104 robj
*o
= createObject(REDIS_ZSET
,zl
);
105 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
109 void freeStringObject(robj
*o
) {
110 if (o
->encoding
== REDIS_ENCODING_RAW
) {
115 void freeListObject(robj
*o
) {
116 switch (o
->encoding
) {
117 case REDIS_ENCODING_LINKEDLIST
:
118 listRelease((list
*) o
->ptr
);
120 case REDIS_ENCODING_ZIPLIST
:
124 redisPanic("Unknown list encoding type");
128 void freeSetObject(robj
*o
) {
129 switch (o
->encoding
) {
130 case REDIS_ENCODING_HT
:
131 dictRelease((dict
*) o
->ptr
);
133 case REDIS_ENCODING_INTSET
:
137 redisPanic("Unknown set encoding type");
141 void freeZsetObject(robj
*o
) {
143 switch (o
->encoding
) {
144 case REDIS_ENCODING_SKIPLIST
:
146 dictRelease(zs
->dict
);
150 case REDIS_ENCODING_ZIPLIST
:
154 redisPanic("Unknown sorted set encoding");
158 void freeHashObject(robj
*o
) {
159 switch (o
->encoding
) {
160 case REDIS_ENCODING_HT
:
161 dictRelease((dict
*) o
->ptr
);
163 case REDIS_ENCODING_ZIPMAP
:
167 redisPanic("Unknown hash encoding type");
172 void incrRefCount(robj
*o
) {
176 void decrRefCount(void *obj
) {
179 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
180 if (o
->refcount
== 1) {
182 case REDIS_STRING
: freeStringObject(o
); break;
183 case REDIS_LIST
: freeListObject(o
); break;
184 case REDIS_SET
: freeSetObject(o
); break;
185 case REDIS_ZSET
: freeZsetObject(o
); break;
186 case REDIS_HASH
: freeHashObject(o
); break;
187 default: redisPanic("Unknown object type"); break;
195 /* This function set the ref count to zero without freeing the object.
196 * It is useful in order to pass a new object to functions incrementing
197 * the ref count of the received object. Example:
199 * functionThatWillIncrementRefCount(resetRefCount(CreateObject(...)));
201 * Otherwise you need to resort to the less elegant pattern:
203 * *obj = createObject(...);
204 * functionThatWillIncrementRefCount(obj);
207 robj
*resetRefCount(robj
*obj
) {
212 int checkType(redisClient
*c
, robj
*o
, int type
) {
213 if (o
->type
!= type
) {
214 addReply(c
,shared
.wrongtypeerr
);
220 int isObjectRepresentableAsLongLong(robj
*o
, long long *llval
) {
221 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
222 if (o
->encoding
== REDIS_ENCODING_INT
) {
223 if (llval
) *llval
= (long) o
->ptr
;
226 return string2ll(o
->ptr
,sdslen(o
->ptr
),llval
) ? REDIS_OK
: REDIS_ERR
;
230 /* Try to encode a string object in order to save space */
231 robj
*tryObjectEncoding(robj
*o
) {
235 if (o
->encoding
!= REDIS_ENCODING_RAW
)
236 return o
; /* Already encoded */
238 /* It's not safe to encode shared objects: shared objects can be shared
239 * everywhere in the "object space" of Redis. Encoded objects can only
240 * appear as "values" (and not, for instance, as keys) */
241 if (o
->refcount
> 1) return o
;
243 /* Currently we try to encode only strings */
244 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
246 /* Check if we can represent this string as a long integer */
247 if (!string2l(s
,sdslen(s
),&value
)) return o
;
249 /* Ok, this object can be encoded...
251 * Can I use a shared object? Only if the object is inside a given
252 * range and if the back end in use is in-memory. For disk store every
253 * object in memory used as value should be independent.
255 * Note that we also avoid using shared integers when maxmemory is used
256 * because every object needs to have a private LRU field for the LRU
257 * algorithm to work well. */
258 if (server
.maxmemory
== 0 && value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
260 incrRefCount(shared
.integers
[value
]);
261 return shared
.integers
[value
];
263 o
->encoding
= REDIS_ENCODING_INT
;
265 o
->ptr
= (void*) value
;
270 /* Get a decoded version of an encoded object (returned as a new object).
271 * If the object is already raw-encoded just increment the ref count. */
272 robj
*getDecodedObject(robj
*o
) {
275 if (o
->encoding
== REDIS_ENCODING_RAW
) {
279 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
282 ll2string(buf
,32,(long)o
->ptr
);
283 dec
= createStringObject(buf
,strlen(buf
));
286 redisPanic("Unknown encoding type");
290 /* Compare two string objects via strcmp() or alike.
291 * Note that the objects may be integer-encoded. In such a case we
292 * use ll2string() to get a string representation of the numbers on the stack
293 * and compare the strings, it's much faster than calling getDecodedObject().
295 * Important note: if objects are not integer encoded, but binary-safe strings,
296 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
298 int compareStringObjects(robj
*a
, robj
*b
) {
299 redisAssertWithInfo(NULL
,a
,a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
300 char bufa
[128], bufb
[128], *astr
, *bstr
;
303 if (a
== b
) return 0;
304 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
305 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
311 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
312 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
318 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
321 /* Equal string objects return 1 if the two objects are the same from the
322 * point of view of a string comparison, otherwise 0 is returned. Note that
323 * this function is faster then checking for (compareStringObject(a,b) == 0)
324 * because it can perform some more optimization. */
325 int equalStringObjects(robj
*a
, robj
*b
) {
326 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
327 return a
->ptr
== b
->ptr
;
329 return compareStringObjects(a
,b
) == 0;
333 size_t stringObjectLen(robj
*o
) {
334 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
335 if (o
->encoding
== REDIS_ENCODING_RAW
) {
336 return sdslen(o
->ptr
);
340 return ll2string(buf
,32,(long)o
->ptr
);
344 int getDoubleFromObject(robj
*o
, double *target
) {
351 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
352 if (o
->encoding
== REDIS_ENCODING_RAW
) {
353 value
= strtod(o
->ptr
, &eptr
);
354 if (eptr
[0] != '\0' || isnan(value
)) return REDIS_ERR
;
355 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
356 value
= (long)o
->ptr
;
358 redisPanic("Unknown string encoding");
366 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
368 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
370 addReplyError(c
,(char*)msg
);
372 addReplyError(c
,"value is not a double");
381 int getLongLongFromObject(robj
*o
, long long *target
) {
388 redisAssertWithInfo(NULL
,o
,o
->type
== REDIS_STRING
);
389 if (o
->encoding
== REDIS_ENCODING_RAW
) {
390 value
= strtoll(o
->ptr
, &eptr
, 10);
391 if (eptr
[0] != '\0') return REDIS_ERR
;
392 if (errno
== ERANGE
&& (value
== LLONG_MIN
|| value
== LLONG_MAX
))
394 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
395 value
= (long)o
->ptr
;
397 redisPanic("Unknown string encoding");
401 if (target
) *target
= value
;
405 int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
407 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
409 addReplyError(c
,(char*)msg
);
411 addReplyError(c
,"value is not an integer or out of range");
420 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
423 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
424 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
426 addReplyError(c
,(char*)msg
);
428 addReplyError(c
,"value is out of range");
437 char *strEncoding(int encoding
) {
439 case REDIS_ENCODING_RAW
: return "raw";
440 case REDIS_ENCODING_INT
: return "int";
441 case REDIS_ENCODING_HT
: return "hashtable";
442 case REDIS_ENCODING_ZIPMAP
: return "zipmap";
443 case REDIS_ENCODING_LINKEDLIST
: return "linkedlist";
444 case REDIS_ENCODING_ZIPLIST
: return "ziplist";
445 case REDIS_ENCODING_INTSET
: return "intset";
446 case REDIS_ENCODING_SKIPLIST
: return "skiplist";
447 default: return "unknown";
451 /* Given an object returns the min number of seconds the object was never
452 * requested, using an approximated LRU algorithm. */
453 unsigned long estimateObjectIdleTime(robj
*o
) {
454 if (server
.lruclock
>= o
->lru
) {
455 return (server
.lruclock
- o
->lru
) * REDIS_LRU_CLOCK_RESOLUTION
;
457 return ((REDIS_LRU_CLOCK_MAX
- o
->lru
) + server
.lruclock
) *
458 REDIS_LRU_CLOCK_RESOLUTION
;
462 /* This is an helper function for the DEBUG command. We need to lookup keys
463 * without any modification of LRU or other parameters. */
464 robj
*objectCommandLookup(redisClient
*c
, robj
*key
) {
467 if ((de
= dictFind(c
->db
->dict
,key
->ptr
)) == NULL
) return NULL
;
468 return (robj
*) dictGetEntryVal(de
);
471 robj
*objectCommandLookupOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
472 robj
*o
= objectCommandLookup(c
,key
);
474 if (!o
) addReply(c
, reply
);
478 /* Object command allows to inspect the internals of an Redis Object.
479 * Usage: OBJECT <verb> ... arguments ... */
480 void objectCommand(redisClient
*c
) {
483 if (!strcasecmp(c
->argv
[1]->ptr
,"refcount") && c
->argc
== 3) {
484 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
486 addReplyLongLong(c
,o
->refcount
);
487 } else if (!strcasecmp(c
->argv
[1]->ptr
,"encoding") && c
->argc
== 3) {
488 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
490 addReplyBulkCString(c
,strEncoding(o
->encoding
));
491 } else if (!strcasecmp(c
->argv
[1]->ptr
,"idletime") && c
->argc
== 3) {
492 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
494 addReplyLongLong(c
,estimateObjectIdleTime(o
));
496 addReplyError(c
,"Syntax error. Try OBJECT (refcount|encoding|idletime)");