X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/8c304be359f15151ed8fe3e5ff61b0238881358d..ad0df18fdc965183533515aae5e4e01009616c86:/src/t_hash.c?ds=sidebyside diff --git a/src/t_hash.c b/src/t_hash.c index 838f29dd..b8e2ad31 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -55,7 +55,7 @@ int hashTypeGet(robj *o, robj *key, robj **objval, unsigned char **v, } else { dictEntry *de = dictFind(o->ptr,key); if (de == NULL) return -1; - *objval = dictGetEntryVal(de); + *objval = dictGetVal(de); } return o->encoding; } @@ -160,7 +160,7 @@ hashTypeIterator *hashTypeInitIterator(robj *subject) { } else if (hi->encoding == REDIS_ENCODING_HT) { hi->di = dictGetIterator(subject->ptr); } else { - redisAssert(NULL); + redisAssertWithInfo(NULL,subject,0); } return hi; } @@ -206,9 +206,9 @@ int hashTypeCurrent(hashTypeIterator *hi, int what, robj **objval, unsigned char } } else { if (what & REDIS_HASH_KEY) - *objval = dictGetEntryKey(hi->de); + *objval = dictGetKey(hi->de); else - *objval = dictGetEntryVal(hi->de); + *objval = dictGetVal(hi->de); } return hi->encoding; } @@ -219,8 +219,8 @@ int hashTypeCurrent(hashTypeIterator *hi, int what, robj **objval, unsigned char * reference is retained. */ robj *hashTypeCurrentObject(hashTypeIterator *hi, int what) { robj *obj; - unsigned char *v; - unsigned int vlen; + unsigned char *v = NULL; + unsigned int vlen = 0; int encoding = hashTypeCurrent(hi,what,&obj,&v,&vlen); if (encoding == REDIS_ENCODING_HT) { @@ -250,7 +250,7 @@ void convertToRealHash(robj *o) { unsigned int klen, vlen; dict *dict = dictCreate(&hashDictType,NULL); - redisAssert(o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT); + redisAssertWithInfo(NULL,o,o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT); p = zipmapRewind(zm); while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) { robj *keyobj, *valobj; @@ -279,7 +279,7 @@ void hsetCommand(redisClient *c) { hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]); update = hashTypeSet(o,c->argv[2],c->argv[3]); addReply(c, update ? shared.czero : shared.cone); - touchWatchedKey(c->db,c->argv[1]); + signalModifiedKey(c->db,c->argv[1]); server.dirty++; } @@ -294,7 +294,7 @@ void hsetnxCommand(redisClient *c) { hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]); hashTypeSet(o,c->argv[2],c->argv[3]); addReply(c, shared.cone); - touchWatchedKey(c->db,c->argv[1]); + signalModifiedKey(c->db,c->argv[1]); server.dirty++; } } @@ -315,12 +315,12 @@ void hmsetCommand(redisClient *c) { hashTypeSet(o,c->argv[i],c->argv[i+1]); } addReply(c, shared.ok); - touchWatchedKey(c->db,c->argv[1]); + signalModifiedKey(c->db,c->argv[1]); server.dirty++; } void hincrbyCommand(redisClient *c) { - long long value, incr; + long long value, incr, oldvalue; robj *o, *current, *new; if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return; @@ -336,13 +336,45 @@ void hincrbyCommand(redisClient *c) { value = 0; } + oldvalue = value; value += incr; + if ((incr < 0 && value > oldvalue) || (incr > 0 && value < oldvalue)) { + addReplyError(c,"increment or decrement would overflow"); + return; + } new = createStringObjectFromLongLong(value); hashTypeTryObjectEncoding(o,&c->argv[2],NULL); hashTypeSet(o,c->argv[2],new); decrRefCount(new); addReplyLongLong(c,value); - touchWatchedKey(c->db,c->argv[1]); + signalModifiedKey(c->db,c->argv[1]); + server.dirty++; +} + +void hincrbyfloatCommand(redisClient *c) { + double long value, incr; + robj *o, *current, *new; + + if (getLongDoubleFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return; + if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return; + if ((current = hashTypeGetObject(o,c->argv[2])) != NULL) { + if (getLongDoubleFromObjectOrReply(c,current,&value, + "hash value is not a valid float") != REDIS_OK) { + decrRefCount(current); + return; + } + decrRefCount(current); + } else { + value = 0; + } + + value += incr; + new = createStringObjectFromLongDouble(value); + hashTypeTryObjectEncoding(o,&c->argv[2],NULL); + hashTypeSet(o,c->argv[2],new); + addReplyBulk(c,new); + decrRefCount(new); + signalModifiedKey(c->db,c->argv[1]); server.dirty++; } @@ -396,17 +428,25 @@ void hmgetCommand(redisClient *c) { void hdelCommand(redisClient *c) { robj *o; + int j, deleted = 0; + if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || checkType(c,o,REDIS_HASH)) return; - if (hashTypeDelete(o,c->argv[2])) { - if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]); - addReply(c,shared.cone); - touchWatchedKey(c->db,c->argv[1]); - server.dirty++; - } else { - addReply(c,shared.czero); + for (j = 2; j < c->argc; j++) { + if (hashTypeDelete(o,c->argv[j])) { + deleted++; + if (hashTypeLength(o) == 0) { + dbDelete(c->db,c->argv[1]); + break; + } + } + } + if (deleted) { + signalModifiedKey(c->db,c->argv[1]); + server.dirty += deleted; } + addReplyLongLong(c,deleted); } void hlenCommand(redisClient *c) { @@ -430,8 +470,8 @@ void genericHgetallCommand(redisClient *c, int flags) { hi = hashTypeInitIterator(o); while (hashTypeNext(hi) != REDIS_ERR) { robj *obj; - unsigned char *v; - unsigned int vlen; + unsigned char *v = NULL; + unsigned int vlen = 0; int encoding; if (flags & REDIS_HASH_KEY) {