From 838124a45bb01707dab339f036ef3df1b91279b3 Mon Sep 17 00:00:00 2001 From: Jeff Yemin Date: Tue, 12 Mar 2024 16:43:50 -0400 Subject: [PATCH] Convert Bson to BsonDocument for hint (#1335) Unlike every other instance of Bson in the CRUD API, the hint option for write operations was not converted to a BsonDocument prior to passing it down to the operation layer. This is a problem because the operation layer doesn't have a CodecRegistry available, and so can not properly due the conversion. In this commit, all hint options are converted to BsonDocument in the same place as all the other Bson options, where a CodecRegistry is available to properly do the conversion. JAVA-5357 --- .../mongodb/internal/bulk/DeleteRequest.java | 7 +++---- .../mongodb/internal/bulk/UpdateRequest.java | 7 +++---- .../internal/connection/SplittablePayload.java | 6 +++--- .../operation/BaseFindAndModifyOperation.java | 9 ++++----- .../operation/FindAndDeleteOperation.java | 3 +-- .../operation/FindAndReplaceOperation.java | 3 +-- .../operation/FindAndUpdateOperation.java | 3 +-- .../mongodb/internal/operation/Operations.java | 18 +++++++++--------- 8 files changed, 25 insertions(+), 31 deletions(-) diff --git a/driver-core/src/main/com/mongodb/internal/bulk/DeleteRequest.java b/driver-core/src/main/com/mongodb/internal/bulk/DeleteRequest.java index 587b24293f1..bcbf43142c1 100644 --- a/driver-core/src/main/com/mongodb/internal/bulk/DeleteRequest.java +++ b/driver-core/src/main/com/mongodb/internal/bulk/DeleteRequest.java @@ -19,7 +19,6 @@ import com.mongodb.client.model.Collation; import com.mongodb.lang.Nullable; import org.bson.BsonDocument; -import org.bson.conversions.Bson; import static com.mongodb.assertions.Assertions.notNull; @@ -32,7 +31,7 @@ public final class DeleteRequest extends WriteRequest { private final BsonDocument filter; private boolean isMulti = true; private Collation collation; - private Bson hint; + private BsonDocument hint; private String hintString; public DeleteRequest(final BsonDocument filter) { @@ -63,11 +62,11 @@ public DeleteRequest collation(@Nullable final Collation collation) { } @Nullable - public Bson getHint() { + public BsonDocument getHint() { return hint; } - public DeleteRequest hint(@Nullable final Bson hint) { + public DeleteRequest hint(@Nullable final BsonDocument hint) { this.hint = hint; return this; } diff --git a/driver-core/src/main/com/mongodb/internal/bulk/UpdateRequest.java b/driver-core/src/main/com/mongodb/internal/bulk/UpdateRequest.java index 454d0b3ba0d..5a7df089641 100644 --- a/driver-core/src/main/com/mongodb/internal/bulk/UpdateRequest.java +++ b/driver-core/src/main/com/mongodb/internal/bulk/UpdateRequest.java @@ -20,7 +20,6 @@ import com.mongodb.lang.Nullable; import org.bson.BsonDocument; import org.bson.BsonValue; -import org.bson.conversions.Bson; import java.util.List; @@ -39,7 +38,7 @@ public final class UpdateRequest extends WriteRequest { private boolean isUpsert = false; private Collation collation; private List arrayFilters; - @Nullable private Bson hint; + @Nullable private BsonDocument hint; @Nullable private String hintString; public UpdateRequest(final BsonDocument filter, @Nullable final BsonValue update, final Type updateType) { @@ -111,11 +110,11 @@ public List getArrayFilters() { } @Nullable - public Bson getHint() { + public BsonDocument getHint() { return hint; } - public UpdateRequest hint(@Nullable final Bson hint) { + public UpdateRequest hint(@Nullable final BsonDocument hint) { this.hint = hint; return this; } diff --git a/driver-core/src/main/com/mongodb/internal/connection/SplittablePayload.java b/driver-core/src/main/com/mongodb/internal/connection/SplittablePayload.java index 50300ceeac0..a71f7a940f0 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/SplittablePayload.java +++ b/driver-core/src/main/com/mongodb/internal/connection/SplittablePayload.java @@ -246,8 +246,8 @@ public void encode(final BsonWriter writer, final WriteRequestWithIndex writeReq } if (update.getHint() != null) { writer.writeName("hint"); - BsonDocument hint = assertNotNull(update.getHint()).toBsonDocument(BsonDocument.class, null); - getCodec(hint).encode(writer, hint, EncoderContext.builder().build()); + getCodec(assertNotNull(update.getHint())).encode(writer, assertNotNull(update.getHint()), + EncoderContext.builder().build()); } else if (update.getHintString() != null) { writer.writeString("hint", update.getHintString()); } @@ -265,7 +265,7 @@ public void encode(final BsonWriter writer, final WriteRequestWithIndex writeReq } if (deleteRequest.getHint() != null) { writer.writeName("hint"); - BsonDocument hint = assertNotNull(deleteRequest.getHint()).toBsonDocument(BsonDocument.class, null); + BsonDocument hint = assertNotNull(deleteRequest.getHint()); getCodec(hint).encode(writer, hint, EncoderContext.builder().build()); } else if (deleteRequest.getHintString() != null) { writer.writeString("hint", deleteRequest.getHintString()); diff --git a/driver-core/src/main/com/mongodb/internal/operation/BaseFindAndModifyOperation.java b/driver-core/src/main/com/mongodb/internal/operation/BaseFindAndModifyOperation.java index e3ae79fa589..5179d3096b3 100644 --- a/driver-core/src/main/com/mongodb/internal/operation/BaseFindAndModifyOperation.java +++ b/driver-core/src/main/com/mongodb/internal/operation/BaseFindAndModifyOperation.java @@ -31,7 +31,6 @@ import org.bson.BsonValue; import org.bson.FieldNameValidator; import org.bson.codecs.Decoder; -import org.bson.conversions.Bson; import java.util.concurrent.TimeUnit; @@ -62,7 +61,7 @@ public abstract class BaseFindAndModifyOperation implements AsyncWriteOperati private BsonDocument sort; private long maxTimeMS; private Collation collation; - private Bson hint; + private BsonDocument hint; private String hintString; private BsonValue comment; private BsonDocument variables; @@ -151,11 +150,11 @@ public Collation getCollation() { } @Nullable - public Bson getHint() { + public BsonDocument getHint() { return hint; } - public BaseFindAndModifyOperation hint(@Nullable final Bson hint) { + public BaseFindAndModifyOperation hint(@Nullable final BsonDocument hint) { this.hint = hint; return this; } @@ -216,7 +215,7 @@ private CommandCreator getCommandCreator(final SessionContext sessionContext) { if (getHint() != null || getHintString() != null) { validateHintForFindAndModify(connectionDescription, getWriteConcern()); if (getHint() != null) { - commandDocument.put("hint", getHint().toBsonDocument(BsonDocument.class, null)); + commandDocument.put("hint", getHint()); } else { commandDocument.put("hint", new BsonString(getHintString())); } diff --git a/driver-core/src/main/com/mongodb/internal/operation/FindAndDeleteOperation.java b/driver-core/src/main/com/mongodb/internal/operation/FindAndDeleteOperation.java index ede7ee51628..928173ba2fb 100644 --- a/driver-core/src/main/com/mongodb/internal/operation/FindAndDeleteOperation.java +++ b/driver-core/src/main/com/mongodb/internal/operation/FindAndDeleteOperation.java @@ -27,7 +27,6 @@ import org.bson.BsonValue; import org.bson.FieldNameValidator; import org.bson.codecs.Decoder; -import org.bson.conversions.Bson; import java.util.concurrent.TimeUnit; @@ -68,7 +67,7 @@ public FindAndDeleteOperation sort(@Nullable final BsonDocument sort) { } @Override - public FindAndDeleteOperation hint(@Nullable final Bson hint) { + public FindAndDeleteOperation hint(@Nullable final BsonDocument hint) { super.hint(hint); return this; } diff --git a/driver-core/src/main/com/mongodb/internal/operation/FindAndReplaceOperation.java b/driver-core/src/main/com/mongodb/internal/operation/FindAndReplaceOperation.java index de988c963a4..303d9c0e208 100644 --- a/driver-core/src/main/com/mongodb/internal/operation/FindAndReplaceOperation.java +++ b/driver-core/src/main/com/mongodb/internal/operation/FindAndReplaceOperation.java @@ -29,7 +29,6 @@ import org.bson.BsonValue; import org.bson.FieldNameValidator; import org.bson.codecs.Decoder; -import org.bson.conversions.Bson; import java.util.HashMap; import java.util.Map; @@ -111,7 +110,7 @@ public FindAndReplaceOperation sort(@Nullable final BsonDocument sort) { } @Override - public FindAndReplaceOperation hint(@Nullable final Bson hint) { + public FindAndReplaceOperation hint(@Nullable final BsonDocument hint) { super.hint(hint); return this; } diff --git a/driver-core/src/main/com/mongodb/internal/operation/FindAndUpdateOperation.java b/driver-core/src/main/com/mongodb/internal/operation/FindAndUpdateOperation.java index 17ce879102d..2c2a00ff437 100644 --- a/driver-core/src/main/com/mongodb/internal/operation/FindAndUpdateOperation.java +++ b/driver-core/src/main/com/mongodb/internal/operation/FindAndUpdateOperation.java @@ -30,7 +30,6 @@ import org.bson.BsonValue; import org.bson.FieldNameValidator; import org.bson.codecs.Decoder; -import org.bson.conversions.Bson; import java.util.HashMap; import java.util.List; @@ -139,7 +138,7 @@ public FindAndUpdateOperation sort(@Nullable final BsonDocument sort) { } @Override - public FindAndUpdateOperation hint(@Nullable final Bson hint) { + public FindAndUpdateOperation hint(@Nullable final BsonDocument hint) { super.hint(hint); return this; } diff --git a/driver-core/src/main/com/mongodb/internal/operation/Operations.java b/driver-core/src/main/com/mongodb/internal/operation/Operations.java index b6765b7cc36..b32beda747b 100644 --- a/driver-core/src/main/com/mongodb/internal/operation/Operations.java +++ b/driver-core/src/main/com/mongodb/internal/operation/Operations.java @@ -322,7 +322,7 @@ FindAndDeleteOperation findOneAndDelete(final Bson filter, final Find .sort(toBsonDocument(options.getSort())) .maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS) .collation(options.getCollation()) - .hint(options.getHint()) + .hint(toBsonDocument(options.getHint())) .hintString(options.getHintString()) .comment(options.getComment()) .let(toBsonDocument(options.getLet())); @@ -340,7 +340,7 @@ FindAndReplaceOperation findOneAndReplace(final Bson filter, final TD .maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS) .bypassDocumentValidation(options.getBypassDocumentValidation()) .collation(options.getCollation()) - .hint(options.getHint()) + .hint(toBsonDocument(options.getHint())) .hintString(options.getHintString()) .comment(options.getComment()) .let(toBsonDocument(options.getLet())); @@ -358,7 +358,7 @@ FindAndUpdateOperation findOneAndUpdate(final Bson filter, final Bson .bypassDocumentValidation(options.getBypassDocumentValidation()) .collation(options.getCollation()) .arrayFilters(toBsonDocumentList(options.getArrayFilters())) - .hint(options.getHint()) + .hint(toBsonDocument(options.getHint())) .hintString(options.getHintString()) .comment(options.getComment()) .let(toBsonDocument(options.getLet())); @@ -377,7 +377,7 @@ FindAndUpdateOperation findOneAndUpdate(final Bson filter, final List .bypassDocumentValidation(options.getBypassDocumentValidation()) .collation(options.getCollation()) .arrayFilters(toBsonDocumentList(options.getArrayFilters())) - .hint(options.getHint()) + .hint(toBsonDocument(options.getHint())) .hintString(options.getHintString()) .comment(options.getComment()) .let(toBsonDocument(options.getLet())); @@ -470,7 +470,7 @@ MixedBulkWriteOperation bulkWrite(final List updateOneModel = (UpdateOneModel) writeModel; @@ -481,7 +481,7 @@ MixedBulkWriteOperation bulkWrite(final List updateManyModel = (UpdateManyModel) writeModel; @@ -492,19 +492,19 @@ MixedBulkWriteOperation bulkWrite(final List deleteOneModel = (DeleteOneModel) writeModel; writeRequest = new DeleteRequest(assertNotNull(toBsonDocument(deleteOneModel.getFilter()))).multi(false) .collation(deleteOneModel.getOptions().getCollation()) - .hint(deleteOneModel.getOptions().getHint()) + .hint(toBsonDocument(deleteOneModel.getOptions().getHint())) .hintString(deleteOneModel.getOptions().getHintString()); } else if (writeModel instanceof DeleteManyModel) { DeleteManyModel deleteManyModel = (DeleteManyModel) writeModel; writeRequest = new DeleteRequest(assertNotNull(toBsonDocument(deleteManyModel.getFilter()))).multi(true) .collation(deleteManyModel.getOptions().getCollation()) - .hint(deleteManyModel.getOptions().getHint()) + .hint(toBsonDocument(deleteManyModel.getOptions().getHint())) .hintString(deleteManyModel.getOptions().getHintString()); } else { throw new UnsupportedOperationException(format("WriteModel of type %s is not supported", writeModel.getClass()));