Skip to content

Commit

Permalink
Convert Bson to BsonDocument for hint (#1335)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jyemin committed Mar 12, 2024
1 parent aa5f77b commit 2ef2bf9
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,7 +38,7 @@ public final class UpdateRequest extends WriteRequest {
private boolean isUpsert = false;
private Collation collation;
private List<BsonDocument> 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) {
Expand Down Expand Up @@ -111,11 +110,11 @@ public List<BsonDocument> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,7 +61,7 @@ public abstract class BaseFindAndModifyOperation<T> 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;
Expand Down Expand Up @@ -151,11 +150,11 @@ public Collation getCollation() {
}

@Nullable
public Bson getHint() {
public BsonDocument getHint() {
return hint;
}

public BaseFindAndModifyOperation<T> hint(@Nullable final Bson hint) {
public BaseFindAndModifyOperation<T> hint(@Nullable final BsonDocument hint) {
this.hint = hint;
return this;
}
Expand Down Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -68,7 +67,7 @@ public FindAndDeleteOperation<T> sort(@Nullable final BsonDocument sort) {
}

@Override
public FindAndDeleteOperation<T> hint(@Nullable final Bson hint) {
public FindAndDeleteOperation<T> hint(@Nullable final BsonDocument hint) {
super.hint(hint);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +110,7 @@ public FindAndReplaceOperation<T> sort(@Nullable final BsonDocument sort) {
}

@Override
public FindAndReplaceOperation<T> hint(@Nullable final Bson hint) {
public FindAndReplaceOperation<T> hint(@Nullable final BsonDocument hint) {
super.hint(hint);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,7 +138,7 @@ public FindAndUpdateOperation<T> sort(@Nullable final BsonDocument sort) {
}

@Override
public FindAndUpdateOperation<T> hint(@Nullable final Bson hint) {
public FindAndUpdateOperation<T> hint(@Nullable final BsonDocument hint) {
super.hint(hint);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ FindAndDeleteOperation<TDocument> 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()));
Expand All @@ -344,7 +344,7 @@ FindAndReplaceOperation<TDocument> 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()));
Expand All @@ -362,7 +362,7 @@ FindAndUpdateOperation<TDocument> 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()));
Expand All @@ -381,7 +381,7 @@ FindAndUpdateOperation<TDocument> 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()));
Expand Down Expand Up @@ -474,7 +474,7 @@ MixedBulkWriteOperation bulkWrite(final List<? extends WriteModel<? extends TDoc
WriteRequest.Type.REPLACE)
.upsert(replaceOneModel.getReplaceOptions().isUpsert())
.collation(replaceOneModel.getReplaceOptions().getCollation())
.hint(replaceOneModel.getReplaceOptions().getHint())
.hint(toBsonDocument(replaceOneModel.getReplaceOptions().getHint()))
.hintString(replaceOneModel.getReplaceOptions().getHintString());
} else if (writeModel instanceof UpdateOneModel) {
UpdateOneModel<TDocument> updateOneModel = (UpdateOneModel<TDocument>) writeModel;
Expand All @@ -485,7 +485,7 @@ MixedBulkWriteOperation bulkWrite(final List<? extends WriteModel<? extends TDoc
.upsert(updateOneModel.getOptions().isUpsert())
.collation(updateOneModel.getOptions().getCollation())
.arrayFilters(toBsonDocumentList(updateOneModel.getOptions().getArrayFilters()))
.hint(updateOneModel.getOptions().getHint())
.hint(toBsonDocument(updateOneModel.getOptions().getHint()))
.hintString(updateOneModel.getOptions().getHintString());
} else if (writeModel instanceof UpdateManyModel) {
UpdateManyModel<TDocument> updateManyModel = (UpdateManyModel<TDocument>) writeModel;
Expand All @@ -496,19 +496,19 @@ MixedBulkWriteOperation bulkWrite(final List<? extends WriteModel<? extends TDoc
.upsert(updateManyModel.getOptions().isUpsert())
.collation(updateManyModel.getOptions().getCollation())
.arrayFilters(toBsonDocumentList(updateManyModel.getOptions().getArrayFilters()))
.hint(updateManyModel.getOptions().getHint())
.hint(toBsonDocument(updateManyModel.getOptions().getHint()))
.hintString(updateManyModel.getOptions().getHintString());
} else if (writeModel instanceof DeleteOneModel) {
DeleteOneModel<TDocument> deleteOneModel = (DeleteOneModel<TDocument>) 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<TDocument> deleteManyModel = (DeleteManyModel<TDocument>) 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()));
Expand Down

0 comments on commit 2ef2bf9

Please sign in to comment.