Skip to content

Commit f609a85

Browse files
committed
JAVA-2632: Add documentKey to ChangeStreamDocument
1 parent 0da7483 commit f609a85

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

driver-async/src/test/unit/com/mongodb/async/client/ChangeStreamIterableSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ChangeStreamIterableSpecification extends Specification {
133133
given:
134134
def count = 0
135135
def cannedResults = ['{_id: 1}', '{_id: 2}', '{_id: 3}'].collect {
136-
new ChangeStreamDocument(RawBsonDocument.parse(it), null, Document.parse(it), null, null)
136+
new ChangeStreamDocument(RawBsonDocument.parse(it), null, Document.parse(it), BsonDocument.parse(it), null, null)
137137

138138
}
139139
def executor = new TestOperationExecutor([cursor(cannedResults), cursor(cannedResults), cursor(cannedResults),

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class ChangeStreamDocument<TDocument> {
4040
@BsonProperty("ns")
4141
private final MongoNamespace namespace;
4242
private final TDocument fullDocument;
43+
private final BsonDocument documentKey;
4344
private final OperationType operationType;
4445
private final UpdateDescription updateDescription;
4546

@@ -48,6 +49,7 @@ public final class ChangeStreamDocument<TDocument> {
4849
*
4950
* @param resumeToken the resume token
5051
* @param namespace the namespace
52+
* @param documentKey a document containing the _id of the changed document
5153
* @param fullDocument the fullDocument
5254
* @param operationType the operation type
5355
* @param updateDescription the update description
@@ -56,10 +58,12 @@ public final class ChangeStreamDocument<TDocument> {
5658
public ChangeStreamDocument(@BsonProperty("resumeToken") final BsonDocument resumeToken,
5759
@BsonProperty("namespace") final MongoNamespace namespace,
5860
@BsonProperty("fullDocument") final TDocument fullDocument,
61+
@BsonProperty("documentKey") final BsonDocument documentKey,
5962
@BsonProperty("operationType") final OperationType operationType,
6063
@BsonProperty("updateDescription") final UpdateDescription updateDescription) {
6164
this.resumeToken = resumeToken;
6265
this.namespace = namespace;
66+
this.documentKey = documentKey;
6367
this.fullDocument = fullDocument;
6468
this.operationType = operationType;
6569
this.updateDescription = updateDescription;
@@ -92,6 +96,21 @@ public TDocument getFullDocument() {
9296
return fullDocument;
9397
}
9498

99+
/**
100+
* Returns a document containing just the _id of the changed document.
101+
* <p>
102+
* For unsharded collections this contains a single field, _id, with the
103+
* value of the _id of the document updated. For sharded collections,
104+
* this will contain all the components of the shard key in order,
105+
* followed by the _id if the _id isn’t part of the shard key.
106+
* </p>
107+
*
108+
* @return the document key
109+
*/
110+
public BsonDocument getDocumentKey() {
111+
return documentKey;
112+
}
113+
95114
/**
96115
* Returns the operationType
97116
*
@@ -134,20 +153,22 @@ public boolean equals(final Object o) {
134153

135154
ChangeStreamDocument<?> that = (ChangeStreamDocument<?>) o;
136155

137-
if (!getResumeToken().equals(that.getResumeToken())) {
156+
if (resumeToken != null ? !resumeToken.equals(that.resumeToken) : that.resumeToken != null) {
157+
return false;
158+
}
159+
if (namespace != null ? !namespace.equals(that.namespace) : that.namespace != null) {
138160
return false;
139161
}
140-
if (!getNamespace().equals(that.getNamespace())) {
162+
if (fullDocument != null ? !fullDocument.equals(that.fullDocument) : that.fullDocument != null) {
141163
return false;
142164
}
143-
if (!getFullDocument().equals(that.getFullDocument())) {
165+
if (documentKey != null ? !documentKey.equals(that.documentKey) : that.documentKey != null) {
144166
return false;
145167
}
146-
if (getOperationType() != that.getOperationType()) {
168+
if (operationType != that.operationType) {
147169
return false;
148170
}
149-
if (getUpdateDescription() != null ? !getUpdateDescription().equals(that.getUpdateDescription())
150-
: that.getUpdateDescription() != null) {
171+
if (updateDescription != null ? !updateDescription.equals(that.updateDescription) : that.updateDescription != null) {
151172
return false;
152173
}
153174

@@ -156,20 +177,22 @@ public boolean equals(final Object o) {
156177

157178
@Override
158179
public int hashCode() {
159-
int result = getResumeToken().hashCode();
160-
result = 31 * result + getNamespace().hashCode();
161-
result = 31 * result + getFullDocument().hashCode();
162-
result = 31 * result + getOperationType().hashCode();
163-
result = 31 * result + (getUpdateDescription() != null ? getUpdateDescription().hashCode() : 0);
180+
int result = resumeToken != null ? resumeToken.hashCode() : 0;
181+
result = 31 * result + (namespace != null ? namespace.hashCode() : 0);
182+
result = 31 * result + (fullDocument != null ? fullDocument.hashCode() : 0);
183+
result = 31 * result + (documentKey != null ? documentKey.hashCode() : 0);
184+
result = 31 * result + (operationType != null ? operationType.hashCode() : 0);
185+
result = 31 * result + (updateDescription != null ? updateDescription.hashCode() : 0);
164186
return result;
165187
}
166188

167189
@Override
168190
public String toString() {
169-
return "ChangeStreamOutput{"
191+
return "ChangeStreamDocument{"
170192
+ "resumeToken=" + resumeToken
171193
+ ", namespace=" + namespace
172194
+ ", fullDocument=" + fullDocument
195+
+ ", documentKey=" + documentKey
173196
+ ", operationType=" + operationType
174197
+ ", updateDescription=" + updateDescription
175198
+ "}";

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@
3636
@SuppressWarnings({"unchecked", "rawtypes"})
3737
final class ChangeStreamDocumentCodec<TResult> implements Codec<ChangeStreamDocument<TResult>> {
3838

39-
private static final BsonDocumentCodec RESUME_TOKEN_CODEC = new BsonDocumentCodec();
39+
private static final BsonDocumentCodec BSON_DOCUMENT_CODEC = new BsonDocumentCodec();
4040
private static final OperationTypeCodec OPERATION_TYPE_CODEC = new OperationTypeCodec();
4141

4242
private final Codec<ChangeStreamDocument<TResult>> codec;
4343

4444
ChangeStreamDocumentCodec(final Class<TResult> fullDocumentClass, final CodecRegistry codecRegistry) {
4545

4646
ClassModelBuilder<ChangeStreamDocument> classModelBuilder = ClassModel.builder(ChangeStreamDocument.class);
47+
((PropertyModelBuilder<BsonDocument>) classModelBuilder.getProperty("documentKey")).codec(BSON_DOCUMENT_CODEC);
4748
((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass));
48-
((PropertyModelBuilder<BsonDocument>) classModelBuilder.getProperty("resumeToken")).codec(RESUME_TOKEN_CODEC);
49+
((PropertyModelBuilder<BsonDocument>) classModelBuilder.getProperty("resumeToken")).codec(BSON_DOCUMENT_CODEC);
4950
((PropertyModelBuilder<OperationType>) classModelBuilder.getProperty("operationType")).codec(OPERATION_TYPE_CODEC);
5051
ClassModel<ChangeStreamDocument> changeStreamDocumentClassModel = classModelBuilder.build();
5152

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.mongodb.MongoNamespace
2020
import org.bson.BsonDocument
2121
import org.bson.BsonDocumentReader
2222
import org.bson.BsonDocumentWriter
23+
import org.bson.BsonInt32
2324
import org.bson.BsonReader
2425
import org.bson.Document
2526
import org.bson.codecs.BsonValueCodecProvider
@@ -58,22 +59,26 @@ class ChangeStreamDocumentCodecSpecification extends Specification {
5859
BsonDocument.parse('{token: true}'),
5960
new MongoNamespace('databaseName.collectionName'),
6061
Document.parse('{key: "value for fullDocument"}'),
62+
new BsonDocument('_id', new BsonInt32(1)),
6163
OperationType.INSERT,
6264
null
6365
),
6466
new ChangeStreamDocument<BsonDocument>(
6567
BsonDocument.parse('{token: true}'),
6668
new MongoNamespace('databaseName.collectionName'),
6769
BsonDocument.parse('{key: "value for fullDocument"}'),
70+
new BsonDocument('_id', new BsonInt32(2)),
6871
OperationType.UPDATE,
6972
new UpdateDescription(['a', 'b'], BsonDocument.parse('{c: 1}'))
7073
)
7174
]
7275
clazz << [Document, BsonDocument]
7376
json << [
74-
'''{_id: {token: true}, ns: {db: "databaseName", coll: "collectionName"}, fullDocument: {key: "value for fullDocument"},
77+
'''{_id: {token: true}, ns: {db: "databaseName", coll: "collectionName"}, documentKey : {_id : 1},
78+
fullDocument: {key: "value for fullDocument"},
7579
operationType: "insert"}''',
76-
'''{_id: {token: true}, ns: {db: "databaseName", coll: "collectionName"}, fullDocument: {key: "value for fullDocument"},
80+
'''{_id: {token: true}, ns: {db: "databaseName", coll: "collectionName"}, documentKey : {_id : 2},
81+
fullDocument: {key: "value for fullDocument"},
7782
operationType: "update", updateDescription: {removedFields: ["a", "b"], updatedFields: {c: 1}}}''',
7883
]
7984
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@ class ChangeStreamDocumentSpecification extends Specification {
2828
def resumeToken = RawBsonDocument.parse('{token: true}')
2929
def namespace = new MongoNamespace('databaseName.collectionName')
3030
def fullDocument = BsonDocument.parse('{key: "value for fullDocument"}')
31+
def documentKey = BsonDocument.parse('{_id : 1}')
3132
def operationType = OperationType.UPDATE
3233
def updateDesc = new UpdateDescription(['a', 'b'], BsonDocument.parse('{c: 1}'))
3334

3435
when:
35-
def changeStreamDocument = new ChangeStreamDocument<BsonDocument>(resumeToken, namespace, fullDocument, operationType, updateDesc)
36+
def changeStreamDocument = new ChangeStreamDocument<BsonDocument>(resumeToken, namespace, fullDocument, documentKey,
37+
operationType, updateDesc)
3638

3739
then:
3840
changeStreamDocument.getResumeToken() == resumeToken
3941
changeStreamDocument.getFullDocument() == fullDocument
42+
changeStreamDocument.getDocumentKey() == documentKey
4043
changeStreamDocument.getNamespace() == namespace
4144
changeStreamDocument.getOperationType() == operationType
4245
changeStreamDocument.getUpdateDescription() == updateDesc

driver/src/test/unit/com/mongodb/ChangeStreamIterableSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class ChangeStreamIterableSpecification extends Specification {
138138
given:
139139
def count = 0
140140
def cannedResults = ['{_id: 1}', '{_id: 2}', '{_id: 3}'].collect {
141-
new ChangeStreamDocument(RawBsonDocument.parse(it), null, Document.parse(it), null, null)
141+
new ChangeStreamDocument(RawBsonDocument.parse(it), null, Document.parse(it), BsonDocument.parse(it), null, null)
142142

143143
}
144144
def executor = new TestOperationExecutor([cursor(cannedResults), cursor(cannedResults), cursor(cannedResults),

0 commit comments

Comments
 (0)