Polishing.

Remove duplicate conversion code.

Original Pull Request: #2292
This commit is contained in:
Mark Paluch
2022-03-30 09:16:15 +02:00
committed by Christoph Strobl
parent 4ab1789953
commit b17bc4a7fd
24 changed files with 128 additions and 137 deletions

View File

@@ -25,7 +25,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Range;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
@@ -49,6 +48,7 @@ import org.springframework.data.redis.connection.stream.StreamInfo.XInfoStream;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;

View File

@@ -23,7 +23,6 @@ import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.domain.Range;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
@@ -41,6 +40,7 @@ import org.springframework.data.redis.connection.stream.StreamInfo.XInfoGroups;
import org.springframework.data.redis.connection.stream.StreamInfo.XInfoStream;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;

View File

@@ -33,7 +33,7 @@ import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;

View File

@@ -20,6 +20,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;
@@ -41,14 +42,8 @@ import org.springframework.util.ObjectUtils;
public interface RedisZSetCommands {
/**
* Sort aggregation operations.
*/
enum Aggregate {
SUM, MIN, MAX;
}
/**
* {@link Range} defines {@literal min} and {@literal max} values to retrieve from a {@literal ZSET}.
* {@link org.springframework.data.domain.Range} defines {@literal min} and {@literal max} values to retrieve from a
* {@literal ZSET}.
*
* @author Christoph Strobl
* @since 1.6
@@ -61,14 +56,15 @@ public interface RedisZSetCommands {
@Nullable Boundary max;
/**
* @return new {@link Range}
* @return new {@link org.springframework.data.domain.Range}
*/
public static Range range() {
return new Range();
}
/**
* @return new {@link Range} with {@literal min} and {@literal max} set to {@link Boundary#infinite()}.
* @return new {@link org.springframework.data.domain.Range} with {@literal min} and {@literal max} set to
* {@link Boundary#infinite()}.
*/
public static Range unbounded() {
@@ -963,7 +959,7 @@ public interface RedisZSetCommands {
Long zRemRange(byte[] key, long start, long end);
/**
* Remove all elements between the lexicographical {@link Range}.
* Remove all elements between the lexicographical {@link org.springframework.data.domain.Range}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
@@ -1299,7 +1295,8 @@ public interface RedisZSetCommands {
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in lexicographical ordering.
* Get all the elements in {@link org.springframework.data.domain.Range} from the sorted set at {@literal key} in
* lexicographical ordering.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
@@ -1313,8 +1310,8 @@ public interface RedisZSetCommands {
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in lexicographical ordering. Result is
* limited via {@link Limit}.
* Get all the elements in {@link org.springframework.data.domain.Range} from the sorted set at {@literal key} in
* lexicographical ordering. Result is limited via {@link Limit}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
@@ -1341,7 +1338,8 @@ public interface RedisZSetCommands {
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in reversed lexicographical ordering.
* Get all the elements in {@link org.springframework.data.domain.Range} from the sorted set at {@literal key} in
* reversed lexicographical ordering.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.
@@ -1355,8 +1353,8 @@ public interface RedisZSetCommands {
}
/**
* Get all the elements in {@link Range} from the sorted set at {@literal key} in reversed lexicographical ordering.
* Result is limited via {@link Limit}.
* Get all the elements in {@link org.springframework.data.domain.Range} from the sorted set at {@literal key} in
* reversed lexicographical ordering. Result is limited via {@link Limit}.
*
* @param key must not be {@literal null}.
* @param range must not be {@literal null}.

View File

@@ -41,6 +41,7 @@ import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.connection.stream.StringRecord;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;

View File

@@ -31,6 +31,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.ClusterSlotHashUtil;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.convert.SetConverter;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;

View File

@@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;

View File

@@ -48,7 +48,6 @@ import org.springframework.data.redis.connection.RedisListCommands.Direction;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.connection.RedisNode.NodeType;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
import org.springframework.data.redis.connection.RedisZSetCommands.Range.Boundary;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter;
@@ -257,19 +256,6 @@ public abstract class LettuceConverters extends Converters {
return StringToRedisClientInfoConverter.INSTANCE.convert(clientList.split("\\r?\\n"));
}
private static String boundaryToString(Boundary boundary, String inclPrefix, String exclPrefix) {
String prefix = boundary.isIncluding() ? inclPrefix : exclPrefix;
String value = null;
if (boundary.getValue() instanceof byte[]) {
value = toString((byte[]) boundary.getValue());
} else {
value = boundary.getValue().toString();
}
return prefix + value;
}
/**
* Convert a {@link Limit} to a Lettuce {@link io.lettuce.core.Limit}.
*
@@ -290,81 +276,44 @@ public abstract class LettuceConverters extends Converters {
* @since 2.0
*/
public static <T> Range<T> toRange(org.springframework.data.domain.Range<T> range) {
return Range.from(lowerBoundaryOf(range, false), upperBoundaryOf(range, false));
return toRange(range, false);
}
/**
* Convert a {@link org.springframework.data.redis.connection.RedisZSetCommands.Range} to a lettuce {@link Range}.
* Convert a {@link org.springframework.data.domain.Range} to a lettuce {@link Range}.
*
* @param range
* @param convertNumberToBytes
* @return
* @since 2.2
*/
public static <T> Range<T> toRange(org.springframework.data.domain.Range<T> range,
boolean convertNumberToBytes) {
return Range.from(lowerBoundaryOf(range, convertNumberToBytes), upperBoundaryOf(range, convertNumberToBytes));
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Range<T> toRange(org.springframework.data.domain.Range<T> range, boolean convertNumberToBytes) {
Range.Boundary upper = RangeConverter.convertBound(range.getUpperBound(), convertNumberToBytes, null,
it -> it.getBytes(StandardCharsets.UTF_8));
Range.Boundary lower = RangeConverter.convertBound(range.getLowerBound(), convertNumberToBytes, null,
it -> it.getBytes(StandardCharsets.UTF_8));
return Range.from(lower, upper);
}
/**
* Convert a {@link org.springframework.data.redis.connection.RedisZSetCommands.Range} to a lettuce {@link Range} and
* reverse boundaries.
* Convert a {@link org.springframework.data.domain.Range} to a lettuce {@link Range} and reverse boundaries.
*
* @param range
* @return
* @since 2.0
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Range<T> toRevRange(org.springframework.data.domain.Range<T> range) {
return Range.from(upperBoundaryOf(range, false), lowerBoundaryOf(range, false));
}
@SuppressWarnings("unchecked")
private static <T> Range.Boundary<T> lowerBoundaryOf(
org.springframework.data.domain.Range<T> range, boolean convertNumberToBytes) {
return (Range.Boundary<T>) rangeToBoundaryArgumentConverter(false, convertNumberToBytes).convert(range);
}
Range.Boundary upper = RangeConverter.convertBound(range.getUpperBound(), false, null,
it -> it.getBytes(StandardCharsets.UTF_8));
Range.Boundary lower = RangeConverter.convertBound(range.getLowerBound(), false, null,
it -> it.getBytes(StandardCharsets.UTF_8));
@SuppressWarnings("unchecked")
private static <T> Range.Boundary<T> upperBoundaryOf(
org.springframework.data.domain.Range<T> range, boolean convertNumberToBytes) {
return (Range.Boundary<T>) rangeToBoundaryArgumentConverter(true, convertNumberToBytes).convert(range);
}
private static Converter<org.springframework.data.domain.Range<?>, Range.Boundary<?>> rangeToBoundaryArgumentConverter(
boolean upper, boolean convertNumberToBytes) {
return (source) -> {
org.springframework.data.domain.Range.Bound<?> sourceBoundary = upper ? source.getUpperBound()
: source.getLowerBound();
if (sourceBoundary == null || !sourceBoundary.isBounded()) {
return Range.Boundary.unbounded();
}
boolean inclusive = sourceBoundary.isInclusive();
Object value = sourceBoundary.getValue().get();
if (value instanceof Number) {
if (convertNumberToBytes) {
value = value.toString();
} else {
return inclusive ? Range.Boundary.including((Number) value) : Range.Boundary.excluding((Number) value);
}
}
if (value instanceof String) {
if (!StringUtils.hasText((String) value) || ObjectUtils.nullSafeEquals(value, "+")
|| ObjectUtils.nullSafeEquals(value, "-")) {
return Range.Boundary.unbounded();
}
return inclusive ? Range.Boundary.including(value.toString().getBytes(StandardCharsets.UTF_8))
: Range.Boundary.excluding(value.toString().getBytes(StandardCharsets.UTF_8));
}
return inclusive ? Range.Boundary.including((byte[]) value) : Range.Boundary.excluding((byte[]) value);
};
return Range.from(upper, lower);
}
/**

View File

@@ -37,7 +37,7 @@ import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyScan
import org.springframework.data.redis.connection.ReactiveRedisConnection.MultiValueResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
import org.springframework.data.redis.connection.ReactiveZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.core.TimeoutUtils;

View File

@@ -31,6 +31,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.ZAddArgs.Flag;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.Cursor;

View File

@@ -19,8 +19,6 @@ import io.lettuce.core.Range;
import io.lettuce.core.Range.Boundary;
import io.lettuce.core.codec.StringCodec;
import java.nio.ByteBuffer;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.springframework.data.domain.Range.Bound;
@@ -79,45 +77,50 @@ class RangeConverter {
@SuppressWarnings("unchecked")
private static <T> Boundary<T> lowerBoundArgOf(org.springframework.data.domain.Range<?> range,
@Nullable T lowerDefault, Function<String, ? extends Object> stringEncoder) {
return (Boundary<T>) rangeToBoundArgumentConverter(false, stringEncoder).apply(range, lowerDefault);
return (Boundary<T>) convertBound(range.getLowerBound(), false, lowerDefault, stringEncoder);
}
@SuppressWarnings("unchecked")
private static <T> Boundary<T> upperBoundArgOf(org.springframework.data.domain.Range<?> range,
@Nullable T upperDefault, Function<String, ? extends Object> stringEncoder) {
return (Boundary<T>) rangeToBoundArgumentConverter(true, stringEncoder).apply(range, upperDefault);
return (Boundary<T>) convertBound(range.getUpperBound(), false, upperDefault, stringEncoder);
}
private static BiFunction<org.springframework.data.domain.Range, Object, Boundary<?>> rangeToBoundArgumentConverter(
boolean upper, Function<String, ? extends Object> stringEncoder) {
static Boundary<?> convertBound(org.springframework.data.domain.Range.Bound source, boolean convertNumberToBytes,
Object defaultValue, Function<String, ? extends Object> stringEncoder) {
return (source, defaultValue) -> {
if (!source.isBounded()) {
return Boundary.unbounded();
}
Boolean inclusive = upper ? source.getUpperBound().isInclusive() : source.getLowerBound().isInclusive();
Object value = upper ? source.getUpperBound().getValue().orElse(defaultValue)
: source.getLowerBound().getValue().orElse(defaultValue);
Boolean inclusive = source.isInclusive();
Object value = source.getValue().orElse(defaultValue);
if (value instanceof Number) {
if (value instanceof Number) {
if (convertNumberToBytes) {
value = value.toString();
} else {
return inclusive ? Boundary.including((Number) value) : Boundary.excluding((Number) value);
}
}
if (value instanceof String) {
if (value instanceof String) {
if (!StringUtils.hasText((String) value) || ObjectUtils.nullSafeEquals(value, "+")
|| ObjectUtils.nullSafeEquals(value, "-")) {
return Boundary.unbounded();
}
Object encoded = stringEncoder.apply((String) value);
return inclusive ? Boundary.including(encoded) : Boundary.excluding(encoded);
}
if (value == null) {
if (!StringUtils.hasText((String) value) || ObjectUtils.nullSafeEquals(value, "+")
|| ObjectUtils.nullSafeEquals(value, "-")) {
return Boundary.unbounded();
}
return inclusive ? Boundary.including((ByteBuffer) value) : Boundary.excluding((ByteBuffer) value);
};
Object encoded = stringEncoder.apply((String) value);
return inclusive ? Boundary.including(encoded) : Boundary.excluding(encoded);
}
if (value == null) {
return Boundary.unbounded();
}
return inclusive ? Boundary.including(value) : Boundary.excluding(value);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.zset;
/**
* Sort aggregation operations.
*
* @author Mark Paluch
* @since 3.0
*/
public enum Aggregate {
SUM, MIN, MAX;
}

View File

@@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

View File

@@ -31,7 +31,7 @@ import org.reactivestreams.Publisher;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.Limit;
import org.springframework.data.redis.connection.ReactiveZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.DefaultTuple;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;

View File

@@ -24,8 +24,8 @@ import java.util.concurrent.TimeUnit;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.RedisZSetCommands.ZAddArgs;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.lang.Nullable;

View File

@@ -25,7 +25,7 @@ import java.util.List;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

View File

@@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate;
import org.springframework.data.redis.connection.zset.Aggregate;
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.connection.zset.Weights;
import org.springframework.lang.Nullable;