35
35
import org .bson .BsonString ;
36
36
import org .bson .ByteBuf ;
37
37
import org .bson .FieldNameValidator ;
38
+ import org .bson .io .BsonOutput ;
38
39
39
40
import java .io .ByteArrayOutputStream ;
40
41
import java .io .UnsupportedEncodingException ;
55
56
import static com .mongodb .connection .ServerType .STANDALONE ;
56
57
import static com .mongodb .internal .connection .BsonWriterHelper .appendElementsToDocument ;
57
58
import 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 ;
58
61
import static com .mongodb .internal .connection .BsonWriterHelper .writeDocumentsOfDualMessageSequences ;
59
62
import static com .mongodb .internal .connection .BsonWriterHelper .writePayload ;
60
63
import static com .mongodb .internal .connection .ByteBufBsonDocument .createList ;
@@ -77,16 +80,20 @@ public final class CommandMessage extends RequestMessage {
77
80
*/
78
81
private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1 ;
79
82
83
+ private static final int UNINITIALIZED_POSITION = -1 ;
84
+
80
85
private final BsonDocument command ;
81
86
private final FieldNameValidator commandFieldNameValidator ;
82
87
private final ReadPreference readPreference ;
83
88
private final boolean exhaustAllowed ;
84
89
private final MessageSequences sequences ;
85
90
private final boolean responseExpected ;
86
91
private final String database ;
92
+ private int firstDocumentPosition = UNINITIALIZED_POSITION ;
93
+
87
94
/**
88
95
* {@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}.
90
97
*/
91
98
@ Nullable
92
99
private Boolean dualMessageSequencesRequireResponse ;
@@ -145,7 +152,7 @@ BsonDocument getCommandDocument(final ByteBufferBsonOutput bsonOutput) {
145
152
try {
146
153
CompositeByteBuf byteBuf = new CompositeByteBuf (byteBuffers );
147
154
try {
148
- byteBuf .position (getEncodingMetadata (). getFirstDocumentPosition () );
155
+ byteBuf .position (firstDocumentPosition );
149
156
ByteBufBsonDocument byteBufBsonDocument = createOne (byteBuf );
150
157
151
158
// 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() {
223
230
}
224
231
225
232
@ 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 );
229
235
}
230
236
231
237
@ SuppressWarnings ("try" )
@@ -237,7 +243,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon
237
243
int commandStartPosition = bsonOutput .getPosition ();
238
244
List <BsonElement > extraElements = getExtraElements (operationContext );
239
245
240
- int commandDocumentSizeInBytes = writeDocument ( command , bsonOutput , commandFieldNameValidator );
246
+ int commandDocumentSizeInBytes = writeCommand ( bsonOutput );
241
247
if (sequences instanceof SplittablePayload ) {
242
248
appendElementsToDocument (bsonOutput , commandStartPosition , extraElements );
243
249
SplittablePayload payload = (SplittablePayload ) sequences ;
@@ -288,7 +294,7 @@ private int writeOpQuery(final ByteBufferBsonOutput bsonOutput) {
288
294
elements = new ArrayList <>(3 );
289
295
addServerApiElements (elements );
290
296
}
291
- writeDocument ( command , bsonOutput , commandFieldNameValidator );
297
+ writeCommand ( bsonOutput );
292
298
appendElementsToDocument (bsonOutput , commandStartPosition , elements );
293
299
return commandStartPosition ;
294
300
}
@@ -416,6 +422,13 @@ public String getDatabase() {
416
422
return database ;
417
423
}
418
424
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
+
419
432
@ FunctionalInterface
420
433
private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
421
434
void close ();
0 commit comments