Skip to content

Commit 34986df

Browse files
committed
DATAMONGO-1678 - Polishing.
Use Lombok's Value annotation for immutable value objects. Use IllegalArgumentException for NonNull validation exceptions. Introduce missing generics. Use static methods where possible. Remove unused WriteConcernResolver. Trim whitespaces, formatting. Original pull request: #472.
1 parent c338343 commit 34986df

File tree

5 files changed

+67
-79
lines changed

5 files changed

+67
-79
lines changed

lombok.config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lombok.nonNull.exceptionType = IllegalArgumentException
2+
lombok.log.fieldName = LOG

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
* 2.6 and make use of low level bulk commands on the protocol level. This interface defines a fluent API to add
2929
* multiple single operations or list of similar operations in sequence which can then eventually be executed by calling
3030
* {@link #execute()}.
31-
*
31+
*
3232
* @author Tobias Trelle
3333
* @author Oliver Gierke
3434
* @since 1.9
@@ -49,23 +49,23 @@ enum BulkMode {
4949

5050
/**
5151
* Add a single insert to the bulk operation.
52-
*
52+
*
5353
* @param documents the document to insert, must not be {@literal null}.
5454
* @return the current {@link BulkOperations} instance with the insert added, will never be {@literal null}.
5555
*/
5656
BulkOperations insert(Object documents);
5757

5858
/**
5959
* Add a list of inserts to the bulk operation.
60-
*
60+
*
6161
* @param documents List of documents to insert, must not be {@literal null}.
6262
* @return the current {@link BulkOperations} instance with the insert added, will never be {@literal null}.
6363
*/
6464
BulkOperations insert(List<? extends Object> documents);
6565

6666
/**
6767
* Add a single update to the bulk operation. For the update request, only the first matching document is updated.
68-
*
68+
*
6969
* @param query update criteria, must not be {@literal null}.
7070
* @param update {@link Update} operation to perform, must not be {@literal null}.
7171
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
@@ -74,15 +74,15 @@ enum BulkMode {
7474

7575
/**
7676
* Add a list of updates to the bulk operation. For each update request, only the first matching document is updated.
77-
*
77+
*
7878
* @param updates Update operations to perform.
7979
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
8080
*/
8181
BulkOperations updateOne(List<Pair<Query, Update>> updates);
8282

8383
/**
8484
* Add a single update to the bulk operation. For the update request, all matching documents are updated.
85-
*
85+
*
8686
* @param query Update criteria.
8787
* @param update Update operation to perform.
8888
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
@@ -91,7 +91,7 @@ enum BulkMode {
9191

9292
/**
9393
* Add a list of updates to the bulk operation. For each update request, all matching documents are updated.
94-
*
94+
*
9595
* @param updates Update operations to perform.
9696
* @return The bulk operation.
9797
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
@@ -101,7 +101,7 @@ enum BulkMode {
101101
/**
102102
* Add a single upsert to the bulk operation. An upsert is an update if the set of matching documents is not empty,
103103
* else an insert.
104-
*
104+
*
105105
* @param query Update criteria.
106106
* @param update Update operation to perform.
107107
* @return The bulk operation.
@@ -112,7 +112,7 @@ enum BulkMode {
112112
/**
113113
* Add a list of upserts to the bulk operation. An upsert is an update if the set of matching documents is not empty,
114114
* else an insert.
115-
*
115+
*
116116
* @param updates Updates/insert operations to perform.
117117
* @return The bulk operation.
118118
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
@@ -121,25 +121,25 @@ enum BulkMode {
121121

122122
/**
123123
* Add a single remove operation to the bulk operation.
124-
*
124+
*
125125
* @param remove the {@link Query} to select the documents to be removed, must not be {@literal null}.
126126
* @return the current {@link BulkOperations} instance with the removal added, will never be {@literal null}.
127127
*/
128128
BulkOperations remove(Query remove);
129129

130130
/**
131131
* Add a list of remove operations to the bulk operation.
132-
*
132+
*
133133
* @param removes the remove operations to perform, must not be {@literal null}.
134134
* @return the current {@link BulkOperations} instance with the removal added, will never be {@literal null}.
135135
*/
136136
BulkOperations remove(List<Query> removes);
137137

138138
/**
139139
* Execute all bulk operations using the default write concern.
140-
*
140+
*
141141
* @return Result of the bulk operation providing counters for inserts/updates etc.
142-
* @throws {@link BulkOperationException} if an error occurred during bulk processing.
142+
* @throws org.springframework.data.mongodb.BulkOperationException if an error occurred during bulk processing.
143143
*/
144144
BulkWriteResult execute();
145145
}

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

+39-47
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import lombok.Data;
18+
import lombok.NonNull;
19+
import lombok.Value;
1920

2021
import java.util.ArrayList;
21-
import java.util.Arrays;
22+
import java.util.Collections;
2223
import java.util.List;
2324
import java.util.Optional;
2425
import java.util.stream.Collectors;
@@ -54,22 +55,21 @@
5455
* @author Tobias Trelle
5556
* @author Oliver Gierke
5657
* @author Christoph Strobl
58+
* @author Mark Paluch
5759
* @since 1.9
5860
*/
5961
class DefaultBulkOperations implements BulkOperations {
6062

6163
private final MongoOperations mongoOperations;
6264
private final String collectionName;
6365
private final BulkOperationContext bulkOperationContext;
66+
private final List<WriteModel<Document>> models = new ArrayList<>();
6467

65-
private WriteConcernResolver writeConcernResolver;
6668
private PersistenceExceptionTranslator exceptionTranslator;
6769
private WriteConcern defaultWriteConcern;
6870

6971
private BulkWriteOptions bulkOptions;
7072

71-
List<WriteModel<Document>> models = new ArrayList<>();
72-
7373
/**
7474
* Creates a new {@link DefaultBulkOperations} for the given {@link MongoOperations}, collection name and
7575
* {@link BulkOperationContext}.
@@ -90,7 +90,7 @@ class DefaultBulkOperations implements BulkOperations {
9090
this.collectionName = collectionName;
9191
this.bulkOperationContext = bulkOperationContext;
9292
this.exceptionTranslator = new MongoExceptionTranslator();
93-
this.bulkOptions = initBulkOperation();
93+
this.bulkOptions = getBulkWriteOptions(bulkOperationContext.getBulkMode());
9494
}
9595

9696
/**
@@ -102,22 +102,12 @@ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTrans
102102
this.exceptionTranslator = exceptionTranslator == null ? new MongoExceptionTranslator() : exceptionTranslator;
103103
}
104104

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-
115105
/**
116106
* Configures the default {@link WriteConcern} to be used. Defaults to {@literal null}.
117107
*
118108
* @param defaultWriteConcern can be {@literal null}.
119109
*/
120-
public void setDefaultWriteConcern(WriteConcern defaultWriteConcern) {
110+
void setDefaultWriteConcern(WriteConcern defaultWriteConcern) {
121111
this.defaultWriteConcern = defaultWriteConcern;
122112
}
123113

@@ -140,6 +130,7 @@ public BulkOperations insert(Object document) {
140130
mongoOperations.getConverter().write(document, sink);
141131

142132
models.add(new InsertOneModel<>(sink));
133+
143134
return this;
144135
}
145136

@@ -152,9 +143,7 @@ public BulkOperations insert(List<? extends Object> documents) {
152143

153144
Assert.notNull(documents, "Documents must not be null!");
154145

155-
for (Object document : documents) {
156-
insert(document);
157-
}
146+
documents.forEach(this::insert);
158147

159148
return this;
160149
}
@@ -170,7 +159,7 @@ public BulkOperations updateOne(Query query, Update update) {
170159
Assert.notNull(query, "Query must not be null!");
171160
Assert.notNull(update, "Update must not be null!");
172161

173-
return updateOne(Arrays.asList(Pair.of(query, update)));
162+
return updateOne(Collections.singletonList(Pair.of(query, update)));
174163
}
175164

176165
/*
@@ -200,7 +189,7 @@ public BulkOperations updateMulti(Query query, Update update) {
200189
Assert.notNull(query, "Query must not be null!");
201190
Assert.notNull(update, "Update must not be null!");
202191

203-
return updateMulti(Arrays.asList(Pair.of(query, update)));
192+
return updateMulti(Collections.singletonList(Pair.of(query, update)));
204193
}
205194

206195
/*
@@ -254,7 +243,8 @@ public BulkOperations remove(Query query) {
254243
DeleteOptions deleteOptions = new DeleteOptions();
255244
query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation);
256245

257-
models.add(new DeleteManyModel(query.getQueryObject(), deleteOptions));
246+
models.add(new DeleteManyModel<>(query.getQueryObject(), deleteOptions));
247+
258248
return this;
259249
}
260250

@@ -296,13 +286,13 @@ public com.mongodb.bulk.BulkWriteResult execute() {
296286
throw toThrow == null ? o_O : toThrow;
297287

298288
} finally {
299-
this.bulkOptions = initBulkOperation();
289+
this.bulkOptions = getBulkWriteOptions(bulkOperationContext.getBulkMode());
300290
}
301291
}
302292

303293
/**
304294
* Performs update and upsert bulk operations.
305-
*
295+
*
306296
* @param query the {@link Query} to determine documents to update.
307297
* @param update the {@link Update} to perform, must not be {@literal null}.
308298
* @param upsert whether to upsert.
@@ -323,20 +313,8 @@ private BulkOperations update(Query query, Update update, boolean upsert, boolea
323313
} else {
324314
models.add(new UpdateOneModel<>(query.getQueryObject(), update.getUpdateObject(), options));
325315
}
326-
return this;
327-
}
328-
329-
private final BulkWriteOptions initBulkOperation() {
330316

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;
340318
}
341319

342320
private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) {
@@ -345,30 +323,30 @@ private WriteModel<Document> mapWriteModel(WriteModel<Document> writeModel) {
345323

346324
UpdateOneModel<Document> model = (UpdateOneModel<Document>) writeModel;
347325

348-
return new UpdateOneModel(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()),
326+
return new UpdateOneModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()),
349327
model.getOptions());
350328
}
351329

352330
if (writeModel instanceof UpdateManyModel) {
353331

354332
UpdateManyModel<Document> model = (UpdateManyModel<Document>) writeModel;
355333

356-
return new UpdateManyModel(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()),
334+
return new UpdateManyModel<>(getMappedQuery(model.getFilter()), getMappedUpdate(model.getUpdate()),
357335
model.getOptions());
358336
}
359337

360338
if (writeModel instanceof DeleteOneModel) {
361339

362340
DeleteOneModel<Document> model = (DeleteOneModel<Document>) writeModel;
363341

364-
return new DeleteOneModel(getMappedQuery(model.getFilter()), model.getOptions());
342+
return new DeleteOneModel<>(getMappedQuery(model.getFilter()), model.getOptions());
365343
}
366344

367345
if (writeModel instanceof DeleteManyModel) {
368346

369347
DeleteManyModel<Document> model = (DeleteManyModel<Document>) writeModel;
370348

371-
return new DeleteManyModel(getMappedQuery(model.getFilter()), model.getOptions());
349+
return new DeleteManyModel<>(getMappedQuery(model.getFilter()), model.getOptions());
372350
}
373351

374352
return writeModel;
@@ -382,6 +360,20 @@ private Bson getMappedQuery(Bson query) {
382360
return bulkOperationContext.getQueryMapper().getMappedObject(query, bulkOperationContext.getEntity());
383361
}
384362

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+
385377
/**
386378
* {@link BulkOperationContext} holds information about
387379
* {@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) {
390382
* @author Christoph Strobl
391383
* @since 2.0
392384
*/
393-
@Data
385+
@Value
394386
static class BulkOperationContext {
395387

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;
400392
}
401393
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ public BulkOperations bulkOps(BulkMode mode, Class<?> entityType, String collect
562562
new BulkOperationContext(mode, getPersistentEntity(entityType), queryMapper, updateMapper));
563563

564564
operations.setExceptionTranslator(exceptionTranslator);
565-
operations.setWriteConcernResolver(writeConcernResolver);
566565
operations.setDefaultWriteConcern(writeConcern);
567566

568567
return operations;

0 commit comments

Comments
 (0)