Skip to content

Commit 2e74c19

Browse files
committed
DATAMONGO-1054 - Polishing.
Tweaked JavaDoc of the APIs to be less specific about implementation internals and rather point to the save(…) methods. Changed SimpleMongoRepository.save(…) methods to inspect the given entity/entities and use the optimized insert(All)-calls if all entities are considered new. Original pull request: spring-projects#253.
1 parent a212b75 commit 2e74c19

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

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

+10-14
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,24 @@ public interface MongoRepository<T, ID extends Serializable> extends PagingAndSo
5151
List<T> findAll(Sort sort);
5252

5353
/**
54-
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
55-
* entity instance completely.
56-
* <p>
57-
* This uses {@link org.springframework.data.mongodb.core.MongoTemplate#insert(Object)} for storing the given entity.
58-
* <p>
59-
* Note that this method does neither fire any save events nor performs any id population or version checking.
54+
* Inserts the given a given entity. Assumes the instance to be new to be able to apply insertion optimizations. Use
55+
* the returned instance for further operations as the save operation might have changed the entity instance
56+
* completely. Prefer using {@link #save(Object)} instead to avoid the usage of store-specific API.
6057
*
61-
* @param entity
58+
* @param entity must not be {@literal null}.
6259
* @return the saved entity
60+
* @since 1.7
6361
*/
6462
<S extends T> S insert(S entity);
6563

6664
/**
67-
* Saves all given entities.
68-
* <p>
69-
* This uses {@link org.springframework.data.mongodb.core.MongoTemplate#insert(Object)} for storing the given entity.
70-
* <p>
71-
* Note that this method does neither fire any save events nor nor performs any id population or version checking.
65+
* Inserts the given entities. Assumes the given entities to have not been persisted yet and thus will optimize the
66+
* insert over a call to {@link #save(Iterable)}. Prefer using {@link #save(Iterable)} to avoid the usage of store
67+
* specific API.
7268
*
73-
* @param entities
69+
* @param entities must not be {@literal null}.
7470
* @return the saved entities
75-
* @throws IllegalArgumentException in case the given entity is (@literal null}.
71+
* @since 1.7
7672
*/
7773
<S extends T> List<S> insert(Iterable<S> entities);
7874
}

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.data.querydsl.EntityPathResolver;
3030
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
3131
import org.springframework.data.querydsl.SimpleEntityPathResolver;
32+
import org.springframework.data.repository.core.EntityInformation;
3233
import org.springframework.data.repository.core.EntityMetadata;
3334
import org.springframework.util.Assert;
3435

@@ -48,13 +49,15 @@ public class QueryDslMongoRepository<T, ID extends Serializable> extends SimpleM
4849
QueryDslPredicateExecutor<T> {
4950

5051
private final PathBuilder<T> builder;
52+
private final EntityInformation<T, ID> entityInformation;
53+
private final MongoOperations mongoOperations;
5154

5255
/**
5356
* Creates a new {@link QueryDslMongoRepository} for the given {@link EntityMetadata} and {@link MongoTemplate}. Uses
5457
* the {@link SimpleEntityPathResolver} to create an {@link EntityPath} for the given domain class.
5558
*
56-
* @param entityInformation
57-
* @param template
59+
* @param entityInformation must not be {@literal null}.
60+
* @param mongoOperations must not be {@literal null}.
5861
*/
5962
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations) {
6063
this(entityInformation, mongoOperations, SimpleEntityPathResolver.INSTANCE);
@@ -64,17 +67,21 @@ public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation,
6467
* Creates a new {@link QueryDslMongoRepository} for the given {@link MongoEntityInformation}, {@link MongoTemplate}
6568
* and {@link EntityPathResolver}.
6669
*
67-
* @param entityInformation
68-
* @param mongoOperations
69-
* @param resolver
70+
* @param entityInformation must not be {@literal null}.
71+
* @param mongoOperations must not be {@literal null}.
72+
* @param resolver must not be {@literal null}.
7073
*/
7174
public QueryDslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
7275
EntityPathResolver resolver) {
7376

7477
super(entityInformation, mongoOperations);
78+
7579
Assert.notNull(resolver);
7680
EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
81+
7782
this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
83+
this.entityInformation = entityInformation;
84+
this.mongoOperations = mongoOperations;
7885
}
7986

8087
/*
@@ -139,9 +146,9 @@ public long count(Predicate predicate) {
139146
*/
140147
private MongodbQuery<T> createQueryFor(Predicate... predicate) {
141148

142-
Class<T> domainType = getEntityInformation().getJavaType();
149+
Class<T> domainType = entityInformation.getJavaType();
143150

144-
MongodbQuery<T> query = new SpringDataMongodbQuery<T>(getMongoOperations(), domainType);
151+
MongodbQuery<T> query = new SpringDataMongodbQuery<T>(mongoOperations, domainType);
145152
return query.where(predicate);
146153
}
147154

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

+39-37
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public <S extends T> S save(S entity) {
7272

7373
Assert.notNull(entity, "Entity must not be null!");
7474

75-
mongoOperations.save(entity, entityInformation.getCollectionName());
75+
if (entityInformation.isNew(entity)) {
76+
mongoOperations.insert(entity, entityInformation.getCollectionName());
77+
} else {
78+
mongoOperations.save(entity, entityInformation.getCollectionName());
79+
}
80+
7681
return entity;
7782
}
7883

