Skip to content

Commit 4cb843d

Browse files
committed
See spring-projects#2655 Original pull request: spring-projects#2672
1 parent 5c87bf0 commit 4cb843d

11 files changed

+90
-86
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public Mono<Long> add(K key, Map<V, Point> memberCoordinateMap) {
9191

9292
Mono<List<GeoLocation<ByteBuffer>>> serializedList = Flux
9393
.fromIterable(() -> memberCoordinateMap.entrySet().iterator())
94-
.map(entry -> new GeoLocation<>(rawValue(entry.getKey()), entry.getValue())).collectList();
94+
.map(entry -> new GeoLocation<>(rawValue(entry.getKey()), entry.getValue()))
95+
.collectList();
9596

9697
return serializedList.flatMap(list -> geoCommands.geoAdd(rawKey(key), list));
9798
});
@@ -106,7 +107,8 @@ public Mono<Long> add(K key, Iterable<GeoLocation<V>> geoLocations) {
106107
return createMono(geoCommands -> {
107108

108109
Mono<List<GeoLocation<ByteBuffer>>> serializedList = Flux.fromIterable(geoLocations)
109-
.map(location -> new GeoLocation<>(rawValue(location.getName()), location.getPoint())).collectList();
110+
.map(location -> new GeoLocation<>(rawValue(location.getName()), location.getPoint()))
111+
.collectList();
110112

111113
return serializedList.flatMap(list -> geoCommands.geoAdd(rawKey(key), list));
112114
});
@@ -220,7 +222,7 @@ public Flux<GeoResult<GeoLocation<V>>> radius(K key, V member, double radius) {
220222

221223
return createFlux(geoCommands ->
222224
geoCommands.geoRadiusByMember(rawKey(key), rawValue(member), new Distance(radius)) //
223-
.map(this::readGeoResult));
225+
.map(this::readGeoResult));
224226
}
225227

226228
@Override
@@ -265,7 +267,7 @@ public Mono<Boolean> delete(K key) {
265267

266268
Assert.notNull(key, "Key must not be null");
267269

268-
return template.doCreateMono(connection -> connection.keyCommands().del(rawKey(key))).map(l -> l != 0);
270+
return template.doCreateMono(connection -> connection.keyCommands().del(rawKey(key))).map(count -> count != 0);
269271
}
270272

271273
@Override
@@ -276,8 +278,8 @@ public Flux<GeoResult<GeoLocation<V>>> search(K key, GeoReference<V> reference,
276278
Assert.notNull(reference, "GeoReference must not be null");
277279
GeoReference<ByteBuffer> rawReference = getGeoReference(reference);
278280

279-
return createFlux(geoCommands -> geoCommands
280-
.geoSearch(rawKey(key), rawReference, geoPredicate, args).map(this::readGeoResult));
281+
return createFlux(geoCommands -> geoCommands.geoSearch(rawKey(key), rawReference, geoPredicate, args)
282+
.map(this::readGeoResult));
281283
}
282284

283285
@Override

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.redis.connection.ReactiveHashCommands;
3131
import org.springframework.data.redis.connection.convert.Converters;
3232
import org.springframework.data.redis.serializer.RedisSerializationContext;
33+
import org.springframework.data.redis.util.RedisAssertions;
3334
import org.springframework.lang.Nullable;
3435
import org.springframework.util.Assert;
3536

