Skip to content

Commit bdd6cee

Browse files
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: spring-projects#2107
1 parent eefbd0d commit bdd6cee

8 files changed

+20
-32
lines changed

src/main/java/org/springframework/data/redis/connection/ReactiveListCommands.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ class LMoveCommand extends KeyCommand {
671671

672672
public LMoveCommand(@Nullable ByteBuffer sourceKey, @Nullable ByteBuffer destinationKey, @Nullable Direction from,
673673
@Nullable Direction to) {
674+
674675
super(sourceKey);
675676
this.destinationKey = destinationKey;
676677
this.from = from;
@@ -697,15 +698,15 @@ public static LMoveCommand from(ByteBuffer sourceKey, Direction sourceDirection)
697698
* properties.
698699
*
699700
* @param destinationKey must not be {@literal null}.
700-
* @param to must not be {@literal null}.
701+
* @param direction must not be {@literal null}.
701702
* @return a new {@link LMoveCommand} with {@literal pivot} applied.
702703
*/
703-
public LMoveCommand to(ByteBuffer destinationKey, Direction destinationDirection) {
704+
public LMoveCommand to(ByteBuffer destinationKey, Direction direction) {
704705

705706
Assert.notNull(destinationKey, "Destination key must not be null!");
706-
Assert.notNull(destinationDirection, "Direction must not be null!");
707+
Assert.notNull(direction, "Direction must not be null!");
707708

708-
return new LMoveCommand(getKey(), destinationKey, from, destinationDirection);
709+
return new LMoveCommand(getKey(), destinationKey, from, direction);
709710
}
710711

711712
/**

src/main/java/org/springframework/data/redis/connection/RedisListCommands.java

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum Position {
4343
* @since 2.6
4444
*/
4545
enum Direction {
46+
4647
LEFT, RIGHT;
4748

4849
/**

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ default Long lPos(String key, String element) {
876876
* @param timeout
877877
* @return {@literal null} when used in pipeline / transaction.
878878
* @since 2.6
879-
* @see <a href="https://redis.io/commands/lmove">Redis Documentation: BLMOVE</a>
879+
* @see <a href="https://redis.io/commands/blmove">Redis Documentation: BLMOVE</a>
880880
* @see #lMove(byte[], byte[], Direction, Direction)
881881
* @see #bLMove(byte[], byte[], Direction, Direction, double)
882882
*/

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -1028,23 +1028,17 @@ static long getUpperBoundIndex(org.springframework.data.domain.Range<Long> range
10281028

10291029
static LMoveArgs toLmoveArgs(Enum<?> from, Enum<?> to) {
10301030

1031-
if (from.name().equals(Direction.LEFT.name()) && to.name().equals(Direction.LEFT.name())) {
1032-
return LMoveArgs.Builder.leftLeft();
1033-
}
1034-
1035-
if (from.name().equals(Direction.LEFT.name()) && to.name().equals(Direction.RIGHT.name())) {
1031+
if (from.name().equals(Direction.LEFT.name())) {
1032+
if (to.name().equals(Direction.LEFT.name())) {
1033+
return LMoveArgs.Builder.leftLeft();
1034+
}
10361035
return LMoveArgs.Builder.leftRight();
10371036
}
10381037

1039-
if (from.name().equals(Direction.RIGHT.name()) && to.name().equals(Direction.LEFT.name())) {
1038+
if (to.name().equals(Direction.LEFT.name())) {
10401039
return LMoveArgs.Builder.rightLeft();
10411040
}
1042-
1043-
if (from.name().equals(Direction.RIGHT.name()) && to.name().equals(Direction.RIGHT.name())) {
1044-
return LMoveArgs.Builder.rightRight();
1045-
}
1046-
1047-
throw new IllegalArgumentException(String.format("Unsupported combination of arguments: %s/%s", from, to));
1041+
return LMoveArgs.Builder.rightRight();
10481042
}
10491043

10501044
/**

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveZSetCommands.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.lettuce.core.Value;
2222
import io.lettuce.core.ZAddArgs;
2323
import io.lettuce.core.ZStoreArgs;
24+
import org.springframework.data.redis.core.TimeoutUtils;
2425
import reactor.core.publisher.Flux;
2526
import reactor.core.publisher.Mono;
2627

@@ -415,7 +416,7 @@ public Flux<CommandResponse<BZPopCommand, Flux<Tuple>>> bZPop(Publisher<BZPopCom
415416

416417
if(command.getTimeUnit() == TimeUnit.MILLISECONDS) {
417418

418-
double timeout = preciseTimeout(command.getTimeout(), command.getTimeUnit());
419+
double timeout = TimeoutUtils.toDoubleSeconds(command.getTimeout(), command.getTimeUnit());
419420

420421
Mono<ScoredValue<ByteBuffer>> result = (command.getDirection() == PopDirection.MIN
421422
? cmd.bzpopmin(timeout, command.getKey())
@@ -807,10 +808,6 @@ private Tuple toTuple(ByteBuffer value, double score) {
807808
return new DefaultTuple(ByteUtils.getBytes(value), score);
808809
}
809810

810-
static double preciseTimeout(long val, TimeUnit unit) {
811-
return (double) unit.toMillis(val) / 1000.0D;
812-
}
813-
814811
protected LettuceReactiveRedisConnection getConnection() {
815812
return connection;
816813
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceZSetCommands.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.redis.core.KeyBoundCursor;
3535
import org.springframework.data.redis.core.ScanIteration;
3636
import org.springframework.data.redis.core.ScanOptions;
37+
import org.springframework.data.redis.core.TimeoutUtils;
3738
import org.springframework.lang.Nullable;
3839
import org.springframework.util.Assert;
3940

@@ -370,7 +371,7 @@ public Tuple bZPopMin(byte[] key, long timeout, TimeUnit unit) {
370371
if(TimeUnit.MILLISECONDS == unit) {
371372

372373
return connection.invoke(connection.getAsyncDedicatedConnection())
373-
.from(RedisSortedSetAsyncCommands::bzpopmin, preciseTimeout(timeout, unit), key)
374+
.from(RedisSortedSetAsyncCommands::bzpopmin, TimeoutUtils.toDoubleSeconds(timeout, unit), key)
374375
.get(it -> it.map(LettuceConverters::toTuple).getValueOrElse(null));
375376
}
376377

@@ -420,7 +421,7 @@ public Tuple bZPopMax(byte[] key, long timeout, TimeUnit unit) {
420421
if(TimeUnit.MILLISECONDS == unit) {
421422

422423
return connection.invoke(connection.getAsyncDedicatedConnection())
423-
.from(RedisSortedSetAsyncCommands::bzpopmax, preciseTimeout(timeout, unit), key)
424+
.from(RedisSortedSetAsyncCommands::bzpopmax, TimeoutUtils.toDoubleSeconds(timeout, unit), key)
424425
.get(it -> it.map(LettuceConverters::toTuple).getValueOrElse(null));
425426
}
426427

@@ -914,8 +915,4 @@ private static io.lettuce.core.ZAddArgs toZAddArgs(ZAddArgs source) {
914915
}
915916
return target;
916917
}
917-
918-
static double preciseTimeout(long val, TimeUnit unit) {
919-
return (double) unit.toMillis(val) / 1000.0D;
920-
}
921918
}

src/main/java/org/springframework/data/redis/core/BoundListOperations.java

-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ public interface BoundListOperations<K, V> extends BoundKeyOperations<K> {
256256
/**
257257
* Removes and returns first {@code} elements in list stored at {@code key}.
258258
*
259-
* @param key must not be {@literal null}.
260259
* @param count
261260
* @return can be {@literal null}.
262261
* @see <a href="https://redis.io/commands/lpop">Redis Documentation: LPOP</a>
@@ -308,7 +307,6 @@ default V leftPop(Duration timeout) {
308307
/**
309308
* Removes and returns last {@code} elements in list stored at {@code key}.
310309
*
311-
* @param key must not be {@literal null}.
312310
* @param count
313311
* @return can be {@literal null}.
314312
* @see <a href="https://redis.io/commands/rpop">Redis Documentation: RPOP</a>

src/main/java/org/springframework/data/redis/core/ListOperations.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class MoveFrom<K> {
196196
final Direction direction;
197197

198198
MoveFrom(K key, Direction direction) {
199+
199200
this.key = key;
200201
this.direction = direction;
201202
}
@@ -207,7 +208,6 @@ public static <K> MoveFrom<K> fromHead(K key) {
207208
public static <K> MoveFrom<K> fromTail(K key) {
208209
return new MoveFrom<>(key, Direction.last());
209210
}
210-
211211
}
212212

213213
/**
@@ -223,6 +223,7 @@ class MoveTo<K> {
223223
final Direction direction;
224224

225225
MoveTo(K key, Direction direction) {
226+
226227
this.key = key;
227228
this.direction = direction;
228229
}
@@ -234,7 +235,6 @@ public static <K> MoveTo<K> toHead(K key) {
234235
public static <K> MoveTo<K> toTail(K key) {
235236
return new MoveTo<>(key, Direction.last());
236237
}
237-
238238
}
239239

240240
/**

0 commit comments

Comments
 (0)