From de79a2ee12ef0e301d05494d64831bc407ba3696 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 1 Feb 2012 17:05:45 +0100 Subject: [PATCH] Make SORT BY STORE ... to always produce the same output by force sorting, so that we have deterministic replication of this command. --- src/sort.c | 9 +++++++++ tests/unit/sort.tcl | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/src/sort.c b/src/sort.c index 11b73ad3..3f02e49a 100644 --- a/src/sort.c +++ b/src/sort.c @@ -203,6 +203,15 @@ void sortCommand(redisClient *c) { j++; } + /* If we have STORE we need to force sorting for deterministic output + * and replication. We use alpha sorting since this is guaranteed to + * work with any input. */ + if (storekey && dontsort) { + dontsort = 0; + alpha = 1; + sortby = NULL; + } + /* Destructively convert encoded sorted sets for SORT. */ if (sortval->type == REDIS_ZSET) zsetConvert(sortval, REDIS_ENCODING_SKIPLIST); diff --git a/tests/unit/sort.tcl b/tests/unit/sort.tcl index a50f0473..20eec44b 100644 --- a/tests/unit/sort.tcl +++ b/tests/unit/sort.tcl @@ -157,6 +157,13 @@ start_server { r exists foo } {0} + test "SORT with BY and STORE should still order output" { + r del myset mylist + r sadd myset a b c d e f g h i l m n o p q r s t u v z + r sort myset alpha by _ store mylist + r lrange mylist 0 -1 + } {a b c d e f g h i l m n o p q r s t u v z} + tags {"slow"} { set num 100 set res [create_random_dataset $num lpush] -- 2.45.2