Skip to content

Commit 07180e4

Browse files
committed
JAVA-1040: Make BulkOperation throw IllegalStateException if executed more than once or with an empty list up updates.
1 parent 923dfeb commit 07180e4

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

src/main/com/mongodb/BulkWriteOperation.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import static org.bson.util.Assertions.isTrue;
23+
2224
/**
2325
* A bulk write operation.
2426
*
@@ -28,6 +30,7 @@ public class BulkWriteOperation {
2830
private final boolean ordered;
2931
private final DBCollection collection;
3032
private final List<WriteRequest> requests = new ArrayList<WriteRequest>();
33+
private boolean closed;
3134

3235
BulkWriteOperation(final boolean ordered, final DBCollection collection) {
3336
this.ordered = ordered;
@@ -52,6 +55,7 @@ public boolean isOrdered() {
5255
* @param document the document to insert
5356
*/
5457
public void insert(final DBObject document) {
58+
isTrue("already executed", !closed);
5559
addRequest(new InsertRequest(document));
5660
}
5761

@@ -62,6 +66,7 @@ public void insert(final DBObject document) {
6266
* @return a builder for a single write request
6367
*/
6468
public BulkWriteRequestBuilder find(final DBObject query) {
69+
isTrue("already executed", !closed);
6570
return new BulkWriteRequestBuilder(this, query);
6671
}
6772

@@ -73,7 +78,13 @@ public BulkWriteRequestBuilder find(final DBObject query) {
7378
* @throws com.mongodb.MongoException
7479
*/
7580
public BulkWriteResult execute() {
76-
return collection.executeBulkWriteOperation(ordered, requests);
81+
isTrue("already executed", !closed);
82+
83+
try {
84+
return collection.executeBulkWriteOperation(ordered, requests);
85+
} finally {
86+
closed = true;
87+
}
7788
}
7889

7990
/**
@@ -86,10 +97,17 @@ public BulkWriteResult execute() {
8697
* @throws com.mongodb.MongoException
8798
*/
8899
public BulkWriteResult execute(final WriteConcern writeConcern) {
89-
return collection.executeBulkWriteOperation(ordered, requests, writeConcern);
100+
isTrue("already executed", !closed);
101+
102+
try {
103+
return collection.executeBulkWriteOperation(ordered, requests, writeConcern);
104+
} finally {
105+
closed = true;
106+
}
90107
}
91108

92109
void addRequest(final WriteRequest request) {
110+
isTrue("already executed", !closed);
93111
requests.add(request);
94112
}
95113

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static com.mongodb.WriteRequest.Type.UPDATE;
4343
import static java.lang.String.format;
4444
import static java.util.Arrays.asList;
45+
import static org.bson.util.Assertions.isTrue;
4546

4647
class DBCollectionImpl extends DBCollection {
4748
private final DBApiLayer db;
@@ -121,6 +122,8 @@ public List<Cursor> parallelScan(final ParallelScanOptions options) {
121122
@Override
122123
BulkWriteResult executeBulkWriteOperation(final boolean ordered, final List<WriteRequest> writeRequests,
123124
final WriteConcern writeConcern, DBEncoder encoder) {
125+
isTrue("no operations", !writeRequests.isEmpty());
126+
124127
if (writeConcern == null) {
125128
throw new IllegalArgumentException("Write concern can not be null");
126129
}

src/test/com/mongodb/BulkWriteOperationSpecification.groovy

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,94 @@ class BulkWriteOperationSpecification extends FunctionalSpecification {
287287
ex.writeErrors[1].code == 11000
288288
}
289289

290+
def 'execute should throw IllegalStateException when already executed'() {
291+
given:
292+
def builder = collection.initializeOrderedBulkOperation();
293+
builder.insert(new BasicDBObject('_id', 1))
294+
builder.execute()
295+
296+
when:
297+
builder.execute()
298+
299+
then:
300+
thrown(IllegalStateException)
301+
}
302+
303+
def 'execute with write concern should throw IllegalStateException when already executed'() {
304+
given:
305+
def builder = collection.initializeOrderedBulkOperation();
306+
builder.insert(new BasicDBObject('_id', 1))
307+
builder.execute()
308+
309+
when:
310+
builder.execute(WriteConcern.ACKNOWLEDGED)
311+
312+
then:
313+
thrown(IllegalStateException)
314+
}
315+
316+
def 'insert should throw IllegalStateException when already executed'() {
317+
given:
318+
def builder = collection.initializeOrderedBulkOperation();
319+
builder.insert(new BasicDBObject('_id', 1))
320+
builder.execute()
321+
322+
when:
323+
builder.insert(new BasicDBObject())
324+
325+
then:
326+
thrown(IllegalStateException)
327+
}
328+
329+
def 'find should throw IllegalStateException when already executed'() {
330+
given:
331+
def builder = collection.initializeOrderedBulkOperation();
332+
builder.insert(new BasicDBObject('_id', 1))
333+
builder.execute()
334+
335+
when:
336+
builder.find(new BasicDBObject());
337+
338+
then:
339+
thrown(IllegalStateException)
340+
}
341+
342+
// just need to check one case here, since the others are checked above
343+
def 'should throw IllegalStateException when already executed with write concern'() {
344+
given:
345+
def builder = collection.initializeOrderedBulkOperation();
346+
builder.insert(new BasicDBObject('_id', 1))
347+
builder.execute(WriteConcern.ACKNOWLEDGED)
348+
349+
when:
350+
builder.execute()
351+
352+
then:
353+
thrown(IllegalStateException)
354+
}
355+
356+
def 'should throw IllegalStateException when executing an empty bulk operation'() {
357+
given:
358+
def builder = collection.initializeOrderedBulkOperation();
359+
360+
when:
361+
builder.execute()
362+
363+
then:
364+
thrown(IllegalStateException)
365+
}
366+
367+
def 'should throw IllegalStateException when executing an empty bulk operation with a write concern'() {
368+
given:
369+
def builder = collection.initializeOrderedBulkOperation();
370+
371+
when:
372+
builder.execute(WriteConcern.ACKNOWLEDGED)
373+
374+
then:
375+
thrown(IllegalStateException)
376+
}
377+
290378
private static void addWritesToBuilder(BulkWriteOperation builder) {
291379
builder.find(new BasicDBObject('_id', 1)).updateOne(new BasicDBObject('$set', new BasicDBObject('x', 2)))
292380
builder.find(new BasicDBObject('_id', 2)).updateOne(new BasicDBObject('$set', new BasicDBObject('x', 3)))
@@ -307,4 +395,5 @@ class BulkWriteOperationSpecification extends FunctionalSpecification {
307395
new BasicDBObject('_id', 6)
308396
]
309397
}
398+
310399
}

0 commit comments

Comments
 (0)