]> git.saurik.com Git - redis.git/blob - src/object.c
minor code aesthetic change
[redis.git] / src / object.c
1 #include "redis.h"
2 #include <pthread.h>
3 #include <math.h>
4
5 robj *createObject(int type, void *ptr) {
6 robj *o = zmalloc(sizeof(*o));
7 o->type = type;
8 o->encoding = REDIS_ENCODING_RAW;
9 o->ptr = ptr;
10 o->refcount = 1;
11
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.
15 *
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 return o;
25 }
26
27 robj *createStringObject(char *ptr, size_t len) {
28 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
29 }
30
31 robj *createStringObjectFromLongLong(long long value) {
32 robj *o;
33 if (value >= 0 && value < REDIS_SHARED_INTEGERS &&
34 !server.ds_enabled &&
35 pthread_equal(pthread_self(),server.mainthread)) {
36 incrRefCount(shared.integers[value]);
37 o = shared.integers[value];
38 } else {
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);
43 } else {
44 o = createObject(REDIS_STRING,sdsfromlonglong(value));
45 }
46 }
47 return o;
48 }
49
50 robj *dupStringObject(robj *o) {
51 redisAssert(o->encoding == REDIS_ENCODING_RAW);
52 return createStringObject(o->ptr,sdslen(o->ptr));
53 }
54
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;
60 return o;
61 }
62
63 robj *createZiplistObject(void) {
64 unsigned char *zl = ziplistNew();
65 robj *o = createObject(REDIS_LIST,zl);
66 o->encoding = REDIS_ENCODING_ZIPLIST;
67 return o;
68 }
69
70 robj *createSetObject(void) {
71 dict *d = dictCreate(&setDictType,NULL);
72 robj *o = createObject(REDIS_SET,d);
73 o->encoding = REDIS_ENCODING_HT;
74 return o;
75 }
76
77 robj *createIntsetObject(void) {
78 intset *is = intsetNew();
79 robj *o = createObject(REDIS_SET,is);
80 o->encoding = REDIS_ENCODING_INTSET;
81 return o;
82 }
83
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
87 * inside. */
88 unsigned char *zm = zipmapNew();
89 robj *o = createObject(REDIS_HASH,zm);
90 o->encoding = REDIS_ENCODING_ZIPMAP;
91 return o;
92 }
93
94 robj *createZsetObject(void) {
95 zset *zs = zmalloc(sizeof(*zs));
96 robj *o;
97
98 zs->dict = dictCreate(&zsetDictType,NULL);
99 zs->zsl = zslCreate();
100 o = createObject(REDIS_ZSET,zs);
101 o->encoding = REDIS_ENCODING_SKIPLIST;
102 return o;
103 }
104
105 robj *createZsetZiplistObject(void) {
106 unsigned char *zl = ziplistNew();
107 robj *o = createObject(REDIS_ZSET,zl);
108 o->encoding = REDIS_ENCODING_ZIPLIST;
109 return o;
110 }
111
112 void freeStringObject(robj *o) {
113 if (o->encoding == REDIS_ENCODING_RAW) {
114 sdsfree(o->ptr);
115 }
116 }
117
118 void freeListObject(robj *o) {
119 switch (o->encoding) {
120 case REDIS_ENCODING_LINKEDLIST:
121 listRelease((list*) o->ptr);
122 break;
123 case REDIS_ENCODING_ZIPLIST:
124 zfree(o->ptr);
125 break;
126 default:
127 redisPanic("Unknown list encoding type");
128 }
129 }
130
131 void freeSetObject(robj *o) {
132 switch (o->encoding) {
133 case REDIS_ENCODING_HT:
134 dictRelease((dict*) o->ptr);
135 break;
136 case REDIS_ENCODING_INTSET:
137 zfree(o->ptr);
138 break;
139 default:
140 redisPanic("Unknown set encoding type");
141 }
142 }
143
144 void freeZsetObject(robj *o) {
145 zset *zs;
146 switch (o->encoding) {
147 case REDIS_ENCODING_SKIPLIST:
148 zs = o->ptr;
149 dictRelease(zs->dict);
150 zslFree(zs->zsl);
151 zfree(zs);
152 break;
153 case REDIS_ENCODING_ZIPLIST:
154 zfree(o->ptr);
155 break;
156 default:
157 redisPanic("Unknown sorted set encoding");
158 }
159 }
160
161 void freeHashObject(robj *o) {
162 switch (o->encoding) {
163 case REDIS_ENCODING_HT:
164 dictRelease((dict*) o->ptr);
165 break;
166 case REDIS_ENCODING_ZIPMAP:
167 zfree(o->ptr);
168 break;
169 default:
170 redisPanic("Unknown hash encoding type");
171 break;
172 }
173 }
174
175 void incrRefCount(robj *o) {
176 o->refcount++;
177 }
178
179 void decrRefCount(void *obj) {
180 robj *o = obj;
181
182 if (o->refcount <= 0) redisPanic("decrRefCount against refcount <= 0");
183 if (o->refcount == 1) {
184 switch(o->type) {
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;
191 }
192 zfree(o);
193 } else {
194 o->refcount--;
195 }
196 }
197
198 int checkType(redisClient *c, robj *o, int type) {
199 if (o->type != type) {
200 addReply(c,shared.wrongtypeerr);
201 return 1;
202 }
203 return 0;
204 }
205
206 int isObjectRepresentableAsLongLong(robj *o, long long *llval) {
207 redisAssert(o->type == REDIS_STRING);
208 if (o->encoding == REDIS_ENCODING_INT) {
209 if (llval) *llval = (long) o->ptr;
210 return REDIS_OK;
211 } else {
212 return string2ll(o->ptr,sdslen(o->ptr),llval) ? REDIS_OK : REDIS_ERR;
213 }
214 }
215
216 /* Try to encode a string object in order to save space */
217 robj *tryObjectEncoding(robj *o) {
218 long value;
219 sds s = o->ptr;
220
221 if (o->encoding != REDIS_ENCODING_RAW)
222 return o; /* Already encoded */
223
224 /* It's not safe to encode shared objects: shared objects can be shared
225 * everywhere in the "object space" of Redis. Encoded objects can only
226 * appear as "values" (and not, for instance, as keys) */
227 if (o->refcount > 1) return o;
228
229 /* Currently we try to encode only strings */
230 redisAssert(o->type == REDIS_STRING);
231
232 /* Check if we can represent this string as a long integer */
233 if (!string2l(s,sdslen(s),&value)) return o;
234
235 /* Ok, this object can be encoded...
236 *
237 * Can I use a shared object? Only if the object is inside a given
238 * range and if the back end in use is in-memory. For disk store every
239 * object in memory used as value should be independent.
240 *
241 * Note that we also avoid using shared integers when maxmemory is used
242 * because every object needs to have a private LRU field for the LRU
243 * algorithm to work well. */
244 if (!server.ds_enabled &&
245 server.maxmemory == 0 && value >= 0 && value < REDIS_SHARED_INTEGERS &&
246 pthread_equal(pthread_self(),server.mainthread))
247 {
248 decrRefCount(o);
249 incrRefCount(shared.integers[value]);
250 return shared.integers[value];
251 } else {
252 o->encoding = REDIS_ENCODING_INT;
253 sdsfree(o->ptr);
254 o->ptr = (void*) value;
255 return o;
256 }
257 }
258
259 /* Get a decoded version of an encoded object (returned as a new object).
260 * If the object is already raw-encoded just increment the ref count. */
261 robj *getDecodedObject(robj *o) {
262 robj *dec;
263
264 if (o->encoding == REDIS_ENCODING_RAW) {
265 incrRefCount(o);
266 return o;
267 }
268 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) {
269 char buf[32];
270
271 ll2string(buf,32,(long)o->ptr);
272 dec = createStringObject(buf,strlen(buf));
273 return dec;
274 } else {
275 redisPanic("Unknown encoding type");
276 }
277 }
278
279 /* Compare two string objects via strcmp() or alike.
280 * Note that the objects may be integer-encoded. In such a case we
281 * use ll2string() to get a string representation of the numbers on the stack
282 * and compare the strings, it's much faster than calling getDecodedObject().
283 *
284 * Important note: if objects are not integer encoded, but binary-safe strings,
285 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
286 * binary safe. */
287 int compareStringObjects(robj *a, robj *b) {
288 redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
289 char bufa[128], bufb[128], *astr, *bstr;
290 int bothsds = 1;
291
292 if (a == b) return 0;
293 if (a->encoding != REDIS_ENCODING_RAW) {
294 ll2string(bufa,sizeof(bufa),(long) a->ptr);
295 astr = bufa;
296 bothsds = 0;
297 } else {
298 astr = a->ptr;
299 }
300 if (b->encoding != REDIS_ENCODING_RAW) {
301 ll2string(bufb,sizeof(bufb),(long) b->ptr);
302 bstr = bufb;
303 bothsds = 0;
304 } else {
305 bstr = b->ptr;
306 }
307 return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr);
308 }
309
310 /* Equal string objects return 1 if the two objects are the same from the
311 * point of view of a string comparison, otherwise 0 is returned. Note that
312 * this function is faster then checking for (compareStringObject(a,b) == 0)
313 * because it can perform some more optimization. */
314 int equalStringObjects(robj *a, robj *b) {
315 if (a->encoding != REDIS_ENCODING_RAW && b->encoding != REDIS_ENCODING_RAW){
316 return a->ptr == b->ptr;
317 } else {
318 return compareStringObjects(a,b) == 0;
319 }
320 }
321
322 size_t stringObjectLen(robj *o) {
323 redisAssert(o->type == REDIS_STRING);
324 if (o->encoding == REDIS_ENCODING_RAW) {
325 return sdslen(o->ptr);
326 } else {
327 char buf[32];
328
329 return ll2string(buf,32,(long)o->ptr);
330 }
331 }
332
333 int getDoubleFromObject(robj *o, double *target) {
334 double value;
335 char *eptr;
336
337 if (o == NULL) {
338 value = 0;
339 } else {
340 redisAssert(o->type == REDIS_STRING);
341 if (o->encoding == REDIS_ENCODING_RAW) {
342 value = strtod(o->ptr, &eptr);
343 if (eptr[0] != '\0' || isnan(value)) return REDIS_ERR;
344 } else if (o->encoding == REDIS_ENCODING_INT) {
345 value = (long)o->ptr;
346 } else {
347 redisPanic("Unknown string encoding");
348 }
349 }
350
351 *target = value;
352 return REDIS_OK;
353 }
354
355 int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg) {
356 double value;
357 if (getDoubleFromObject(o, &value) != REDIS_OK) {
358 if (msg != NULL) {
359 addReplyError(c,(char*)msg);
360 } else {
361 addReplyError(c,"value is not a double");
362 }
363 return REDIS_ERR;
364 }
365
366 *target = value;
367 return REDIS_OK;
368 }
369
370 int getLongLongFromObject(robj *o, long long *target) {
371 long long value;
372 char *eptr;
373
374 if (o == NULL) {
375 value = 0;
376 } else {
377 redisAssert(o->type == REDIS_STRING);
378 if (o->encoding == REDIS_ENCODING_RAW) {
379 value = strtoll(o->ptr, &eptr, 10);
380 if (eptr[0] != '\0') return REDIS_ERR;
381 if (errno == ERANGE && (value == LLONG_MIN || value == LLONG_MAX))
382 return REDIS_ERR;
383 } else if (o->encoding == REDIS_ENCODING_INT) {
384 value = (long)o->ptr;
385 } else {
386 redisPanic("Unknown string encoding");
387 }
388 }
389
390 if (target) *target = value;
391 return REDIS_OK;
392 }
393
394 int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg) {
395 long long value;
396 if (getLongLongFromObject(o, &value) != REDIS_OK) {
397 if (msg != NULL) {
398 addReplyError(c,(char*)msg);
399 } else {
400 addReplyError(c,"value is not an integer or out of range");
401 }
402 return REDIS_ERR;
403 }
404
405 *target = value;
406 return REDIS_OK;
407 }
408
409 int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg) {
410 long long value;
411
412 if (getLongLongFromObjectOrReply(c, o, &value, msg) != REDIS_OK) return REDIS_ERR;
413 if (value < LONG_MIN || value > LONG_MAX) {
414 if (msg != NULL) {
415 addReplyError(c,(char*)msg);
416 } else {
417 addReplyError(c,"value is out of range");
418 }
419 return REDIS_ERR;
420 }
421
422 *target = value;
423 return REDIS_OK;
424 }
425
426 char *strEncoding(int encoding) {
427 switch(encoding) {
428 case REDIS_ENCODING_RAW: return "raw";
429 case REDIS_ENCODING_INT: return "int";
430 case REDIS_ENCODING_HT: return "hashtable";
431 case REDIS_ENCODING_ZIPMAP: return "zipmap";
432 case REDIS_ENCODING_LINKEDLIST: return "linkedlist";
433 case REDIS_ENCODING_ZIPLIST: return "ziplist";
434 case REDIS_ENCODING_INTSET: return "intset";
435 case REDIS_ENCODING_SKIPLIST: return "skiplist";
436 default: return "unknown";
437 }
438 }
439
440 /* Given an object returns the min number of seconds the object was never
441 * requested, using an approximated LRU algorithm. */
442 unsigned long estimateObjectIdleTime(robj *o) {
443 if (server.lruclock >= o->lru) {
444 return (server.lruclock - o->lru) * REDIS_LRU_CLOCK_RESOLUTION;
445 } else {
446 return ((REDIS_LRU_CLOCK_MAX - o->lru) + server.lruclock) *
447 REDIS_LRU_CLOCK_RESOLUTION;
448 }
449 }
450
451 /* This is an helper function for the DEBUG command. We need to lookup keys
452 * without any modification of LRU or other parameters. */
453 robj *objectCommandLookup(redisClient *c, robj *key) {
454 dictEntry *de;
455
456 if ((de = dictFind(c->db->dict,key->ptr)) == NULL) return NULL;
457 return (robj*) dictGetEntryVal(de);
458 }
459
460 robj *objectCommandLookupOrReply(redisClient *c, robj *key, robj *reply) {
461 robj *o = objectCommandLookup(c,key);
462
463 if (!o) addReply(c, reply);
464 return o;
465 }
466
467 /* Object command allows to inspect the internals of an Redis Object.
468 * Usage: OBJECT <verb> ... arguments ... */
469 void objectCommand(redisClient *c) {
470 robj *o;
471
472 if (!strcasecmp(c->argv[1]->ptr,"refcount") && c->argc == 3) {
473 if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
474 == NULL) return;
475 addReplyLongLong(c,o->refcount);
476 } else if (!strcasecmp(c->argv[1]->ptr,"encoding") && c->argc == 3) {
477 if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
478 == NULL) return;
479 addReplyBulkCString(c,strEncoding(o->encoding));
480 } else if (!strcasecmp(c->argv[1]->ptr,"idletime") && c->argc == 3) {
481 if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
482 == NULL) return;
483 addReplyLongLong(c,estimateObjectIdleTime(o));
484 } else {
485 addReplyError(c,"Syntax error. Try OBJECT (refcount|encoding|idletime)");
486 }
487 }
488