From 667dd27bb5ea23c0d9973a5c33c052387fb7ee1f Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 27 Oct 2023 15:00:06 +0200 Subject: [PATCH] Polishing. Tweak naming. Simplify converters by removing unused methods. See #2754 --- .../connection/jedis/JedisConverters.java | 27 ++---------- .../jedis/JedisExceptionConverter.java | 16 +++---- .../jedis/JedisScriptReturnConverter.java | 6 +-- .../connection/jedis/StreamConverters.java | 13 +++--- .../lettuce/LettuceExceptionConverter.java | 4 +- .../connection/lettuce/StreamConverters.java | 43 +------------------ .../connection/util/ByteArrayWrapper.java | 4 +- .../data/redis/core/IndexWriter.java | 12 +++--- .../data/redis/core/RedisConnectionUtils.java | 5 +-- .../redis/core/convert/SpelIndexResolver.java | 4 +- .../support/collections/DefaultRedisList.java | 4 +- 11 files changed, 35 insertions(+), 103 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java index 376bb8bd29..a095b943db 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConverters.java @@ -171,18 +171,7 @@ public static Map toTupleMap(Set tuples) { 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 @@ public static String toString(@Nullable byte[] source) { 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 @@ private static byte[] boundaryToBytes(org.springframework.data.domain.Range.Boun 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 { diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java index 9f4b037c19..15cd693c99 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisExceptionConverter.java @@ -45,29 +45,25 @@ public class JedisExceptionConverter implements Converter convertToByteRecord(byte[] key, Object source) { continue; } - String entryIdString = SafeEncoder.encode((byte[]) res.get(0)); + String entryIdString = JedisConverters.toString((byte[]) res.get(0)); List hash = (List) res.get(1); Iterator hashIterator = hash.iterator(); @@ -163,10 +162,10 @@ static PendingMessagesSummary toPendingMessagesSummary(String groupName, Object List objectList = (List) source; long total = BuilderFactory.LONG.build(objectList.get(0)); Range.Bound 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 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> consumerObjList = (List>) objectList.get(3); Map map; @@ -174,8 +173,8 @@ static PendingMessagesSummary toPendingMessagesSummary(String groupName, Object if (consumerObjList != null) { map = new HashMap<>(consumerObjList.size()); for (List 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 @@ static PendingMessagesSummary toPendingMessagesSummary(String groupName, Object } /** - * 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 diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java index 7a057d802d..f6bfa7a1cd 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceExceptionConverter.java @@ -52,8 +52,8 @@ public DataAccessException convert(Exception ex) { 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) { diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/StreamConverters.java b/src/main/java/org/springframework/data/redis/connection/lettuce/StreamConverters.java index 5125e1d9a6..8d1fe8b153 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/StreamConverters.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/StreamConverters.java @@ -21,9 +21,7 @@ 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.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 @@ static Converter, ByteRecord> byteRecordConverter( return (it) -> StreamRecords.newRecord().in(it.getStream()).withId(it.getId()).ofBytes(it.getBody()); } - static Converter, RecordId> messageToIdConverter() { - return (it) -> RecordId.of(it.getId()); - } - /** * Convert the raw Lettuce xpending result to {@link PendingMessages}. * @@ -106,7 +97,7 @@ static org.springframework.data.redis.connection.stream.PendingMessages toPendin } /** - * 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 @@ static PendingMessagesSummary toPendingMessagesInfo(String groupName, PendingMes 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 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}. */ diff --git a/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java b/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java index 2d7e18dc95..549ccdf11c 100644 --- a/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java +++ b/src/main/java/org/springframework/data/redis/connection/util/ByteArrayWrapper.java @@ -41,8 +41,8 @@ public ByteArrayWrapper(byte[] array) { } 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; diff --git a/src/main/java/org/springframework/data/redis/core/IndexWriter.java b/src/main/java/org/springframework/data/redis/core/IndexWriter.java index 67f02bad2b..1cb2d7ba91 100644 --- a/src/main/java/org/springframework/data/redis/core/IndexWriter.java +++ b/src/main/java/org/springframework/data/redis/core/IndexWriter.java @@ -208,9 +208,9 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) { 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 @@ protected void addKeyToIndex(byte[] key, IndexedData indexedData) { // 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 @@ private byte[] toBytes(@Nullable Object source) { * @author Christoph Strobl * @since 1.8 */ - private static enum IndexWriteMode { + private enum IndexWriteMode { CREATE, UPDATE, PARTIAL_UPDATE } diff --git a/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java b/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java index 9e80e83b78..0502843817 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java +++ b/src/main/java/org/springframework/data/redis/core/RedisConnectionUtils.java @@ -22,7 +22,6 @@ 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 @@ private static RedisConnection getTargetConnection(RedisConnection connection) { RedisConnection connectionToUse = connection; - while (connectionToUse instanceof RedisConnectionProxy redisConnectionProxy) { - connectionToUse = redisConnectionProxy.getTargetConnection(); + while (connectionToUse instanceof RedisConnectionProxy proxy) { + connectionToUse = proxy.getTargetConnection(); } return connectionToUse; diff --git a/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java index 70485f4229..f15322582e 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java @@ -93,9 +93,9 @@ public Set resolveIndexesFor(TypeInformation typeInformation, @N 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); diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java index e42fa818fa..03b7b405f8 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisList.java @@ -158,8 +158,8 @@ public E moveLastTo(RedisList destination, RedisListCommands.Direction destin @SuppressWarnings("unchecked") private void potentiallyCap(RedisList destination) { - if (destination instanceof DefaultRedisList defaultRedisList) { - defaultRedisList.cap(); + if (destination instanceof DefaultRedisList redisList) { + redisList.cap(); } }