17
17
package com .mongodb .client .model .changestream ;
18
18
19
19
import com .mongodb .MongoNamespace ;
20
- import com .mongodb .assertions .Assertions ;
21
20
import com .mongodb .lang .Nullable ;
22
21
import org .bson .BsonDocument ;
23
22
import org .bson .BsonInt64 ;
24
- import org .bson .BsonString ;
25
23
import org .bson .BsonTimestamp ;
26
24
import org .bson .codecs .Codec ;
27
25
import org .bson .codecs .configuration .CodecRegistry ;
30
28
import org .bson .codecs .pojo .annotations .BsonIgnore ;
31
29
import org .bson .codecs .pojo .annotations .BsonProperty ;
32
30
31
+ import java .util .Objects ;
32
+
33
33
/**
34
34
* Represents the {@code $changeStream} aggregation output document.
35
35
*
@@ -48,6 +48,9 @@ public final class ChangeStreamDocument<TDocument> {
48
48
private final TDocument fullDocument ;
49
49
private final BsonDocument documentKey ;
50
50
private final BsonTimestamp clusterTime ;
51
+ @ BsonProperty ("operationType" )
52
+ private final String operationTypeString ;
53
+ @ BsonIgnore
51
54
private final OperationType operationType ;
52
55
private final UpdateDescription updateDescription ;
53
56
private final BsonInt64 txnNumber ;
@@ -56,7 +59,7 @@ public final class ChangeStreamDocument<TDocument> {
56
59
/**
57
60
* Creates a new instance
58
61
*
59
- * @param operationType the operation type
62
+ * @param operationTypeString the operation type
60
63
* @param resumeToken the resume token
61
64
* @param namespaceDocument the BsonDocument representing the namespace
62
65
* @param destinationNamespaceDocument the BsonDocument representing the destinatation namespace
@@ -67,10 +70,10 @@ public final class ChangeStreamDocument<TDocument> {
67
70
* @param txnNumber the transaction number
68
71
* @param lsid the identifier for the session associated with the transaction
69
72
*
70
- * @since 3.11
73
+ * @since 4.6
71
74
*/
72
75
@ BsonCreator
73
- public ChangeStreamDocument (@ BsonProperty ("operationType" ) final OperationType operationType ,
76
+ public ChangeStreamDocument (@ BsonProperty ("operationType" ) final String operationTypeString ,
74
77
@ BsonProperty ("resumeToken" ) final BsonDocument resumeToken ,
75
78
@ Nullable @ BsonProperty ("ns" ) final BsonDocument namespaceDocument ,
76
79
@ Nullable @ BsonProperty ("to" ) final BsonDocument destinationNamespaceDocument ,
@@ -86,16 +89,42 @@ public ChangeStreamDocument(@BsonProperty("operationType") final OperationType o
86
89
this .documentKey = documentKey ;
87
90
this .fullDocument = fullDocument ;
88
91
this .clusterTime = clusterTime ;
89
- this .operationType = operationType ;
92
+ this .operationTypeString = operationTypeString ;
93
+ this .operationType = OperationType .fromString (operationTypeString );
90
94
this .updateDescription = updateDescription ;
91
95
this .txnNumber = txnNumber ;
92
96
this .lsid = lsid ;
93
97
}
94
98
95
- private static BsonDocument namespaceToDocument (final MongoNamespace namespace ) {
96
- Assertions .notNull ("namespace" , namespace );
97
- return new BsonDocument ("db" , new BsonString (namespace .getDatabaseName ()))
98
- .append ("coll" , new BsonString (namespace .getCollectionName ()));
99
+ /**
100
+ * Creates a new instance
101
+ *
102
+ * @param operationType the operation type
103
+ * @param resumeToken the resume token
104
+ * @param namespaceDocument the BsonDocument representing the namespace
105
+ * @param destinationNamespaceDocument the BsonDocument representing the destinatation namespace
106
+ * @param fullDocument the full document
107
+ * @param documentKey a document containing the _id of the changed document
108
+ * @param clusterTime the cluster time at which the change occured
109
+ * @param updateDescription the update description
110
+ * @param txnNumber the transaction number
111
+ * @param lsid the identifier for the session associated with the transaction
112
+ *
113
+ * @since 3.11
114
+ */
115
+ @ Deprecated
116
+ public ChangeStreamDocument (final OperationType operationType ,
117
+ final BsonDocument resumeToken ,
118
+ final BsonDocument namespaceDocument ,
119
+ final BsonDocument destinationNamespaceDocument ,
120
+ final TDocument fullDocument ,
121
+ final BsonDocument documentKey ,
122
+ final BsonTimestamp clusterTime ,
123
+ final UpdateDescription updateDescription ,
124
+ final BsonInt64 txnNumber ,
125
+ final BsonDocument lsid ) {
126
+ this (operationType .getValue (), resumeToken , namespaceDocument , destinationNamespaceDocument , fullDocument , documentKey ,
127
+ clusterTime , updateDescription , txnNumber , lsid );
99
128
}
100
129
101
130
/**
@@ -240,6 +269,24 @@ public BsonTimestamp getClusterTime() {
240
269
return clusterTime ;
241
270
}
242
271
272
+
273
+ /**
274
+ * Returns the operation type as a string.
275
+ *
276
+ * <p>
277
+ * This method is useful when using a driver release that has not yet been updated to include a newer operation type in the
278
+ * {@link OperationType} enum. In that case, {@link #getOperationType()} will return {@link OperationType#OTHER} and this method can
279
+ * be used to retrieve the actual operation type as a string value.
280
+ * </p>
281
+ *
282
+ * @return the operation type as a string
283
+ * @since 4.6
284
+ * @see #getOperationType()
285
+ */
286
+ public String getOperationTypeString () {
287
+ return operationTypeString ;
288
+ }
289
+
243
290
/**
244
291
* Returns the operationType
245
292
*
@@ -324,10 +371,10 @@ public boolean equals(final Object o) {
324
371
if (documentKey != null ? !documentKey .equals (that .documentKey ) : that .documentKey != null ) {
325
372
return false ;
326
373
}
327
- if (clusterTime != null ? ! clusterTime .equals (that . clusterTime ) : that .clusterTime != null ) {
374
+ if (! Objects .equals (operationTypeString , that .operationTypeString ) ) {
328
375
return false ;
329
376
}
330
- if (operationType != that .operationType ) {
377
+ if (clusterTime != null ? ! clusterTime . equals ( that .clusterTime ) : that . clusterTime != null ) {
331
378
return false ;
332
379
}
333
380
if (updateDescription != null ? !updateDescription .equals (that .updateDescription ) : that .updateDescription != null ) {
@@ -351,7 +398,7 @@ public int hashCode() {
351
398
result = 31 * result + (fullDocument != null ? fullDocument .hashCode () : 0 );
352
399
result = 31 * result + (documentKey != null ? documentKey .hashCode () : 0 );
353
400
result = 31 * result + (clusterTime != null ? clusterTime .hashCode () : 0 );
354
- result = 31 * result + (operationType != null ? operationType .hashCode () : 0 );
401
+ result = 31 * result + (operationTypeString != null ? operationTypeString .hashCode () : 0 );
355
402
result = 31 * result + (updateDescription != null ? updateDescription .hashCode () : 0 );
356
403
result = 31 * result + (txnNumber != null ? txnNumber .hashCode () : 0 );
357
404
result = 31 * result + (lsid != null ? lsid .hashCode () : 0 );
@@ -361,7 +408,7 @@ public int hashCode() {
361
408
@ Override
362
409
public String toString () {
363
410
return "ChangeStreamDocument{"
364
- + " operationType=" + operationType
411
+ + " operationType=" + operationTypeString
365
412
+ ", resumeToken=" + resumeToken
366
413
+ ", namespace=" + getNamespace ()
367
414
+ ", destinationNamespace=" + getDestinationNamespace ()
0 commit comments