Polishing.

Tweak naming. Simplify converters by removing unused methods.

See #2754
This commit is contained in:
Mark Paluch
2023-10-27 15:00:06 +02:00
parent 85e9ae50ff
commit 667dd27bb5
11 changed files with 35 additions and 103 deletions

View File

@@ -171,18 +171,7 @@ abstract class JedisConverters extends Converters {
return args;
}
public static byte[] toBytes(Integer source) {
return String.valueOf(source).getBytes();
}
public static byte[] toBytes(Long source) {
return String.valueOf(source).getBytes();
}
/**
* @since 1.6
*/
public static byte[] toBytes(Double source) {
public static byte[] toBytes(Number source) {
return toBytes(String.valueOf(source));
}
@@ -196,10 +185,6 @@ abstract class JedisConverters extends Converters {
return source == null ? null : SafeEncoder.encode(source);
}
public static Long toLong(byte[] source) {
return Long.valueOf(toString(source));
}
/**
* Convert the given {@code source} value to the corresponding {@link ValueEncoding}.
*
@@ -466,16 +451,12 @@ abstract class JedisConverters extends Converters {
byte[] exclPrefix) {
byte[] prefix = boundary.isInclusive() ? inclPrefix : exclPrefix;
byte[] value = null;
byte[] value;
Object theValue = boundary.getValue().get();
if (theValue instanceof byte[] bytes) {
value = bytes;
} else if (theValue instanceof Double doubleValue) {
value = toBytes(doubleValue);
} else if (theValue instanceof Long longValue) {
value = toBytes(longValue);
} else if (theValue instanceof Integer integer) {
value = toBytes(integer);
} else if (theValue instanceof Number number) {
value = toBytes(number);
} else if (theValue instanceof String string) {
value = toBytes(string);
} else {

View File

@@ -45,29 +45,25 @@ public class JedisExceptionConverter implements Converter<Exception, DataAccessE
public DataAccessException convert(Exception ex) {
if (ex instanceof DataAccessException dataAccessException) {
return dataAccessException;
}
if (ex instanceof UnsupportedOperationException) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
if (ex instanceof DataAccessException dae) {
return dae;
}
if (ex instanceof JedisClusterOperationException && "No more cluster attempts left".equals(ex.getMessage())) {
return new TooManyClusterRedirectionsException(ex.getMessage(), ex);
}
if (ex instanceof JedisRedirectionException redirectionException) {
if (ex instanceof JedisRedirectionException rex) {
return new ClusterRedirectException(redirectionException.getSlot(),
redirectionException.getTargetNode().getHost(), redirectionException.getTargetNode().getPort(), ex);
return new ClusterRedirectException(rex.getSlot(), rex.getTargetNode().getHost(), rex.getTargetNode().getPort(),
ex);
}
if (ex instanceof JedisConnectionException) {
return new RedisConnectionFailureException(ex.getMessage(), ex);
}
if (ex instanceof JedisException) {
if (ex instanceof JedisException || ex instanceof UnsupportedOperationException) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.data.redis.connection.jedis;
import redis.clients.jedis.util.SafeEncoder;
import java.util.ArrayList;
import java.util.List;
@@ -41,7 +39,7 @@ public class JedisScriptReturnConverter implements Converter<Object, Object> {
public Object convert(@Nullable Object result) {
if (result instanceof String stringResult) {
// evalsha converts byte[] to String. Convert back for consistency
return SafeEncoder.encode(stringResult);
return JedisConverters.toBytes(stringResult);
}
if (returnType == ReturnType.STATUS) {
return JedisConverters.toString((byte[]) result);
@@ -60,7 +58,7 @@ public class JedisScriptReturnConverter implements Converter<Object, Object> {
if (res instanceof String stringResult) {
// evalsha converts byte[] to String. Convert back for
// consistency
convertedResults.add(SafeEncoder.encode(stringResult));
convertedResults.add(JedisConverters.toBytes(stringResult));
} else {
convertedResults.add(res);
}

View File

@@ -24,7 +24,6 @@ import redis.clients.jedis.params.XReadGroupParams;
import redis.clients.jedis.params.XReadParams;
import redis.clients.jedis.resps.StreamEntry;
import redis.clients.jedis.resps.StreamPendingEntry;
import redis.clients.jedis.util.SafeEncoder;
import java.time.Duration;
import java.util.ArrayList;
@@ -132,7 +131,7 @@ class StreamConverters {
continue;
}
String entryIdString = SafeEncoder.encode((byte[]) res.get(0));
String entryIdString = JedisConverters.toString((byte[]) res.get(0));
List<byte[]> hash = (List<byte[]>) res.get(1);
Iterator<byte[]> hashIterator = hash.iterator();
@@ -163,10 +162,10 @@ class StreamConverters {
List<Object> objectList = (List<Object>) source;
long total = BuilderFactory.LONG.build(objectList.get(0));
Range.Bound<String> lower = objectList.get(1) != null
? Range.Bound.inclusive(SafeEncoder.encode((byte[]) objectList.get(1)))
? Range.Bound.inclusive(JedisConverters.toString((byte[]) objectList.get(1)))
: Range.Bound.unbounded();
Range.Bound<String> upper = objectList.get(2) != null
? Range.Bound.inclusive(SafeEncoder.encode((byte[]) objectList.get(2)))
? Range.Bound.inclusive(JedisConverters.toString((byte[]) objectList.get(2)))
: Range.Bound.unbounded();
List<List<Object>> consumerObjList = (List<List<Object>>) objectList.get(3);
Map<String, Long> map;
@@ -174,8 +173,8 @@ class StreamConverters {
if (consumerObjList != null) {
map = new HashMap<>(consumerObjList.size());
for (List<Object> consumerObj : consumerObjList) {
map.put(SafeEncoder.encode((byte[]) consumerObj.get(0)),
Long.parseLong(SafeEncoder.encode((byte[]) consumerObj.get(1))));
map.put(JedisConverters.toString((byte[]) consumerObj.get(0)),
Long.parseLong(JedisConverters.toString((byte[]) consumerObj.get(1))));
}
} else {
map = Collections.emptyMap();
@@ -185,7 +184,7 @@ class StreamConverters {
}
/**
* Convert the raw Jedis xpending result to {@link PendingMessages}.
* Convert the raw Jedis {@code xpending} result to {@link PendingMessages}.
*
* @param groupName the group name
* @param range the range of messages requested

View File

@@ -52,8 +52,8 @@ public class LettuceExceptionConverter implements Converter<Exception, DataAcces
return new RedisSystemException("Error in execution", ex);
}
if (ex instanceof DataAccessException dataAccessException) {
return dataAccessException;
if (ex instanceof DataAccessException dae) {
return dae;
}
if (ex instanceof RedisCommandInterruptedException) {

View File

@@ -21,9 +21,7 @@ import io.lettuce.core.XReadArgs;
import io.lettuce.core.models.stream.PendingMessage;
import io.lettuce.core.models.stream.PendingMessages;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.convert.converter.Converter;
@@ -34,9 +32,6 @@ import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.NumberUtils;
/**
* Converters for Redis Stream-specific types.
@@ -76,10 +71,6 @@ class StreamConverters {
return (it) -> StreamRecords.newRecord().in(it.getStream()).withId(it.getId()).ofBytes(it.getBody());
}
static Converter<StreamMessage<byte[], byte[]>, RecordId> messageToIdConverter() {
return (it) -> RecordId.of(it.getId());
}
/**
* Convert the raw Lettuce xpending result to {@link PendingMessages}.
*
@@ -106,7 +97,7 @@ class StreamConverters {
}
/**
* Convert the raw Lettuce xpending result to {@link PendingMessagesSummary}.
* Convert the raw Lettuce {@code xpending} result to {@link PendingMessagesSummary}.
*
* @param groupName
* @param source the raw lettuce response.
@@ -123,38 +114,6 @@ class StreamConverters {
return new PendingMessagesSummary(groupName, source.getCount(), range, source.getConsumerMessageCount());
}
/**
* We need to convert values into the correct target type since lettuce will give us {@link ByteBuffer} or arrays but
* the parser requires us to have them as {@link String} or numeric values. Oh and {@literal null} values aren't real
* good citizens as well, so we make them empty strings instead - see it works - somehow ;P
*
* @param value dont't get me started om this.
* @return preconverted values that Lettuce parsers are able to understand \ö/.
*/
private static Object preConvertNativeValues(@Nullable Object value) {
if (value instanceof ByteBuffer || value instanceof byte[]) {
byte[] targetArray = value instanceof ByteBuffer byteBuffer ? ByteUtils.getBytes(byteBuffer) : (byte[]) value;
String tmp = LettuceConverters.toString(targetArray);
try {
return NumberUtils.parseNumber(tmp, Long.class);
} catch (NumberFormatException ex) {
return tmp;
}
}
if (value instanceof List listValue) {
List<Object> targetList = new ArrayList<>();
for (Object it : listValue) {
targetList.add(preConvertNativeValues(it));
}
return targetList;
}
return value != null ? value : "";
}
/**
* {@link Converter} to convert {@link StreamReadOptions} to Lettuce's {@link XReadArgs}.
*/

View File

@@ -41,8 +41,8 @@ public class ByteArrayWrapper implements Comparable<ByteArrayWrapper> {
}
public boolean equals(@Nullable Object obj) {
if (obj instanceof ByteArrayWrapper byteArrayWrapper) {
return Arrays.equals(array, byteArrayWrapper.array);
if (obj instanceof ByteArrayWrapper other) {
return Arrays.equals(array, other.array);
}
return false;

View File

@@ -208,9 +208,9 @@ class IndexWriter {
return;
}
if (indexedData instanceof SimpleIndexedPropertyValue simpleIndexedData) {
if (indexedData instanceof SimpleIndexedPropertyValue propertyValue) {
Object value = simpleIndexedData.getValue();
Object value = propertyValue.getValue();
if (value == null) {
return;
@@ -222,15 +222,15 @@ class IndexWriter {
// keep track of indexes used for the object
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
} else if (indexedData instanceof GeoIndexedPropertyValue geoIndexedData) {
} else if (indexedData instanceof GeoIndexedPropertyValue propertyValue) {
Object value = geoIndexedData.getValue();
Object value = propertyValue.getValue();
if (value == null) {
return;
}
byte[] indexKey = toBytes(indexedData.getKeyspace() + ":" + indexedData.getIndexName());
connection.geoAdd(indexKey, geoIndexedData.getPoint(), key);
connection.geoAdd(indexKey, propertyValue.getPoint(), key);
// keep track of indexes used for the object
connection.sAdd(ByteUtils.concatAll(toBytes(indexedData.getKeyspace() + ":"), key, toBytes(":idx")), indexKey);
@@ -264,7 +264,7 @@ class IndexWriter {
* @author Christoph Strobl
* @since 1.8
*/
private static enum IndexWriteMode {
private enum IndexWriteMode {
CREATE, UPDATE, PARTIAL_UPDATE
}

View File

@@ -22,7 +22,6 @@ import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.RawTargetAccess;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.dao.DataAccessException;
@@ -305,8 +304,8 @@ public abstract class RedisConnectionUtils {
RedisConnection connectionToUse = connection;
while (connectionToUse instanceof RedisConnectionProxy redisConnectionProxy) {
connectionToUse = redisConnectionProxy.getTargetConnection();
while (connectionToUse instanceof RedisConnectionProxy proxy) {
connectionToUse = proxy.getTargetConnection();
}
return connectionToUse;

View File

@@ -93,9 +93,9 @@ public class SpelIndexResolver implements IndexResolver {
for (IndexDefinition setting : settings.getIndexDefinitionsFor(keyspace)) {
if (setting instanceof SpelIndexDefinition spelIndexDefinition) {
if (setting instanceof SpelIndexDefinition spel) {
Expression expression = getAndCacheIfAbsent(spelIndexDefinition);
Expression expression = getAndCacheIfAbsent(spel);
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(value);

View File

@@ -158,8 +158,8 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@SuppressWarnings("unchecked")
private void potentiallyCap(RedisList<E> destination) {
if (destination instanceof DefaultRedisList<?> defaultRedisList) {
defaultRedisList.cap();
if (destination instanceof DefaultRedisList<?> redisList) {
redisList.cap();
}
}