Skip to content

Commit 798d153

Browse files
committed
DATAMONGO-1245 - Add changes from review.
1 parent 6be07ad commit 798d153

20 files changed

+244
-369
lines changed

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

+51-83
Large diffs are not rendered by default.

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

+59-72
Large diffs are not rendered by default.

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20+
import java.util.Collections;
2021
import java.util.HashMap;
21-
import java.util.HashSet;
2222
import java.util.Iterator;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Map.Entry;
2526
import java.util.Set;
2627
import java.util.Stack;
2728
import java.util.regex.Pattern;
2829

30+
import org.bson.BasicBSONObject;
2931
import org.springframework.data.domain.Example;
32+
import org.springframework.data.domain.ExampleSpec;
3033
import org.springframework.data.domain.ExampleSpec.NullHandler;
3134
import org.springframework.data.domain.ExampleSpec.PropertyValueTransformer;
3235
import org.springframework.data.domain.ExampleSpec.StringMatcher;
@@ -101,13 +104,11 @@ public DBObject getMappedExample(Example<?> example, MongoPersistentEntity<?> en
101104
applyPropertySpecs("", reference, example.getProbeType(), exampleSpecAccessor);
102105

103106
if (exampleSpecAccessor.isTyped()) {
104-
Set<Class<?>> restrictedTypes = new HashSet<Class<?>>();
105-
restrictedTypes.add(example.getResultType());
106-
this.converter.getTypeMapper().writeTypeRestrictions(reference, restrictedTypes);
107+
this.converter.getTypeMapper().writeTypeRestrictions(reference, (Set) Collections.singleton(example.getResultType()));
107108
}
108109

109110
return ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, exampleSpecAccessor.getNullHandler()) ? reference
110-
: new BasicDBObject(SerializationUtils.flatMap(reference));
111+
: new BasicDBObject(SerializationUtils.flattenMap(reference));
111112
}
112113

