From bdd6cee61c207cd0f3d84104872c3ec461152c5b Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 1 Jul 2021 14:25:09 +0200 Subject: [PATCH] Switch to TimeoutUtils for zsetcommands. Remove precise timeout calculation in zsetcommands and switch to TimeoutUtils and update a bit of docs and add some new lines here and there. Original Pull Request: #2107 --- .../redis/connection/ReactiveListCommands.java | 9 +++++---- .../redis/connection/RedisListCommands.java | 1 + .../connection/StringRedisConnection.java | 2 +- .../connection/lettuce/LettuceConverters.java | 18 ++++++------------ .../lettuce/LettuceReactiveZSetCommands.java | 7 ++----- .../lettuce/LettuceZSetCommands.java | 9 +++------ .../data/redis/core/BoundListOperations.java | 2 -- .../data/redis/core/ListOperations.java | 4 ++-- 8 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/ReactiveListCommands.java b/src/main/java/org/springframework/data/redis/connection/ReactiveListCommands.java index a04b97530..516e7f966 100644 --- a/src/main/java/org/springframework/data/redis/connection/ReactiveListCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/ReactiveListCommands.java @@ -671,6 +671,7 @@ public interface ReactiveListCommands { public LMoveCommand(@Nullable ByteBuffer sourceKey, @Nullable ByteBuffer destinationKey, @Nullable Direction from, @Nullable Direction to) { + super(sourceKey); this.destinationKey = destinationKey; this.from = from; @@ -697,15 +698,15 @@ public interface ReactiveListCommands { * properties. * * @param destinationKey must not be {@literal null}. - * @param to must not be {@literal null}. + * @param direction must not be {@literal null}. * @return a new {@link LMoveCommand} with {@literal pivot} applied. */ - public LMoveCommand to(ByteBuffer destinationKey, Direction destinationDirection) { + public LMoveCommand to(ByteBuffer destinationKey, Direction direction) { Assert.notNull(destinationKey, "Destination key must not be null!"); - Assert.notNull(destinationDirection, "Direction must not be null!"); + Assert.notNull(direction, "Direction must not be null!"); - return new LMoveCommand(getKey(), destinationKey, from, destinationDirection); + return new LMoveCommand(getKey(), destinationKey, from, direction); } /** diff --git a/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java b/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java index fb7a887ed..055baec3b 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java @@ -43,6 +43,7 @@ public interface RedisListCommands { * @since 2.6 */ enum Direction { + LEFT, RIGHT; /** diff --git a/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java index 50cee51bc..3d62afac5 100644 --- a/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java @@ -876,7 +876,7 @@ public interface StringRedisConnection extends RedisConnection { * @param timeout * @return {@literal null} when used in pipeline / transaction. * @since 2.6 - * @see Redis Documentation: BLMOVE + * @see Redis Documentation: BLMOVE * @see #lMove(byte[], byte[], Direction, Direction) * @see #bLMove(byte[], byte[], Direction, Direction, double) */ diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java index 48940eb08..19f27d18a 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java @@ -1028,23 +1028,17 @@ public abstract class LettuceConverters extends Converters { static LMoveArgs toLmoveArgs(Enum from, Enum to) { - if (from.name().equals(Direction.LEFT.name()) && to.name().equals(Direction.LEFT.name())) { - return LMoveArgs.Builder.leftLeft(); - } - - if (from.name().equals(Direction.LEFT.name()) && to.name().equals(Direction.RIGHT.name())) { + if (from.name().equals(Direction.LEFT.name())) { + if (to.name().equals(Direction.LEFT.name())) { + return LMoveArgs.Builder.leftLeft(); + } return LMoveArgs.Builder.leftRight(); } - if (from.name().equals(Direction.RIGHT.name()) && to.name().equals(Direction.LEFT.name())) { + if (to.name().equals(Direction.LEFT.name())) { return LMoveArgs.Builder.rightLeft(); } - - if (from.name().equals(Direction.RIGHT.name()) && to.name().equals(Direction.RIGHT.name())) { - return LMoveArgs.Builder.rightRight(); - } - - throw new IllegalArgumentException(String.format("Unsupported combination of arguments: %s/%s", from, to)); + return LMoveArgs.Builder.rightRight(); } /** diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveZSetCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveZSetCommands.java index 10f79ffdb..48ba68b51 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveZSetCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveZSetCommands.java @@ -21,6 +21,7 @@ import io.lettuce.core.ScoredValue; import io.lettuce.core.Value; import io.lettuce.core.ZAddArgs; import io.lettuce.core.ZStoreArgs; +import org.springframework.data.redis.core.TimeoutUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -415,7 +416,7 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands { if(command.getTimeUnit() == TimeUnit.MILLISECONDS) { - double timeout = preciseTimeout(command.getTimeout(), command.getTimeUnit()); + double timeout = TimeoutUtils.toDoubleSeconds(command.getTimeout(), command.getTimeUnit()); Mono> result = (command.getDirection() == PopDirection.MIN ? cmd.bzpopmin(timeout, command.getKey()) @@ -807,10 +808,6 @@ class LettuceReactiveZSetCommands implements ReactiveZSetCommands { return new DefaultTuple(ByteUtils.getBytes(value), score); } - static double preciseTimeout(long val, TimeUnit unit) { - return (double) unit.toMillis(val) / 1000.0D; - } - protected LettuceReactiveRedisConnection getConnection() { return connection; } diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceZSetCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceZSetCommands.java index 04b96fab3..87e768e78 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceZSetCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceZSetCommands.java @@ -34,6 +34,7 @@ import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.KeyBoundCursor; import org.springframework.data.redis.core.ScanIteration; import org.springframework.data.redis.core.ScanOptions; +import org.springframework.data.redis.core.TimeoutUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -370,7 +371,7 @@ class LettuceZSetCommands implements RedisZSetCommands { if(TimeUnit.MILLISECONDS == unit) { return connection.invoke(connection.getAsyncDedicatedConnection()) - .from(RedisSortedSetAsyncCommands::bzpopmin, preciseTimeout(timeout, unit), key) + .from(RedisSortedSetAsyncCommands::bzpopmin, TimeoutUtils.toDoubleSeconds(timeout, unit), key) .get(it -> it.map(LettuceConverters::toTuple).getValueOrElse(null)); } @@ -420,7 +421,7 @@ class LettuceZSetCommands implements RedisZSetCommands { if(TimeUnit.MILLISECONDS == unit) { return connection.invoke(connection.getAsyncDedicatedConnection()) - .from(RedisSortedSetAsyncCommands::bzpopmax, preciseTimeout(timeout, unit), key) + .from(RedisSortedSetAsyncCommands::bzpopmax, TimeoutUtils.toDoubleSeconds(timeout, unit), key) .get(it -> it.map(LettuceConverters::toTuple).getValueOrElse(null)); } @@ -914,8 +915,4 @@ class LettuceZSetCommands implements RedisZSetCommands { } return target; } - - static double preciseTimeout(long val, TimeUnit unit) { - return (double) unit.toMillis(val) / 1000.0D; - } } diff --git a/src/main/java/org/springframework/data/redis/core/BoundListOperations.java b/src/main/java/org/springframework/data/redis/core/BoundListOperations.java index 87aff4d08..7d21a8fe0 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundListOperations.java @@ -256,7 +256,6 @@ public interface BoundListOperations extends BoundKeyOperations { /** * Removes and returns first {@code} elements in list stored at {@code key}. * - * @param key must not be {@literal null}. * @param count * @return can be {@literal null}. * @see Redis Documentation: LPOP @@ -308,7 +307,6 @@ public interface BoundListOperations extends BoundKeyOperations { /** * Removes and returns last {@code} elements in list stored at {@code key}. * - * @param key must not be {@literal null}. * @param count * @return can be {@literal null}. * @see Redis Documentation: RPOP diff --git a/src/main/java/org/springframework/data/redis/core/ListOperations.java b/src/main/java/org/springframework/data/redis/core/ListOperations.java index b7ca5e97c..73d8a4888 100644 --- a/src/main/java/org/springframework/data/redis/core/ListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ListOperations.java @@ -196,6 +196,7 @@ public interface ListOperations { final Direction direction; MoveFrom(K key, Direction direction) { + this.key = key; this.direction = direction; } @@ -207,7 +208,6 @@ public interface ListOperations { public static MoveFrom fromTail(K key) { return new MoveFrom<>(key, Direction.last()); } - } /** @@ -223,6 +223,7 @@ public interface ListOperations { final Direction direction; MoveTo(K key, Direction direction) { + this.key = key; this.direction = direction; } @@ -234,7 +235,6 @@ public interface ListOperations { public static MoveTo toTail(K key) { return new MoveTo<>(key, Direction.last()); } - } /**