From d7f43c081a49e31aac6b060ca8dfbc259da2c53d Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 27 Oct 2009 18:31:12 +0100 Subject: [PATCH] A lot of ZSETs tests implemented, and a bug fixed thanks to this new tests --- TODO | 2 +- client-libraries/tcl/redis.tcl | 2 +- redis.c | 31 +++++++----- test-redis.tcl | 91 ++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 14 deletions(-) diff --git a/TODO b/TODO index 41f30741..dcfd03b0 100644 --- a/TODO +++ b/TODO @@ -3,7 +3,7 @@ Pre 1.1 todo * For now only the last argument gets integer encoded, so make sure that: 1) every multi bulk commands implemented will have the last arg that is indeed a value, and not used otherwise. 2) to explicitly call the function to encode the object in MSET and other commands where there are multiple "values". * Man pages for MSET MSETNX and SRANDMEMBER. * Hashes (HSET, HGET, HEXISTS, HLEN, ...). -* ZSETs missing stuff: ZINCRBY, ZSCORE. +* ZSETs missing stuff: ZINCRBY * An utility able to export an .rdb file into a text-only JSON dump, we can't live anymore without such a tool. Probably an extension to redis-cli. After 1.1 todo diff --git a/client-libraries/tcl/redis.tcl b/client-libraries/tcl/redis.tcl index 8bca7886..61dae0c1 100644 --- a/client-libraries/tcl/redis.tcl +++ b/client-libraries/tcl/redis.tcl @@ -20,7 +20,7 @@ array set ::redis::multibulkarg {} # Flag commands requiring last argument as a bulk write operation foreach redis_bulk_cmd { - set setnx rpush lpush lset lrem sadd srem sismember echo getset smove zadd zrem + set setnx rpush lpush lset lrem sadd srem sismember echo getset smove zadd zrem zscore } { set ::redis::bulkarg($redis_bulk_cmd) {} } diff --git a/redis.c b/redis.c index 509d2f08..eb44138f 100644 --- a/redis.c +++ b/redis.c @@ -2074,24 +2074,31 @@ static robj *getDecodedObject(const robj *o) { } } +/* Compare two string objects via strcmp() or alike. + * Note that the objects may be integer-encoded. In such a case we + * use snprintf() to get a string representation of the numbers on the stack + * and compare the strings, it's much faster than calling getDecodedObject(). */ static int compareStringObjects(robj *a, robj *b) { assert(a->type == REDIS_STRING && b->type == REDIS_STRING); + char bufa[128], bufb[128], *astr, *bstr; + int bothsds = 1; if (a == b) return 0; - if (a->encoding == REDIS_ENCODING_INT && b->encoding == REDIS_ENCODING_INT){ - return (long)a->ptr - (long)b->ptr; + if (a->encoding != REDIS_ENCODING_RAW) { + snprintf(bufa,sizeof(bufa),"%ld",(long) a->ptr); + astr = bufa; + bothsds = 0; } else { - int retval; - - incrRefCount(a); - incrRefCount(b); - if (a->encoding != REDIS_ENCODING_RAW) a = getDecodedObject(a); - if (b->encoding != REDIS_ENCODING_RAW) b = getDecodedObject(a); - retval = sdscmp(a->ptr,b->ptr); - decrRefCount(a); - decrRefCount(b); - return retval; + astr = a->ptr; } + if (b->encoding != REDIS_ENCODING_RAW) { + snprintf(bufb,sizeof(bufb),"%ld",(long) b->ptr); + bstr = bufb; + bothsds = 0; + } else { + bstr = b->ptr; + } + return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr); } static size_t stringObjectLen(robj *o) { diff --git a/test-redis.tcl b/test-redis.tcl index a5182670..1eee7db3 100644 --- a/test-redis.tcl +++ b/test-redis.tcl @@ -38,6 +38,13 @@ proc randstring {min max {type binary}} { return $output } +# Useful for some test +proc zlistAlikeSort {a b} { + if {[lindex $a 0] > [lindex $b 0]} {return 1} + if {[lindex $a 0] < [lindex $b 0]} {return -1} + string compare [lindex $a 1] [lindex $b 1] +} + proc main {server port} { set r [redis $server $port] set err "" @@ -528,6 +535,9 @@ proc main {server port} { $r lpush mysavelist world $r set myemptykey {} $r set mynormalkey {blablablba} + $r zadd mytestzset a 10 + $r zadd mytestzset b 20 + $r zadd mytestzset c 30 $r save } {OK} @@ -768,6 +778,87 @@ proc main {server port} { list [$r msetnx x1 xxx y2 yyy] [$r get x1] [$r get y2] } {1 xxx yyy} + test {ZSET basic ZADD and score update} { + $r zadd ztmp 10 x + $r zadd ztmp 20 y + $r zadd ztmp 30 z + set aux1 [$r zrange ztmp 0 -1] + $r zadd ztmp 1 y + set aux2 [$r zrange ztmp 0 -1] + list $aux1 $aux2 + } {{x y z} {y x z}} + + test {ZSCORE} { + list [$r zscore ztmp x] [$r zscore ztmp y] [$r zscore ztmp z] + } {10 1 30} + + test {ZRANGE and ZREVRANGE} { + list [$r zrange ztmp 0 -1] [$r zrevrange ztmp 0 -1] + } {{y x z} {z x y}} + + test {ZSETs stress tester - sorting is working well?} { + set delta 0 + for {set test 0} {$test < 2} {incr test} { + unset -nocomplain auxarray + array set auxarray {} + set auxlist {} + $r del myzset + for {set i 0} {$i < 1000} {incr i} { + if {$test == 0} { + set score [expr rand()] + } else { + set score [expr int(rand()*10)] + } + set auxarray($i) $score + $r zadd myzset $score $i + # Random update + if {[expr rand()] < .2} { + set j [expr int(rand()*1000)] + if {$test == 0} { + set score [expr rand()] + } else { + set score [expr int(rand()*10)] + } + set auxarray($j) $score + $r zadd myzset $score $j + } + } + foreach {item score} [array get auxarray] { + lappend auxlist [list $score $item] + } + set sorted [lsort -command zlistAlikeSort $auxlist] + set auxlist {} + foreach x $sorted { + lappend auxlist [lindex $x 1] + } + set fromredis [$r zrange myzset 0 -1] + set delta 0 + for {set i 0} {$i < [llength $fromredis]} {incr i} { + if {[lindex $fromredis $i] != [lindex $auxlist $i]} { + incr delta + } + } + } + format $delta + } {0} + + test {ZSETs skiplist implementation backlink consistency test} { + set diff 0 + set elements 10000 + for {set j 0} {$j < $elements} {incr j} { + $r zadd myzset [expr rand()] "Element-$j" + $r zrem myzset "Element-[expr int(rand()*$elements)]" + } + set l1 [$r zrange myzset 0 -1] + set l2 [$r zrevrange myzset 0 -1] + for {set j 0} {$j < [llength $l1]} {incr j} { + if {[lindex $l1 $j] ne [lindex $l2 end-$j]} { + incr diff + } + } + format $diff + } {0} + foreach fuzztype {binary alpha compr} { test "FUZZ stresser with data model $fuzztype" { set err 0