30
30
31
31
import org .bson .Document ;
32
32
import org .bson .conversions .Bson ;
33
- import org .bson .types .ObjectId ;
34
33
import org .slf4j .Logger ;
35
34
import org .slf4j .LoggerFactory ;
36
35
import org .springframework .beans .BeansException ;
107
106
import org .springframework .data .util .CloseableIterator ;
108
107
import org .springframework .data .util .Optionals ;
109
108
import org .springframework .data .util .Pair ;
109
+ import org .springframework .data .util .StreamUtils ;
110
110
import org .springframework .jca .cci .core .ConnectionCallback ;
111
111
import org .springframework .util .Assert ;
112
112
import org .springframework .util .ClassUtils ;
@@ -971,7 +971,7 @@ protected <T> void doInsertBatch(String collectionName, Collection<? extends T>
971
971
documentList .add (document );
972
972
}
973
973
974
- List <Object > ids = consolidateIdentifiers ( insertDocumentList (collectionName , documentList ) , documentList );
974
+ List <Object > ids = insertDocumentList (collectionName , documentList );
975
975
976
976
int i = 0 ;
977
977
for (T obj : batchToSave ) {
@@ -1086,9 +1086,8 @@ public Object doInCollection(MongoCollection<Document> collection) throws MongoE
1086
1086
});
1087
1087
}
1088
1088
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
+
1092
1091
if (documents .isEmpty ()) {
1093
1092
return Collections .emptyList ();
1094
1093
}
@@ -1097,33 +1096,24 @@ protected List<ObjectId> insertDocumentList(final String collectionName, final L
1097
1096
LOGGER .debug ("Inserting list of Documents containing {} items" , documents .size ());
1098
1097
}
1099
1098
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 -> {
1105
1100
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 );
1111
1104
1112
- return null ;
1105
+ if (writeConcernToUse == null ) {
1106
+ collection .insertMany (documents );
1107
+ } else {
1108
+ collection .withWriteConcern (writeConcernToUse ).insertMany (documents );
1113
1109
}
1110
+
1111
+ return null ;
1114
1112
});
1115
1113
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 ());
1127
1117
}
1128
1118
1129
1119
protected Object saveDocument (final String collectionName , final Document dbDoc , final Class <?> entityClass ) {
@@ -2374,28 +2364,6 @@ static RuntimeException potentiallyConvertRuntimeException(RuntimeException ex,
2374
2364
return resolved == null ? ex : resolved ;
2375
2365
}
2376
2366
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
-
2399
2367
// Callback implementations
2400
2368
2401
2369
/**
0 commit comments