2727import java .util .function .Function ;
2828
2929import org .reactivestreams .Publisher ;
30+ import org .springframework .dao .InvalidDataAccessApiUsageException ;
3031import org .springframework .data .redis .connection .ReactiveListCommands ;
3132import org .springframework .data .redis .connection .ReactiveListCommands .Direction ;
3233import org .springframework .data .redis .connection .ReactiveListCommands .LPosCommand ;
3334import org .springframework .data .redis .connection .RedisListCommands .Position ;
3435import org .springframework .data .redis .serializer .RedisSerializationContext ;
36+ import org .springframework .lang .Nullable ;
3537import org .springframework .util .Assert ;
3638
3739/**
@@ -59,7 +61,7 @@ public Flux<V> range(K key, long start, long end) {
5961
6062 Assert .notNull (key , "Key must not be null" );
6163
62- return createFlux (listCommands -> listCommands .lRange (rawKey (key ), start , end ).map (this ::readValue ));
64+ return createFlux (connection -> connection .lRange (rawKey (key ), start , end ).map (this ::readRequiredValue ));
6365 }
6466
6567 @ Override
@@ -172,8 +174,8 @@ public Mono<V> move(K sourceKey, Direction from, K destinationKey, Direction to)
172174 Assert .notNull (from , "From direction must not be null" );
173175 Assert .notNull (to , "To direction must not be null" );
174176
175- return createMono (listCommands ->
176- listCommands . lMove ( rawKey ( sourceKey ), rawKey ( destinationKey ), from , to ). map (this ::readValue ));
177+ return createMono (connection -> connection . lMove ( rawKey ( sourceKey ), rawKey ( destinationKey ), from , to )
178+ . map (this ::readRequiredValue ));
177179 }
178180
179181 @ Override
@@ -185,8 +187,8 @@ public Mono<V> move(K sourceKey, Direction from, K destinationKey, Direction to,
185187 Assert .notNull (to , "To direction must not be null" );
186188 Assert .notNull (timeout , "Timeout must not be null" );
187189
188- return createMono (listCommands ->
189- listCommands . bLMove ( rawKey ( sourceKey ), rawKey ( destinationKey ), from , to , timeout ). map (this ::readValue ));
190+ return createMono (connection -> connection . bLMove ( rawKey ( sourceKey ), rawKey ( destinationKey ), from , to , timeout )
191+ . map (this ::readRequiredValue ));
190192 }
191193
192194 @ Override
@@ -211,7 +213,7 @@ public Mono<V> index(K key, long index) {
211213
212214 Assert .notNull (key , "Key must not be null" );
213215
214- return createMono (listCommands -> listCommands .lIndex (rawKey (key ), index ).map (this ::readValue ));
216+ return createMono (connection -> connection .lIndex (rawKey (key ), index ).map (this ::readRequiredValue ));
215217 }
216218
217219 @ Override
@@ -236,7 +238,7 @@ public Mono<V> leftPop(K key) {
236238
237239 Assert .notNull (key , "Key must not be null" );
238240
239- return createMono (listCommands -> listCommands .lPop (rawKey (key )).map (this ::readValue ));
241+ return createMono (connection -> connection .lPop (rawKey (key )).map (this ::readRequiredValue ));
240242
241243 }
242244
@@ -245,7 +247,7 @@ public Flux<V> leftPop(K key, long count) {
245247
246248 Assert .notNull (key , "Key must not be null" );
247249
248- return createFlux (listCommands -> listCommands .lPop (rawKey (key ), count ).map (this ::readValue ));
250+ return createFlux (listCommands -> listCommands .lPop (rawKey (key ), count ).map (this ::readRequiredValue ));
249251 }
250252
251253 @ Override
@@ -255,25 +257,24 @@ public Mono<V> leftPop(K key, Duration timeout) {
255257 Assert .notNull (timeout , "Duration must not be null" );
256258 Assert .isTrue (isZeroOrGreaterOneSecond (timeout ), "Duration must be either zero or greater or equal to 1 second" );
257259
258- return createMono (listCommands ->
259- listCommands .blPop (Collections .singletonList (rawKey (key )), timeout )
260- .map (popResult -> readValue (popResult .getValue ())));
260+ return createMono (connection -> connection .blPop (Collections .singletonList (rawKey (key )), timeout )
261+ .mapNotNull (popResult -> readValue (popResult .getValue ())));
261262 }
262263
263264 @ Override
264265 public Mono <V > rightPop (K key ) {
265266
266267 Assert .notNull (key , "Key must not be null" );
267268
268- return createMono (listCommands -> listCommands .rPop (rawKey (key )).map (this ::readValue ));
269+ return createMono (listCommands -> listCommands .rPop (rawKey (key )).map (this ::readRequiredValue ));
269270 }
270271
271272 @ Override
272273 public Flux <V > rightPop (K key , long count ) {
273274
274275 Assert .notNull (key , "Key must not be null" );
275276
276- return createFlux (listCommands -> listCommands .rPop (rawKey (key ), count ).map (this ::readValue ));
277+ return createFlux (listCommands -> listCommands .rPop (rawKey (key ), count ).map (this ::readRequiredValue ));
277278 }
278279
279280 @ Override
@@ -283,9 +284,8 @@ public Mono<V> rightPop(K key, Duration timeout) {
283284 Assert .notNull (timeout , "Duration must not be null" );
284285 Assert .isTrue (isZeroOrGreaterOneSecond (timeout ), "Duration must be either zero or greater or equal to 1 second" );
285286
286- return createMono (listCommands ->
287- listCommands .brPop (Collections .singletonList (rawKey (key )), timeout )
288- .map (popResult -> readValue (popResult .getValue ())));
287+ return createMono (connection -> connection .brPop (Collections .singletonList (rawKey (key )), timeout )
288+ .mapNotNull (popResult -> readValue (popResult .getValue ())));
289289 }
290290
291291 @ Override
@@ -294,8 +294,8 @@ public Mono<V> rightPopAndLeftPush(K sourceKey, K destinationKey) {
294294 Assert .notNull (sourceKey , "Source key must not be null" );
295295 Assert .notNull (destinationKey , "Destination key must not be null" );
296296
297- return createMono (listCommands ->
298- listCommands . rPopLPush ( rawKey ( sourceKey ), rawKey ( destinationKey )). map (this ::readValue ));
297+ return createMono (connection -> connection . rPopLPush ( rawKey ( sourceKey ), rawKey ( destinationKey ))
298+ . map (this ::readRequiredValue ));
299299 }
300300
301301 @ Override
@@ -306,8 +306,8 @@ public Mono<V> rightPopAndLeftPush(K sourceKey, K destinationKey, Duration timeo
306306 Assert .notNull (timeout , "Duration must not be null" );
307307 Assert .isTrue (isZeroOrGreaterOneSecond (timeout ), "Duration must be either zero or greater or equal to 1 second" );
308308
309- return createMono (listCommands ->
310- listCommands . bRPopLPush ( rawKey ( sourceKey ), rawKey ( destinationKey ), timeout ). map (this ::readValue ));
309+ return createMono (connection -> connection . bRPopLPush ( rawKey ( sourceKey ), rawKey ( destinationKey ), timeout )
310+ . map (this ::readRequiredValue ));
311311 }
312312
313313 @ Override
@@ -344,7 +344,19 @@ private ByteBuffer rawValue(V value) {
344344 return serializationContext .getValueSerializationPair ().write (value );
345345 }
346346
347+ @ Nullable
347348 private V readValue (ByteBuffer buffer ) {
348349 return serializationContext .getValueSerializationPair ().read (buffer );
349350 }
351+
352+ private V readRequiredValue (ByteBuffer buffer ) {
353+
354+ V v = readValue (buffer );
355+
356+ if (v == null ) {
357+ throw new InvalidDataAccessApiUsageException ("Deserialized list value is null" );
358+ }
359+
360+ return v ;
361+ }
350362}
0 commit comments