org.springframework.data.redis.connection.lettuce.LettuceConverters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.spring-data-redis
Show all versions of org.apache.servicemix.bundles.spring-data-redis
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar files.
/*
* Copyright 2013-2020 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.lettuce;
import io.lettuce.core.*;
import io.lettuce.core.cluster.models.partitions.Partitions;
import io.lettuce.core.cluster.models.partitions.RedisClusterNode.NodeFlag;
import io.lettuce.core.models.stream.PendingMessage;
import io.lettuce.core.models.stream.PendingMessages;
import io.lettuce.core.models.stream.PendingParser;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldGet;
import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldIncrBy;
import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldIncrBy.Overflow;
import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldSet;
import org.springframework.data.redis.connection.BitFieldSubCommands.BitFieldSubCommand;
import org.springframework.data.redis.connection.DefaultTuple;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisClusterNode.Flag;
import org.springframework.data.redis.connection.RedisClusterNode.LinkState;
import org.springframework.data.redis.connection.RedisClusterNode.SlotRange;
import org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisNode.NodeType;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.Range.Boundary;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.convert.ListConverter;
import org.springframework.data.redis.connection.convert.LongToBooleanConverter;
import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter;
import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.NumberUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Lettuce type converters
*
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Thomas Darimont
* @author Mark Paluch
* @author Ninad Divadkar
*/
abstract public class LettuceConverters extends Converters {
private static final Converter DATE_TO_LONG;
private static final Converter, Set> BYTES_LIST_TO_BYTES_SET;
private static final Converter BYTES_TO_STRING;
private static final Converter STRING_TO_BYTES;
private static final Converter, List> BYTES_SET_TO_BYTES_LIST;
private static final Converter, List> BYTES_COLLECTION_TO_BYTES_LIST;
private static final Converter, List> KEY_VALUE_TO_BYTES_LIST;
private static final Converter>, Set> SCORED_VALUES_TO_TUPLE_SET;
private static final Converter>, List> SCORED_VALUES_TO_TUPLE_LIST;
private static final Converter, Tuple> SCORED_VALUE_TO_TUPLE;
private static final Converter EXCEPTION_CONVERTER = new LettuceExceptionConverter();
private static final Converter LONG_TO_BOOLEAN = new LongToBooleanConverter();
private static final Converter, Map> BYTES_LIST_TO_MAP;
private static final Converter, List> BYTES_LIST_TO_TUPLE_LIST_CONVERTER;
private static final Converter> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();
private static final Converter> PARTITIONS_TO_CLUSTER_NODES;
private static Converter CLUSTER_NODE_TO_CLUSTER_NODE_CONVERTER;
private static final Converter, Long> BYTES_LIST_TO_TIME_CONVERTER;
private static final Converter GEO_COORDINATE_TO_POINT_CONVERTER;
private static final ListConverter GEO_COORDINATE_LIST_TO_POINT_LIST_CONVERTER;
private static final Converter, Object> KEY_VALUE_UNWRAPPER;
private static final ListConverter, Object> KEY_VALUE_LIST_UNWRAPPER;
private static final Converter> TRANSACTION_RESULT_UNWRAPPER;
public static final byte[] PLUS_BYTES;
public static final byte[] MINUS_BYTES;
public static final byte[] POSITIVE_INFINITY_BYTES;
public static final byte[] NEGATIVE_INFINITY_BYTES;
private static final long INDEXED_RANGE_START = 0;
private static final long INDEXED_RANGE_END = -1;
static {
DATE_TO_LONG = source -> source != null ? source.getTime() : null;
BYTES_LIST_TO_BYTES_SET = results -> results != null ? new LinkedHashSet<>(results) : null;
BYTES_TO_STRING = source -> {
if (source == null || Arrays.equals(source, new byte[0])) {
return null;
}
return new String(source);
};
STRING_TO_BYTES = source -> {
if (source == null) {
return null;
}
return source.getBytes();
};
BYTES_SET_TO_BYTES_LIST = results -> results != null ? new ArrayList<>(results) : null;
BYTES_COLLECTION_TO_BYTES_LIST = results -> {
if (results instanceof List) {
return (List) results;
}
return results != null ? new ArrayList<>(results) : null;
};
KEY_VALUE_TO_BYTES_LIST = source -> {
if (source == null) {
return null;
}
List list = new ArrayList<>(2);
list.add(source.getKey());
list.add(source.getValue());
return list;
};
BYTES_LIST_TO_MAP = source -> {
if (CollectionUtils.isEmpty(source)) {
return Collections.emptyMap();
}
Map target = new LinkedHashMap<>();
Iterator kv = source.iterator();
while (kv.hasNext()) {
target.put(kv.next(), kv.hasNext() ? kv.next() : null);
}
return target;
};
SCORED_VALUES_TO_TUPLE_SET = source -> {
if (source == null) {
return null;
}
Set tuples = new LinkedHashSet<>(source.size());
for (ScoredValue value : source) {
tuples.add(LettuceConverters.toTuple(value));
}
return tuples;
};
SCORED_VALUES_TO_TUPLE_LIST = source -> {
if (source == null) {
return null;
}
List tuples = new ArrayList<>(source.size());
for (ScoredValue value : source) {
tuples.add(LettuceConverters.toTuple(value));
}
return tuples;
};
SCORED_VALUE_TO_TUPLE = source -> source != null
? new DefaultTuple(source.getValue(), Double.valueOf(source.getScore()))
: null;
BYTES_LIST_TO_TUPLE_LIST_CONVERTER = source -> {
if (CollectionUtils.isEmpty(source)) {
return Collections.emptyList();
}
List tuples = new ArrayList<>();
Iterator it = source.iterator();
while (it.hasNext()) {
tuples.add(new DefaultTuple(it.next(), it.hasNext() ? Double.valueOf(toString(it.next())) : null));
}
return tuples;
};
PARTITIONS_TO_CLUSTER_NODES = new Converter>() {
@Override
public List convert(Partitions source) {
if (source == null) {
return Collections.emptyList();
}
List nodes = new ArrayList<>();
for (io.lettuce.core.cluster.models.partitions.RedisClusterNode node : source) {
nodes.add(CLUSTER_NODE_TO_CLUSTER_NODE_CONVERTER.convert(node));
}
return nodes;
};
};
CLUSTER_NODE_TO_CLUSTER_NODE_CONVERTER = new Converter() {
@Override
public RedisClusterNode convert(io.lettuce.core.cluster.models.partitions.RedisClusterNode source) {
Set flags = parseFlags(source.getFlags());
return RedisClusterNode.newRedisClusterNode().listeningAt(source.getUri().getHost(), source.getUri().getPort())
.withId(source.getNodeId()).promotedAs(flags.contains(Flag.MASTER) ? NodeType.MASTER : NodeType.SLAVE)
.serving(new SlotRange(source.getSlots())).withFlags(flags)
.linkState(source.isConnected() ? LinkState.CONNECTED : LinkState.DISCONNECTED).slaveOf(source.getSlaveOf())
.build();
}
private Set parseFlags(Set source) {
Set flags = new LinkedHashSet<>(source != null ? source.size() : 8, 1);
for (NodeFlag flag : source) {
switch (flag) {
case NOFLAGS:
flags.add(Flag.NOFLAGS);
break;
case EVENTUAL_FAIL:
flags.add(Flag.PFAIL);
break;
case FAIL:
flags.add(Flag.FAIL);
break;
case HANDSHAKE:
flags.add(Flag.HANDSHAKE);
break;
case MASTER:
flags.add(Flag.MASTER);
break;
case MYSELF:
flags.add(Flag.MYSELF);
break;
case NOADDR:
flags.add(Flag.NOADDR);
break;
case SLAVE:
flags.add(Flag.SLAVE);
break;
}
}
return flags;
}
};
PLUS_BYTES = toBytes("+");
MINUS_BYTES = toBytes("-");
POSITIVE_INFINITY_BYTES = toBytes("+inf");
NEGATIVE_INFINITY_BYTES = toBytes("-inf");
BYTES_LIST_TO_TIME_CONVERTER = source -> {
Assert.notEmpty(source, "Received invalid result from server. Expected 2 items in collection.");
Assert.isTrue(source.size() == 2,
"Received invalid nr of arguments from redis server. Expected 2 received " + source.size());
return toTimeMillis(toString(source.get(0)), toString(source.get(1)));
};
GEO_COORDINATE_TO_POINT_CONVERTER = geoCoordinate -> geoCoordinate != null
? new Point(geoCoordinate.getX().doubleValue(), geoCoordinate.getY().doubleValue())
: null;
GEO_COORDINATE_LIST_TO_POINT_LIST_CONVERTER = new ListConverter<>(GEO_COORDINATE_TO_POINT_CONVERTER);
KEY_VALUE_UNWRAPPER = source -> source.getValueOrElse(null);
KEY_VALUE_LIST_UNWRAPPER = new ListConverter<>(KEY_VALUE_UNWRAPPER);
TRANSACTION_RESULT_UNWRAPPER = transactionResult -> transactionResult.stream().collect(Collectors.toList());
}
public static List toTuple(List list) {
return BYTES_LIST_TO_TUPLE_LIST_CONVERTER.convert(list);
}
public static Converter, List> bytesListToTupleListConverter() {
return BYTES_LIST_TO_TUPLE_LIST_CONVERTER;
}
public static Point geoCoordinatesToPoint(GeoCoordinates geoCoordinates) {
return GEO_COORDINATE_TO_POINT_CONVERTER.convert(geoCoordinates);
}
public static Converter> stringToRedisClientListConverter() {
return source -> {
if (!StringUtils.hasText(source)) {
return Collections.emptyList();
}
return STRING_TO_LIST_OF_CLIENT_INFO.convert(source.split("\\r?\\n"));
};
}
public static Converter dateToLong() {
return DATE_TO_LONG;
}
public static Converter, Set> bytesListToBytesSet() {
return BYTES_LIST_TO_BYTES_SET;
}
public static Converter bytesToString() {
return BYTES_TO_STRING;
}
public static Converter, List> keyValueToBytesList() {
return KEY_VALUE_TO_BYTES_LIST;
}
public static Converter, List> bytesSetToBytesList() {
return BYTES_COLLECTION_TO_BYTES_LIST;
}
public static Converter, List> bytesCollectionToBytesList() {
return BYTES_COLLECTION_TO_BYTES_LIST;
}
public static Converter>, Set> scoredValuesToTupleSet() {
return SCORED_VALUES_TO_TUPLE_SET;
}
public static Converter>, List> scoredValuesToTupleList() {
return SCORED_VALUES_TO_TUPLE_LIST;
}
public static Converter, Tuple> scoredValueToTuple() {
return SCORED_VALUE_TO_TUPLE;
}
public static Converter exceptionConverter() {
return EXCEPTION_CONVERTER;
}
/**
* @return
* @sice 1.3
*/
public static Converter longToBooleanConverter() {
return LONG_TO_BOOLEAN;
}
public static Long toLong(Date source) {
return DATE_TO_LONG.convert(source);
}
public static Set toBytesSet(List source) {
return BYTES_LIST_TO_BYTES_SET.convert(source);
}
public static List toBytesList(KeyValue source) {
return KEY_VALUE_TO_BYTES_LIST.convert(source);
}
public static List toBytesList(Collection source) {
return BYTES_COLLECTION_TO_BYTES_LIST.convert(source);
}
public static Set toTupleSet(List> source) {
return SCORED_VALUES_TO_TUPLE_SET.convert(source);
}
public static Tuple toTuple(ScoredValue source) {
return SCORED_VALUE_TO_TUPLE.convert(source);
}
public static String toString(byte[] source) {
return BYTES_TO_STRING.convert(source);
}
public static ScriptOutputType toScriptOutputType(ReturnType returnType) {
switch (returnType) {
case BOOLEAN:
return ScriptOutputType.BOOLEAN;
case MULTI:
return ScriptOutputType.MULTI;
case VALUE:
return ScriptOutputType.VALUE;
case INTEGER:
return ScriptOutputType.INTEGER;
case STATUS:
return ScriptOutputType.STATUS;
default:
throw new IllegalArgumentException("Return type " + returnType + " is not a supported script output type");
}
}
public static boolean toBoolean(Position where) {
Assert.notNull(where, "list positions are mandatory");
return (Position.AFTER.equals(where) ? false : true);
}
public static int toInt(boolean value) {
return (value ? 1 : 0);
}
public static Map toMap(List source) {
return BYTES_LIST_TO_MAP.convert(source);
}
public static Converter, Map> bytesListToMapConverter() {
return BYTES_LIST_TO_MAP;
}
public static SortArgs toSortArgs(SortParameters params) {
SortArgs args = new SortArgs();
if (params == null) {
return args;
}
if (params.getByPattern() != null) {
args.by(new String(params.getByPattern(), StandardCharsets.US_ASCII));
}
if (params.getLimit() != null) {
args.limit(params.getLimit().getStart(), params.getLimit().getCount());
}
if (params.getGetPattern() != null) {
byte[][] pattern = params.getGetPattern();
for (byte[] bs : pattern) {
args.get(new String(bs, StandardCharsets.US_ASCII));
}
}
if (params.getOrder() != null) {
if (params.getOrder() == Order.ASC) {
args.asc();
} else {
args.desc();
}
}
Boolean isAlpha = params.isAlphabetic();
if (isAlpha != null && isAlpha) {
args.alpha();
}
return args;
}
public static List toListOfRedisClientInformation(String clientList) {
return stringToRedisClientListConverter().convert(clientList);
}
public static byte[][] subarray(byte[][] input, int index) {
if (input.length > index) {
byte[][] output = new byte[input.length - index][];
System.arraycopy(input, index, output, 0, output.length);
return output;
}
return null;
}
public static String boundaryToStringForZRange(Boundary boundary, String defaultValue) {
if (boundary == null || boundary.getValue() == null) {
return defaultValue;
}
return boundaryToString(boundary, "", "(");
}
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 org.springframework.data.redis.connection.RedisZSetCommands.Limit} to a Lettuce
* {@link io.lettuce.core.Limit}.
*
* @param limit
* @return a lettuce {@link io.lettuce.core.Limit}.
* @since 2.0
*/
public static io.lettuce.core.Limit toLimit(RedisZSetCommands.Limit limit) {
return limit.isUnlimited() ? Limit.unlimited() : Limit.create(limit.getOffset(), limit.getCount());
}
/**
* Convert a {@link org.springframework.data.redis.connection.RedisZSetCommands.Range} to a lettuce {@link Range}.
*
* @param range
* @return
* @since 2.0
*/
public static Range toRange(org.springframework.data.redis.connection.RedisZSetCommands.Range range) {
return Range.from(lowerBoundaryOf(range, false), upperBoundaryOf(range, false));
}
/**
* Convert a {@link org.springframework.data.redis.connection.RedisZSetCommands.Range} to a lettuce {@link Range}.
*
* @param range
* @param convertNumberToBytes
* @return
* @since 2.2
*/
public static Range toRange(org.springframework.data.redis.connection.RedisZSetCommands.Range range,
boolean convertNumberToBytes) {
return Range.from(lowerBoundaryOf(range, convertNumberToBytes), upperBoundaryOf(range, convertNumberToBytes));
}
/**
* Convert a {@link org.springframework.data.redis.connection.RedisZSetCommands.Range} to a lettuce {@link Range} and
* reverse boundaries.
*
* @param range
* @return
* @since 2.0
*/
public static Range toRevRange(org.springframework.data.redis.connection.RedisZSetCommands.Range range) {
return Range.from(upperBoundaryOf(range, false), lowerBoundaryOf(range, false));
}
@SuppressWarnings("unchecked")
private static Range.Boundary lowerBoundaryOf(
org.springframework.data.redis.connection.RedisZSetCommands.Range range, boolean convertNumberToBytes) {
return (Range.Boundary) rangeToBoundaryArgumentConverter(false, convertNumberToBytes).convert(range);
}
@SuppressWarnings("unchecked")
private static Range.Boundary upperBoundaryOf(
org.springframework.data.redis.connection.RedisZSetCommands.Range range, boolean convertNumberToBytes) {
return (Range.Boundary) rangeToBoundaryArgumentConverter(true, convertNumberToBytes).convert(range);
}
private static Converter> rangeToBoundaryArgumentConverter(
boolean upper, boolean convertNumberToBytes) {
return (source) -> {
Boundary sourceBoundary = upper ? source.getMax() : source.getMin();
if (sourceBoundary == null || sourceBoundary.getValue() == null) {
return Range.Boundary.unbounded();
}
boolean inclusive = sourceBoundary.isIncluding();
Object value = sourceBoundary.getValue();
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);
};
}
/**
* @param source List of Maps containing node details from SENTINEL SLAVES or SENTINEL MASTERS. May be empty or
* {@literal null}.
* @return List of {@link RedisServer}'s. List is empty if List of Maps is empty.
* @since 1.5
*/
public static List toListOfRedisServer(List