17
17
package com .mongodb .client .model .changestream ;
18
18
19
19
import com .mongodb .MongoNamespace ;
20
+ import com .mongodb .assertions .Assertions ;
20
21
import com .mongodb .lang .Nullable ;
21
22
import org .bson .BsonDocument ;
23
+ import org .bson .BsonString ;
22
24
import org .bson .BsonTimestamp ;
23
25
import org .bson .codecs .Codec ;
24
26
import org .bson .codecs .configuration .CodecRegistry ;
25
27
import org .bson .codecs .pojo .annotations .BsonCreator ;
26
28
import org .bson .codecs .pojo .annotations .BsonId ;
29
+ import org .bson .codecs .pojo .annotations .BsonIgnore ;
27
30
import org .bson .codecs .pojo .annotations .BsonProperty ;
28
31
29
32
/**
@@ -39,8 +42,7 @@ public final class ChangeStreamDocument<TDocument> {
39
42
40
43
@ BsonId ()
41
44
private final BsonDocument resumeToken ;
42
- @ BsonProperty ("ns" )
43
- private final MongoNamespace namespace ;
45
+ private final BsonDocument namespaceDocument ;
44
46
private final TDocument fullDocument ;
45
47
private final BsonDocument documentKey ;
46
48
private final BsonTimestamp clusterTime ;
@@ -60,12 +62,12 @@ public final class ChangeStreamDocument<TDocument> {
60
62
* UpdateDescription)}
61
63
*/
62
64
@ Deprecated
63
- public ChangeStreamDocument (@ BsonProperty ( "resumeToken" ) final BsonDocument resumeToken ,
64
- @ BsonProperty ( "namespace" ) final MongoNamespace namespace ,
65
- @ BsonProperty ( "fullDocument" ) final TDocument fullDocument ,
66
- @ BsonProperty ( "documentKey" ) final BsonDocument documentKey ,
67
- @ BsonProperty ( "operationType" ) final OperationType operationType ,
68
- @ BsonProperty ( "updateDescription" ) final UpdateDescription updateDescription ) {
65
+ public ChangeStreamDocument (final BsonDocument resumeToken ,
66
+ final MongoNamespace namespace ,
67
+ final TDocument fullDocument ,
68
+ final BsonDocument documentKey ,
69
+ final OperationType operationType ,
70
+ final UpdateDescription updateDescription ) {
69
71
this (resumeToken , namespace , fullDocument , documentKey , null , operationType , updateDescription );
70
72
}
71
73
@@ -80,23 +82,54 @@ public ChangeStreamDocument(@BsonProperty("resumeToken") final BsonDocument resu
80
82
* @param operationType the operation type
81
83
* @param updateDescription the update description
82
84
*/
85
+ @ Deprecated
86
+ public ChangeStreamDocument (final BsonDocument resumeToken ,
87
+ final MongoNamespace namespace ,
88
+ final TDocument fullDocument ,
89
+ final BsonDocument documentKey ,
90
+ @ Nullable final BsonTimestamp clusterTime ,
91
+ final OperationType operationType ,
92
+ final UpdateDescription updateDescription ) {
93
+ this (resumeToken , namespaceToDocument (namespace ), fullDocument , documentKey ,
94
+ clusterTime , operationType , updateDescription );
95
+ }
96
+
97
+ /**
98
+ * Creates a new instance
99
+ *
100
+ * @param resumeToken the resume token
101
+ * @param namespaceDocument the BsonDocument representing the namespace
102
+ * @param fullDocument the full document
103
+ * @param documentKey a document containing the _id of the changed document
104
+ * @param clusterTime the cluster time at which the change occured
105
+ * @param operationType the operation type
106
+ * @param updateDescription the update description
107
+ *
108
+ * @since 3.8
109
+ */
83
110
@ BsonCreator
84
111
public ChangeStreamDocument (@ BsonProperty ("resumeToken" ) final BsonDocument resumeToken ,
85
- @ BsonProperty ("namespace " ) final MongoNamespace namespace ,
112
+ @ BsonProperty ("ns " ) final BsonDocument namespaceDocument ,
86
113
@ BsonProperty ("fullDocument" ) final TDocument fullDocument ,
87
114
@ BsonProperty ("documentKey" ) final BsonDocument documentKey ,
88
115
@ Nullable @ BsonProperty ("clusterTime" ) final BsonTimestamp clusterTime ,
89
116
@ BsonProperty ("operationType" ) final OperationType operationType ,
90
117
@ BsonProperty ("updateDescription" ) final UpdateDescription updateDescription ) {
91
118
this .resumeToken = resumeToken ;
92
- this .namespace = namespace ;
119
+ this .namespaceDocument = namespaceDocument ;
93
120
this .documentKey = documentKey ;
94
121
this .fullDocument = fullDocument ;
95
122
this .clusterTime = clusterTime ;
96
123
this .operationType = operationType ;
97
124
this .updateDescription = updateDescription ;
98
125
}
99
126
127
+ private static BsonDocument namespaceToDocument (final MongoNamespace namespace ) {
128
+ Assertions .notNull ("namespace" , namespace );
129
+ return new BsonDocument ("db" , new BsonString (namespace .getDatabaseName ()))
130
+ .append ("coll" , new BsonString (namespace .getCollectionName ()));
131
+ }
132
+
100
133
/**
101
134
* Returns the resumeToken
102
135
*
@@ -109,10 +142,55 @@ public BsonDocument getResumeToken() {
109
142
/**
110
143
* Returns the namespace
111
144
*
112
- * @return the namespace
145
+ * The invalidate operation type does include a MongoNamespace in the ChangeStreamDocument response. The
146
+ * dropDatabase operation type includes a MongoNamespace, but does not include a collection name as part
147
+ * of the namespace.
148
+ *
149
+ * @return the namespace. If the namespaceDocument is null or if it is missing either the 'db' or 'coll' keys,
150
+ * then this will return null.
113
151
*/
152
+ @ BsonIgnore @ Nullable
114
153
public MongoNamespace getNamespace () {
115
- return namespace ;
154
+ if (namespaceDocument == null ) {
155
+ return null ;
156
+ }
157
+ if (!namespaceDocument .containsKey ("db" ) || !namespaceDocument .containsKey ("coll" )) {
158
+ return null ;
159
+ }
160
+
161
+ return new MongoNamespace (namespaceDocument .getString ("db" ).getValue (), namespaceDocument .getString ("coll" ).getValue ());
162
+ }
163
+
164
+ /**
165
+ * Returns the namespaceDocument
166
+ *
167
+ * The namespaceDocument is a BsonDocument containing the values associated with a MongoNamespace. The
168
+ * 'db' key refers to the database name and the 'coll' key refers to the collection name.
169
+ *
170
+ * @return the namespaceDocument
171
+ * @since 3.8
172
+ */
173
+ @ BsonProperty ("ns" )
174
+ public BsonDocument getNamespaceDocument () {
175
+ return namespaceDocument ;
176
+ }
177
+
178
+ /**
179
+ * Returns the database name
180
+ *
181
+ * @return the databaseName. If the namespaceDocument is null or if it is missing the 'db' key, then this will
182
+ * return null.
183
+ * @since 3.8
184
+ */
185
+ @ BsonIgnore @ Nullable
186
+ public String getDatabaseName () {
187
+ if (namespaceDocument == null ) {
188
+ return null ;
189
+ }
190
+ if (!namespaceDocument .containsKey ("db" )) {
191
+ return null ;
192
+ }
193
+ return namespaceDocument .getString ("db" ).getValue ();
116
194
}
117
195
118
196
/**
@@ -196,7 +274,7 @@ public boolean equals(final Object o) {
196
274
if (resumeToken != null ? !resumeToken .equals (that .resumeToken ) : that .resumeToken != null ) {
197
275
return false ;
198
276
}
199
- if (namespace != null ? !namespace .equals (that .namespace ) : that .namespace != null ) {
277
+ if (namespaceDocument != null ? !namespaceDocument .equals (that .namespaceDocument ) : that .namespaceDocument != null ) {
200
278
return false ;
201
279
}
202
280
if (fullDocument != null ? !fullDocument .equals (that .fullDocument ) : that .fullDocument != null ) {
@@ -221,7 +299,7 @@ public boolean equals(final Object o) {
221
299
@ Override
222
300
public int hashCode () {
223
301
int result = resumeToken != null ? resumeToken .hashCode () : 0 ;
224
- result = 31 * result + (namespace != null ? namespace .hashCode () : 0 );
302
+ result = 31 * result + (namespaceDocument != null ? namespaceDocument .hashCode () : 0 );
225
303
result = 31 * result + (fullDocument != null ? fullDocument .hashCode () : 0 );
226
304
result = 31 * result + (documentKey != null ? documentKey .hashCode () : 0 );
227
305
result = 31 * result + (clusterTime != null ? clusterTime .hashCode () : 0 );
@@ -234,7 +312,7 @@ public int hashCode() {
234
312
public String toString () {
235
313
return "ChangeStreamDocument{"
236
314
+ "resumeToken=" + resumeToken
237
- + ", namespace=" + namespace
315
+ + ", namespace=" + getNamespace ()
238
316
+ ", fullDocument=" + fullDocument
239
317
+ ", documentKey=" + documentKey
240
318
+ ", clusterTime=" + clusterTime
0 commit comments