@@ -84,11 +89,22 @@ public <S extends T> List<S> save(Iterable<S> entities) {
8489

8590
Assert.notNull(entities, "The given Iterable of entities not be null!");
8691

87-
List<S> result = new ArrayList<S>(tryDetermineRealSizeOrReturn(entities, 10));
92+
List<S> result = convertIterableToList(entities);
93+
boolean allNew = true;
8894

8995
for (S entity : entities) {
90-
save(entity);
91-
result.add(entity);
96+
if (allNew && !entityInformation.isNew(entity)) {
97+
allNew = false;
98+
}
99+
}
100+
101+
if (allNew) {
102+
mongoOperations.insertAll(result);
103+
} else {
104+
105+
for (S entity : result) {
106+
save(entity);
107+
}
92108
}
93109

94110
return result;
@@ -211,32 +227,8 @@ public List<T> findAll(Sort sort) {
211227
return findAll(new Query().with(sort));
212228
}
213229

214-
private List<T> findAll(Query query) {
215-
216-
if (query == null) {
217-
return Collections.emptyList();
218-
}
219-
220-
return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
221-
}
222-
223-
/**
224-
* Returns the underlying {@link MongoOperations} instance.
225-
*
226-
* @return
227-
*/
228-
protected MongoOperations getMongoOperations() {
229-
return this.mongoOperations;
230-
}
231-
232-
/**
233-
* @return the entityInformation
234-
*/
235-
protected MongoEntityInformation<T, ID> getEntityInformation() {
236-
return entityInformation;
237-
}
238-
239-
/* (non-Javadoc)
230+
/*
231+
* (non-Javadoc)
240232
* @see org.springframework.data.mongodb.repository.MongoRepository#insert(java.lang.Object)
241233
*/
242234
@Override
@@ -248,7 +240,8 @@ public <S extends T> S insert(S entity) {
248240
return entity;
249241
}
250242

251-
/* (non-Javadoc)
243+
/*
244+
* (non-Javadoc)
252245
* @see org.springframework.data.mongodb.repository.MongoRepository#insert(java.lang.Iterable)
253246
*/
254247
@Override
@@ -266,27 +259,36 @@ public <S extends T> List<S> insert(Iterable<S> entities) {
266259
return list;
267260
}
268261

269-
private <S extends T> List<S> convertIterableToList(Iterable<S> entities) {
262+
private List<T> findAll(Query query) {
263+
264+
if (query == null) {
265+
return Collections.emptyList();
266+
}
267+
268+
return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
269+
}
270+
271+
private static <T> List<T> convertIterableToList(Iterable<T> entities) {
270272

271273
if (entities instanceof List) {
272-
return (List<S>) entities;
274+
return (List<T>) entities;
273275
}
274276

275277
int capacity = tryDetermineRealSizeOrReturn(entities, 10);
276278

277279
if (capacity == 0 || entities == null) {
278-
return Collections.<S> emptyList();
280+
return Collections.<T> emptyList();
279281
}
280282

281-
List<S> list = new ArrayList<S>(capacity);
282-
for (S entity : entities) {
283+
List<T> list = new ArrayList<T>(capacity);
284+
for (T entity : entities) {
283285
list.add(entity);
284286
}
285287

286288
return list;
287289
}
288290

289-
private int tryDetermineRealSizeOrReturn(Iterable<?> iterable, int defaultSize) {
291+
private static int tryDetermineRealSizeOrReturn(Iterable<?> iterable, int defaultSize) {
290292
return iterable == null ? 0 : (iterable instanceof Collection) ? ((Collection<?>) iterable).size() : defaultSize;
291293
}
292294
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ public void shouldInsertSingle() {
123123
public void shouldInsertMutlipleFromList() {
124124

125125
String randomId = UUID.randomUUID().toString();
126-
127126
Map<String, Person> idToPerson = new HashMap<String, Person>();
128127
List<Person> persons = new ArrayList<Person>();
128+
129129
for (int i = 0; i < 10; i++) {
130130
Person person = new Person("First" + i + randomId, "Last" + randomId + i, 42 + i);
131131
idToPerson.put(person.getId(), person);
@@ -135,7 +135,6 @@ public void shouldInsertMutlipleFromList() {
135135
List<Person> saved = repository.insert(persons);
136136

137137
assertThat(saved, hasSize(persons.size()));
138-
139138
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
140139
}
141140

@@ -146,9 +145,9 @@ public void shouldInsertMutlipleFromList() {
146145
public void shouldInsertMutlipleFromSet() {
147146

148147
String randomId = UUID.randomUUID().toString();
149-
150148
Map<String, Person> idToPerson = new HashMap<String, Person>();
151149
Set<Person> persons = new HashSet<Person>();
150+
152151
for (int i = 0; i < 10; i++) {
153152
Person person = new Person("First" + i + randomId, "Last" + i + randomId, 42 + i);
154153
idToPerson.put(person.getId(), person);
@@ -158,7 +157,6 @@ public void shouldInsertMutlipleFromSet() {
158157
List<Person> saved = repository.insert(persons);
159158

160159
assertThat(saved, hasSize(persons.size()));
161-
162160
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
163161
}
164162

0 commit comments

Comments
 (0)