Skip to content

Commit 7cbeca1

Browse files
committed
Polishing.
Remove ZAddArgs.empty() method to avoid confusion with none() method. Use NONE constant only internally. Adapt calling methods. Reformat code. See #1794 Original pull request: #1988.
1 parent 4d77392 commit 7cbeca1

File tree

6 files changed

+79
-67
lines changed

6 files changed

+79
-67
lines changed

Diff for: src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

+18
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,15 @@ public Long touch(String... keys) {
26842684
return touch(serializeMulti(keys));
26852685
}
26862686

2687+
/*
2688+
* (non-Javadoc)
2689+
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, double, java.lang.String)
2690+
*/
2691+
@Override
2692+
public Boolean zAdd(String key, double score, String value) {
2693+
return zAdd(serialize(key), score, serialize(value));
2694+
}
2695+
26872696
/*
26882697
* (non-Javadoc)
26892698
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, double, java.lang.String, org.springframework.data.redis.connection.RedisZSetCommands.ZAddArgs)
@@ -2693,6 +2702,15 @@ public Boolean zAdd(String key, double score, String value, ZAddArgs args) {
26932702
return zAdd(serialize(key), score, serialize(value), args);
26942703
}
26952704

2705+
/*
2706+
* (non-Javadoc)
2707+
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, java.util.Set)
2708+
*/
2709+
@Override
2710+
public Long zAdd(String key, Set<StringTuple> tuples) {
2711+
return zAdd(serialize(key), stringTupleToTuple.convert(tuples));
2712+
}
2713+
26962714
/*
26972715
* (non-Javadoc)
26982716
* @see org.springframework.data.redis.connection.StringRedisConnection#zAdd(java.lang.String, java.util.Set, org.springframework.data.redis.connection.RedisZSetCommands.ZAddArgs)

Diff for: src/main/java/org/springframework/data/redis/connection/RedisZSetCommands.java

+7-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.util.Arrays;
1919
import java.util.Collections;
20-
import java.util.LinkedHashSet;
20+
import java.util.EnumSet;
2121
import java.util.List;
2222
import java.util.Set;
2323
import java.util.function.DoubleUnaryOperator;
@@ -400,32 +400,25 @@ public static Limit unlimited() {
400400
/**
401401
* {@code ZADD} specific arguments. <br />
402402
* Looking of the {@code INCR} flag? Use the {@code ZINCRBY} operation instead.
403-
*
404-
* @since 2.3
403+
*
404+
* @since 2.5
405405
* @see <a href="https://redis.io/commands/zadd">Redis Documentation: ZADD</a>
406406
*/
407407
class ZAddArgs {
408408

409-
private static final ZAddArgs NONE = new ZAddArgs(Collections.emptySet());
409+
private static final ZAddArgs NONE = new ZAddArgs(EnumSet.noneOf(Flag.class));
410410

411411
private final Set<Flag> flags;
412412

413413
private ZAddArgs(Set<Flag> flags) {
414414
this.flags = flags;
415415
}
416416

417-
/**
418-
* @return immutable {@link ZAddArgs}.
419-
*/
420-
public static ZAddArgs none() {
421-
return NONE;
422-
}
423-
424417
/**
425418
* @return new instance of {@link ZAddArgs} without any flags set.
426419
*/
427420
public static ZAddArgs empty() {
428-
return new ZAddArgs(new LinkedHashSet<>(5));
421+
return new ZAddArgs(EnumSet.noneOf(Flag.class));
429422
}
430423

431424
/**
@@ -573,7 +566,7 @@ public enum Flag {
573566
*/
574567
@Nullable
575568
default Boolean zAdd(byte[] key, double score, byte[] value) {
576-
return zAdd(key, score, value, ZAddArgs.none());
569+
return zAdd(key, score, value, ZAddArgs.NONE);
577570
}
578571

579572
/**
@@ -601,7 +594,7 @@ default Boolean zAdd(byte[] key, double score, byte[] value) {
601594
*/
602595
@Nullable
603596
default Long zAdd(byte[] key, Set<Tuple> tuples) {
604-
return zAdd(key, tuples, ZAddArgs.none());
597+
return zAdd(key, tuples, ZAddArgs.NONE);
605598
}
606599

607600
/**

Diff for: src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1102,9 +1102,7 @@ default Long lPos(String key, String element) {
11021102
* @see <a href="https://redis.io/commands/zadd">Redis Documentation: ZADD</a>
11031103
* @see RedisZSetCommands#zAdd(byte[], double, byte[])
11041104
*/
1105-
default Boolean zAdd(String key, double score, String value) {
1106-
return zAdd(key, score, value, ZAddArgs.none());
1107-
}
1105+
Boolean zAdd(String key, double score, String value);
11081106

11091107
/**
11101108
* Add the {@code value} to a sorted set at {@code key}, or update its {@code score} depending on the given
@@ -1130,9 +1128,7 @@ default Boolean zAdd(String key, double score, String value) {
11301128
* @see <a href="https://redis.io/commands/zadd">Redis Documentation: ZADD</a>
11311129
* @see RedisZSetCommands#zAdd(byte[], Set)
11321130
*/
1133-
default Long zAdd(String key, Set<StringTuple> tuples) {
1134-
return zAdd(key, tuples, ZAddArgs.none());
1135-
}
1131+
Long zAdd(String key, Set<StringTuple> tuples);
11361132

11371133
/**
11381134
* Add {@code tuples} to a sorted set at {@code key}, or update its {@code score} depending on the given

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ public Flux<NumericResponse<ZAddCommand, Number>> zAdd(Publisher<ZAddCommand> co
9898
}
9999

100100
if (command.isUpsert()) {
101-
args = ZAddArgs.Builder.nx();
101+
args = args == null ? ZAddArgs.Builder.nx() : args.nx();
102102
} else {
103-
args = ZAddArgs.Builder.xx();
103+
args = args == null ? ZAddArgs.Builder.xx() : args.xx();
104104
}
105-
if(command.isGt()) {
105+
106+
if (command.isGt()) {
106107
args = args == null ? ZAddArgs.Builder.gt() : args.gt();
107108
}
108-
if(command.isLt()) {
109+
if (command.isLt()) {
109110
args = args == null ? ZAddArgs.Builder.lt() : args.lt();
110111
}
111112
}
@@ -168,7 +169,8 @@ public Flux<NumericResponse<ZRankCommand, Long>> zRank(Publisher<ZRankCommand> c
168169
Assert.notNull(command.getValue(), "Value must not be null!");
169170

170171
Mono<Long> result = ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)
171-
? cmd.zrank(command.getKey(), command.getValue()) : cmd.zrevrank(command.getKey(), command.getValue());
172+
? cmd.zrank(command.getKey(), command.getValue())
173+
: cmd.zrevrank(command.getKey(), command.getValue());
172174

173175
return result.map(value -> new NumericResponse<>(command, value));
174176
}));
@@ -194,24 +196,21 @@ public Flux<CommandResponse<ZRangeCommand, Flux<Tuple>>> zRange(Publisher<ZRange
194196
if (ObjectUtils.nullSafeEquals(command.getDirection(), Direction.ASC)) {
195197
if (command.isWithScores()) {
196198

197-
result = cmd
198-
.zrangeWithScores(command.getKey(), start, stop).map(sc -> new DefaultTuple(getBytes(sc), sc.getScore()));
199+
result = cmd.zrangeWithScores(command.getKey(), start, stop)
200+
.map(sc -> new DefaultTuple(getBytes(sc), sc.getScore()));
199201
} else {
200202

201-
result = cmd
202-
.zrange(command.getKey(), start, stop)
203+
result = cmd.zrange(command.getKey(), start, stop)
203204
.map(value -> new DefaultTuple(ByteUtils.getBytes(value), Double.NaN));
204205
}
205206
} else {
206207
if (command.isWithScores()) {
207208

208-
result = cmd
209-
.zrevrangeWithScores(command.getKey(), start, stop)
209+
result = cmd.zrevrangeWithScores(command.getKey(), start, stop)
210210
.map(sc -> new DefaultTuple(getBytes(sc), sc.getScore()));
211211
} else {
212212

213-
result = cmd
214-
.zrevrange(command.getKey(), start, stop)
213+
result = cmd.zrevrange(command.getKey(), start, stop)
215214
.map(value -> new DefaultTuple(ByteUtils.getBytes(value), Double.NaN));
216215
}
217216
}
@@ -399,8 +398,7 @@ public Flux<NumericResponse<ZRemRangeByRankCommand, Long>> zRemRangeByRank(
399398
LettuceConverters.getLowerBoundIndex(command.getRange()), //
400399
LettuceConverters.getUpperBoundIndex(command.getRange()));
401400

402-
return result
403-
.map(value -> new NumericResponse<>(command, value));
401+
return result.map(value -> new NumericResponse<>(command, value));
404402
}));
405403
}
406404

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

+31-30
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public Boolean zAdd(byte[] key, double score, byte[] value, ZAddArgs args) {
5959
Assert.notNull(key, "Key must not be null!");
6060
Assert.notNull(value, "Value must not be null!");
6161

62-
return connection.invoke().from(RedisSortedSetAsyncCommands::zadd, key, LettuceZSetCommands.toZAddArgs(args), score, value)
62+
return connection.invoke()
63+
.from(RedisSortedSetAsyncCommands::zadd, key, LettuceZSetCommands.toZAddArgs(args), score, value)
6364
.get(LettuceConverters.longToBoolean());
6465
}
6566

@@ -165,15 +166,15 @@ public Set<Tuple> zRangeByScoreWithScores(byte[] key, Range range, Limit limit)
165166
Assert.notNull(range, "Range for ZRANGEBYSCOREWITHSCORES must not be null!");
166167
Assert.notNull(limit, "Limit must not be null!");
167168

168-
if (limit.isUnlimited()) {
169-
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
170-
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
171-
}
169+
if (limit.isUnlimited()) {
170+
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
171+
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
172+
}
172173

173-
return connection.invoke()
174-
.fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
175-
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
176-
.toSet(LettuceConverters::toTuple);
174+
return connection
175+
.invoke().fromMany(RedisSortedSetAsyncCommands::zrangebyscoreWithScores, key,
176+
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
177+
.toSet(LettuceConverters::toTuple);
177178

178179
}
179180

@@ -214,15 +215,15 @@ public Set<byte[]> zRevRangeByScore(byte[] key, Range range, Limit limit) {
214215
Assert.notNull(range, "Range for ZREVRANGEBYSCORE must not be null!");
215216
Assert.notNull(limit, "Limit must not be null!");
216217

217-
if (limit.isUnlimited()) {
218+
if (limit.isUnlimited()) {
218219

219-
return connection.invoke()
220-
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key, LettuceConverters.<Number> toRange(range))
221-
.toSet();
222-
}
220+
return connection.invoke()
221+
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key, LettuceConverters.<Number> toRange(range))
222+
.toSet();
223+
}
223224

224-
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key,
225-
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit)).toSet();
225+
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscore, key,
226+
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit)).toSet();
226227

227228
}
228229

@@ -237,15 +238,15 @@ public Set<Tuple> zRevRangeByScoreWithScores(byte[] key, Range range, Limit limi
237238
Assert.notNull(range, "Range for ZREVRANGEBYSCOREWITHSCORES must not be null!");
238239
Assert.notNull(limit, "Limit must not be null!");
239240

240-
if (limit.isUnlimited()) {
241-
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
242-
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
243-
}
241+
if (limit.isUnlimited()) {
242+
return connection.invoke().fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
243+
LettuceConverters.<Number> toRange(range)).toSet(LettuceConverters::toTuple);
244+
}
244245

245-
return connection.invoke()
246-
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
247-
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
248-
.toSet(LettuceConverters::toTuple);
246+
return connection.invoke()
247+
.fromMany(RedisSortedSetAsyncCommands::zrevrangebyscoreWithScores, key,
248+
LettuceConverters.<Number> toRange(range), LettuceConverters.toLimit(limit))
249+
.toSet(LettuceConverters::toTuple);
249250

250251
}
251252

@@ -580,23 +581,23 @@ private static io.lettuce.core.ZAddArgs toZAddArgs(ZAddArgs source) {
580581

581582
io.lettuce.core.ZAddArgs target = new io.lettuce.core.ZAddArgs();
582583

583-
if(!source.isEmpty()) {
584+
if (!source.isEmpty()) {
584585
return target;
585586
}
586587

587-
if(source.contains(Flag.XX)) {
588+
if (source.contains(Flag.XX)) {
588589
target.xx();
589590
}
590-
if(source.contains(Flag.NX)) {
591+
if (source.contains(Flag.NX)) {
591592
target.nx();
592593
}
593-
if(source.contains(Flag.GT)) {
594+
if (source.contains(Flag.GT)) {
594595
target.gt();
595596
}
596-
if(source.contains(Flag.LT)) {
597+
if (source.contains(Flag.LT)) {
597598
target.lt();
598599
}
599-
if(source.contains(Flag.CH)) {
600+
if (source.contains(Flag.CH)) {
600601
target.ch();
601602
}
602603
return target;

Diff for: src/main/java/org/springframework/data/redis/core/DefaultZSetOperations.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
5050
*/
5151
@Override
5252
public Boolean add(K key, V value, double score) {
53-
return add(key, value, score, ZAddArgs.none());
53+
54+
byte[] rawKey = rawKey(key);
55+
byte[] rawValue = rawValue(value);
56+
return execute(connection -> connection.zAdd(rawKey, score, rawValue), true);
5457
}
5558

5659
/*
@@ -83,7 +86,10 @@ protected Boolean add(K key, V value, double score, ZAddArgs args) {
8386
*/
8487
@Override
8588
public Long add(K key, Set<TypedTuple<V>> tuples) {
86-
return add(key, tuples, ZAddArgs.none());
89+
90+
byte[] rawKey = rawKey(key);
91+
Set<Tuple> rawValues = rawTupleValues(tuples);
92+
return execute(connection -> connection.zAdd(rawKey, rawValues), true);
8793
}
8894

8995
/*

0 commit comments

Comments
 (0)