@@ -211,7 +212,7 @@ public Flux<HV> values(H key) {
211212

212213
Assert.notNull(key, "Key must not be null");
213214

214-
return createFlux(connection -> connection.hVals(rawKey(key)) //
215+
return createFlux(hashCommands -> hashCommands.hVals(rawKey(key)) //
215216
.map(this::readRequiredHashValue));
216217
}
217218

@@ -276,30 +277,20 @@ private HK readHashKey(ByteBuffer value) {
276277

277278
private HK readRequiredHashKey(ByteBuffer buffer) {
278279

279-
HK hashKey = readHashKey(buffer);
280-
281-
if (hashKey == null) {
282-
throw new InvalidDataAccessApiUsageException("Deserialized hash key is null");
283-
}
284-
285-
return hashKey;
280+
return RedisAssertions.requireNonNull(readHashKey(buffer),
281+
() -> new InvalidDataAccessApiUsageException("Deserialized hash key is null"));
286282
}
287283

288284
@SuppressWarnings("unchecked")
289285
@Nullable
290286
private HV readHashValue(@Nullable ByteBuffer value) {
291-
return (HV) (value == null ? null : serializationContext.getHashValueSerializationPair().read(value));
287+
return value != null ? (HV) serializationContext.getHashValueSerializationPair().read(value) : null;
292288
}
293289

294290
private HV readRequiredHashValue(ByteBuffer buffer) {
295291

296-
HV hashValue = readHashValue(buffer);
297-
298-
if (hashValue == null) {
299-
throw new InvalidDataAccessApiUsageException("Deserialized hash value is null");
300-
}
301-
302-
return hashValue;
292+
return RedisAssertions.requireNonNull(readHashValue(buffer),
293+
() -> new InvalidDataAccessApiUsageException("Deserialized hash value is null"));
303294
}
304295

305296
private Map.Entry<HK, HV> deserializeHashEntry(Map.Entry<ByteBuffer, ByteBuffer> source) {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.redis.connection.ReactiveListCommands.LPosCommand;
3434
import org.springframework.data.redis.connection.RedisListCommands.Position;
3535
import org.springframework.data.redis.serializer.RedisSerializationContext;
36+
import org.springframework.data.redis.util.RedisAssertions;
3637
import org.springframework.lang.Nullable;
3738
import org.springframework.util.Assert;
3839

@@ -351,12 +352,7 @@ private V readValue(ByteBuffer buffer) {
351352

352353
private V readRequiredValue(ByteBuffer buffer) {
353354

354-
V v = readValue(buffer);
355-
356-
if (v == null) {
357-
throw new InvalidDataAccessApiUsageException("Deserialized list value is null");
358-
}
359-
360-
return v;
355+
return RedisAssertions.requireNonNull(readValue(buffer),
356+
() -> new InvalidDataAccessApiUsageException("Deserialized list value is null"));
361357
}
362358
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.dao.InvalidDataAccessApiUsageException;
3232
import org.springframework.data.redis.connection.ReactiveSetCommands;
3333
import org.springframework.data.redis.serializer.RedisSerializationContext;
34+
import org.springframework.data.redis.util.RedisAssertions;
3435
import org.springframework.lang.Nullable;
3536
import org.springframework.util.Assert;
3637

@@ -424,12 +425,7 @@ private V readValue(ByteBuffer buffer) {
424425

425426
private V readRequiredValue(ByteBuffer buffer) {
426427

427-
V v = readValue(buffer);
428-
429-
if (v == null) {
430-
throw new InvalidDataAccessApiUsageException("Deserialized set value is null");
431-
}
432-
433-
return v;
428+
return RedisAssertions.requireNonNull(readValue(buffer),
429+
() -> new InvalidDataAccessApiUsageException("Deserialized set value is null"));
434430
}
435431
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.redis.core.types.Expiration;
3636
import org.springframework.data.redis.serializer.RedisSerializationContext;
3737
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
38+
import org.springframework.data.redis.util.RedisAssertions;
3839
import org.springframework.lang.Nullable;
3940
import org.springframework.util.Assert;
4041

@@ -336,13 +337,8 @@ private V readValue(ByteBuffer buffer) {
336337

337338
private V readRequiredValue(ByteBuffer buffer) {
338339

339-
V v = readValue(buffer);
340-
341-
if (v == null) {
342-
throw new InvalidDataAccessApiUsageException("Deserialized value is null");
343-
}
344-
345-
return v;
340+
return RedisAssertions.requireNonNull(readValue(buffer),
341+
() -> new InvalidDataAccessApiUsageException("Deserialized value is null"));
346342
}
347343

348344
private SerializationPair<String> stringSerializationPair() {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
3939
import org.springframework.data.redis.serializer.RedisSerializationContext;
4040
import org.springframework.data.redis.util.ByteUtils;
41+
import org.springframework.data.redis.util.RedisAssertions;
4142
import org.springframework.lang.Nullable;
4243
import org.springframework.util.Assert;
4344

@@ -744,13 +745,8 @@ private V readValue(ByteBuffer buffer) {
744745

745746
private V readRequiredValue(ByteBuffer buffer) {
746747

747-
V v = readValue(buffer);
748-
749-
if (v == null) {
750-
throw new InvalidDataAccessApiUsageException("Deserialized sorted set value is null");
751-
}
752-
753-
return v;
748+
return RedisAssertions.requireNonNull(readValue(buffer),
749+
() -> new InvalidDataAccessApiUsageException("Deserialized sorted set value is null"));
754750
}
755751

756752
private TypedTuple<V> readTypedTuple(Tuple raw) {

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.data.redis.serializer.RedisElementReader;
4444
import org.springframework.data.redis.serializer.RedisElementWriter;
4545
import org.springframework.data.redis.serializer.RedisSerializationContext;
46+
import org.springframework.data.redis.util.RedisAssertions;
4647
import org.springframework.lang.Nullable;
4748
import org.springframework.util.Assert;
4849
import org.springframework.util.ClassUtils;
@@ -61,12 +62,12 @@
6162
* {@link org.springframework.data.redis.serializer.RedisElementReader#read(ByteBuffer)} returns {@code null} for a
6263
* particular element as Reactive Streams prohibit the usage of {@code null} values.
6364
*
65+
* @param <K> the Redis key type against which the template works (usually a String)
66+
* @param <V> the Redis value type against which the template works
6467
* @author Mark Paluch
6568
* @author Christoph Strobl
6669
* @author Petromir Dzhunev
6770
* @since 2.0
68-
* @param <K> the Redis key type against which the template works (usually a String)
69-
* @param <V> the Redis value type against which the template works
7071
*/
7172
public class ReactiveRedisTemplate<K, V> implements ReactiveRedisOperations<K, V> {
7273

@@ -678,12 +679,7 @@ private K readKey(ByteBuffer buffer) {
678679

679680
private K readRequiredKey(ByteBuffer buffer) {
680681

681-
K key = readKey(buffer);
682-
683-
if (key == null) {
684-
throw new InvalidDataAccessApiUsageException("Deserialized key is null");
685-
}
686-
687-
return key;
682+
return RedisAssertions.requireNonNull(readKey(buffer),
683+
() -> new InvalidDataAccessApiUsageException("Deserialized key is null"));
688684
}
689685
}

src/main/java/org/springframework/data/redis/core/script/DefaultReactiveScriptExecutor.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.data.redis.serializer.RedisElementWriter;
3333
import org.springframework.data.redis.serializer.RedisSerializationContext;
3434
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
35+
import org.springframework.data.redis.util.RedisAssertions;
3536
import org.springframework.util.Assert;
3637

3738
/**
@@ -105,45 +106,33 @@ protected <T> Flux<T> eval(ReactiveRedisConnection connection, RedisScript<T> sc
105106

106107
Flux<T> result = connection.scriptingCommands().evalSha(script.getSha1(), returnType, numKeys, keysAndArgs);
107108

108-
result = result.onErrorResume(e -> {
109+
result = result.onErrorResume(cause -> {
109110

110-
if (ScriptUtils.exceptionContainsNoScriptError(e)) {
111+
if (ScriptUtils.exceptionContainsNoScriptError(cause)) {
111112
return connection.scriptingCommands().eval(scriptBytes(script), returnType, numKeys, keysAndArgs);
112113
}
113114

114-
return Flux
115-
.error(e instanceof RuntimeException ? (RuntimeException) e : new RedisSystemException(e.getMessage(), e));
115+
return Flux.error(cause instanceof RuntimeException ? cause
116+
: new RedisSystemException(cause.getMessage(), cause));
116117
});
117118

118119
return script.returnsRawValue() ? result : deserializeResult(resultReader, result);
119120
}
120121

121-
@SuppressWarnings("Convert2MethodRef")
122+
@SuppressWarnings({ "Convert2MethodRef", "rawtypes", "unchecked" })
122123
protected ByteBuffer[] keysAndArgs(RedisElementWriter argsWriter, List<K> keys, List<?> args) {
123124

124125
return Stream.concat(keys.stream().map(t -> keySerializer().getWriter().write(t)),
125126
args.stream().map(t -> argsWriter.write(t))).toArray(size -> new ByteBuffer[size]);
126127
}
127128

128-
/**
129-
* @param script
130-
* @return
131-
*/
132129
protected ByteBuffer scriptBytes(RedisScript<?> script) {
133130
return serializationContext.getStringSerializationPair().getWriter().write(script.getScriptAsString());
134131
}
135132

136133
protected <T> Flux<T> deserializeResult(RedisElementReader<T> reader, Flux<T> result) {
137-
return result.map(it -> {
138-
139-
T value = ScriptUtils.deserializeResult(reader, it);
140-
141-
if (value == null) {
142-
throw new InvalidDataAccessApiUsageException("Deserialized script result is null");
143-
}
144-
145-
return value;
146-
});
134+
return result.map(it -> RedisAssertions.requireNonNull(ScriptUtils.deserializeResult(reader, it),
135+
() -> new InvalidDataAccessApiUsageException("Deserialized script result is null")));
147136
}
148137

149138
protected SerializationPair<K> keySerializer() {
@@ -169,6 +158,6 @@ private <T> Flux<T> execute(ReactiveRedisCallback<T> action) {
169158
}
170159

171160
public ReactiveRedisConnectionFactory getConnectionFactory() {
172-
return connectionFactory;
161+
return this.connectionFactory;
173162
}
174163
}

src/main/java/org/springframework/data/redis/util/RedisAssertions.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ public static <T> T requireNonNull(@Nullable T target, Supplier<String> message)
5757
return target;
5858
}
5959

60+
/**
61+
* Asserts the given {@link Object} is not {@literal null} throwing the given {@link RuntimeException}
62+
* if {@link Object} is {@literal null}.
63+
*
64+
* @param <T> {@link Class type} of {@link Object} being asserted.
65+
* @param target {@link Object} to evaluate.
66+
* @param cause {@link Supplier} of a {@link RuntimeException} to throw
67+
* if the given {@link Object} is {@literal null}.
68+
* @return the given {@link Object}.
69+
*/
70+
public static <T> T requireNonNull(@Nullable T target, RuntimeExceptionSupplier cause) {
71+
72+
if (target == null) {
73+
throw cause.get();
74+
}
75+
76+
return target;
77+
}
78+
6079
/**
6180
* Asserts the given {@link Object} is not {@literal null}.
6281
*
@@ -85,4 +104,7 @@ public static <T> T requireState(@Nullable T target, Supplier<String> message) {
85104
Assert.state(target != null, message);
86105
return target;
87106
}
107+
108+
public interface RuntimeExceptionSupplier extends Supplier<RuntimeException> { }
109+
88110
}

src/test/java/org/springframework/data/redis/core/ReactiveStringRedisTemplateIntegrationTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@ExtendWith(LettuceConnectionFactoryExtension.class)
3737
public class ReactiveStringRedisTemplateIntegrationTests {
3838

39-
private ReactiveRedisConnectionFactory connectionFactory;
39+
private final ReactiveRedisConnectionFactory connectionFactory;
4040

4141
private ReactiveStringRedisTemplate template;
4242

@@ -66,17 +66,16 @@ void keysFailsOnNullElements() {
6666
template.opsForValue().set("a", "1").as(StepVerifier::create).expectNext(true).verifyComplete();
6767
template.opsForValue().set("b", "1").as(StepVerifier::create).expectNext(true).verifyComplete();
6868

69-
RedisElementWriter<String> writer = RedisElementWriter.from(StringRedisSerializer.UTF_8);
7069
RedisElementReader<String> reader = RedisElementReader.from(StringRedisSerializer.UTF_8);
70+
RedisElementWriter<String> writer = RedisElementWriter.from(StringRedisSerializer.UTF_8);
71+
7172
RedisSerializationContext<String, String> nullReadingContext = RedisSerializationContext
72-
.<String, String> newSerializationContext(StringRedisSerializer.UTF_8).key(buffer -> {
73+
.<String, String>newSerializationContext(StringRedisSerializer.UTF_8).key(buffer -> {
7374

7475
String read = reader.read(buffer);
75-
if ("a".equals(read)) {
76-
return null;
77-
}
7876

79-
return read;
77+
return "a".equals(read) ? null : read;
78+
8079
}, writer).build();
8180

8281
ReactiveRedisTemplate<String, String> customTemplate = new ReactiveRedisTemplate<>(template.getConnectionFactory(),

0 commit comments

Comments
 (0)