15
15
*/
16
16
package org .springframework .data .mongodb .core ;
17
17
18
- import lombok .Data ;
18
+ import lombok .NonNull ;
19
+ import lombok .Value ;
19
20
20
21
import java .util .ArrayList ;
21
- import java .util .Arrays ;
22
+ import java .util .Collections ;
22
23
import java .util .List ;
23
24
import java .util .Optional ;
24
25
import java .util .stream .Collectors ;
54
55
* @author Tobias Trelle
55
56
* @author Oliver Gierke
56
57
* @author Christoph Strobl
58
+ * @author Mark Paluch
57
59
* @since 1.9
58
60
*/
59
61
class DefaultBulkOperations implements BulkOperations {
60
62
61
63
private final MongoOperations mongoOperations ;
62
64
private final String collectionName ;
63
65
private final BulkOperationContext bulkOperationContext ;
66
+ private final List <WriteModel <Document >> models = new ArrayList <>();
64
67
65
- private WriteConcernResolver writeConcernResolver ;
66
68
private PersistenceExceptionTranslator exceptionTranslator ;
67
69
private WriteConcern defaultWriteConcern ;
68
70
69
71
private BulkWriteOptions bulkOptions ;
70
72
71
- List <WriteModel <Document >> models = new ArrayList <>();
72
-
73
73
/**
74
74
* Creates a new {@link DefaultBulkOperations} for the given {@link MongoOperations}, collection name and
75
75
* {@link BulkOperationContext}.
@@ -90,7 +90,7 @@ class DefaultBulkOperations implements BulkOperations {
90
90
this .collectionName = collectionName ;
91
91
this .bulkOperationContext = bulkOperationContext ;
92
92
this .exceptionTranslator = new MongoExceptionTranslator ();
93
- this .bulkOptions = initBulkOperation ( );
93
+ this .bulkOptions = getBulkWriteOptions ( bulkOperationContext . getBulkMode () );
94
94
}
95
95
96
96
/**
@@ -102,22 +102,12 @@ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTrans
102
102
this .exceptionTranslator = exceptionTranslator == null ? new MongoExceptionTranslator () : exceptionTranslator ;
103
103
}
104
104
105
- /**
106
- * Configures the {@link WriteConcernResolver} to be used. Defaults to {@link DefaultWriteConcernResolver}.
107
- *
108
- * @param writeConcernResolver can be {@literal null}.
109
- */
110
- public void setWriteConcernResolver (WriteConcernResolver writeConcernResolver ) {
111
- this .writeConcernResolver = writeConcernResolver == null ? DefaultWriteConcernResolver .INSTANCE
112
- : writeConcernResolver ;
113
- }
114
-
115
105
/**
116
106
* Configures the default {@link WriteConcern} to be used. Defaults to {@literal null}.
117
107
*
118
108
* @param defaultWriteConcern can be {@literal null}.
119
109
*/
120
- public void setDefaultWriteConcern (WriteConcern defaultWriteConcern ) {
110
+ void setDefaultWriteConcern (WriteConcern defaultWriteConcern ) {
121
111
this .defaultWriteConcern = defaultWriteConcern ;
122
112
}
123
113
@@ -140,6 +130,7 @@ public BulkOperations insert(Object document) {
140
130
mongoOperations .getConverter ().write (document , sink );
141
131
142
132
models .add (new InsertOneModel <>(sink ));
133
+
143
134
return this ;
144
135
}
145
136
@@ -152,9 +143,7 @@ public BulkOperations insert(List<? extends Object> documents) {
152
143
153
144
Assert .notNull (documents , "Documents must not be null!" );
154
145
155
- for (Object document : documents ) {
156
- insert (document );
157
- }
146
+ documents .forEach (this ::insert );
158
147
159
148
return this ;
160
149
}
@@ -170,7 +159,7 @@ public BulkOperations updateOne(Query query, Update update) {
170
159
Assert .notNull (query , "Query must not be null!" );
171
160
Assert .notNull (update , "Update must not be null!" );
172
161
173
- return updateOne (Arrays . asList (Pair .of (query , update )));
162
+ return updateOne (Collections . singletonList (Pair .of (query , update )));
174
163
}
175
164
176
165
/*
@@ -200,7 +189,7 @@ public BulkOperations updateMulti(Query query, Update update) {
200
189
Assert .notNull (query , "Query must not be null!" );
201
190
Assert .notNull (update , "Update must not be null!" );
202
191
203
- return updateMulti (Arrays . asList (Pair .of (query , update )));
192
+ return updateMulti (Collections . singletonList (Pair .of (query , update )));
204
193
}
205
194
206
195
/*
@@ -254,7 +243,8 @@ public BulkOperations remove(Query query) {
254
243
DeleteOptions deleteOptions = new DeleteOptions ();
255
244
query .getCollation ().map (Collation ::toMongoCollation ).ifPresent (deleteOptions ::collation );
256
245
257
- models .add (new DeleteManyModel (query .getQueryObject (), deleteOptions ));
246
+ models .add (new DeleteManyModel <>(query .getQueryObject (), deleteOptions ));
247
+
258
248
return this ;
259
249
}
260
250
@@ -296,13 +286,13 @@ public com.mongodb.bulk.BulkWriteResult execute() {
296
286
throw toThrow == null ? o_O : toThrow ;
297
287
298
288
} finally {
299
- this .bulkOptions = initBulkOperation ( );
289
+ this .bulkOptions = getBulkWriteOptions ( bulkOperationContext . getBulkMode () );
300
290
}
301
291
}
302
292
303
293
/**
304
294
* Performs update and upsert bulk operations.
305
- *
295
+ *
306
296
* @param query the {@link Query} to determine documents to update.
307
297
* @param update the {@link Update} to perform, must not be {@literal null}.
308
298
* @param upsert whether to upsert.
@@ -323,20 +313,8 @@ private BulkOperations update(Query query, Update update, boolean upsert, boolea
323
313
} else {
324
314
models .add (new UpdateOneModel <>(query .getQueryObject (), update .getUpdateObject (), options ));
325
315
}
326
- return this ;
327
- }
328
-
329
- private final BulkWriteOptions initBulkOperation () {
330
316
331
- BulkWriteOptions options = new BulkWriteOptions ();
332
-
333
- switch (bulkOperationContext .getBulkMode ()) {
334
- case ORDERED :
335
- return options .ordered (true );
336
- case UNORDERED :
337
- return options .ordered (false );
338
- }
339
- throw new IllegalStateException ("BulkMode was null!" );
317
+ return this ;
340
318
}
341
319
342
320
private WriteModel <Document > mapWriteModel (WriteModel <Document > writeModel ) {
@@ -345,30 +323,30 @@ private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) {
345
323
346
324
UpdateOneModel <Document > model = (UpdateOneModel <Document >) writeModel ;
347
325
348
- return new UpdateOneModel (getMappedQuery (model .getFilter ()), getMappedUpdate (model .getUpdate ()),
326
+ return new UpdateOneModel <> (getMappedQuery (model .getFilter ()), getMappedUpdate (model .getUpdate ()),
349
327
model .getOptions ());
350
328
}
351
329
352
330
if (writeModel instanceof UpdateManyModel ) {
353
331
354
332
UpdateManyModel <Document > model = (UpdateManyModel <Document >) writeModel ;
355
333
356
- return new UpdateManyModel (getMappedQuery (model .getFilter ()), getMappedUpdate (model .getUpdate ()),
334
+ return new UpdateManyModel <> (getMappedQuery (model .getFilter ()), getMappedUpdate (model .getUpdate ()),
357
335
model .getOptions ());
358
336
}
359
337
360
338
if (writeModel instanceof DeleteOneModel ) {
361
339
362
340
DeleteOneModel <Document > model = (DeleteOneModel <Document >) writeModel ;
363
341
364
- return new DeleteOneModel (getMappedQuery (model .getFilter ()), model .getOptions ());
342
+ return new DeleteOneModel <> (getMappedQuery (model .getFilter ()), model .getOptions ());
365
343
}
366
344
367
345
if (writeModel instanceof DeleteManyModel ) {
368
346
369
347
DeleteManyModel <Document > model = (DeleteManyModel <Document >) writeModel ;
370
348
371
- return new DeleteManyModel (getMappedQuery (model .getFilter ()), model .getOptions ());
349
+ return new DeleteManyModel <> (getMappedQuery (model .getFilter ()), model .getOptions ());
372
350
}
373
351
374
352
return writeModel ;
@@ -382,6 +360,20 @@ private Bson getMappedQuery(Bson query) {
382
360
return bulkOperationContext .getQueryMapper ().getMappedObject (query , bulkOperationContext .getEntity ());
383
361
}
384
362
363
+ private static BulkWriteOptions getBulkWriteOptions (BulkMode bulkMode ) {
364
+
365
+ BulkWriteOptions options = new BulkWriteOptions ();
366
+
367
+ switch (bulkMode ) {
368
+ case ORDERED :
369
+ return options .ordered (true );
370
+ case UNORDERED :
371
+ return options .ordered (false );
372
+ }
373
+
374
+ throw new IllegalStateException ("BulkMode was null!" );
375
+ }
376
+
385
377
/**
386
378
* {@link BulkOperationContext} holds information about
387
379
* {@link org.springframework.data.mongodb.core.BulkOperations.BulkMode} the entity in use as well as references to
@@ -390,12 +382,12 @@ private Bson getMappedQuery(Bson query) {
390
382
* @author Christoph Strobl
391
383
* @since 2.0
392
384
*/
393
- @ Data
385
+ @ Value
394
386
static class BulkOperationContext {
395
387
396
- final BulkMode bulkMode ;
397
- final Optional <? extends MongoPersistentEntity <?>> entity ;
398
- final QueryMapper queryMapper ;
399
- final UpdateMapper updateMapper ;
388
+ @ NonNull BulkMode bulkMode ;
389
+ @ NonNull Optional <? extends MongoPersistentEntity <?>> entity ;
390
+ @ NonNull QueryMapper queryMapper ;
391
+ @ NonNull UpdateMapper updateMapper ;
400
392
}
401
393
}
0 commit comments