22
22
import com .mongodb .ServerApi ;
23
23
import com .mongodb .connection .ClusterConnectionMode ;
24
24
import com .mongodb .internal .session .SessionContext ;
25
- import com .mongodb .internal .validator .MappedFieldNameValidator ;
26
25
import com .mongodb .lang .Nullable ;
27
26
import org .bson .BsonArray ;
28
27
import org .bson .BsonBinaryWriter ;
31
30
import org .bson .BsonElement ;
32
31
import org .bson .BsonInt64 ;
33
32
import org .bson .BsonString ;
34
- import org .bson .BsonWriter ;
35
33
import org .bson .FieldNameValidator ;
36
- import org .bson .codecs .EncoderContext ;
37
34
import org .bson .io .BsonOutput ;
38
35
39
36
import java .nio .charset .StandardCharsets ;
40
37
import java .util .ArrayList ;
41
- import java .util .HashMap ;
42
38
import java .util .List ;
43
- import java .util .Map ;
44
39
45
40
import static com .mongodb .ReadPreference .primary ;
46
41
import static com .mongodb .ReadPreference .primaryPreferred ;
@@ -117,7 +112,7 @@ BsonDocument getCommandDocument(final ByteBufferBsonOutput bsonOutput) {
117
112
getEncodingMetadata ().getFirstDocumentPosition ());
118
113
BsonDocument commandBsonDocument ;
119
114
120
- if (useOpMsg () && containsPayload ()) {
115
+ if (containsPayload ()) {
121
116
commandBsonDocument = byteBufBsonDocument .toBaseBsonDocument ();
122
117
123
118
int payloadStartPosition = getEncodingMetadata ().getFirstDocumentPosition ()
@@ -131,10 +126,7 @@ BsonDocument getCommandDocument(final ByteBufferBsonOutput bsonOutput) {
131
126
commandBsonDocument = byteBufBsonDocument ;
132
127
}
133
128
134
- if (commandBsonDocument .containsKey ("$query" )) {
135
- commandBsonDocument = commandBsonDocument .getDocument ("$query" );
136
- }
137
- return commandBsonDocument ;
129
+ return commandBsonDocument ;
138
130
}
139
131
140
132
boolean containsPayload () {
@@ -160,7 +152,7 @@ protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput bsonOu
160
152
bsonOutput .writeByte (0 ); // payload type
161
153
commandStartPosition = bsonOutput .getPosition ();
162
154
163
- addDocument (getCommandToEncode () , bsonOutput , commandFieldNameValidator , getExtraElements (sessionContext ));
155
+ addDocument (command , bsonOutput , commandFieldNameValidator , getExtraElements (sessionContext ));
164
156
165
157
if (payload != null ) {
166
158
bsonOutput .writeByte (1 ); // payload type
@@ -177,48 +169,24 @@ protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput bsonOu
177
169
// Write the flag bits
178
170
bsonOutput .writeInt32 (flagPosition , getOpMsgFlagBits ());
179
171
} else {
180
- bsonOutput .writeInt32 (getOpQueryFlagBits () );
172
+ bsonOutput .writeInt32 (0 );
181
173
bsonOutput .writeCString (namespace .getFullName ());
182
174
bsonOutput .writeInt32 (0 );
183
175
bsonOutput .writeInt32 (-1 );
184
176
185
177
commandStartPosition = bsonOutput .getPosition ();
186
178
187
- if (payload == null ) {
188
- List <BsonElement > elements = null ;
189
- if (serverApi != null ) {
190
- elements = new ArrayList <>(3 );
191
- addServerApiElements (elements );
192
- }
193
- addDocument (getCommandToEncode (), bsonOutput , commandFieldNameValidator , elements );
194
- } else {
195
- // We're not concerned with adding ServerApi elements here. The only reason we do it for OP_QUERY-based commands is that
196
- // OP_QUERY is always used for the handshake, and we have to pass ServerApi elements in the handshake. Other than that,
197
- // all servers that support ServerApi also support OP_MSG, so this code path should never be hit.
198
- addDocumentWithPayload (bsonOutput , messageStartPosition );
179
+ List <BsonElement > elements = null ;
180
+ if (serverApi != null ) {
181
+ elements = new ArrayList <>(3 );
182
+ addServerApiElements (elements );
199
183
}
184
+ addDocument (command , bsonOutput , commandFieldNameValidator , elements );
200
185
}
201
186
return new EncodingMetadata (commandStartPosition );
202
187
}
203
188
204
- private FieldNameValidator getPayloadArrayFieldNameValidator () {
205
- Map <String , FieldNameValidator > rootMap = new HashMap <String , FieldNameValidator >();
206
- rootMap .put (payload .getPayloadName (), payloadFieldNameValidator );
207
- return new MappedFieldNameValidator (commandFieldNameValidator , rootMap );
208
- }
209
-
210
- private void addDocumentWithPayload (final BsonOutput bsonOutput , final int messageStartPosition ) {
211
- BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter (bsonOutput , getPayloadArrayFieldNameValidator ());
212
- BsonWriter bsonWriter = new SplittablePayloadBsonWriter (bsonBinaryWriter , bsonOutput , messageStartPosition , getSettings (), payload );
213
- BsonDocument commandToEncode = getCommandToEncode ();
214
- getCodec (commandToEncode ).encode (bsonWriter , commandToEncode , EncoderContext .builder ().build ());
215
- }
216
-
217
189
private int getOpMsgFlagBits () {
218
- return getOpMsgResponseExpectedFlagBit ();
219
- }
220
-
221
- private int getOpMsgResponseExpectedFlagBit () {
222
190
int flagBits = 0 ;
223
191
if (!requireOpMsgResponse ()) {
224
192
flagBits = 1 << 1 ;
@@ -237,22 +205,6 @@ private boolean requireOpMsgResponse() {
237
205
}
238
206
}
239
207
240
- private int getOpQueryFlagBits () {
241
- return getOpQuerySecondaryOkFlagBit ();
242
- }
243
-
244
- private int getOpQuerySecondaryOkFlagBit () {
245
- if (isSecondaryOk ()) {
246
- return 1 << 2 ;
247
- } else {
248
- return 0 ;
249
- }
250
- }
251
-
252
- private boolean isSecondaryOk () {
253
- return (readPreference != null && readPreference .isSecondaryOk ()) || isDirectConnectionToReplicaSetMember ();
254
- }
255
-
256
208
private boolean isDirectConnectionToReplicaSetMember () {
257
209
return clusterConnectionMode == SINGLE
258
210
&& getSettings ().getServerType () != SHARD_ROUTER
@@ -263,16 +215,8 @@ private boolean useOpMsg() {
263
215
return getOpCode ().equals (OpCode .OP_MSG );
264
216
}
265
217
266
- private BsonDocument getCommandToEncode () {
267
- BsonDocument commandToEncode = command ;
268
- if (!useOpMsg () && readPreference != null && !readPreference .equals (primary ())) {
269
- commandToEncode = new BsonDocument ("$query" , command ).append ("$readPreference" , readPreference .toDocument ());
270
- }
271
- return commandToEncode ;
272
- }
273
-
274
218
private List <BsonElement > getExtraElements (final SessionContext sessionContext ) {
275
- List <BsonElement > extraElements = new ArrayList <BsonElement >();
219
+ List <BsonElement > extraElements = new ArrayList <>();
276
220
extraElements .add (new BsonElement ("$db" , new BsonString (new MongoNamespace (getCollectionName ()).getDatabaseName ())));
277
221
if (sessionContext .getClusterTime () != null ) {
278
222
extraElements .add (new BsonElement ("$clusterTime" , sessionContext .getClusterTime ()));
@@ -345,5 +289,4 @@ private static OpCode getOpCode(final MessageSettings settings, final ClusterCon
345
289
private static boolean isServerVersionAtLeastThreeDotSix (final MessageSettings settings ) {
346
290
return settings .getMaxWireVersion () >= THREE_DOT_SIX_WIRE_VERSION ;
347
291
}
348
-
349
292
}
0 commit comments