Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PathIndexResolver. #2775

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/
public class PathIndexResolver implements IndexResolver {

private final Set<Class<?>> VALUE_TYPES = new HashSet<>(Arrays.<Class<?>> asList(Point.class, GeoLocation.class));
private static final Set<Class<?>> VALUE_TYPES = new HashSet<>(Arrays.asList(Point.class, GeoLocation.class));

private final ConfigurableIndexDefinitionProvider indexConfiguration;
private final RedisMappingContext mappingContext;
Expand Down Expand Up @@ -83,13 +83,14 @@ public PathIndexResolver(RedisMappingContext mappingContext) {
}

public Set<IndexedData> resolveIndexesFor(TypeInformation<?> typeInformation, @Nullable Object value) {
return doResolveIndexesFor(mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace(), "",
typeInformation, null, value);
String keyspace = mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace();
Assert.notNull(keyspace, "Keyspace for type " + typeInformation + " must not be null!");
return doResolveIndexesFor(keyspace, "", typeInformation, null, value);
}

@Override
public Set<IndexedData> resolveIndexesFor(String keyspace, String path, TypeInformation<?> typeInformation,
Object value) {
@Nullable Object value) {
return doResolveIndexesFor(keyspace, path, typeInformation, null, value);
}

Expand All @@ -109,87 +110,80 @@ private Set<IndexedData> 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<IndexedData> indexes = new LinkedHashSet<>();

entity.doWithProperties(new PropertyHandler<RedisPersistentProperty>() {
entity.doWithProperties((PropertyHandler<RedisPersistentProperty>) 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 (persistentProperty.isMap()) {

for (Entry<?, ?> entry : ((Map<?, ?>) propertyValue).entrySet()) {

TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue());
indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), typeToUse.getActualType(),
persistentProperty, entry.getValue()));
}

} else if (persistentProperty.isCollectionLike()) {
if (propertyValue == null) {
return;
}

final Iterable<?> iterable;
TypeInformation<?> typeHint = persistentProperty.isMap()
? persistentProperty.getTypeInformation().getRequiredMapValueType()
: persistentProperty.getTypeInformation().getActualType();

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");
}
if (persistentProperty.isMap()) {

for (Object listValue : iterable) {
for (Entry<?, ?> entry : ((Map<?, ?>) propertyValue).entrySet()) {

if (listValue != null) {
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, listValue);
indexes.addAll(
doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue));
}
}
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue());
indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), typeToUse.getActualType(),
persistentProperty, entry.getValue()));
}

else if (persistentProperty.isEntity()
|| persistentProperty.getTypeInformation().getActualType().equals(TypeInformation.OBJECT)) {
} else if (persistentProperty.isCollectionLike()) {

final Iterable<?> iterable;

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");
}

}
for (Object listValue : iterable) {

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
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));
}

});

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<IndexedData> resolveIndex(String keyspace, String propertyPath,
@Nullable PersistentProperty<?> property, @Nullable Object value) {

Expand Down