DATAREDIS-697 - Polishing.

Extract Range value access to methods. Remove unused imports. Add type hint for BITPOS via execute(…).

Original pull request: #335.
This commit is contained in:
Mark Paluch
2018-04-30 16:30:49 +02:00
parent e9a3a5b6d9
commit abb7d9f684
4 changed files with 40 additions and 24 deletions

View File

@@ -1019,6 +1019,7 @@ public class LettuceConnection extends AbstractRedisConnection {
// INTEGER
COMMAND_OUTPUT_TYPE_MAPPING.put(BITCOUNT, IntegerOutput.class);
COMMAND_OUTPUT_TYPE_MAPPING.put(BITOP, IntegerOutput.class);
COMMAND_OUTPUT_TYPE_MAPPING.put(BITPOS, IntegerOutput.class);
COMMAND_OUTPUT_TYPE_MAPPING.put(DBSIZE, IntegerOutput.class);
COMMAND_OUTPUT_TYPE_MAPPING.put(DECR, IntegerOutput.class);
COMMAND_OUTPUT_TYPE_MAPPING.put(DECRBY, IntegerOutput.class);

View File

@@ -16,10 +16,6 @@
package org.springframework.data.redis.connection.lettuce;
import io.lettuce.core.SetArgs;
import io.lettuce.core.codec.ByteArrayCodec;
import io.lettuce.core.output.IntegerOutput;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandType;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -36,8 +32,6 @@ import org.springframework.data.redis.connection.ReactiveRedisConnection.MultiVa
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.RangeCommand;
import org.springframework.data.redis.connection.ReactiveStringCommands;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.lettuce.LettuceReactiveRedisConnection.ByteBufferCodec;
import org.springframework.util.Assert;
/**
@@ -383,17 +377,20 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands {
return Flux.from(commands).flatMap(command -> {
Mono<Long> result = cmd.bitpos(command.getKey(), command.getBit());
Mono<Long> result;
Range<Long> range = command.getRange();
if (command.getRange().getLowerBound().isBounded()) {
if (range.getLowerBound().isBounded()) {
result = cmd.bitpos(command.getKey(), command.getBit(), command.getRange().getLowerBound().getValue().get());
result = cmd.bitpos(command.getKey(), command.getBit(), getLowerValue(range));
if (command.getRange().getUpperBound().isBounded()) {
result = cmd.bitpos(command.getKey(), command.getBit(), command.getRange().getLowerBound().getValue().get(),
command.getRange().getUpperBound().getValue().get());
if (range.getUpperBound().isBounded()) {
result = cmd.bitpos(command.getKey(), command.getBit(), getLowerValue(range), getUpperValue(range));
}
} else {
result = cmd.bitpos(command.getKey(), command.getBit());
}
return result.map(respValue -> new NumericResponse<>(command, respValue));
});
});
@@ -417,4 +414,14 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands {
protected LettuceReactiveRedisConnection getConnection() {
return connection;
}
private static <T extends Comparable<T>> T getUpperValue(Range<T> range) {
return range.getUpperBound().getValue()
.orElseThrow(() -> new IllegalArgumentException("Range does not contain upper bound value!"));
}
private static <T extends Comparable<T>> T getLowerValue(Range<T> range) {
return range.getLowerBound().getValue()
.orElseThrow(() -> new IllegalArgumentException("Range does not contain lower bound value!"));
}
}

View File

@@ -684,34 +684,32 @@ class LettuceStringCommands implements RedisStringCommands {
if (isPipelined() || isQueueing()) {
RedisFuture<Long> futureResult;
RedisClusterAsyncCommands<byte[], byte[]> connection = getAsyncConnection();
if (range.getLowerBound().isBounded()) {
if (range.getUpperBound().isBounded()) {
futureResult = getAsyncConnection().bitpos(key, bit, range.getLowerBound().getValue().get(),
range.getUpperBound().getValue().get());
futureResult = connection.bitpos(key, bit, getLowerValue(range), getUpperValue(range));
} else {
futureResult = getAsyncConnection().bitpos(key, bit, range.getLowerBound().getValue().get());
futureResult = connection.bitpos(key, bit, getLowerValue(range));
}
} else {
futureResult = getAsyncConnection().bitpos(key, bit);
futureResult = connection.bitpos(key, bit);
}
if (isPipelined()) {
pipeline(connection.newLettuceResult(futureResult));
pipeline(this.connection.newLettuceResult(futureResult));
}
else if (isQueueing()) {
transaction(connection.newLettuceResult(futureResult));
transaction(this.connection.newLettuceResult(futureResult));
}
return null;
}
if (range.getLowerBound().isBounded()) {
if (range.getUpperBound().isBounded()) {
return getConnection().bitpos(key, bit, range.getLowerBound().getValue().get(),
range.getUpperBound().getValue().get());
return getConnection().bitpos(key, bit, getLowerValue(range), getUpperValue(range));
}
return getConnection().bitpos(key, bit, range.getLowerBound().getValue().get());
return getConnection().bitpos(key, bit, getLowerValue(range));
}
return getConnection().bitpos(key, bit);
@@ -771,4 +769,14 @@ class LettuceStringCommands implements RedisStringCommands {
private DataAccessException convertLettuceAccessException(Exception ex) {
return connection.convertLettuceAccessException(ex);
}
private static <T extends Comparable<T>> T getUpperValue(Range<T> range) {
return range.getUpperBound().getValue()
.orElseThrow(() -> new IllegalArgumentException("Range does not contain upper bound value!"));
}
private static <T extends Comparable<T>> T getLowerValue(Range<T> range) {
return range.getLowerBound().getValue()
.orElseThrow(() -> new IllegalArgumentException("Range does not contain lower bound value!"));
}
}

View File

@@ -41,7 +41,7 @@ public enum RedisCommand {
BGSAVE("r", 0, 0), //
BITCOUNT("r", 1, 3), //
BITOP("rw", 3), //
BITPOS("r", 2), //
BITPOS("r", 2, 4), //
BLPOP("rw", 2), //
BRPOP("rw", 2), //
BRPOPLPUSH("rw", 3), //