diff --git a/src/StackExchange.Redis/RedisDatabase.cs b/src/StackExchange.Redis/RedisDatabase.cs index 6a0210e6b..258381f93 100644 --- a/src/StackExchange.Redis/RedisDatabase.cs +++ b/src/StackExchange.Redis/RedisDatabase.cs @@ -3445,12 +3445,15 @@ private Message GetSortedSetMultiPopMessage(RedisKey[] keys, Order order, long c return tran; } - private static RedisValue GetLexRange(RedisValue value, Exclude exclude, bool isStart) + private static RedisValue GetLexRange(RedisValue value, Exclude exclude, bool isStart, Order order) { if (value.IsNull) { - return isStart ? RedisLiterals.MinusSymbol : RedisLiterals.PlusSymbol; + if (order == Order.Ascending) return isStart ? RedisLiterals.MinusSymbol : RedisLiterals.PlusSymbol; + + return isStart ? RedisLiterals.PlusSymbol : RedisLiterals.MinusSymbol; // 24.01.2024: when descending order: Plus and Minus have to be reversed } + byte[] orig = value!; byte[] result = new byte[orig.Length + 1]; @@ -4553,9 +4556,9 @@ private Message GetStringSetAndGetMessage( return new ScanEnumerable(this, server, key, pattern, pageSize, cursor, pageOffset, flags, command, processor); } - private Message GetLexMessage(RedisCommand command, RedisKey key, RedisValue min, RedisValue max, Exclude exclude, long skip, long take, CommandFlags flags) + private Message GetLexMessage(RedisCommand command, RedisKey key, RedisValue min, RedisValue max, Exclude exclude, long skip, long take, CommandFlags flags, Order order) { - RedisValue start = GetLexRange(min, exclude, true), stop = GetLexRange(max, exclude, false); + RedisValue start = GetLexRange(min, exclude, true, order), stop = GetLexRange(max, exclude, false, order); if (skip == 0 && take == -1) return Message.Create(Database, flags, command, key, start, stop); @@ -4565,7 +4568,7 @@ private Message GetLexMessage(RedisCommand command, RedisKey key, RedisValue min public long SortedSetLengthByValue(RedisKey key, RedisValue min, RedisValue max, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None) { - var msg = GetLexMessage(RedisCommand.ZLEXCOUNT, key, min, max, exclude, 0, -1, flags); + var msg = GetLexMessage(RedisCommand.ZLEXCOUNT, key, min, max, exclude, 0, -1, flags, Order.Ascending); return ExecuteSync(msg, ResultProcessor.Int64); } @@ -4591,19 +4594,19 @@ public RedisValue[] SortedSetRangeByValue(RedisKey key, RedisValue min = default Exclude exclude = Exclude.None, Order order = Order.Ascending, long skip = 0, long take = -1, CommandFlags flags = CommandFlags.None) { ReverseLimits(order, ref exclude, ref min, ref max); - var msg = GetLexMessage(order == Order.Ascending ? RedisCommand.ZRANGEBYLEX : RedisCommand.ZREVRANGEBYLEX, key, min, max, exclude, skip, take, flags); + var msg = GetLexMessage(order == Order.Ascending ? RedisCommand.ZRANGEBYLEX : RedisCommand.ZREVRANGEBYLEX, key, min, max, exclude, skip, take, flags, order); return ExecuteSync(msg, ResultProcessor.RedisValueArray, defaultValue: Array.Empty()); } public long SortedSetRemoveRangeByValue(RedisKey key, RedisValue min, RedisValue max, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None) { - var msg = GetLexMessage(RedisCommand.ZREMRANGEBYLEX, key, min, max, exclude, 0, -1, flags); + var msg = GetLexMessage(RedisCommand.ZREMRANGEBYLEX, key, min, max, exclude, 0, -1, flags, Order.Ascending); return ExecuteSync(msg, ResultProcessor.Int64); } public Task SortedSetLengthByValueAsync(RedisKey key, RedisValue min, RedisValue max, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None) { - var msg = GetLexMessage(RedisCommand.ZLEXCOUNT, key, min, max, exclude, 0, -1, flags); + var msg = GetLexMessage(RedisCommand.ZLEXCOUNT, key, min, max, exclude, 0, -1, flags, Order.Ascending); return ExecuteAsync(msg, ResultProcessor.Int64); } @@ -4614,13 +4617,13 @@ public Task SortedSetRangeByValueAsync(RedisKey key, RedisValue mi Exclude exclude = Exclude.None, Order order = Order.Ascending, long skip = 0, long take = -1, CommandFlags flags = CommandFlags.None) { ReverseLimits(order, ref exclude, ref min, ref max); - var msg = GetLexMessage(order == Order.Ascending ? RedisCommand.ZRANGEBYLEX : RedisCommand.ZREVRANGEBYLEX, key, min, max, exclude, skip, take, flags); + var msg = GetLexMessage(order == Order.Ascending ? RedisCommand.ZRANGEBYLEX : RedisCommand.ZREVRANGEBYLEX, key, min, max, exclude, skip, take, flags, order); return ExecuteAsync(msg, ResultProcessor.RedisValueArray, defaultValue: Array.Empty()); } public Task SortedSetRemoveRangeByValueAsync(RedisKey key, RedisValue min, RedisValue max, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None) { - var msg = GetLexMessage(RedisCommand.ZREMRANGEBYLEX, key, min, max, exclude, 0, -1, flags); + var msg = GetLexMessage(RedisCommand.ZREMRANGEBYLEX, key, min, max, exclude, 0, -1, flags, Order.Ascending); return ExecuteAsync(msg, ResultProcessor.Int64); }