Skip to content

Commit f16809a

Browse files
committed
DATAMONGO-1519 - Method signature change for MongoTemplate.insertDocumentList(…).
Implemented return type change for protected method insertDocumentList(String, List<Document>). Related ticket: DATAMONGO-1513.
1 parent 58b33e9 commit f16809a

File tree

1 file changed

+17
-49
lines changed

1 file changed

+17
-49
lines changed

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

+17-49
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import org.bson.Document;
3232
import org.bson.conversions.Bson;
33-
import org.bson.types.ObjectId;
3433
import org.slf4j.Logger;
3534
import org.slf4j.LoggerFactory;
3635
import org.springframework.beans.BeansException;
@@ -107,6 +106,7 @@
107106
import org.springframework.data.util.CloseableIterator;
108107
import org.springframework.data.util.Optionals;
109108
import org.springframework.data.util.Pair;
109+
import org.springframework.data.util.StreamUtils;
110110
import org.springframework.jca.cci.core.ConnectionCallback;
111111
import org.springframework.util.Assert;
112112
import org.springframework.util.ClassUtils;
@@ -971,7 +971,7 @@ protected <T> void doInsertBatch(String collectionName, Collection<? extends T>
971971
documentList.add(document);
972972
}
973973

974-
List<Object> ids = consolidateIdentifiers(insertDocumentList(collectionName, documentList), documentList);
974+
List<Object> ids = insertDocumentList(collectionName, documentList);
975975

976976
int i = 0;
977977
for (T obj : batchToSave) {
@@ -1086,9 +1086,8 @@ public Object doInCollection(MongoCollection<Document> collection) throws MongoE
10861086
});
10871087
}
10881088

1089-
// TODO: 2.0 - Change method signature to return List<Object> and return all identifiers (DATAMONGO-1513,
1090-
// DATAMONGO-1519)
1091-
protected List<ObjectId> insertDocumentList(final String collectionName, final List<Document> documents) {
1089+
protected List<Object> insertDocumentList(final String collectionName, final List<Document> documents) {
1090+
10921091
if (documents.isEmpty()) {
10931092
return Collections.emptyList();
10941093
}
@@ -1097,33 +1096,24 @@ protected List<ObjectId> insertDocumentList(final String collectionName, final L
10971096
LOGGER.debug("Inserting list of Documents containing {} items", documents.size());
10981097
}
10991098

1100-
execute(collectionName, new CollectionCallback<Void>() {
1101-
public Void doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
1102-
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
1103-
null, null);
1104-
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
1099+
execute(collectionName, collection -> {
11051100

1106-
if (writeConcernToUse == null) {
1107-
collection.insertMany(documents);
1108-
} else {
1109-
collection.withWriteConcern(writeConcernToUse).insertMany(documents);
1110-
}
1101+
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
1102+
null, null);
1103+
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
11111104

1112-
return null;
1105+
if (writeConcernToUse == null) {
1106+
collection.insertMany(documents);
1107+
} else {
1108+
collection.withWriteConcern(writeConcernToUse).insertMany(documents);
11131109
}
1110+
1111+
return null;
11141112
});
11151113

1116-
List<ObjectId> ids = new ArrayList<ObjectId>();
1117-
for (Document dbo : documents) {
1118-
Object id = dbo.get(ID_FIELD);
1119-
if (id instanceof ObjectId) {
1120-
ids.add((ObjectId) id);
1121-
} else {
1122-
// no id was generated
1123-
ids.add(null);
1124-
}
1125-
}
1126-
return ids;
1114+
return documents.stream()//
1115+
.map(it -> it.get(ID_FIELD))//
1116+
.collect(StreamUtils.toUnmodifiableList());
11271117
}
11281118

11291119
protected Object saveDocument(final String collectionName, final Document dbDoc, final Class<?> entityClass) {
@@ -2374,28 +2364,6 @@ static RuntimeException potentiallyConvertRuntimeException(RuntimeException ex,
23742364
return resolved == null ? ex : resolved;
23752365
}
23762366

2377-
/**
2378-
* Returns all identifiers for the given documents. Will augment the given identifiers and fill in only the ones that
2379-
* are {@literal null} currently. This would've been better solved in {@link #insertDBObjectList(String, List)}
2380-
* directly but would require a signature change of that method.
2381-
*
2382-
* @param ids
2383-
* @param documents
2384-
* @return TODO: Remove for 2.0 and change method signature of {@link #insertDBObjectList(String, List)}.
2385-
*/
2386-
private static List<Object> consolidateIdentifiers(List<ObjectId> ids, List<Document> documents) {
2387-
2388-
List<Object> result = new ArrayList<Object>(ids.size());
2389-
2390-
for (int i = 0; i < ids.size(); i++) {
2391-
2392-
ObjectId objectId = ids.get(i);
2393-
result.add(objectId == null ? documents.get(i).get(ID_FIELD) : objectId);
2394-
}
2395-
2396-
return result;
2397-
}
2398-
23992367
// Callback implementations
24002368

24012369
/**

0 commit comments

Comments
 (0)