From db02e6708e7489f24ee11651d12cd398bc563c35 Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Fri, 17 Nov 2023 10:47:57 +0900 Subject: [PATCH 1/5] Make constant value a static. --- .../data/redis/core/convert/PathIndexResolver.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java index bcfacf2c88..591d50cfc7 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java @@ -55,7 +55,7 @@ */ public class PathIndexResolver implements IndexResolver { - private final Set> VALUE_TYPES = new HashSet<>(Arrays.> asList(Point.class, GeoLocation.class)); + private static final Set> VALUE_TYPES = new HashSet<>(Arrays.asList(Point.class, GeoLocation.class)); private final ConfigurableIndexDefinitionProvider indexConfiguration; private final RedisMappingContext mappingContext; @@ -158,10 +158,7 @@ public void doWithPersistentProperty(RedisPersistentProperty persistentProperty) doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue)); } } - } - - else if (persistentProperty.isEntity() - || persistentProperty.getTypeInformation().getActualType().equals(TypeInformation.OBJECT)) { + } else if (persistentProperty.isEntity() || persistentProperty.getTypeInformation().getActualType().equals(TypeInformation.OBJECT)) { typeHint = updateTypeHintForActualValue(typeHint, propertyValue); indexes.addAll( From 308aa08c7d1bcb8b4f725656a43f8319188761ea Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Fri, 17 Nov 2023 10:48:11 +0900 Subject: [PATCH 2/5] Add null checking for resolveIndexesFor method. --- .../data/redis/core/convert/PathIndexResolver.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java index 591d50cfc7..3d75405495 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java @@ -83,7 +83,9 @@ public PathIndexResolver(RedisMappingContext mappingContext) { } public Set resolveIndexesFor(TypeInformation typeInformation, @Nullable Object value) { - return doResolveIndexesFor(mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace(), "", + String keyspace = mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace(); + Assert.notNull(keyspace, "Keyspace for type " + typeInformation + " must not be null!"); + return doResolveIndexesFor(keyspace, "", typeInformation, null, value); } From 86433b9f2ee6936a3693fd9335702a0c2288f759 Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Fri, 17 Nov 2023 11:51:47 +0900 Subject: [PATCH 3/5] Make doWithProperties a lambda expression. --- .../redis/core/convert/PathIndexResolver.java | 103 +++++++++--------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java index 3d75405495..7101c21f11 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java @@ -85,8 +85,7 @@ public PathIndexResolver(RedisMappingContext mappingContext) { public Set resolveIndexesFor(TypeInformation typeInformation, @Nullable Object value) { String keyspace = mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace(); Assert.notNull(keyspace, "Keyspace for type " + typeInformation + " must not be null!"); - return doResolveIndexesFor(keyspace, "", - typeInformation, null, value); + return doResolveIndexesFor(keyspace, "", typeInformation, null, value); } @Override @@ -114,74 +113,57 @@ private Set doResolveIndexesFor(final String keyspace, final String final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value); final Set indexes = new LinkedHashSet<>(); - entity.doWithProperties(new PropertyHandler() { + entity.doWithProperties((PropertyHandler) persistentProperty -> { - @Override - public void doWithPersistentProperty(RedisPersistentProperty persistentProperty) { + String currentPath = !path.isEmpty() ? path + "." + persistentProperty.getName() : persistentProperty.getName(); - String currentPath = !path.isEmpty() ? path + "." + persistentProperty.getName() : persistentProperty.getName(); + Object propertyValue = accessor.getProperty(persistentProperty); - Object propertyValue = accessor.getProperty(persistentProperty); - - if (propertyValue == null) { - return; - } - - TypeInformation typeHint = persistentProperty.isMap() - ? persistentProperty.getTypeInformation().getRequiredMapValueType() - : persistentProperty.getTypeInformation().getActualType(); + if (propertyValue == null) { + return; + } - if (persistentProperty.isMap()) { + TypeInformation typeHint = persistentProperty.isMap() + ? persistentProperty.getTypeInformation().getRequiredMapValueType() + : persistentProperty.getTypeInformation().getActualType(); - for (Entry entry : ((Map) propertyValue).entrySet()) { + if (persistentProperty.isMap()) { - TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue()); - indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), typeToUse.getActualType(), - persistentProperty, entry.getValue())); - } + for (Entry entry : ((Map) propertyValue).entrySet()) { - } else if (persistentProperty.isCollectionLike()) { - - final Iterable iterable; + TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue()); + indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), typeToUse.getActualType(), + persistentProperty, entry.getValue())); + } - if (Iterable.class.isAssignableFrom(propertyValue.getClass())) { - iterable = (Iterable) propertyValue; - } else if (propertyValue.getClass().isArray()) { - iterable = CollectionUtils.arrayToList(propertyValue); - } else { - throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type of collection"); - } + } else if (persistentProperty.isCollectionLike()) { - for (Object listValue : iterable) { + final Iterable iterable; - if (listValue != null) { - TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, listValue); - indexes.addAll( - doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue)); - } - } - } else if (persistentProperty.isEntity() || persistentProperty.getTypeInformation().getActualType().equals(TypeInformation.OBJECT)) { - - typeHint = updateTypeHintForActualValue(typeHint, propertyValue); - indexes.addAll( - doResolveIndexesFor(keyspace, currentPath, typeHint.getActualType(), persistentProperty, propertyValue)); + if (Iterable.class.isAssignableFrom(propertyValue.getClass())) { + iterable = (Iterable) propertyValue; + } else if (propertyValue.getClass().isArray()) { + iterable = CollectionUtils.arrayToList(propertyValue); } else { - indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue)); + throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type of collection"); } - } - - private TypeInformation updateTypeHintForActualValue(TypeInformation typeHint, Object propertyValue) { + for (Object listValue : iterable) { - if (typeHint.equals(TypeInformation.OBJECT) || typeHint.getClass().isInterface()) { - try { - typeHint = mappingContext.getRequiredPersistentEntity(propertyValue.getClass()).getTypeInformation(); - } catch (Exception ignore) { - // ignore for cases where property value cannot be resolved as an entity, in that case - // the provided type hint has to be sufficient + if (listValue != null) { + TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, listValue); + indexes.addAll( + doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue)); } } - return typeHint; + } else if (persistentProperty.isEntity() + || persistentProperty.getTypeInformation().getActualType().equals(TypeInformation.OBJECT)) { + + typeHint = updateTypeHintForActualValue(typeHint, propertyValue); + indexes.addAll( + doResolveIndexesFor(keyspace, currentPath, typeHint.getActualType(), persistentProperty, propertyValue)); + } else { + indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue)); } }); @@ -189,6 +171,19 @@ private TypeInformation updateTypeHintForActualValue(TypeInformation typeH return indexes; } + private TypeInformation updateTypeHintForActualValue(TypeInformation typeHint, Object propertyValue) { + + if (typeHint.equals(TypeInformation.OBJECT) || typeHint.getClass().isInterface()) { + try { + typeHint = mappingContext.getRequiredPersistentEntity(propertyValue.getClass()).getTypeInformation(); + } catch (Exception ignore) { + // ignore for cases where property value cannot be resolved as an entity, in that case + // the provided type hint has to be sufficient + } + } + return typeHint; + } + protected Set resolveIndex(String keyspace, String propertyPath, @Nullable PersistentProperty property, @Nullable Object value) { From 23811f17c76b57d0a2609a3485714a024f2519f0 Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Mon, 20 Nov 2023 11:24:12 +0900 Subject: [PATCH 4/5] Add missing nullable annotation. --- .../data/redis/core/convert/PathIndexResolver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java index 7101c21f11..97a678ac97 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java @@ -90,7 +90,7 @@ public Set resolveIndexesFor(TypeInformation typeInformation, @N @Override public Set resolveIndexesFor(String keyspace, String path, TypeInformation typeInformation, - Object value) { + @Nullable Object value) { return doResolveIndexesFor(keyspace, path, typeInformation, null, value); } From cef69b23b73c349944fcb5234f821739b0cc1c63 Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Mon, 20 Nov 2023 11:30:30 +0900 Subject: [PATCH 5/5] Use wildcard to avoid raw use of generic class. --- .../data/redis/core/convert/PathIndexResolver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java index 97a678ac97..fe18e8576e 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java @@ -110,7 +110,7 @@ private Set doResolveIndexesFor(final String keyspace, final String return resolveIndex(keyspace, path, entity.getPersistentProperty(propertyName), value); } - final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value); + final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value); final Set indexes = new LinkedHashSet<>(); entity.doWithProperties((PropertyHandler) persistentProperty -> {