3535import org .bson .BsonString ;
3636import org .bson .ByteBuf ;
3737import org .bson .FieldNameValidator ;
38+ import org .bson .io .BsonOutput ;
3839
3940import java .io .ByteArrayOutputStream ;
4041import java .io .UnsupportedEncodingException ;
5556import static com .mongodb .connection .ServerType .STANDALONE ;
5657import static com .mongodb .internal .connection .BsonWriterHelper .appendElementsToDocument ;
5758import static com .mongodb .internal .connection .BsonWriterHelper .backpatchLength ;
59+ import static com .mongodb .internal .connection .BsonWriterHelper .createBsonBinaryWriter ;
60+ import static com .mongodb .internal .connection .BsonWriterHelper .encodeUsingRegistry ;
5861import static com .mongodb .internal .connection .BsonWriterHelper .writeDocumentsOfDualMessageSequences ;
5962import static com .mongodb .internal .connection .BsonWriterHelper .writePayload ;
6063import static com .mongodb .internal .connection .ByteBufBsonDocument .createList ;
@@ -77,16 +80,20 @@ public final class CommandMessage extends RequestMessage {
7780 */
7881 private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1 ;
7982
83+ private static final int UNINITIALIZED_POSITION = -1 ;
84+
8085 private final BsonDocument command ;
8186 private final FieldNameValidator commandFieldNameValidator ;
8287 private final ReadPreference readPreference ;
8388 private final boolean exhaustAllowed ;
8489 private final MessageSequences sequences ;
8590 private final boolean responseExpected ;
8691 private final String database ;
92+ private int firstDocumentPosition = UNINITIALIZED_POSITION ;
93+
8794 /**
8895 * {@code null} iff either {@link #sequences} is not of the {@link DualMessageSequences} type,
89- * or it is of that type, but it has not been {@linkplain #encodeMessageBodyWithMetadata (ByteBufferBsonOutput, OperationContext) encoded}.
96+ * or it is of that type, but it has not been {@linkplain #encodeMessageBody (ByteBufferBsonOutput, OperationContext) encoded}.
9097 */
9198 @ Nullable
9299 private Boolean dualMessageSequencesRequireResponse ;
@@ -145,7 +152,7 @@ BsonDocument getCommandDocument(final ByteBufferBsonOutput bsonOutput) {
145152 try {
146153 CompositeByteBuf byteBuf = new CompositeByteBuf (byteBuffers );
147154 try {
148- byteBuf .position (getEncodingMetadata (). getFirstDocumentPosition () );
155+ byteBuf .position (firstDocumentPosition );
149156 ByteBufBsonDocument byteBufBsonDocument = createOne (byteBuf );
150157
151158 // If true, it means there is at least one `PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE` section in the OP_MSG
@@ -223,9 +230,8 @@ boolean isResponseExpected() {
223230 }
224231
225232 @ Override
226- protected EncodingMetadata encodeMessageBodyWithMetadata (final ByteBufferBsonOutput bsonOutput , final OperationContext operationContext ) {
227- int commandStartPosition = useOpMsg () ? writeOpMsg (bsonOutput , operationContext ) : writeOpQuery (bsonOutput );
228- return new EncodingMetadata (commandStartPosition );
233+ protected void encodeMessageBody (final ByteBufferBsonOutput bsonOutput , final OperationContext operationContext ) {
234+ this .firstDocumentPosition = useOpMsg () ? writeOpMsg (bsonOutput , operationContext ) : writeOpQuery (bsonOutput );
229235 }
230236
231237 @ SuppressWarnings ("try" )
@@ -237,7 +243,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon
237243 int commandStartPosition = bsonOutput .getPosition ();
238244 List <BsonElement > extraElements = getExtraElements (operationContext );
239245
240- int commandDocumentSizeInBytes = writeDocument ( command , bsonOutput , commandFieldNameValidator );
246+ int commandDocumentSizeInBytes = writeCommand ( bsonOutput );
241247 if (sequences instanceof SplittablePayload ) {
242248 appendElementsToDocument (bsonOutput , commandStartPosition , extraElements );
243249 SplittablePayload payload = (SplittablePayload ) sequences ;
@@ -288,7 +294,7 @@ private int writeOpQuery(final ByteBufferBsonOutput bsonOutput) {
288294 elements = new ArrayList <>(3 );
289295 addServerApiElements (elements );
290296 }
291- writeDocument ( command , bsonOutput , commandFieldNameValidator );
297+ writeCommand ( bsonOutput );
292298 appendElementsToDocument (bsonOutput , commandStartPosition , elements );
293299 return commandStartPosition ;
294300 }
@@ -416,6 +422,13 @@ public String getDatabase() {
416422 return database ;
417423 }
418424
425+ private int writeCommand (final BsonOutput bsonOutput ) {
426+ BsonBinaryWriter writer = createBsonBinaryWriter (bsonOutput , commandFieldNameValidator , getSettings ());
427+ int documentStart = bsonOutput .getPosition ();
428+ encodeUsingRegistry (writer , command );
429+ return bsonOutput .getPosition () - documentStart ;
430+ }
431+
419432 @ FunctionalInterface
420433 private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
421434 void close ();
0 commit comments