Skip to content

Commit c89d2e3

Browse files
committed
Polishing.
Reorder API methods, remove unused MongoPersistentProperty.isNullable method, reduce visibility where possible. Add Javadoc. Introduce DotPath utility to abstract dot path concatenation.
1 parent 55699d3 commit c89d2e3

19 files changed

+174
-96
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DocumentAccessor.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,17 @@ Bson getDocument() {
6767
return this.document;
6868
}
6969

70-
public void putAll(MongoPersistentProperty prop, Document value) {
71-
value.entrySet().forEach(entry -> BsonUtils.asMap(document).put(entry.getKey(), entry.getValue()));
70+
/**
71+
* Copies all of the mappings from the given {@link Document} to the underlying target {@link Document}. These
72+
* mappings will replace any mappings that the target document had for any of the keys currently in the specified map.
73+
*
74+
* @param source
75+
*/
76+
public void putAll(Document source) {
77+
78+
Map<String, Object> target = BsonUtils.asMap(document);
79+
80+
target.putAll(source);
7281
}
7382

7483
/**

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
672672
Document target = new Document();
673673
writeInternal(obj, target, mappingContext.getPersistentEntity(prop));
674674

675-
accessor.putAll(prop, target);
675+
accessor.putAll(target);
676676
return;
677677
}
678678

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoExampleMapper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.mongodb.core.query.MongoRegexCreator.MatchMode;
4141
import org.springframework.data.mongodb.core.query.SerializationUtils;
4242
import org.springframework.data.mongodb.core.query.UntypedExampleMatcher;
43+
import org.springframework.data.mongodb.util.DotPath;
4344
import org.springframework.data.support.ExampleMatcherAccessor;
4445
import org.springframework.data.util.TypeInformation;
4546
import org.springframework.util.Assert;
@@ -134,7 +135,7 @@ private void applyPropertySpecs(String path, Document source, Class<?> probeType
134135
while (iter.hasNext()) {
135136

136137
Map.Entry<String, Object> entry = iter.next();
137-
String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey();
138+
String propertyPath = DotPath.from(path).append(entry.getKey()).toString();
138139
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType);
139140

140141
if (isEmptyIdProperty(entry)) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.bson.Document;
2525
import org.bson.conversions.Bson;
2626
import org.bson.types.ObjectId;
27+
2728
import org.springframework.core.convert.ConversionService;
2829
import org.springframework.core.convert.converter.Converter;
2930
import org.springframework.data.domain.Example;
@@ -32,7 +33,6 @@
3233
import org.springframework.data.mapping.PersistentEntity;
3334
import org.springframework.data.mapping.PersistentProperty;
3435
import org.springframework.data.mapping.PersistentPropertyPath;
35-
import org.springframework.data.mapping.PropertyHandler;
3636
import org.springframework.data.mapping.PropertyPath;
3737
import org.springframework.data.mapping.PropertyReferenceException;
3838
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
@@ -43,6 +43,7 @@
4343
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty.PropertyToFieldNameConverter;
4444
import org.springframework.data.mongodb.core.query.Query;
4545
import org.springframework.data.mongodb.util.BsonUtils;
46+
import org.springframework.data.mongodb.util.DotPath;
4647
import org.springframework.data.util.ClassTypeInformation;
4748
import org.springframework.data.util.TypeInformation;
4849
import org.springframework.lang.Nullable;
@@ -255,17 +256,18 @@ private Document filterEmbeddedObjects(Document fieldsObject, @Nullable MongoPer
255256
PropertyPath path = PropertyPath.from(field.getKey(), entity.getTypeInformation());
256257
PersistentPropertyPath<MongoPersistentProperty> persistentPropertyPath = mappingContext
257258
.getPersistentPropertyPath(path);
258-
MongoPersistentProperty property = mappingContext.getPersistentPropertyPath(path).getLeafProperty();
259+
MongoPersistentProperty property = mappingContext.getPersistentPropertyPath(path).getRequiredLeafProperty();
259260

260261
if (property.isEmbedded() && property.isEntity()) {
261262

262-
mappingContext.getPersistentEntity(property)
263-
.doWithProperties((PropertyHandler<MongoPersistentProperty>) embedded -> {
263+
MongoPersistentEntity<?> embeddedEntity = mappingContext.getRequiredPersistentEntity(property);
264+
265+
for (MongoPersistentProperty embedded : embeddedEntity) {
266+
267+
DotPath dotPath = DotPath.from(persistentPropertyPath.toDotPath()).append(embedded.getName());
268+
target.put(dotPath.toString(), field.getValue());
269+
}
264270

265-
String dotPath = persistentPropertyPath.toDotPath();
266-
dotPath = dotPath + (StringUtils.hasText(dotPath) ? "." : "") + embedded.getName();
267-
target.put(dotPath, field.getValue());
268-
});
269271
} else {
270272
target.put(field.getKey(), field.getValue());
271273
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java

-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.data.domain.Sort.Order;
2727
import org.springframework.data.mapping.Association;
2828
import org.springframework.data.mapping.context.MappingContext;
29-
import org.springframework.data.mongodb.core.mapping.EmbeddedMongoPersistentEntity;
3029
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
3130
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
3231
import org.springframework.data.mongodb.core.query.Query;
@@ -164,10 +163,6 @@ protected Entry<String, Object> getMappedObjectForField(Field field, Object rawV
164163
return getMappedUpdateModifier(field, rawValue);
165164
}
166165

167-
if(field.getProperty() != null && field.getProperty().isEmbedded()) {
168-
System.out.println("here we are: ");
169-
}
170-
171166
return super.getMappedObjectForField(field, rawValue);
172167
}
173168

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexOperationsProvider.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@
2828
public interface IndexOperationsProvider {
2929

3030
/**
31-
* Returns the operations that can be performed on indexes
31+
* Returns the operations that can be performed on indexes.
3232
*
3333
* @param collectionName name of the MongoDB collection, must not be {@literal null}.
34-
* @param type the type used for field mapping. Can be {@literal null}.
3534
* @return index operations on the named collection
36-
* @since 2.5
3735
*/
38-
IndexOperations indexOps(String collectionName, @Nullable Class<?> type);
36+
default IndexOperations indexOps(String collectionName) {
37+
return indexOps(collectionName, null);
38+
}
3939

4040
/**
41-
* Returns the operations that can be performed on indexes
41+
* Returns the operations that can be performed on indexes.
4242
*
4343
* @param collectionName name of the MongoDB collection, must not be {@literal null}.
44+
* @param type the type used for field mapping. Can be {@literal null}.
4445
* @return index operations on the named collection
46+
* @since 3.2
4547
*/
46-
default IndexOperations indexOps(String collectionName) {
47-
return indexOps(collectionName, null);
48-
}
48+
IndexOperations indexOps(String collectionName, @Nullable Class<?> type);
4949
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java

+21-20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
4848
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
4949
import org.springframework.data.mongodb.util.BsonUtils;
50+
import org.springframework.data.mongodb.util.DotPath;
5051
import org.springframework.data.spel.EvaluationContextProvider;
5152
import org.springframework.data.util.TypeInformation;
5253
import org.springframework.expression.EvaluationContext;
@@ -161,16 +162,16 @@ private void potentiallyAddIndexForProperty(MongoPersistentEntity<?> root, Mongo
161162
* @return List of {@link IndexDefinitionHolder} representing indexes for given type and its referenced property
162163
* types. Will never be {@code null}.
163164
*/
164-
private List<IndexDefinitionHolder> resolveIndexForClass(final TypeInformation<?> type, final String dotPath,
165-
final Path path, final String collection, final CycleGuard guard) {
165+
private List<IndexDefinitionHolder> resolveIndexForClass( TypeInformation<?> type, String dotPath,
166+
Path path, String collection, CycleGuard guard) {
166167

167168
return resolveIndexForEntity(mappingContext.getRequiredPersistentEntity(type), dotPath, path, collection, guard);
168169
}
169170

170-
private List<IndexDefinitionHolder> resolveIndexForEntity(MongoPersistentEntity<?> entity, final String dotPath,
171-
final Path path, final String collection, final CycleGuard guard) {
171+
private List<IndexDefinitionHolder> resolveIndexForEntity(MongoPersistentEntity<?> entity, String dotPath,
172+
Path path, String collection, CycleGuard guard) {
172173

173-
final List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
174+
List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
174175
indexInformation.addAll(potentiallyCreateCompoundIndexDefinitions(dotPath, collection, entity));
175176

176177
entity.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> this
@@ -184,25 +185,25 @@ private List<IndexDefinitionHolder> resolveIndexForEntity(MongoPersistentEntity<
184185
private void guardAndPotentiallyAddIndexForProperty(MongoPersistentProperty persistentProperty, String dotPath,
185186
Path path, String collection, List<IndexDefinitionHolder> indexes, CycleGuard guard) {
186187

187-
String propertyDotPath = dotPath;
188+
DotPath propertyDotPath = DotPath.from(dotPath);
188189

189190
if (!persistentProperty.isEmbedded()) {
190-
propertyDotPath = (StringUtils.hasText(dotPath) ? dotPath + "." : "") + persistentProperty.getFieldName();
191+
propertyDotPath = propertyDotPath.append(persistentProperty.getFieldName());
191192
}
192193

193194
Path propertyPath = path.append(persistentProperty);
194195
guard.protect(persistentProperty, propertyPath);
195196

196197
if (persistentProperty.isEntity()) {
197198
try {
198-
indexes.addAll(resolveIndexForEntity(mappingContext.getPersistentEntity(persistentProperty), propertyDotPath,
199+
indexes.addAll(resolveIndexForEntity(mappingContext.getPersistentEntity(persistentProperty), propertyDotPath.toString(),
199200
propertyPath, collection, guard));
200201
} catch (CyclicPropertyReferenceException e) {
201202
LOGGER.info(e.getMessage());
202203
}
203204
}
204205

205-
List<IndexDefinitionHolder> indexDefinitions = createIndexDefinitionHolderForProperty(propertyDotPath, collection,
206+
List<IndexDefinitionHolder> indexDefinitions = createIndexDefinitionHolderForProperty(propertyDotPath.toString(), collection,
206207
persistentProperty);
207208

208209
if (!indexDefinitions.isEmpty()) {
@@ -270,7 +271,7 @@ private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDe
270271
}
271272

272273
try {
273-
appendTextIndexInformation("", Path.empty(), indexDefinitionBuilder, root,
274+
appendTextIndexInformation(DotPath.empty(), Path.empty(), indexDefinitionBuilder, root,
274275
new TextIndexIncludeOptions(IncludeStrategy.DEFAULT), new CycleGuard());
275276
} catch (CyclicPropertyReferenceException e) {
276277
LOGGER.info(e.getMessage());
@@ -291,9 +292,9 @@ private Collection<? extends IndexDefinitionHolder> potentiallyCreateTextIndexDe
291292

292293
}
293294

294-
private void appendTextIndexInformation(final String dotPath, final Path path,
295-
final TextIndexDefinitionBuilder indexDefinitionBuilder, final MongoPersistentEntity<?> entity,
296-
final TextIndexIncludeOptions includeOptions, final CycleGuard guard) {
295+
private void appendTextIndexInformation(DotPath dotPath, Path path,
296+
TextIndexDefinitionBuilder indexDefinitionBuilder, MongoPersistentEntity<?> entity,
297+
TextIndexIncludeOptions includeOptions, CycleGuard guard) {
297298

298299
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
299300

@@ -302,16 +303,16 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
302303

303304
guard.protect(persistentProperty, path);
304305

305-
if (persistentProperty.isExplicitLanguageProperty() && !StringUtils.hasText(dotPath)) {
306+
if (persistentProperty.isExplicitLanguageProperty() && dotPath.isEmpty()) {
306307
indexDefinitionBuilder.withLanguageOverride(persistentProperty.getFieldName());
307308
}
308309

309310
TextIndexed indexed = persistentProperty.findAnnotation(TextIndexed.class);
310311

311312
if (includeOptions.isForce() || indexed != null || persistentProperty.isEntity()) {
312313

313-
String propertyDotPath = (StringUtils.hasText(dotPath) ? dotPath + "." : "")
314-
+ persistentProperty.getFieldName();
314+
DotPath propertyDotPath = dotPath
315+
.append(persistentProperty.getFieldName());
315316

316317
Path propertyPath = path.append(persistentProperty);
317318

@@ -324,7 +325,7 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
324325
TextIndexIncludeOptions optionsForNestedType = includeOptions;
325326
if (!IncludeStrategy.FORCE.equals(includeOptions.getStrategy()) && indexed != null) {
326327
optionsForNestedType = new TextIndexIncludeOptions(IncludeStrategy.FORCE,
327-
new TextIndexedFieldSpec(propertyDotPath, weight));
328+
new TextIndexedFieldSpec(propertyDotPath.toString(), weight));
328329
}
329330

330331
try {
@@ -337,7 +338,7 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
337338
entity.getName()), e);
338339
}
339340
} else if (includeOptions.isForce() || indexed != null) {
340-
indexDefinitionBuilder.onField(propertyDotPath, weight);
341+
indexDefinitionBuilder.onField(propertyDotPath.toString(), weight);
341342
}
342343
}
343344

@@ -648,15 +649,15 @@ private void resolveAndAddIndexesForAssociation(Association<MongoPersistentPrope
648649

649650
MongoPersistentProperty property = association.getInverse();
650651

651-
String propertyDotPath = (StringUtils.hasText(path) ? path + "." : "") + property.getFieldName();
652+
DotPath propertyDotPath = DotPath.from(path).append(property.getFieldName());
652653

653654
if (property.isAnnotationPresent(GeoSpatialIndexed.class) || property.isAnnotationPresent(TextIndexed.class)) {
654655
throw new MappingException(
655656
String.format("Cannot create geospatial-/text- index on DBRef in collection '%s' for path '%s'.", collection,
656657
propertyDotPath));
657658
}
658659

659-
List<IndexDefinitionHolder> indexDefinitions = createIndexDefinitionHolderForProperty(propertyDotPath, collection,
660+
List<IndexDefinitionHolder> indexDefinitions = createIndexDefinitionHolderForProperty(propertyDotPath.toString(), collection,
660661
property);
661662

662663
if (!indexDefinitions.isEmpty()) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EmbeddedEntityContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @author Christoph Strobl
2020
* @since 3.2
2121
*/
22-
public class EmbeddedEntityContext {
22+
class EmbeddedEntityContext {
2323

2424
private final MongoPersistentProperty property;
2525

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EmbeddedMongoPersistentEntity.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
import org.springframework.lang.Nullable;
3333

3434
/**
35+
* Embedded variant of {@link MongoPersistentEntity}.
36+
*
3537
* @author Christoph Strobl
36-
* @since 2020/12
38+
* @see Embedded
3739
*/
38-
public class EmbeddedMongoPersistentEntity<T> implements MongoPersistentEntity<T> {
40+
class EmbeddedMongoPersistentEntity<T> implements MongoPersistentEntity<T> {
3941

40-
private EmbeddedEntityContext context;
41-
private MongoPersistentEntity<T> delegate;
42+
private final EmbeddedEntityContext context;
43+
private final MongoPersistentEntity<T> delegate;
4244

4345
public EmbeddedMongoPersistentEntity(MongoPersistentEntity<T> delegate, EmbeddedEntityContext context) {
4446

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EmbeddedMongoPersistentProperty.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import org.springframework.lang.Nullable;
2727

2828
/**
29+
* Embedded variant of {@link MongoPersistentProperty}.
30+
*
2931
* @author Christoph Strobl
30-
* @since 2020/12
32+
* @see Embedded
3133
*/
32-
public class EmbeddedMongoPersistentProperty implements MongoPersistentProperty {
34+
class EmbeddedMongoPersistentProperty implements MongoPersistentProperty {
3335

3436
private final MongoPersistentProperty delegate;
3537
private final EmbeddedEntityContext context;
@@ -204,10 +206,6 @@ public boolean isEmbedded() {
204206
return delegate.isEmbedded();
205207
}
206208

207-
public boolean isNullable() {
208-
return delegate.isNullable();
209-
}
210-
211209
@Nullable
212210
public Class<?> getComponentType() {
213211
return delegate.getComponentType();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoMappingContext.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
* @author Jon Brisbin
3737
* @author Oliver Gierke
38+
* @author Christoph Strobl
3839
*/
3940
public class MongoMappingContext extends AbstractMappingContext<MongoPersistentEntity<?>, MongoPersistentProperty>
4041
implements ApplicationContextAware {
@@ -81,18 +82,13 @@ public MongoPersistentProperty createPersistentProperty(Property property, Mongo
8182
return new CachingMongoPersistentProperty(property, owner, simpleTypeHolder, fieldNamingStrategy);
8283
}
8384

84-
// @Override
85-
// protected MongoPersistentProperty createPersistentProperty(Property property, MongoPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
86-
// return null;
87-
// }
88-
8985
/*
9086
* (non-Javadoc)
9187
* @see org.springframework.data.mapping.BasicMappingContext#createPersistentEntity(org.springframework.data.util.TypeInformation, org.springframework.data.mapping.model.MappingContext)
9288
*/
9389
@Override
9490
protected <T> BasicMongoPersistentEntity<T> createPersistentEntity(TypeInformation<T> typeInformation) {
95-
return new BasicMongoPersistentEntity<T>(typeInformation);
91+
return new BasicMongoPersistentEntity<>(typeInformation);
9692
}
9793

9894
/*
@@ -101,7 +97,6 @@ protected <T> BasicMongoPersistentEntity<T> createPersistentEntity(TypeInformati
10197
*/
10298
@Override
10399
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
104-
105100
super.setApplicationContext(applicationContext);
106101
}
107102

@@ -132,16 +127,16 @@ public void setAutoIndexCreation(boolean autoCreateIndexes) {
132127
this.autoIndexCreation = autoCreateIndexes;
133128
}
134129

135-
136130
@Nullable
137131
@Override
138132
public MongoPersistentEntity<?> getPersistentEntity(MongoPersistentProperty persistentProperty) {
139133

140-
MongoPersistentEntity entity = super.getPersistentEntity(persistentProperty);
134+
MongoPersistentEntity<?> entity = super.getPersistentEntity(persistentProperty);
135+
141136
if(entity == null || !persistentProperty.isEmbedded()) {
142137
return entity;
143138
}
144139

145-
return new EmbeddedMongoPersistentEntity(entity, new EmbeddedEntityContext(persistentProperty));
140+
return new EmbeddedMongoPersistentEntity<>(entity, new EmbeddedEntityContext(persistentProperty));
146141
}
147142
}

0 commit comments

Comments
 (0)