@@ -77,13 +77,13 @@ public final class CommandMessage extends RequestMessage {
77
77
*/
78
78
private static final byte PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE = 1 ;
79
79
80
- private final MongoNamespace namespace ;
81
80
private final BsonDocument command ;
82
81
private final FieldNameValidator commandFieldNameValidator ;
83
82
private final ReadPreference readPreference ;
84
83
private final boolean exhaustAllowed ;
85
84
private final MessageSequences sequences ;
86
85
private final boolean responseExpected ;
86
+ private final String database ;
87
87
/**
88
88
* {@code null} iff either {@link #sequences} is not of the {@link DualMessageSequences} type,
89
89
* or it is of that type, but it has not been {@linkplain #encodeMessageBodyWithMetadata(ByteBufferBsonOutput, OperationContext) encoded}.
@@ -93,35 +93,35 @@ public final class CommandMessage extends RequestMessage {
93
93
private final ClusterConnectionMode clusterConnectionMode ;
94
94
private final ServerApi serverApi ;
95
95
96
- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
96
+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
97
97
final ReadPreference readPreference , final MessageSettings settings , final ClusterConnectionMode clusterConnectionMode ,
98
98
@ Nullable final ServerApi serverApi ) {
99
- this (namespace , command , commandFieldNameValidator , readPreference , settings , true , EmptyMessageSequences .INSTANCE ,
99
+ this (database , command , commandFieldNameValidator , readPreference , settings , true , EmptyMessageSequences .INSTANCE ,
100
100
clusterConnectionMode , serverApi );
101
101
}
102
102
103
- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
103
+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
104
104
final ReadPreference readPreference , final MessageSettings settings , final boolean exhaustAllowed ,
105
105
final ClusterConnectionMode clusterConnectionMode , @ Nullable final ServerApi serverApi ) {
106
- this (namespace , command , commandFieldNameValidator , readPreference , settings , true , exhaustAllowed , EmptyMessageSequences .INSTANCE ,
106
+ this (database , command , commandFieldNameValidator , readPreference , settings , true , exhaustAllowed , EmptyMessageSequences .INSTANCE ,
107
107
clusterConnectionMode , serverApi );
108
108
}
109
109
110
- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
110
+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
111
111
final ReadPreference readPreference , final MessageSettings settings , final boolean responseExpected ,
112
112
final MessageSequences sequences ,
113
113
final ClusterConnectionMode clusterConnectionMode , @ Nullable final ServerApi serverApi ) {
114
- this (namespace , command , commandFieldNameValidator , readPreference , settings , responseExpected , false ,
114
+ this (database , command , commandFieldNameValidator , readPreference , settings , responseExpected , false ,
115
115
sequences , clusterConnectionMode , serverApi );
116
116
}
117
117
118
- CommandMessage (final MongoNamespace namespace , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
118
+ CommandMessage (final String database , final BsonDocument command , final FieldNameValidator commandFieldNameValidator ,
119
119
final ReadPreference readPreference , final MessageSettings settings ,
120
120
final boolean responseExpected , final boolean exhaustAllowed ,
121
121
final MessageSequences sequences ,
122
122
final ClusterConnectionMode clusterConnectionMode , @ Nullable final ServerApi serverApi ) {
123
- super (namespace . getFullName (), getOpCode (settings , clusterConnectionMode , serverApi ), settings );
124
- this .namespace = namespace ;
123
+ super (getOpCode (settings , clusterConnectionMode , serverApi ), settings );
124
+ this .database = database ;
125
125
this .command = command ;
126
126
this .commandFieldNameValidator = commandFieldNameValidator ;
127
127
this .readPreference = readPreference ;
@@ -222,10 +222,6 @@ boolean isResponseExpected() {
222
222
}
223
223
}
224
224
225
- MongoNamespace getNamespace () {
226
- return namespace ;
227
- }
228
-
229
225
@ Override
230
226
protected EncodingMetadata encodeMessageBodyWithMetadata (final ByteBufferBsonOutput bsonOutput , final OperationContext operationContext ) {
231
227
int commandStartPosition = useOpMsg () ? writeOpMsg (bsonOutput , operationContext ) : writeOpQuery (bsonOutput );
@@ -281,7 +277,7 @@ private int writeOpMsg(final ByteBufferBsonOutput bsonOutput, final OperationCon
281
277
282
278
private int writeOpQuery (final ByteBufferBsonOutput bsonOutput ) {
283
279
bsonOutput .writeInt32 (0 );
284
- bsonOutput .writeCString (namespace .getFullName ());
280
+ bsonOutput .writeCString (new MongoNamespace ( getDatabase (), "$cmd" ) .getFullName ());
285
281
bsonOutput .writeInt32 (0 );
286
282
bsonOutput .writeInt32 (-1 );
287
283
@@ -328,7 +324,7 @@ private List<BsonElement> getExtraElements(final OperationContext operationConte
328
324
extraElements .add (new BsonElement ("maxTimeMS" , new BsonInt64 (maxTimeMS )))
329
325
);
330
326
}
331
- extraElements .add (new BsonElement ("$db" , new BsonString (new MongoNamespace ( getCollectionName ()). getDatabaseName ())));
327
+ extraElements .add (new BsonElement ("$db" , new BsonString (getDatabase ())));
332
328
if (sessionContext .getClusterTime () != null ) {
333
329
extraElements .add (new BsonElement ("$clusterTime" , sessionContext .getClusterTime ()));
334
330
}
@@ -411,6 +407,15 @@ private static boolean isServerVersionKnown(final MessageSettings settings) {
411
407
return settings .getMaxWireVersion () != UNKNOWN_WIRE_VERSION ;
412
408
}
413
409
410
+ /**
411
+ * Gets the database name
412
+ *
413
+ * @return the database name
414
+ */
415
+ public String getDatabase () {
416
+ return database ;
417
+ }
418
+
414
419
@ FunctionalInterface
415
420
private interface FinishOpMsgSectionWithPayloadType1 extends AutoCloseable {
416
421
void close ();
0 commit comments