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
&&
35 pthread_equal(pthread_self(),server
.mainthread
)) {
36 incrRefCount(shared
.integers
[value
]);
37 o
= shared
.integers
[value
];
39 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
40 o
= createObject(REDIS_STRING
, NULL
);
41 o
->encoding
= REDIS_ENCODING_INT
;
42 o
->ptr
= (void*)((long)value
);
44 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
50 robj
*dupStringObject(robj
*o
) {
51 redisAssert(o
->encoding
== REDIS_ENCODING_RAW
);
52 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
55 robj
*createListObject(void) {
56 list
*l
= listCreate();
57 robj
*o
= createObject(REDIS_LIST
,l
);
58 listSetFreeMethod(l
,decrRefCount
);
59 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
63 robj
*createZiplistObject(void) {
64 unsigned char *zl
= ziplistNew();
65 robj
*o
= createObject(REDIS_LIST
,zl
);
66 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
70 robj
*createSetObject(void) {
71 dict
*d
= dictCreate(&setDictType
,NULL
);
72 robj
*o
= createObject(REDIS_SET
,d
);
73 o
->encoding
= REDIS_ENCODING_HT
;
77 robj
*createIntsetObject(void) {
78 intset
*is
= intsetNew();
79 robj
*o
= createObject(REDIS_SET
,is
);
80 o
->encoding
= REDIS_ENCODING_INTSET
;
84 robj
*createHashObject(void) {
85 /* All the Hashes start as zipmaps. Will be automatically converted
86 * into hash tables if there are enough elements or big elements
88 unsigned char *zm
= zipmapNew();
89 robj
*o
= createObject(REDIS_HASH
,zm
);
90 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
94 robj
*createZsetObject(void) {
95 zset
*zs
= zmalloc(sizeof(*zs
));
98 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
99 zs
->zsl
= zslCreate();
100 o
= createObject(REDIS_ZSET
,zs
);
101 o
->encoding
= REDIS_ENCODING_SKIPLIST
;
105 robj
*createZsetZiplistObject(void) {
106 unsigned char *zl
= ziplistNew();
107 robj
*o
= createObject(REDIS_ZSET
,zl
);
108 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
112 void freeStringObject(robj
*o
) {
113 if (o
->encoding
== REDIS_ENCODING_RAW
) {
118 void freeListObject(robj
*o
) {
119 switch (o
->encoding
) {
120 case REDIS_ENCODING_LINKEDLIST
:
121 listRelease((list
*) o
->ptr
);
123 case REDIS_ENCODING_ZIPLIST
:
127 redisPanic("Unknown list encoding type");
131 void freeSetObject(robj
*o
) {
132 switch (o
->encoding
) {
133 case REDIS_ENCODING_HT
:
134 dictRelease((dict
*) o
->ptr
);
136 case REDIS_ENCODING_INTSET
:
140 redisPanic("Unknown set encoding type");
144 void freeZsetObject(robj
*o
) {
146 switch (o
->encoding
) {
147 case REDIS_ENCODING_RAW
:
149 dictRelease(zs
->dict
);
153 case REDIS_ENCODING_ZIPLIST
:
157 redisPanic("Unknown sorted set encoding");
161 void freeHashObject(robj
*o
) {
162 switch (o
->encoding
) {
163 case REDIS_ENCODING_HT
:
164 dictRelease((dict
*) o
->ptr
);
166 case REDIS_ENCODING_ZIPMAP
:
170 redisPanic("Unknown hash encoding type");
175 void incrRefCount(robj
*o
) {
179 void decrRefCount(void *obj
) {
182 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
183 if (--(o
->refcount
) == 0) {
185 case REDIS_STRING
: freeStringObject(o
); break;
186 case REDIS_LIST
: freeListObject(o
); break;
187 case REDIS_SET
: freeSetObject(o
); break;
188 case REDIS_ZSET
: freeZsetObject(o
); break;
189 case REDIS_HASH
: freeHashObject(o
); break;
190 default: redisPanic("Unknown object type"); break;
192 o
->ptr
= NULL
; /* defensive programming. We'll see NULL in traces. */
197 int checkType(redisClient
*c
, robj
*o
, int type
) {
198 if (o
->type
!= type
) {
199 addReply(c
,shared
.wrongtypeerr
);
205 /* Try to encode a string object in order to save space */
206 robj
*tryObjectEncoding(robj
*o
) {
210 if (o
->encoding
!= REDIS_ENCODING_RAW
)
211 return o
; /* Already encoded */
213 /* It's not safe to encode shared objects: shared objects can be shared
214 * everywhere in the "object space" of Redis. Encoded objects can only
215 * appear as "values" (and not, for instance, as keys) */
216 if (o
->refcount
> 1) return o
;
218 /* Currently we try to encode only strings */
219 redisAssert(o
->type
== REDIS_STRING
);
221 /* Check if we can represent this string as a long integer */
222 if (isStringRepresentableAsLong(s
,&value
) == REDIS_ERR
) return o
;
224 /* Ok, this object can be encoded...
226 * Can I use a shared object? Only if the object is inside a given
227 * range and if the back end in use is in-memory. For disk store every
228 * object in memory used as value should be independent.
230 * Note that we also avoid using shared integers when maxmemory is used
231 * because every object needs to have a private LRU field for the LRU
232 * algorithm to work well. */
233 if (!server
.ds_enabled
&&
234 server
.maxmemory
== 0 && value
>= 0 && value
< REDIS_SHARED_INTEGERS
&&
235 pthread_equal(pthread_self(),server
.mainthread
))
238 incrRefCount(shared
.integers
[value
]);
239 return shared
.integers
[value
];
241 o
->encoding
= REDIS_ENCODING_INT
;
243 o
->ptr
= (void*) value
;
248 /* Get a decoded version of an encoded object (returned as a new object).
249 * If the object is already raw-encoded just increment the ref count. */
250 robj
*getDecodedObject(robj
*o
) {
253 if (o
->encoding
== REDIS_ENCODING_RAW
) {
257 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
260 ll2string(buf
,32,(long)o
->ptr
);
261 dec
= createStringObject(buf
,strlen(buf
));
264 redisPanic("Unknown encoding type");
268 /* Compare two string objects via strcmp() or alike.
269 * Note that the objects may be integer-encoded. In such a case we
270 * use ll2string() to get a string representation of the numbers on the stack
271 * and compare the strings, it's much faster than calling getDecodedObject().
273 * Important note: if objects are not integer encoded, but binary-safe strings,
274 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
276 int compareStringObjects(robj
*a
, robj
*b
) {
277 redisAssert(a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
278 char bufa
[128], bufb
[128], *astr
, *bstr
;
281 if (a
== b
) return 0;
282 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
283 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
289 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
290 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
296 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
299 /* Equal string objects return 1 if the two objects are the same from the
300 * point of view of a string comparison, otherwise 0 is returned. Note that
301 * this function is faster then checking for (compareStringObject(a,b) == 0)
302 * because it can perform some more optimization. */
303 int equalStringObjects(robj
*a
, robj
*b
) {
304 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
305 return a
->ptr
== b
->ptr
;
307 return compareStringObjects(a
,b
) == 0;
311 size_t stringObjectLen(robj
*o
) {
312 redisAssert(o
->type
== REDIS_STRING
);
313 if (o
->encoding
== REDIS_ENCODING_RAW
) {
314 return sdslen(o
->ptr
);
318 return ll2string(buf
,32,(long)o
->ptr
);
322 int getDoubleFromObject(robj
*o
, double *target
) {
329 redisAssert(o
->type
== REDIS_STRING
);
330 if (o
->encoding
== REDIS_ENCODING_RAW
) {
331 value
= strtod(o
->ptr
, &eptr
);
332 if (eptr
[0] != '\0' || isnan(value
)) return REDIS_ERR
;
333 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
334 value
= (long)o
->ptr
;
336 redisPanic("Unknown string encoding");
344 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
346 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
348 addReplyError(c
,(char*)msg
);
350 addReplyError(c
,"value is not a double");
359 int getLongLongFromObject(robj
*o
, long long *target
) {
366 redisAssert(o
->type
== REDIS_STRING
);
367 if (o
->encoding
== REDIS_ENCODING_RAW
) {
368 value
= strtoll(o
->ptr
, &eptr
, 10);
369 if (eptr
[0] != '\0') return REDIS_ERR
;
370 if (errno
== ERANGE
&& (value
== LLONG_MIN
|| value
== LLONG_MAX
))
372 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
373 value
= (long)o
->ptr
;
375 redisPanic("Unknown string encoding");
379 if (target
) *target
= value
;
383 int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
385 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
387 addReplyError(c
,(char*)msg
);
389 addReplyError(c
,"value is not an integer or out of range");
398 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
401 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
402 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
404 addReplyError(c
,(char*)msg
);
406 addReplyError(c
,"value is out of range");
415 char *strEncoding(int encoding
) {
417 case REDIS_ENCODING_RAW
: return "raw";
418 case REDIS_ENCODING_INT
: return "int";
419 case REDIS_ENCODING_HT
: return "hashtable";
420 case REDIS_ENCODING_ZIPMAP
: return "zipmap";
421 case REDIS_ENCODING_LINKEDLIST
: return "linkedlist";
422 case REDIS_ENCODING_ZIPLIST
: return "ziplist";
423 case REDIS_ENCODING_INTSET
: return "intset";
424 case REDIS_ENCODING_SKIPLIST
: return "skiplist";
425 default: return "unknown";
429 /* Given an object returns the min number of seconds the object was never
430 * requested, using an approximated LRU algorithm. */
431 unsigned long estimateObjectIdleTime(robj
*o
) {
432 if (server
.lruclock
>= o
->lru
) {
433 return (server
.lruclock
- o
->lru
) * REDIS_LRU_CLOCK_RESOLUTION
;
435 return ((REDIS_LRU_CLOCK_MAX
- o
->lru
) + server
.lruclock
) *
436 REDIS_LRU_CLOCK_RESOLUTION
;
440 /* This is an helper function for the DEBUG command. We need to lookup keys
441 * without any modification of LRU or other parameters. */
442 robj
*objectCommandLookup(redisClient
*c
, robj
*key
) {
445 if ((de
= dictFind(c
->db
->dict
,key
->ptr
)) == NULL
) return NULL
;
446 return (robj
*) dictGetEntryVal(de
);
449 robj
*objectCommandLookupOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
450 robj
*o
= objectCommandLookup(c
,key
);
452 if (!o
) addReply(c
, reply
);
456 /* Object command allows to inspect the internals of an Redis Object.
457 * Usage: OBJECT <verb> ... arguments ... */
458 void objectCommand(redisClient
*c
) {
461 if (!strcasecmp(c
->argv
[1]->ptr
,"refcount") && c
->argc
== 3) {
462 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
464 addReplyLongLong(c
,o
->refcount
);
465 } else if (!strcasecmp(c
->argv
[1]->ptr
,"encoding") && c
->argc
== 3) {
466 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
468 addReplyBulkCString(c
,strEncoding(o
->encoding
));
469 } else if (!strcasecmp(c
->argv
[1]->ptr
,"idletime") && c
->argc
== 3) {
470 if ((o
= objectCommandLookupOrReply(c
,c
->argv
[2],shared
.nullbulk
))
472 addReplyLongLong(c
,estimateObjectIdleTime(o
));
474 addReplyError(c
,"Syntax error. Try OBJECT (refcount|encoding|idletime)");