Skip to content

Commit fe6cbaa

Browse files
ttrelleodrotbohm
authored andcommitted
DATAMONGO-934 - Added support for bulk operations.
Introduced BulkOperations that can be obtained via MongoOperations, register operations to be eventually executed in a bulk. Original pull request: spring-projects#327.
1 parent 9ef1fc7 commit fe6cbaa

File tree

7 files changed

+815
-0
lines changed

7 files changed

+815
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb;
17+
18+
import java.util.List;
19+
20+
import org.springframework.dao.DataAccessException;
21+
22+
import com.mongodb.BulkWriteError;
23+
import com.mongodb.BulkWriteException;
24+
import com.mongodb.BulkWriteResult;
25+
26+
/**
27+
* Is thrown when errors occur during bulk operations.
28+
*
29+
* @author Tobias Trelle
30+
*/
31+
public class BulkOperationException extends DataAccessException {
32+
private static final long serialVersionUID = 73929601661154421L;
33+
private final List<BulkWriteError> errors;
34+
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();
40+
}
41+
42+
public List<BulkWriteError> getErrors() {
43+
return errors;
44+
}
45+
46+
public BulkWriteResult getResult() {
47+
return result;
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.mongodb.BulkOperationException;
21+
import org.springframework.data.mongodb.core.query.Query;
22+
import org.springframework.data.mongodb.core.query.Update;
23+
import org.springframework.data.mongodb.util.Tuple;
24+
25+
import com.mongodb.BulkWriteResult;
26+
import com.mongodb.WriteConcern;
27+
28+
/**
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.
34+
*
35+
* @author Tobias Trelle
36+
*/
37+
public interface BulkOperations {
38+
39+
/** Mode for bulk operation. */
40+
public enum BulkMode {
41+
42+
/** Perform bulk operations in sequence. The first error will cancel processing. */
43+
ORDERED,
44+
45+
/** Perform bulk operations in parallel. Processing will continue on errors. */
46+
UNORDERED
47+
};
48+
/**
49+
* Add a single insert to the bulk operation.
50+
*
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.
56+
*/
57+
BulkOperations insert(Object documents);
58+
59+
/**
60+
* Add a list of inserts to the bulk operation.
61+
*
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.
67+
*/
68+
BulkOperations insert(List<? extends Object> documents);
69+
70+
/**
71+
* Add a single update to the bulk operation. For the update request, only the first matching document is updated.
72+
*
73+
* @param query Update criteria.
74+
* @param update Update operation to perform.
75+
*
76+
* @return The bulk operation.
77+
*/
78+
BulkOperations updateOne(Query query, Update update);
79+
80+
/**
81+
* Add a list of updates to the bulk operation. For each update request, only the first matching document is updated.
82+
*
83+
* @param updates Update operations to perform.
84+
*
85+
* @return The bulk operation.
86+
*/
87+
BulkOperations updateOne(List<Tuple<Query, Update>> updates);
88+
89+
/**
90+
* Add a single update to the bulk operation. For the update request, all matching documents are updated.
91+
*
92+
* @param query Update criteria.
93+
* @param update Update operation to perform.
94+
*
95+
* @return The bulk operation.
96+
*/
97+
BulkOperations updateMulti(Query query, Update update);
98+
99+
/**
100+
* Add a list of updates to the bulk operation. For each update request, all matching documents are updated.
101+
*
102+
* @param updates Update operations to perform.
103+
*
104+
* @return The bulk operation.
105+
*/
106+
BulkOperations updateMulti(List<Tuple<Query, Update>> updates);
107+
108+
/**
109+
* Add a single upsert to the bulk operation. An upsert is an update if the set of matching documents is not empty,
110+
* else an insert.
111+
*
112+
* @param query Update criteria.
113+
* @param update Update operation to perform.
114+
*
115+
* @return The bulk operation.
116+
*/
117+
BulkOperations upsert(Query query, Update update);
118+
119+
/**
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.
122+
*
123+
* @param updates Updates/insert operations to perform.
124+
*
125+
* @return The bulk operation.
126+
*/
127+
BulkOperations upsert(List<Tuple<Query, Update>> updates);
128+
129+
/**
130+
* Add a single remove operation to the bulk operation.
131+
*
132+
* @param remove operations to perform.
133+
*
134+
* @return The bulk operation.
135+
*/
136+
BulkOperations remove(Query remove);
137+
138+
/**
139+
* Add a list of remove operations to the bulk operation.
140+
*
141+
* @param remove operations to perform.
142+
*
143+
* @return The bulk operation.
144+
*/
145+
BulkOperations remove(List<Query> removes);
146+
147+
/**
148+
* Execute all bulk operations using the default write concern.
149+
*
150+
* @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.
153+
*/
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+
167+
}

0 commit comments

Comments
 (0)