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. */
24 o
->storage
= REDIS_DS_MEMORY
;
28 robj
*createStringObject(char *ptr
, size_t len
) {
29 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
32 robj
*createStringObjectFromLongLong(long long value
) {
34 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
&&
36 pthread_equal(pthread_self(),server
.mainthread
)) {
37 incrRefCount(shared
.integers
[value
]);
38 o
= shared
.integers
[value
];
40 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
41 o
= createObject(REDIS_STRING
, NULL
);
42 o
->encoding
= REDIS_ENCODING_INT
;
43 o
->ptr
= (void*)((long)value
);
45 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
51 robj
*dupStringObject(robj
*o
) {
52 redisAssert(o
->encoding
== REDIS_ENCODING_RAW
);
53 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
56 robj
*createListObject(void) {
57 list
*l
= listCreate();
58 robj
*o
= createObject(REDIS_LIST
,l
);
59 listSetFreeMethod(l
,decrRefCount
);
60 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
64 robj
*createZiplistObject(void) {
65 unsigned char *zl
= ziplistNew();
66 robj
*o
= createObject(REDIS_LIST
,zl
);
67 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
71 robj
*createSetObject(void) {
72 dict
*d
= dictCreate(&setDictType
,NULL
);
73 robj
*o
= createObject(REDIS_SET
,d
);
74 o
->encoding
= REDIS_ENCODING_HT
;
78 robj
*createIntsetObject(void) {
79 intset
*is
= intsetNew();
80 robj
*o
= createObject(REDIS_SET
,is
);
81 o
->encoding
= REDIS_ENCODING_INTSET
;
85 robj
*createHashObject(void) {
86 /* All the Hashes start as zipmaps. Will be automatically converted
87 * into hash tables if there are enough elements or big elements
89 unsigned char *zm
= zipmapNew();
90 robj
*o
= createObject(REDIS_HASH
,zm
);
91 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
95 robj
*createZsetObject(void) {
96 zset
*zs
= zmalloc(sizeof(*zs
));
98 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
99 zs
->zsl
= zslCreate();
100 return createObject(REDIS_ZSET
,zs
);
103 void freeStringObject(robj
*o
) {
104 if (o
->encoding
== REDIS_ENCODING_RAW
) {
109 void freeListObject(robj
*o
) {
110 switch (o
->encoding
) {
111 case REDIS_ENCODING_LINKEDLIST
:
112 listRelease((list
*) o
->ptr
);
114 case REDIS_ENCODING_ZIPLIST
:
118 redisPanic("Unknown list encoding type");
122 void freeSetObject(robj
*o
) {
123 switch (o
->encoding
) {
124 case REDIS_ENCODING_HT
:
125 dictRelease((dict
*) o
->ptr
);
127 case REDIS_ENCODING_INTSET
:
131 redisPanic("Unknown set encoding type");
135 void freeZsetObject(robj
*o
) {
138 dictRelease(zs
->dict
);
143 void freeHashObject(robj
*o
) {
144 switch (o
->encoding
) {
145 case REDIS_ENCODING_HT
:
146 dictRelease((dict
*) o
->ptr
);
148 case REDIS_ENCODING_ZIPMAP
:
152 redisPanic("Unknown hash encoding type");
157 void incrRefCount(robj
*o
) {
161 void decrRefCount(void *obj
) {
164 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
165 if (--(o
->refcount
) == 0) {
166 /* DS_SAVING objects should always have a reference in the
167 * IO Job structure. So we should never reach this state. */
168 redisAssert(o
->storage
!= REDIS_DS_SAVING
);
170 case REDIS_STRING
: freeStringObject(o
); break;
171 case REDIS_LIST
: freeListObject(o
); break;
172 case REDIS_SET
: freeSetObject(o
); break;
173 case REDIS_ZSET
: freeZsetObject(o
); break;
174 case REDIS_HASH
: freeHashObject(o
); break;
175 default: redisPanic("Unknown object type"); break;
177 o
->ptr
= NULL
; /* defensive programming. We'll see NULL in traces. */
182 int checkType(redisClient
*c
, robj
*o
, int type
) {
183 if (o
->type
!= type
) {
184 addReply(c
,shared
.wrongtypeerr
);
190 /* Try to encode a string object in order to save space */
191 robj
*tryObjectEncoding(robj
*o
) {
195 if (o
->encoding
!= REDIS_ENCODING_RAW
)
196 return o
; /* Already encoded */
198 /* It's not safe to encode shared objects: shared objects can be shared
199 * everywhere in the "object space" of Redis. Encoded objects can only
200 * appear as "values" (and not, for instance, as keys) */
201 if (o
->refcount
> 1) return o
;
203 /* Currently we try to encode only strings */
204 redisAssert(o
->type
== REDIS_STRING
);
206 /* Check if we can represent this string as a long integer */
207 if (isStringRepresentableAsLong(s
,&value
) == REDIS_ERR
) return o
;
209 /* Ok, this object can be encoded...
211 * Can I use a shared object? Only if the object is inside a given
212 * range and if the back end in use is in-memory. For disk store every
213 * object in memory used as value should be independent.
215 * Note that we also avoid using shared integers when maxmemory is used
216 * because every object needs to have a private LRU field for the LRU
217 * algorithm to work well. */
218 if (!server
.ds_enabled
&&
219 server
.maxmemory
== 0 && value
>= 0 && value
< REDIS_SHARED_INTEGERS
&&
220 pthread_equal(pthread_self(),server
.mainthread
))
223 incrRefCount(shared
.integers
[value
]);
224 return shared
.integers
[value
];
226 o
->encoding
= REDIS_ENCODING_INT
;
228 o
->ptr
= (void*) value
;
233 /* Get a decoded version of an encoded object (returned as a new object).
234 * If the object is already raw-encoded just increment the ref count. */
235 robj
*getDecodedObject(robj
*o
) {
238 if (o
->encoding
== REDIS_ENCODING_RAW
) {
242 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
245 ll2string(buf
,32,(long)o
->ptr
);
246 dec
= createStringObject(buf
,strlen(buf
));
249 redisPanic("Unknown encoding type");
253 /* Compare two string objects via strcmp() or alike.
254 * Note that the objects may be integer-encoded. In such a case we
255 * use ll2string() to get a string representation of the numbers on the stack
256 * and compare the strings, it's much faster than calling getDecodedObject().
258 * Important note: if objects are not integer encoded, but binary-safe strings,
259 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
261 int compareStringObjects(robj
*a
, robj
*b
) {
262 redisAssert(a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
263 char bufa
[128], bufb
[128], *astr
, *bstr
;
266 if (a
== b
) return 0;
267 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
268 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
274 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
275 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
281 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
284 /* Equal string objects return 1 if the two objects are the same from the
285 * point of view of a string comparison, otherwise 0 is returned. Note that
286 * this function is faster then checking for (compareStringObject(a,b) == 0)
287 * because it can perform some more optimization. */
288 int equalStringObjects(robj
*a
, robj
*b
) {
289 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
290 return a
->ptr
== b
->ptr
;
292 return compareStringObjects(a
,b
) == 0;
296 size_t stringObjectLen(robj
*o
) {
297 redisAssert(o
->type
== REDIS_STRING
);
298 if (o
->encoding
== REDIS_ENCODING_RAW
) {
299 return sdslen(o
->ptr
);
303 return ll2string(buf
,32,(long)o
->ptr
);
307 int getDoubleFromObject(robj
*o
, double *target
) {
314 redisAssert(o
->type
== REDIS_STRING
);
315 if (o
->encoding
== REDIS_ENCODING_RAW
) {
316 value
= strtod(o
->ptr
, &eptr
);
317 if (eptr
[0] != '\0' || isnan(value
)) return REDIS_ERR
;
318 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
319 value
= (long)o
->ptr
;
321 redisPanic("Unknown string encoding");
329 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
331 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
333 addReplyError(c
,(char*)msg
);
335 addReplyError(c
,"value is not a double");
344 int getLongLongFromObject(robj
*o
, long long *target
) {
351 redisAssert(o
->type
== REDIS_STRING
);
352 if (o
->encoding
== REDIS_ENCODING_RAW
) {
353 value
= strtoll(o
->ptr
, &eptr
, 10);
354 if (eptr
[0] != '\0') return REDIS_ERR
;
355 if (errno
== ERANGE
&& (value
== LLONG_MIN
|| value
== LLONG_MAX
))
357 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
358 value
= (long)o
->ptr
;
360 redisPanic("Unknown string encoding");
364 if (target
) *target
= value
;
368 int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
370 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
372 addReplyError(c
,(char*)msg
);
374 addReplyError(c
,"value is not an integer or out of range");
383 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
386 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
387 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
389 addReplyError(c
,(char*)msg
);
391 addReplyError(c
,"value is out of range");
400 char *strEncoding(int encoding
) {
402 case REDIS_ENCODING_RAW
: return "raw";
403 case REDIS_ENCODING_INT
: return "int";
404 case REDIS_ENCODING_HT
: return "hashtable";
405 case REDIS_ENCODING_ZIPMAP
: return "zipmap";
406 case REDIS_ENCODING_LINKEDLIST
: return "linkedlist";
407 case REDIS_ENCODING_ZIPLIST
: return "ziplist";
408 case REDIS_ENCODING_INTSET
: return "intset";
409 default: return "unknown";
413 /* Given an object returns the min number of seconds the object was never
414 * requested, using an approximated LRU algorithm. */
415 unsigned long estimateObjectIdleTime(robj
*o
) {
416 if (server
.lruclock
>= o
->lru
) {
417 return (server
.lruclock
- o
->lru
) * REDIS_LRU_CLOCK_RESOLUTION
;
419 return ((REDIS_LRU_CLOCK_MAX
- o
->lru
) + server
.lruclock
) *
420 REDIS_LRU_CLOCK_RESOLUTION
;