Skip to content

Commit 5257d60

Browse files
committed
Added Drop and Other ChangeStream operation types
JAVA-2939 JAVA-2919
1 parent d4667e2 commit 5257d60

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

driver-core/src/main/com/mongodb/client/model/changestream/OperationType.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.mongodb.client.model.changestream;
1818

19-
import static java.lang.String.format;
20-
2119
/**
2220
* The {@code $changeStream} operation type.
2321
*
@@ -48,7 +46,24 @@ public enum OperationType {
4846
/**
4947
* The invalidate operation type
5048
*/
51-
INVALIDATE("invalidate");
49+
INVALIDATE("invalidate"),
50+
51+
/**
52+
* The drop operation type
53+
*
54+
* @since 3.8.2
55+
*/
56+
DROP("drop"),
57+
58+
/**
59+
* The other operation type.
60+
*
61+
* <p>A placeholder for newer operation types issued by the server.
62+
* Users encountering OTHER operation types are advised to update the driver to get the actual operation type.</p>
63+
*
64+
* @since 3.8.2
65+
*/
66+
OTHER("other");
5267

5368
private final String value;
5469
OperationType(final String operationTypeName) {
@@ -76,7 +91,7 @@ public static OperationType fromString(final String operationTypeName) {
7691
}
7792
}
7893
}
79-
throw new IllegalArgumentException(format("'%s' is not a valid OperationType", operationTypeName));
94+
return OTHER;
8095
}
8196

8297
@Override

driver-core/src/test/functional/com/mongodb/operation/ChangeStreamOperationSpecification.groovy

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,35 @@ class ChangeStreamOperationSpecification extends OperationFunctionalSpecificatio
303303
waitForLastRelease(getCluster())
304304
}
305305

306+
@IgnoreIf({ !serverVersionAtLeast([4, 0, 1]) })
307+
def 'should decode drop to ChangeStreamDocument '() {
308+
given:
309+
def helper = getHelper()
310+
311+
def pipeline = [BsonDocument.parse('{$match: {operationType: "drop"}}')]
312+
def operation = new ChangeStreamOperation<BsonDocument>(helper.getNamespace(), FullDocument.UPDATE_LOOKUP, pipeline,
313+
ChangeStreamDocument.createCodec(BsonDocument, fromProviders(new BsonValueCodecProvider(), new ValueCodecProvider())))
314+
helper.insertDocuments(BsonDocument.parse('{ _id : 2, x : 2, y : 3 }'))
315+
316+
when:
317+
def cursor = execute(operation, false)
318+
helper.drop()
319+
ChangeStreamDocument<BsonDocument> next = next(cursor, false).get(0)
320+
321+
then:
322+
next.getResumeToken() != null
323+
next.getDocumentKey() == null
324+
next.getFullDocument() == null
325+
next.getNamespace() == helper.getNamespace()
326+
next.getOperationType() == OperationType.DROP
327+
next.getUpdateDescription() == null
328+
329+
cleanup:
330+
cursor?.close()
331+
waitForLastRelease(getCluster())
332+
}
333+
334+
306335
def 'should throw if the _id field is projected out'() {
307336
given:
308337
def helper = getHelper()

driver-core/src/test/unit/com/mongodb/client/model/changestream/OperationTypeSpecification.groovy

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ class OperationTypeSpecification extends Specification {
2727
where:
2828
operationType | expectedString
2929
OperationType.DELETE | 'delete'
30+
OperationType.DROP | 'drop'
3031
OperationType.INSERT | 'insert'
3132
OperationType.INVALIDATE | 'invalidate'
33+
OperationType.OTHER | 'other'
3234
OperationType.REPLACE | 'replace'
3335
OperationType.UPDATE | 'update'
3436
}
@@ -40,20 +42,19 @@ class OperationTypeSpecification extends Specification {
4042
where:
4143
operationType | stringValue
4244
OperationType.DELETE | 'delete'
45+
OperationType.DROP | 'drop'
4346
OperationType.INSERT | 'insert'
4447
OperationType.INVALIDATE | 'invalidate'
48+
OperationType.OTHER | 'other'
4549
OperationType.REPLACE | 'replace'
4650
OperationType.UPDATE | 'update'
4751
}
4852

49-
def 'should throw an illegal argument exception for invalid values'() {
50-
when:
51-
OperationType.fromString(stringValue)
52-
53-
then:
54-
thrown(IllegalArgumentException)
53+
def 'should return UNKNOWN for new / unknown values'() {
54+
expect:
55+
OperationType.fromString(stringValue) == OperationType.OTHER
5556

5657
where:
57-
stringValue << [null, 'info']
58+
stringValue << [null, 'info', 'reIndex']
5859
}
5960
}

0 commit comments

Comments
 (0)