]> git.saurik.com Git - redis.git/blame - src/t_hash.c
Release notes updated with the new 2.6 features.
[redis.git] / src / t_hash.c
CommitLineData
e2641e09 1#include "redis.h"
e2641e09 2#include <math.h>
3
4/*-----------------------------------------------------------------------------
5 * Hash type API
6 *----------------------------------------------------------------------------*/
7
8/* Check the length of a number of objects to see if we need to convert a
ebd85e9a 9 * ziplist to a real hash. Note that we only check string encoded objects
e2641e09 10 * as their string length can be queried in constant time. */
ebd85e9a 11void hashTypeTryConversion(robj *o, robj **argv, int start, int end) {
e2641e09 12 int i;
ebd85e9a
PN
13
14 if (o->encoding != REDIS_ENCODING_ZIPLIST) return;
e2641e09 15
16 for (i = start; i <= end; i++) {
17 if (argv[i]->encoding == REDIS_ENCODING_RAW &&
ebd85e9a 18 sdslen(argv[i]->ptr) > server.hash_max_ziplist_value)
e2641e09 19 {
ebd85e9a
PN
20 hashTypeConvert(o, REDIS_ENCODING_HT);
21 break;
e2641e09 22 }
23 }
24}
25
26/* Encode given objects in-place when the hash uses a dict. */
27void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2) {
28 if (subject->encoding == REDIS_ENCODING_HT) {
29 if (o1) *o1 = tryObjectEncoding(*o1);
30 if (o2) *o2 = tryObjectEncoding(*o2);
31 }
32}
33
ebd85e9a
PN
34/* Get the value from a ziplist encoded hash, identified by field.
35 * Returns -1 when the field cannot be found. */
36int hashTypeGetFromZiplist(robj *o, robj *field,
37 unsigned char **vstr,
38 unsigned int *vlen,
39 long long *vll)
3d24304f 40{
ebd85e9a
PN
41 unsigned char *zl, *fptr = NULL, *vptr = NULL;
42 int ret;
3d24304f 43
ebd85e9a
PN
44 redisAssert(o->encoding == REDIS_ENCODING_ZIPLIST);
45
46 field = getDecodedObject(field);
47
48 zl = o->ptr;
49 fptr = ziplistIndex(zl, ZIPLIST_HEAD);
fe458402
PN
50 if (fptr != NULL) {
51 fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1);
52 if (fptr != NULL) {
53 /* Grab pointer to the value (fptr points to the field) */
54 vptr = ziplistNext(zl, fptr);
55 redisAssert(vptr != NULL);
ebd85e9a 56 }
3d24304f 57 }
ebd85e9a
PN
58
59 decrRefCount(field);
60
fe458402 61 if (vptr != NULL) {
ebd85e9a
PN
62 ret = ziplistGet(vptr, vstr, vlen, vll);
63 redisAssert(ret);
64 return 0;
65 }
66
67 return -1;
3d24304f 68}
69
ebd85e9a
PN
70/* Get the value from a hash table encoded hash, identified by field.
71 * Returns -1 when the field cannot be found. */
72int hashTypeGetFromHashTable(robj *o, robj *field, robj **value) {
73 dictEntry *de;
74
75 redisAssert(o->encoding == REDIS_ENCODING_HT);
76
77 de = dictFind(o->ptr, field);
c0caa1cf 78 if (de == NULL) return -1;
ebd85e9a
PN
79 *value = dictGetVal(de);
80 return 0;
81}
82
83/* Higher level function of hashTypeGet*() that always returns a Redis
3d24304f 84 * object (either new or with refcount incremented), so that the caller
85 * can retain a reference or call decrRefCount after the usage.
86 *
87 * The lower level function can prevent copy on write so it is
88 * the preferred way of doing read operations. */
ebd85e9a
PN
89robj *hashTypeGetObject(robj *o, robj *field) {
90 robj *value = NULL;
91
92 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
93 unsigned char *vstr = NULL;
94 unsigned int vlen = UINT_MAX;
95 long long vll = LLONG_MAX;
96
97 if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0) {
98 if (vstr) {
99 value = createStringObject((char*)vstr, vlen);
100 } else {
101 value = createStringObjectFromLongLong(vll);
102 }
103 }
104
105 } else if (o->encoding == REDIS_ENCODING_HT) {
106 robj *aux;
107
108 if (hashTypeGetFromHashTable(o, field, &aux) == 0) {
109 incrRefCount(aux);
110 value = aux;
e2641e09 111 }
e2641e09 112 } else {
ebd85e9a
PN
113 redisPanic("Unknown hash encoding");
114 }
ebd85e9a
PN
115 return value;
116}
117
118/* Test if the specified field exists in the given hash. Returns 1 if the field
119 * exists, and 0 when it doesn't. */
120int hashTypeExists(robj *o, robj *field) {
121 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
122 unsigned char *vstr = NULL;
123 unsigned int vlen = UINT_MAX;
124 long long vll = LLONG_MAX;
125
753bb3dc 126 if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0) return 1;
ebd85e9a
PN
127 } else if (o->encoding == REDIS_ENCODING_HT) {
128 robj *aux;
129
753bb3dc 130 if (hashTypeGetFromHashTable(o, field, &aux) == 0) return 1;
ebd85e9a
PN
131 } else {
132 redisPanic("Unknown hash encoding");
e2641e09 133 }
134 return 0;
135}
136
137/* Add an element, discard the old if the key already exists.
52192552 138 * Return 0 on insert and 1 on update.
139 * This function will take care of incrementing the reference count of the
140 * retained fields and value objects. */
ebd85e9a 141int hashTypeSet(robj *o, robj *field, robj *value) {
e2641e09 142 int update = 0;
ebd85e9a
PN
143
144 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
145 unsigned char *zl, *fptr, *vptr;
146
147 field = getDecodedObject(field);
e2641e09 148 value = getDecodedObject(value);
ebd85e9a
PN
149
150 zl = o->ptr;
151 fptr = ziplistIndex(zl, ZIPLIST_HEAD);
fe458402
PN
152 if (fptr != NULL) {
153 fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1);
154 if (fptr != NULL) {
155 /* Grab pointer to the value (fptr points to the field) */
156 vptr = ziplistNext(zl, fptr);
157 redisAssert(vptr != NULL);
ebd85e9a 158 update = 1;
ebd85e9a 159
fe458402
PN
160 /* Delete value */
161 zl = ziplistDelete(zl, &vptr);
162
163 /* Insert new value */
164 zl = ziplistInsert(zl, vptr, value->ptr, sdslen(value->ptr));
165 }
166 }
ebd85e9a 167
fe458402
PN
168 if (!update) {
169 /* Push new field/value pair onto the tail of the ziplist */
170 zl = ziplistPush(zl, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL);
171 zl = ziplistPush(zl, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL);
ebd85e9a 172 }
ebd85e9a 173 o->ptr = zl;
ebd85e9a 174 decrRefCount(field);
e2641e09 175 decrRefCount(value);
176
ebd85e9a 177 /* Check if the ziplist needs to be converted to a hash table */
52192552 178 if (hashTypeLength(o) > server.hash_max_ziplist_entries)
ebd85e9a 179 hashTypeConvert(o, REDIS_ENCODING_HT);
ebd85e9a
PN
180 } else if (o->encoding == REDIS_ENCODING_HT) {
181 if (dictReplace(o->ptr, field, value)) { /* Insert */
182 incrRefCount(field);
183 } else { /* Update */
e2641e09 184 update = 1;
185 }
186 incrRefCount(value);
ebd85e9a
PN
187 } else {
188 redisPanic("Unknown hash encoding");
e2641e09 189 }
190 return update;
191}
192
193/* Delete an element from a hash.
194 * Return 1 on deleted and 0 on not found. */
ebd85e9a 195int hashTypeDelete(robj *o, robj *field) {
e2641e09 196 int deleted = 0;
ebd85e9a
PN
197
198 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
fe458402 199 unsigned char *zl, *fptr;
ebd85e9a
PN
200
201 field = getDecodedObject(field);
202
203 zl = o->ptr;
204 fptr = ziplistIndex(zl, ZIPLIST_HEAD);
fe458402
PN
205 if (fptr != NULL) {
206 fptr = ziplistFind(fptr, field->ptr, sdslen(field->ptr), 1);
207 if (fptr != NULL) {
ebd85e9a
PN
208 zl = ziplistDelete(zl,&fptr);
209 zl = ziplistDelete(zl,&fptr);
210 o->ptr = zl;
211 deleted = 1;
ebd85e9a 212 }
ebd85e9a
PN
213 }
214
215 decrRefCount(field);
216
217 } else if (o->encoding == REDIS_ENCODING_HT) {
218 if (dictDelete((dict*)o->ptr, field) == REDIS_OK) {
219 deleted = 1;
220
221 /* Always check if the dictionary needs a resize after a delete. */
222 if (htNeedsResize(o->ptr)) dictResize(o->ptr);
223 }
224
e2641e09 225 } else {
ebd85e9a 226 redisPanic("Unknown hash encoding");
e2641e09 227 }
ebd85e9a 228
e2641e09 229 return deleted;
230}
231
232/* Return the number of elements in a hash. */
233unsigned long hashTypeLength(robj *o) {
ebd85e9a
PN
234 unsigned long length = ULONG_MAX;
235
236 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
237 length = ziplistLen(o->ptr) / 2;
238 } else if (o->encoding == REDIS_ENCODING_HT) {
239 length = dictSize((dict*)o->ptr);
240 } else {
241 redisPanic("Unknown hash encoding");
242 }
243
244 return length;
e2641e09 245}
246
247hashTypeIterator *hashTypeInitIterator(robj *subject) {
248 hashTypeIterator *hi = zmalloc(sizeof(hashTypeIterator));
ebd85e9a 249 hi->subject = subject;
e2641e09 250 hi->encoding = subject->encoding;
ebd85e9a
PN
251
252 if (hi->encoding == REDIS_ENCODING_ZIPLIST) {
253 hi->fptr = NULL;
254 hi->vptr = NULL;
e2641e09 255 } else if (hi->encoding == REDIS_ENCODING_HT) {
256 hi->di = dictGetIterator(subject->ptr);
257 } else {
ebd85e9a 258 redisPanic("Unknown hash encoding");
e2641e09 259 }
ebd85e9a 260
e2641e09 261 return hi;
262}
263
264void hashTypeReleaseIterator(hashTypeIterator *hi) {
265 if (hi->encoding == REDIS_ENCODING_HT) {
266 dictReleaseIterator(hi->di);
267 }
ebd85e9a 268
e2641e09 269 zfree(hi);
270}
271
272/* Move to the next entry in the hash. Return REDIS_OK when the next entry
273 * could be found and REDIS_ERR when the iterator reaches the end. */
274int hashTypeNext(hashTypeIterator *hi) {
ebd85e9a
PN
275 if (hi->encoding == REDIS_ENCODING_ZIPLIST) {
276 unsigned char *zl;
277 unsigned char *fptr, *vptr;
278
279 zl = hi->subject->ptr;
280 fptr = hi->fptr;
281 vptr = hi->vptr;
282
283 if (fptr == NULL) {
284 /* Initialize cursor */
285 redisAssert(vptr == NULL);
286 fptr = ziplistIndex(zl, 0);
287 } else {
288 /* Advance cursor */
289 redisAssert(vptr != NULL);
290 fptr = ziplistNext(zl, vptr);
291 }
753bb3dc 292 if (fptr == NULL) return REDIS_ERR;
ebd85e9a
PN
293
294 /* Grab pointer to the value (fptr points to the field) */
295 vptr = ziplistNext(zl, fptr);
296 redisAssert(vptr != NULL);
297
298 /* fptr, vptr now point to the first or next pair */
299 hi->fptr = fptr;
300 hi->vptr = vptr;
ebd85e9a 301 } else if (hi->encoding == REDIS_ENCODING_HT) {
753bb3dc 302 if ((hi->de = dictNext(hi->di)) == NULL) return REDIS_ERR;
e2641e09 303 } else {
ebd85e9a 304 redisPanic("Unknown hash encoding");
e2641e09 305 }
306 return REDIS_OK;
307}
308
ebd85e9a
PN
309/* Get the field or value at iterator cursor, for an iterator on a hash value
310 * encoded as a ziplist. Prototype is similar to `hashTypeGetFromZiplist`. */
311void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,
312 unsigned char **vstr,
313 unsigned int *vlen,
314 long long *vll)
315{
316 int ret;
317
318 redisAssert(hi->encoding == REDIS_ENCODING_ZIPLIST);
319
320 if (what & REDIS_HASH_KEY) {
321 ret = ziplistGet(hi->fptr, vstr, vlen, vll);
322 redisAssert(ret);
323 } else {
324 ret = ziplistGet(hi->vptr, vstr, vlen, vll);
325 redisAssert(ret);
326 }
327}
328
329/* Get the field or value at iterator cursor, for an iterator on a hash value
330 * encoded as a ziplist. Prototype is similar to `hashTypeGetFromHashTable`. */
331void hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what, robj **dst) {
332 redisAssert(hi->encoding == REDIS_ENCODING_HT);
333
334 if (what & REDIS_HASH_KEY) {
335 *dst = dictGetKey(hi->de);
e2641e09 336 } else {
ebd85e9a 337 *dst = dictGetVal(hi->de);
8c304be3 338 }
8c304be3 339}
340
ebd85e9a
PN
341/* A non copy-on-write friendly but higher level version of hashTypeCurrent*()
342 * that returns an object with incremented refcount (or a new object). It is up
343 * to the caller to decrRefCount() the object if no reference is retained. */
8c304be3 344robj *hashTypeCurrentObject(hashTypeIterator *hi, int what) {
ebd85e9a
PN
345 robj *dst;
346
347 if (hi->encoding == REDIS_ENCODING_ZIPLIST) {
348 unsigned char *vstr = NULL;
349 unsigned int vlen = UINT_MAX;
350 long long vll = LLONG_MAX;
351
352 hashTypeCurrentFromZiplist(hi, what, &vstr, &vlen, &vll);
353 if (vstr) {
354 dst = createStringObject((char*)vstr, vlen);
355 } else {
356 dst = createStringObjectFromLongLong(vll);
357 }
358
359 } else if (hi->encoding == REDIS_ENCODING_HT) {
360 hashTypeCurrentFromHashTable(hi, what, &dst);
361 incrRefCount(dst);
362
8c304be3 363 } else {
ebd85e9a 364 redisPanic("Unknown hash encoding");
e2641e09 365 }
ebd85e9a
PN
366
367 return dst;
e2641e09 368}
369
370robj *hashTypeLookupWriteOrCreate(redisClient *c, robj *key) {
371 robj *o = lookupKeyWrite(c->db,key);
372 if (o == NULL) {
373 o = createHashObject();
374 dbAdd(c->db,key,o);
375 } else {
376 if (o->type != REDIS_HASH) {
377 addReply(c,shared.wrongtypeerr);
378 return NULL;
379 }
380 }
381 return o;
382}
383
ebd85e9a
PN
384void hashTypeConvertZiplist(robj *o, int enc) {
385 redisAssert(o->encoding == REDIS_ENCODING_ZIPLIST);
386
387 if (enc == REDIS_ENCODING_ZIPLIST) {
388 /* Nothing to do... */
389
390 } else if (enc == REDIS_ENCODING_HT) {
391 hashTypeIterator *hi;
392 dict *dict;
393 int ret;
e2641e09 394
ebd85e9a
PN
395 hi = hashTypeInitIterator(o);
396 dict = dictCreate(&hashDictType, NULL);
e2641e09 397
ebd85e9a
PN
398 while (hashTypeNext(hi) != REDIS_ERR) {
399 robj *field, *value;
400
401 field = hashTypeCurrentObject(hi, REDIS_HASH_KEY);
402 field = tryObjectEncoding(field);
403 value = hashTypeCurrentObject(hi, REDIS_HASH_VALUE);
404 value = tryObjectEncoding(value);
405 ret = dictAdd(dict, field, value);
406 redisAssert(ret == DICT_OK);
407 }
408
409 hashTypeReleaseIterator(hi);
410 zfree(o->ptr);
411
412 o->encoding = REDIS_ENCODING_HT;
413 o->ptr = dict;
414
415 } else {
416 redisPanic("Unknown hash encoding");
417 }
418}
419
420void hashTypeConvert(robj *o, int enc) {
421 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
422 hashTypeConvertZiplist(o, enc);
423 } else if (o->encoding == REDIS_ENCODING_HT) {
424 redisPanic("Not implemented");
425 } else {
426 redisPanic("Unknown hash encoding");
e2641e09 427 }
e2641e09 428}
429
430/*-----------------------------------------------------------------------------
431 * Hash type commands
432 *----------------------------------------------------------------------------*/
433
434void hsetCommand(redisClient *c) {
435 int update;
436 robj *o;
437
438 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
439 hashTypeTryConversion(o,c->argv,2,3);
440 hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
441 update = hashTypeSet(o,c->argv[2],c->argv[3]);
442 addReply(c, update ? shared.czero : shared.cone);
cea8c5cd 443 signalModifiedKey(c->db,c->argv[1]);
e2641e09 444 server.dirty++;
445}
446
447void hsetnxCommand(redisClient *c) {
448 robj *o;
449 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
450 hashTypeTryConversion(o,c->argv,2,3);
451
452 if (hashTypeExists(o, c->argv[2])) {
453 addReply(c, shared.czero);
454 } else {
455 hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
456 hashTypeSet(o,c->argv[2],c->argv[3]);
457 addReply(c, shared.cone);
cea8c5cd 458 signalModifiedKey(c->db,c->argv[1]);
e2641e09 459 server.dirty++;
460 }
461}
462
463void hmsetCommand(redisClient *c) {
464 int i;
465 robj *o;
466
467 if ((c->argc % 2) == 1) {
3ab20376 468 addReplyError(c,"wrong number of arguments for HMSET");
e2641e09 469 return;
470 }
471
472 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
473 hashTypeTryConversion(o,c->argv,2,c->argc-1);
474 for (i = 2; i < c->argc; i += 2) {
475 hashTypeTryObjectEncoding(o,&c->argv[i], &c->argv[i+1]);
476 hashTypeSet(o,c->argv[i],c->argv[i+1]);
477 }
478 addReply(c, shared.ok);
cea8c5cd 479 signalModifiedKey(c->db,c->argv[1]);
e2641e09 480 server.dirty++;
481}
482
483void hincrbyCommand(redisClient *c) {
a400a9b2 484 long long value, incr, oldvalue;
e2641e09 485 robj *o, *current, *new;
486
487 if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
488 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
3d24304f 489 if ((current = hashTypeGetObject(o,c->argv[2])) != NULL) {
e2641e09 490 if (getLongLongFromObjectOrReply(c,current,&value,
491 "hash value is not an integer") != REDIS_OK) {
492 decrRefCount(current);
493 return;
494 }
495 decrRefCount(current);
496 } else {
497 value = 0;
498 }
499
a400a9b2 500 oldvalue = value;
7c96b467 501 if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||
502 (incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {
a400a9b2 503 addReplyError(c,"increment or decrement would overflow");
504 return;
505 }
7c96b467 506 value += incr;
e2641e09 507 new = createStringObjectFromLongLong(value);
508 hashTypeTryObjectEncoding(o,&c->argv[2],NULL);
509 hashTypeSet(o,c->argv[2],new);
510 decrRefCount(new);
511 addReplyLongLong(c,value);
cea8c5cd 512 signalModifiedKey(c->db,c->argv[1]);
e2641e09 513 server.dirty++;
514}
515
68bfe993 516void hincrbyfloatCommand(redisClient *c) {
517 double long value, incr;
52192552 518 robj *o, *current, *new, *aux;
68bfe993 519
520 if (getLongDoubleFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
521 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
522 if ((current = hashTypeGetObject(o,c->argv[2])) != NULL) {
523 if (getLongDoubleFromObjectOrReply(c,current,&value,
524 "hash value is not a valid float") != REDIS_OK) {
525 decrRefCount(current);
526 return;
527 }
528 decrRefCount(current);
529 } else {
530 value = 0;
531 }
532
533 value += incr;
534 new = createStringObjectFromLongDouble(value);
535 hashTypeTryObjectEncoding(o,&c->argv[2],NULL);
536 hashTypeSet(o,c->argv[2],new);
537 addReplyBulk(c,new);
68bfe993 538 signalModifiedKey(c->db,c->argv[1]);
539 server.dirty++;
52192552 540
541 /* Always replicate HINCRBYFLOAT as an HSET command with the final value
542 * in order to make sure that differences in float pricision or formatting
543 * will not create differences in replicas or after an AOF restart. */
544 aux = createStringObject("HSET",4);
545 rewriteClientCommandArgument(c,0,aux);
546 decrRefCount(aux);
547 rewriteClientCommandArgument(c,3,new);
548 decrRefCount(new);
68bfe993 549}
550
ebd85e9a
PN
551static void addHashFieldToReply(redisClient *c, robj *o, robj *field) {
552 int ret;
553
554 if (o == NULL) {
555 addReply(c, shared.nullbulk);
556 return;
557 }
558
559 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
560 unsigned char *vstr = NULL;
561 unsigned int vlen = UINT_MAX;
562 long long vll = LLONG_MAX;
563
564 ret = hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll);
565 if (ret < 0) {
566 addReply(c, shared.nullbulk);
567 } else {
568 if (vstr) {
569 addReplyBulkCBuffer(c, vstr, vlen);
570 } else {
571 addReplyBulkLongLong(c, vll);
572 }
573 }
574
575 } else if (o->encoding == REDIS_ENCODING_HT) {
576 robj *value;
577
578 ret = hashTypeGetFromHashTable(o, field, &value);
579 if (ret < 0) {
580 addReply(c, shared.nullbulk);
581 } else {
582 addReplyBulk(c, value);
583 }
584
585 } else {
586 redisPanic("Unknown hash encoding");
587 }
588}
589
e2641e09 590void hgetCommand(redisClient *c) {
ebd85e9a 591 robj *o;
3d24304f 592
e2641e09 593 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
594 checkType(c,o,REDIS_HASH)) return;
595
ebd85e9a 596 addHashFieldToReply(c, o, c->argv[2]);
e2641e09 597}
598
599void hmgetCommand(redisClient *c) {
ebd85e9a
PN
600 robj *o;
601 int i;
3d24304f 602
ebd85e9a
PN
603 /* Don't abort when the key cannot be found. Non-existing keys are empty
604 * hashes, where HMGET should respond with a series of null bulks. */
605 o = lookupKeyRead(c->db, c->argv[1]);
e2641e09 606 if (o != NULL && o->type != REDIS_HASH) {
ebd85e9a 607 addReply(c, shared.wrongtypeerr);
e584d82f 608 return;
e2641e09 609 }
610
ebd85e9a 611 addReplyMultiBulkLen(c, c->argc-2);
e2641e09 612 for (i = 2; i < c->argc; i++) {
ebd85e9a 613 addHashFieldToReply(c, o, c->argv[i]);
e2641e09 614 }
615}
616
617void hdelCommand(redisClient *c) {
618 robj *o;
64a13a36 619 int j, deleted = 0;
620
e2641e09 621 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
622 checkType(c,o,REDIS_HASH)) return;
623
64a13a36 624 for (j = 2; j < c->argc; j++) {
625 if (hashTypeDelete(o,c->argv[j])) {
64a13a36 626 deleted++;
2d7162bb
PN
627 if (hashTypeLength(o) == 0) {
628 dbDelete(c->db,c->argv[1]);
629 break;
630 }
64a13a36 631 }
632 }
633 if (deleted) {
cea8c5cd 634 signalModifiedKey(c->db,c->argv[1]);
64a13a36 635 server.dirty += deleted;
e2641e09 636 }
64a13a36 637 addReplyLongLong(c,deleted);
e2641e09 638}
639
640void hlenCommand(redisClient *c) {
641 robj *o;
642 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
643 checkType(c,o,REDIS_HASH)) return;
644
b70d3555 645 addReplyLongLong(c,hashTypeLength(o));
e2641e09 646}
647
ebd85e9a
PN
648static void addHashIteratorCursorToReply(redisClient *c, hashTypeIterator *hi, int what) {
649 if (hi->encoding == REDIS_ENCODING_ZIPLIST) {
650 unsigned char *vstr = NULL;
651 unsigned int vlen = UINT_MAX;
652 long long vll = LLONG_MAX;
653
654 hashTypeCurrentFromZiplist(hi, what, &vstr, &vlen, &vll);
655 if (vstr) {
656 addReplyBulkCBuffer(c, vstr, vlen);
657 } else {
658 addReplyBulkLongLong(c, vll);
659 }
660
661 } else if (hi->encoding == REDIS_ENCODING_HT) {
662 robj *value;
663
664 hashTypeCurrentFromHashTable(hi, what, &value);
665 addReplyBulk(c, value);
666
667 } else {
668 redisPanic("Unknown hash encoding");
669 }
670}
671
e2641e09 672void genericHgetallCommand(redisClient *c, int flags) {
8c304be3 673 robj *o;
e2641e09 674 hashTypeIterator *hi;
ebd85e9a
PN
675 int multiplier = 0;
676 int length, count = 0;
e2641e09 677
678 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
679 || checkType(c,o,REDIS_HASH)) return;
680
ebd85e9a
PN
681 if (flags & REDIS_HASH_KEY) multiplier++;
682 if (flags & REDIS_HASH_VALUE) multiplier++;
683
684 length = hashTypeLength(o) * multiplier;
685 addReplyMultiBulkLen(c, length);
686
e2641e09 687 hi = hashTypeInitIterator(o);
688 while (hashTypeNext(hi) != REDIS_ERR) {
689 if (flags & REDIS_HASH_KEY) {
ebd85e9a 690 addHashIteratorCursorToReply(c, hi, REDIS_HASH_KEY);
e2641e09 691 count++;
692 }
693 if (flags & REDIS_HASH_VALUE) {
ebd85e9a 694 addHashIteratorCursorToReply(c, hi, REDIS_HASH_VALUE);
e2641e09 695 count++;
696 }
697 }
ebd85e9a 698
e2641e09 699 hashTypeReleaseIterator(hi);
ebd85e9a 700 redisAssert(count == length);
e2641e09 701}
702
703void hkeysCommand(redisClient *c) {
704 genericHgetallCommand(c,REDIS_HASH_KEY);
705}
706
707void hvalsCommand(redisClient *c) {
708 genericHgetallCommand(c,REDIS_HASH_VALUE);
709}
710
711void hgetallCommand(redisClient *c) {
712 genericHgetallCommand(c,REDIS_HASH_KEY|REDIS_HASH_VALUE);
713}
714
715void hexistsCommand(redisClient *c) {
716 robj *o;
717 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
718 checkType(c,o,REDIS_HASH)) return;
719
720 addReply(c, hashTypeExists(o,c->argv[2]) ? shared.cone : shared.czero);
721}