113114
private String getMappedPropertyPath(String path, Class<?> probeType) {
@@ -169,15 +170,14 @@ private void applyPropertySpecs(String path, DBObject source, Class<?> probeType
169170
while (iter.hasNext()) {
170171

171172
Map.Entry<String, Object> entry = iter.next();
172-
173-
if (entry.getKey().equals("_id") && entry.getValue() == null) {
173+
String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey();
174+
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType);
175+
176+
if(isEmptyIdProperty(entry)) {
174177
iter.remove();
175178
continue;
176179
}
177-
178-
String propertyPath = StringUtils.hasText(path) ? path + "." + entry.getKey() : entry.getKey();
179-
180-
String mappedPropertyPath = getMappedPropertyPath(propertyPath, probeType);
180+
181181
if (exampleSpecAccessor.isIgnoredPath(propertyPath) || exampleSpecAccessor.isIgnoredPath(mappedPropertyPath)) {
182182
iter.remove();
183183
continue;
@@ -217,6 +217,10 @@ private void applyPropertySpecs(String path, DBObject source, Class<?> probeType
217217
}
218218
}
219219

220+
private boolean isEmptyIdProperty(Entry<String, Object> entry) {
221+
return entry.getKey().equals("_id") && entry.getValue() == null;
222+
}
223+
220224
private void applyStringMatcher(Map.Entry<String, Object> entry, StringMatcher stringMatcher, boolean ignoreCase) {
221225

222226
BasicDBObject dbo = new BasicDBObject();

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ protected DBObject getMappedKeyword(Field property, Keyword keyword) {
261261
boolean needsAssociationConversion = property.isAssociation() && !keyword.isExists();
262262
Object value = keyword.getValue();
263263

264-
Object convertedValue = needsAssociationConversion ? convertAssociation(value, property) : getMappedValue(
265-
property.with(keyword.getKey()), value);
264+
Object convertedValue = needsAssociationConversion ? convertAssociation(value, property)
265+
: getMappedValue(property.with(keyword.getKey()), value);
266266

267267
return new BasicDBObject(keyword.key, convertedValue);
268268
}
@@ -484,8 +484,8 @@ public Object convertId(Object id) {
484484
}
485485

486486
try {
487-
return conversionService.canConvert(id.getClass(), ObjectId.class) ? conversionService
488-
.convert(id, ObjectId.class) : delegateConvertToMongoType(id, null);
487+
return conversionService.canConvert(id.getClass(), ObjectId.class) ? conversionService.convert(id, ObjectId.class)
488+
: delegateConvertToMongoType(id, null);
489489
} catch (ConversionException o_O) {
490490
return delegateConvertToMongoType(id, null);
491491
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/SerializationUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ private SerializationUtils() {
6363
* @return {@link Collections#emptyMap()} when source is {@literal null}
6464
* @since 1.8
6565
*/
66-
public static Map<String, Object> flatMap(DBObject source) {
66+
public static Map<String, Object> flattenMap(DBObject source) {
6767

6868
if (source == null) {
6969
return Collections.emptyMap();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java

-5
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,18 @@ public interface MongoRepository<T, ID extends Serializable>
4040
* (non-Javadoc)
4141
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
4242
*/
43-
@Override
4443
<S extends T> List<S> save(Iterable<S> entites);
4544

4645
/*
4746
* (non-Javadoc)
4847
* @see org.springframework.data.repository.CrudRepository#findAll()
4948
*/
50-
@Override
5149
List<T> findAll();
5250

5351
/*
5452
* (non-Javadoc)
5553
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
5654
*/
57-
@Override
5855
List<T> findAll(Sort sort);
5956

6057
/**
@@ -82,13 +79,11 @@ public interface MongoRepository<T, ID extends Serializable>
8279
/* (non-Javadoc)
8380
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
8481
*/
85-
@Override
8682
<S extends T> List<S> findAll(Example<S> example);
8783

8884
/* (non-Javadoc)
8985
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
9086
*/
91-
@Override
9287
<S extends T> List<S> findAll(Example<S> example, Sort sort);
9388

9489
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java

-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323

24-
import org.springframework.data.domain.Example;
2524
import org.springframework.data.domain.Pageable;
2625
import org.springframework.data.domain.Range;
2726
import org.springframework.data.domain.Sort;
@@ -133,11 +132,6 @@ public TextCriteria getFullText() {
133132
return delegate.getFullText();
134133
}
135134

136-
@Override
137-
public Example<?> getSampleObject() {
138-
return delegate.getSampleObject();
139-
}
140-
141135
/**
142136
* Converts the given value with the underlying {@link MongoWriter}.
143137
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParameterAccessor.java

-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18-
import org.springframework.data.domain.Example;
1918
import org.springframework.data.domain.Range;
2019
import org.springframework.data.geo.Distance;
2120
import org.springframework.data.geo.Point;
@@ -61,12 +60,4 @@ public interface MongoParameterAccessor extends ParameterAccessor {
6160
* @since 1.8
6261
*/
6362
Object[] getValues();
64-
65-
/**
66-
* Get the sample for query by example
67-
*
68-
* @return
69-
* @since 1.8
70-
*/
71-
Example<?> getSampleObject();
7263
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParameters.java

+3-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121

2222
import org.springframework.core.MethodParameter;
23-
import org.springframework.data.domain.Example;
2423
import org.springframework.data.domain.Range;
2524
import org.springframework.data.geo.Distance;
2625
import org.springframework.data.geo.Point;
@@ -43,7 +42,6 @@ public class MongoParameters extends Parameters<MongoParameters, MongoParameter>
4342
private final int rangeIndex;
4443
private final int maxDistanceIndex;
4544
private final Integer fullTextIndex;
46-
private final int sampleObjectIndex;
4745

4846
private Integer nearIndex;
4947

@@ -71,20 +69,17 @@ public MongoParameters(Method method, boolean isGeoNearMethod) {
7169
} else if (this.nearIndex == null) {
7270
this.nearIndex = -1;
7371
}
74-
75-
this.sampleObjectIndex = parameterTypes.indexOf(Example.class);
7672
}
7773

7874
private MongoParameters(List<MongoParameter> parameters, int maxDistanceIndex, Integer nearIndex,
79-
Integer fullTextIndex, int rangeIndex, int sampleObjectIndex) {
75+
Integer fullTextIndex, int rangeIndex) {
8076

8177
super(parameters);
8278

8379
this.nearIndex = nearIndex;
8480
this.fullTextIndex = fullTextIndex;
8581
this.maxDistanceIndex = maxDistanceIndex;
8682
this.rangeIndex = rangeIndex;
87-
this.sampleObjectIndex = sampleObjectIndex;
8883
}
8984

9085
private final int getNearIndex(List<Class<?>> parameterTypes) {
@@ -187,22 +182,13 @@ public int getRangeIndex() {
187182
return rangeIndex;
188183
}
189184

190-
/**
191-
* @return
192-
* @since 1.8
193-
*/
194-
public int getSampleObjectParameterIndex() {
195-
return sampleObjectIndex;
196-
}
197-
198185
/*
199186
* (non-Javadoc)
200187
* @see org.springframework.data.repository.query.Parameters#createFrom(java.util.List)
201188
*/
202189
@Override
203190
protected MongoParameters createFrom(List<MongoParameter> parameters) {
204-
return new MongoParameters(parameters, this.maxDistanceIndex, this.nearIndex, this.fullTextIndex, this.rangeIndex,
205-
this.sampleObjectIndex);
191+
return new MongoParameters(parameters, this.maxDistanceIndex, this.nearIndex, this.fullTextIndex, this.rangeIndex);
206192
}
207193

208194
private int getTypeIndex(List<TypeInformation<?>> parameterTypes, Class<?> type, Class<?> componentType) {
@@ -254,7 +240,7 @@ class MongoParameter extends Parameter {
254240
@Override
255241
public boolean isSpecialParameter() {
256242
return super.isSpecialParameter() || Distance.class.isAssignableFrom(getType()) || isNearParameter()
257-
|| TextCriteria.class.isAssignableFrom(getType()) || isExample();
243+
|| TextCriteria.class.isAssignableFrom(getType());
258244
}
259245

260246
private boolean isNearParameter() {
@@ -274,10 +260,6 @@ private boolean hasNearAnnotation() {
274260
return parameter.getParameterAnnotation(Near.class) != null;
275261
}
276262

277-
private boolean isExample() {
278-
return Example.class.isAssignableFrom(getType());
279-
}
280-
281263
}
282264

283265
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessor.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Arrays;
1919
import java.util.List;
2020

21-
import org.springframework.data.domain.Example;
2221
import org.springframework.data.domain.Range;
2322
import org.springframework.data.geo.Distance;
2423
import org.springframework.data.geo.Point;
@@ -126,15 +125,9 @@ protected TextCriteria potentiallyConvertFullText(Object fullText) {
126125
return ((TextCriteria) fullText);
127126
}
128127

129-
throw new IllegalArgumentException(String.format(
130-
"Expected full text parameter to be one of String, Term or TextCriteria but found %s.",
131-
ClassUtils.getShortName(fullText.getClass())));
132-
}
133-
134-
public Example<?> getSampleObject() {
135-
136-
int index = method.getParameters().getSampleObjectParameterIndex();
137-
return index >= 0 ? (Example<?>) getValue(index) : null;
128+
throw new IllegalArgumentException(
129+
String.format("Expected full text parameter to be one of String, Term or TextCriteria but found %s.",
130+
ClassUtils.getShortName(fullText.getClass())));
138131
}
139132

140133
/*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java

+5-18
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,7 @@ protected Criteria or(Criteria base, Criteria criteria) {
151151
@Override
152152
protected Query complete(Criteria criteria, Sort sort) {
153153

154-
Criteria toUse = null;
155-
if (accessor.getSampleObject() != null) {
156-
toUse = new Criteria().alike(accessor.getSampleObject());
157-
}
158-
159-
if (criteria != null) {
160-
if (toUse == null) {
161-
toUse = criteria;
162-
} else {
163-
toUse.andOperator(criteria);
164-
}
165-
}
166-
167-
Query query = (toUse == null ? new Query() : new Query(toUse)).with(sort);
154+
Query query = (criteria == null ? new Query() : new Query(criteria)).with(sort);
168155

169156
if (LOG.isDebugEnabled()) {
170157
LOG.debug("Created query " + query);
@@ -299,8 +286,8 @@ private Criteria createLikeRegexCriteriaOrThrow(Part part, MongoPersistentProper
299286

300287
case ALWAYS:
301288
if (path.getType() != String.class) {
302-
throw new IllegalArgumentException(String.format("Part %s must be of type String but was %s", path,
303-
path.getType()));
289+
throw new IllegalArgumentException(
290+
String.format("Part %s must be of type String but was %s", path, path.getType()));
304291
}
305292
// fall-through
306293

@@ -386,8 +373,8 @@ private <T> T nextAs(Iterator<Object> iterator, Class<T> type) {
386373
return (T) parameter;
387374
}
388375

389-
throw new IllegalArgumentException(String.format("Expected parameter type of %s but got %s!", type,
390-
parameter.getClass()));
376+
throw new IllegalArgumentException(
377+
String.format("Expected parameter type of %s but got %s!", type, parameter.getClass()));
391378
}
392379

393380
private Object[] nextAsArray(PotentiallyConvertingIterator iterator, MongoPersistentProperty property) {

0 commit comments

Comments
 (0)