Skip to content

Commit 7862841

Browse files
committed
DATAMONGO-934 - Polishing.
Polished JavaDoc and implementation as well as tests. Extracted Tuple to Spring Data Commons. Moved exception translation into MongoExceptionTranslator. Changed implementation of DefaultBulkOperations to consider the WriteConcernResolver of the underlying MongoTemplate to avoid exposing the WriteConcern on execution. Original pull request: spring-projects#327. Related tickets: DATACMNS-790.
1 parent fe6cbaa commit 7862841

File tree

9 files changed

+510
-360
lines changed

9 files changed

+510
-360
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/BulkOperationException.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,28 @@
2727
* Is thrown when errors occur during bulk operations.
2828
*
2929
* @author Tobias Trelle
30+
* @author Oliver Gierke
31+
* @since 1.9
3032
*/
3133
public class BulkOperationException extends DataAccessException {
34+
3235
private static final long serialVersionUID = 73929601661154421L;
36+
3337
private final List<BulkWriteError> errors;
3438
private final BulkWriteResult result;
35-
36-
public BulkOperationException(String msg, BulkWriteException e) {
37-
super(msg, e);
38-
this.errors = e.getWriteErrors();
39-
this.result = e.getWriteResult();
39+
40+
/**
41+
* Creates a new {@link BulkOperationException} with the given message and source {@link BulkWriteException}.
42+
*
43+
* @param message must not be {@literal null}.
44+
* @param source must not be {@literal null}.
45+
*/
46+
public BulkOperationException(String message, BulkWriteException source) {
47+
48+
super(message, source);
49+
50+
this.errors = source.getWriteErrors();
51+
this.result = source.getWriteResult();
4052
}
4153

4254
public List<BulkWriteError> getErrors() {
@@ -46,5 +58,4 @@ public List<BulkWriteError> getErrors() {
4658
public BulkWriteResult getResult() {
4759
return result;
4860
}
49-
5061
}

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

+42-64
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.data.mongodb.BulkOperationException;
2120
import org.springframework.data.mongodb.core.query.Query;
2221
import org.springframework.data.mongodb.core.query.Update;
23-
import org.springframework.data.mongodb.util.Tuple;
22+
import org.springframework.data.util.Tuple;
2423

2524
import com.mongodb.BulkWriteResult;
26-
import com.mongodb.WriteConcern;
2725

2826
/**
29-
* Bulk operations for insert/update/remove actions on a collection.
30-
* <p/>
31-
* These bulks operation are available since MongoDB 2.6 and make use of low level bulk commands on the protocol level.
32-
* <p/>
33-
* This interface defines a fluent API add multiple single operations or list of similar operations in sequence.
27+
* Bulk operations for insert/update/remove actions on a collection. These bulks operation are available since MongoDB
28+
* 2.6 and make use of low level bulk commands on the protocol level. This interface defines a fluent API to add
29+
* multiple single operations or list of similar operations in sequence which can then eventually be executed by calling
30+
* {@link #execute()}.
3431
*
3532
* @author Tobias Trelle
33+
* @author Oliver Gierke
34+
* @since 1.9
3635
*/
3736
public interface BulkOperations {
3837

39-
/** Mode for bulk operation. */
38+
/**
39+
* Mode for bulk operation.
40+
**/
4041
public enum BulkMode {
4142

4243
/** Perform bulk operations in sequence. The first error will cancel processing. */
@@ -45,44 +46,37 @@ public enum BulkMode {
4546
/** Perform bulk operations in parallel. Processing will continue on errors. */
4647
UNORDERED
4748
};
49+
4850
/**
4951
* Add a single insert to the bulk operation.
5052
*
51-
* @param documents List of documents to insert.
52-
*
53-
* @return The bulk operation.
54-
*
55-
* @throws BulkOperationException if an error occured during bulk processing.
53+
* @param documents the document to insert, must not be {@literal null}.
54+
* @return the current {@link BulkOperations} instance with the insert added, will never be {@literal null}.
5655
*/
57-
BulkOperations insert(Object documents);
58-
56+
BulkOperations insert(Object documents);
57+
5958
/**
6059
* Add a list of inserts to the bulk operation.
6160
*
62-
* @param documents List of documents to insert.
63-
*
64-
* @return The bulk operation.
65-
*
66-
* @throws BulkOperationException if an error occured during bulk processing.
61+
* @param documents List of documents to insert, must not be {@literal null}.
62+
* @return the current {@link BulkOperations} instance with the insert added, will never be {@literal null}.
6763
*/
6864
BulkOperations insert(List<? extends Object> documents);
6965

7066
/**
71-
* Add a single update to the bulk operation. For the update request, only the first matching document is updated.
67+
* Add a single update to the bulk operation. For the update request, only the first matching document is updated.
7268
*
73-
* @param query Update criteria.
74-
* @param update Update operation to perform.
75-
*
76-
* @return The bulk operation.
69+
* @param query update criteria, must not be {@literal null}.
70+
* @param update {@link Update} operation to perform, must not be {@literal null}.
71+
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
7772
*/
78-
BulkOperations updateOne(Query query, Update update);
79-
73+
BulkOperations updateOne(Query query, Update update);
74+
8075
/**
81-
* Add a list of updates to the bulk operation. For each update request, only the first matching document is updated.
76+
* Add a list of updates to the bulk operation. For each update request, only the first matching document is updated.
8277
*
8378
* @param updates Update operations to perform.
84-
*
85-
* @return The bulk operation.
79+
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
8680
*/
8781
BulkOperations updateOne(List<Tuple<Query, Update>> updates);
8882

@@ -91,17 +85,16 @@ public enum BulkMode {
9185
*
9286
* @param query Update criteria.
9387
* @param update Update operation to perform.
94-
*
95-
* @return The bulk operation.
88+
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
9689
*/
9790
BulkOperations updateMulti(Query query, Update update);
98-
91+
9992
/**
10093
* Add a list of updates to the bulk operation. For each update request, all matching documents are updated.
10194
*
10295
* @param updates Update operations to perform.
103-
*
10496
* @return The bulk operation.
97+
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
10598
*/
10699
BulkOperations updateMulti(List<Tuple<Query, Update>> updates);
107100

@@ -111,57 +104,42 @@ public enum BulkMode {
111104
*
112105
* @param query Update criteria.
113106
* @param update Update operation to perform.
114-
*
115107
* @return The bulk operation.
108+
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
116109
*/
117-
BulkOperations upsert(Query query, Update update);
118-
110+
BulkOperations upsert(Query query, Update update);
111+
119112
/**
120-
* Add a list of upserts to the bulk operation. An upsert is an update if the set of matching documents is not empty, else an
121-
* insert.
113+
* Add a list of upserts to the bulk operation. An upsert is an update if the set of matching documents is not empty,
114+
* else an insert.
122115
*
123116
* @param updates Updates/insert operations to perform.
124-
*
125117
* @return The bulk operation.
118+
* @return the current {@link BulkOperations} instance with the update added, will never be {@literal null}.
126119
*/
127120
BulkOperations upsert(List<Tuple<Query, Update>> updates);
128121

129122
/**
130123
* Add a single remove operation to the bulk operation.
131124
*
132-
* @param remove operations to perform.
133-
*
134-
* @return The bulk operation.
125+
* @param remove the {@link Query} to select the documents to be removed, must not be {@literal null}.
126+
* @return the current {@link BulkOperations} instance with the removal added, will never be {@literal null}.
135127
*/
136-
BulkOperations remove(Query remove);
137-
128+
BulkOperations remove(Query remove);
129+
138130
/**
139131
* Add a list of remove operations to the bulk operation.
140132
*
141-
* @param remove operations to perform.
142-
*
143-
* @return The bulk operation.
133+
* @param removes the remove operations to perform, must not be {@literal null}.
134+
* @return the current {@link BulkOperations} instance with the removal added, will never be {@literal null}.
144135
*/
145136
BulkOperations remove(List<Query> removes);
146137

147138
/**
148139
* Execute all bulk operations using the default write concern.
149140
*
150141
* @return Result of the bulk operation providing counters for inserts/updates etc.
151-
*
152-
* @throws BulkOperationException if errors occur during the exection of the bulk operations.
142+
* @throws {@link BulkOperationException} if an error occurred during bulk processing.
153143
*/
154-
BulkWriteResult executeBulk();
155-
156-
/**
157-
* Execute all bulk operations using the given write concern.
158-
*
159-
* @param writeConcern Write concern to use.
160-
*
161-
* @return Result of the bulk operation providing counters for inserts/updates etc.
162-
*
163-
* @throws BulkOperationException if errors occur during the exection of the bulk operations.
164-
*/
165-
BulkWriteResult executeBulk(WriteConcern writeConcern);
166-
144+
BulkWriteResult execute();
167145
}

0 commit comments

Comments
 (0)