Skip to content

Commit 9299d86

Browse files
DATAREDIS-425 - Fix index resolution single/multi mismatch.
1 parent e897dd6 commit 9299d86

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/main/java/org/springframework/data/redis/core/convert/IndexResolverImpl.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ private Set<IndexedData> doResolveIndexesFor(final String keyspace, final String
8080
RedisPersistentEntity<?> entity = mappingContext.getPersistentEntity(typeInformation);
8181

8282
if (entity == null) {
83-
84-
IndexedData index = resolveIndex(keyspace, path, null, value);
85-
if (index != null) {
86-
return Collections.singleton(index);
87-
}
88-
return Collections.emptySet();
83+
return resolveIndex(keyspace, path, null, value);
8984
}
9085

9186
final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
@@ -105,11 +100,7 @@ public void doWithPersistentProperty(KeyValuePersistentProperty persistentProper
105100
TypeInformation<?> typeHint = persistentProperty.isMap() ? persistentProperty.getTypeInformation()
106101
.getMapValueType() : persistentProperty.getTypeInformation().getActualType();
107102

108-
IndexedData index = resolveIndex(keyspace, currentPath, persistentProperty, propertyValue);
109-
110-
if (index != null) {
111-
indexes.add(index);
112-
}
103+
indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue));
113104

114105
if (persistentProperty.isMap()) {
115106

@@ -157,20 +148,22 @@ private TypeInformation<?> updateTypeHintForActualValue(TypeInformation<?> typeH
157148
return indexes;
158149
}
159150

160-
protected IndexedData resolveIndex(String keyspace, String propertyPath, PersistentProperty<?> property, Object value) {
151+
protected Set<IndexedData> resolveIndex(String keyspace, String propertyPath, PersistentProperty<?> property,
152+
Object value) {
161153

162154
if (value == null) {
163-
return null;
155+
return Collections.emptySet();
164156
}
165157

166158
String path = normalizeIndexPath(propertyPath, property);
167159

160+
Set<IndexedData> data = new LinkedHashSet<IndexedData>();
161+
168162
if (indexConfiguration.hasIndexFor(keyspace, path)) {
169-
// FIXME it seems there is a mis-match between IndexConfiguration
170-
// resolving many RedisIndexSetting objects to resolving a single
171-
// IndexData in this method.
172-
RedisIndexSetting indexSetting = indexConfiguration.getIndexDefinitionsFor(keyspace, path).iterator().next();
173-
return new SimpleIndexedPropertyValue(keyspace, indexSetting.getIndexName(), value);
163+
for (RedisIndexSetting indexSetting : indexConfiguration.getIndexDefinitionsFor(keyspace, path)) {
164+
data.add(new SimpleIndexedPropertyValue(keyspace, indexSetting.getIndexName(), value));
165+
}
166+
174167
}
175168

176169
else if (property != null && property.isAnnotationPresent(Indexed.class)) {
@@ -181,13 +174,14 @@ else if (property != null && property.isAnnotationPresent(Indexed.class)) {
181174

182175
switch (indexed.type()) {
183176
case SIMPLE:
184-
return new SimpleIndexedPropertyValue(keyspace, path, value);
177+
data.add(new SimpleIndexedPropertyValue(keyspace, path, value));
178+
break;
185179
default:
186180
throw new IllegalArgumentException(String.format("Unsupported index type '%s' for path '%s'.",
187181
indexed.type(), path));
188182
}
189183
}
190-
return null;
184+
return data;
191185
}
192186

193187
private String normalizeIndexPath(String path, PersistentProperty<?> property) {

src/test/java/org/springframework/data/redis/core/convert/IndexResolverImplUnitTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,15 @@ public void resolveIndexAllowCustomIndexName() {
427427
}
428428

429429
private IndexedData resolve(String path, Object value) {
430-
return indexResolver.resolveIndex(KEYSPACE_PERSON, path, propertyMock, value);
430+
431+
Set<IndexedData> data = indexResolver.resolveIndex(KEYSPACE_PERSON, path, propertyMock, value);
432+
433+
if (data.isEmpty()) {
434+
return null;
435+
}
436+
437+
assertThat(data.size(), is(1));
438+
return data.iterator().next();
431439
}
432440

433441
private Indexed createIndexedInstance(final IndexType type) {

0 commit comments

Comments
 (0)