]> git.saurik.com Git - redis.git/blobdiff - src/t_hash.c
The universe is 80 columns wide, accept it.
[redis.git] / src / t_hash.c
index eebb66f3638bc395aa42e2352b5fcd01319baf5d..f97fc9926a8078d9830268f8098b4ab925403bc9 100644 (file)
@@ -320,7 +320,7 @@ void hmsetCommand(redisClient *c) {
 }
 
 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,6 +336,12 @@ void hincrbyCommand(redisClient *c) {
         value = 0;
     }
 
+    oldvalue = value;
+    if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||
+        (incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {
+        addReplyError(c,"increment or decrement would overflow");
+        return;
+    }
     value += incr;
     new = createStringObjectFromLongLong(value);
     hashTypeTryObjectEncoding(o,&c->argv[2],NULL);
@@ -346,6 +352,33 @@ void hincrbyCommand(redisClient *c) {
     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++;
+}
+
 void hgetCommand(redisClient *c) {
     robj *o, *value;
     unsigned char *v;