Skip to content

Commit 1507402

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 fcefc53 commit 1507402

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
/**
@@ -74,8 +74,9 @@ public Flux<MultiValueResponse<List<ByteBuffer>, ByteBuffer>> mGet(Publisher<Lis
7474

7575
Assert.notNull(keys, "Keys must not be null!");
7676

77-
return cmd.mget(keys.toArray(new ByteBuffer[0])).map((value) -> value.getValueOrElse(EMPTY_BYTE_BUFFER))
78-
.collectList().map((values) -> new MultiValueResponse<>(keys, values));
77+
return cmd.mget(keys.toArray(new ByteBuffer[0])).collectList().map((value) -> {
78+
return value.stream().map(keyValue -> keyValue.getValueOrElse(null)).collect(Collectors.toList());
79+
}).map((values) -> new MultiValueResponse<>(keys, values));
7980
}));
8081
}
8182

@@ -363,9 +364,9 @@ public Flux<NumericResponse<BitCountCommand, Long>> bitCount(Publisher<BitCountC
363364

364365
Range<Long> range = command.getRange();
365366

366-
return (!Range.unbounded().equals(range) ? cmd.bitcount(command.getKey(),
367-
LettuceConverters.getLowerBoundIndex(range), //
368-
LettuceConverters.getUpperBoundIndex(range)) //
367+
return (!Range.unbounded().equals(range)
368+
? cmd.bitcount(command.getKey(), LettuceConverters.getLowerBoundIndex(range), //
369+
LettuceConverters.getUpperBoundIndex(range)) //
369370
: cmd.bitcount(command.getKey())).map(responseValue -> new NumericResponse<>(command, responseValue));
370371
}));
371372
}

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)