Skip to content

Commit 3576bbf

Browse files
committed
Return null elements for absent keys using reactive MGET.
We now use correctly null to indicate absent keys when using reactive MGET. Previously, we used an empty byte buffer that could be incorrectly translated to an empty string when using the string codec. An empty byte buffer can also be returned if the value length is zero leading to a state that doesn't allow distinguishing between absence and empty value. Closes #2402
1 parent 359d917 commit 3576bbf

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
import io.lettuce.core.BitFieldArgs;
1919
import io.lettuce.core.GetExArgs;
20+
import io.lettuce.core.KeyValue;
2021
import io.lettuce.core.SetArgs;
2122
import reactor.core.publisher.Flux;
2223
import reactor.core.publisher.Mono;
2324

2425
import java.nio.ByteBuffer;
26+
import java.util.ArrayList;
2527
import java.util.List;
2628
import java.util.stream.Collectors;
2729

@@ -47,8 +49,6 @@
4749
*/
4850
class LettuceReactiveStringCommands implements ReactiveStringCommands {
4951

50-
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]);
51-
5252
private final LettuceReactiveRedisConnection connection;
5353

5454
/**
@@ -70,8 +70,9 @@ public Flux<MultiValueResponse<List<ByteBuffer>, ByteBuffer>> mGet(Publisher<Lis
7070

7171
Assert.notNull(keys, "Keys must not be null");
7272

73-
return cmd.mget(keys.toArray(new ByteBuffer[0])).map((value) -> value.getValueOrElse(EMPTY_BYTE_BUFFER))
74-
.collectList().map((values) -> new MultiValueResponse<>(keys, values));
73+
return cmd.mget(keys.toArray(new ByteBuffer[0])).collectList().map((value) -> {
74+
return value.stream().map(keyValue -> keyValue.getValueOrElse(null)).collect(Collectors.toList());
75+
}).map((values) -> new MultiValueResponse<>(keys, values));
7576
}));
7677
}
7778

@@ -295,9 +296,9 @@ public Flux<NumericResponse<BitCountCommand, Long>> bitCount(Publisher<BitCountC
295296

296297
Range<Long> range = command.getRange();
297298

298-
return (!Range.unbounded().equals(range) ? cmd.bitcount(command.getKey(),
299-
LettuceConverters.getLowerBoundIndex(range), //
300-
LettuceConverters.getUpperBoundIndex(range)) //
299+
return (!Range.unbounded().equals(range)
300+
? cmd.bitcount(command.getKey(), LettuceConverters.getLowerBoundIndex(range), //
301+
LettuceConverters.getUpperBoundIndex(range)) //
301302
: cmd.bitcount(command.getKey())).map(responseValue -> new NumericResponse<>(command, responseValue));
302303
}));
303304
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveStringCommandsIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void mGetShouldRetrieveNullValueCorrectly() {
202202
Mono<List<ByteBuffer>> result = connection.stringCommands()
203203
.mGet(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER, KEY_3_BBUFFER));
204204

205-
assertThat(result.block()).containsExactly(VALUE_1_BBUFFER, ByteBuffer.allocate(0), VALUE_3_BBUFFER);
205+
assertThat(result.block()).containsExactly(VALUE_1_BBUFFER, null, VALUE_3_BBUFFER);
206206
}
207207

208208
@ParameterizedRedisTest // DATAREDIS-525

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,6 @@ void multiGet() {
306306
V value2 = valueFactory.instance();
307307
V absentValue = null;
308308

309-
if (serializer instanceof StringRedisSerializer) {
310-
absentValue = (V) "";
311-
}
312-
if (value1 instanceof ByteBuffer) {
313-
absentValue = (V) ByteBuffer.wrap(new byte[0]);
314-
}
315-
316309
Map<K, V> map = new LinkedHashMap<>();
317310
map.put(key1, value1);
318311
map.put(key2, value2);

0 commit comments

Comments
 (0)