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
This commit is contained in:
Mark Paluch
2022-09-13 15:11:41 +02:00
parent 359d917a9e
commit 3576bbfa8c
3 changed files with 9 additions and 15 deletions

View File

@@ -17,11 +17,13 @@ package org.springframework.data.redis.connection.lettuce;
import io.lettuce.core.BitFieldArgs;
import io.lettuce.core.GetExArgs;
import io.lettuce.core.KeyValue;
import io.lettuce.core.SetArgs;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -47,8 +49,6 @@ import org.springframework.util.Assert;
*/
class LettuceReactiveStringCommands implements ReactiveStringCommands {
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]);
private final LettuceReactiveRedisConnection connection;
/**
@@ -70,8 +70,9 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands {
Assert.notNull(keys, "Keys must not be null");
return cmd.mget(keys.toArray(new ByteBuffer[0])).map((value) -> value.getValueOrElse(EMPTY_BYTE_BUFFER))
.collectList().map((values) -> new MultiValueResponse<>(keys, values));
return cmd.mget(keys.toArray(new ByteBuffer[0])).collectList().map((value) -> {
return value.stream().map(keyValue -> keyValue.getValueOrElse(null)).collect(Collectors.toList());
}).map((values) -> new MultiValueResponse<>(keys, values));
}));
}
@@ -295,9 +296,9 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands {
Range<Long> range = command.getRange();
return (!Range.unbounded().equals(range) ? cmd.bitcount(command.getKey(),
LettuceConverters.getLowerBoundIndex(range), //
LettuceConverters.getUpperBoundIndex(range)) //
return (!Range.unbounded().equals(range)
? cmd.bitcount(command.getKey(), LettuceConverters.getLowerBoundIndex(range), //
LettuceConverters.getUpperBoundIndex(range)) //
: cmd.bitcount(command.getKey())).map(responseValue -> new NumericResponse<>(command, responseValue));
}));
}