Skip to content

Commit e58fa28

Browse files
author
Thomas Darimont
committed
DATAMONGO-1054 - Add support for fast insertion via MongoRepository.insert(..).
Guesstimate the proper capacity for the result lists.
1 parent 714446b commit e58fa28

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public <S extends T> List<S> save(Iterable<S> entities) {
8484

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

87-
List<S> result = new ArrayList<S>();
87+
List<S> result = new ArrayList<S>(tryDetermineRealSizeOrReturn(entities, 10));
8888

8989
for (S entity : entities) {
9090
save(entity);
@@ -183,7 +183,7 @@ public List<T> findAll() {
183183
*/
184184
public Iterable<T> findAll(Iterable<ID> ids) {
185185

186-
Set<ID> parameters = new HashSet<ID>();
186+
Set<ID> parameters = new HashSet<ID>(tryDetermineRealSizeOrReturn(ids, 10));
187187
for (ID id : ids) {
188188
parameters.add(id);
189189
}
@@ -272,7 +272,7 @@ private <S extends T> List<S> convertIterableToList(Iterable<S> entities) {
272272
return (List<S>) entities;
273273
}
274274

275-
int capacity = (entities instanceof Collection) ? ((Collection<?>) entities).size() : 10;
275+
int capacity = tryDetermineRealSizeOrReturn(entities, 10);
276276

277277
if (capacity == 0 || entities == null) {
278278
return Collections.<S> emptyList();
@@ -285,4 +285,8 @@ private <S extends T> List<S> convertIterableToList(Iterable<S> entities) {
285285

286286
return list;
287287
}
288+
289+
private int tryDetermineRealSizeOrReturn(Iterable<?> iterable, int defaultSize) {
290+
return iterable == null ? 0 : (iterable instanceof Collection) ? ((Collection<?>) iterable).size() : defaultSize;
291+
}
288292
}

0 commit comments

Comments
 (0)