19
19
using MongoDB . Bson ;
20
20
using MongoDB . Bson . Serialization . Serializers ;
21
21
using MongoDB . Driver . Core . Bindings ;
22
+ using MongoDB . Driver . Core . Connections ;
22
23
using MongoDB . Driver . Core . Misc ;
23
24
using MongoDB . Driver . Core . WireProtocol . Messages . Encoders ;
24
25
25
26
namespace MongoDB . Driver . Core . Operations
26
27
{
28
+ /// <summary>
29
+ /// Represents an explainable operation.
30
+ /// </summary>
31
+ public interface IExplainableOperation
32
+ {
33
+ /// <summary>
34
+ /// Creates the command to be explained.
35
+ /// </summary>
36
+ /// <param name="connectionDescription">The connection description.</param>
37
+ /// <param name="session">The session.</param>
38
+ /// <returns>The command.</returns>
39
+ BsonDocument CreateCommand ( ConnectionDescription connectionDescription , ICoreSession session ) ;
40
+ }
41
+
27
42
/// <summary>
28
43
/// Represents an explain operation.
29
44
/// </summary>
30
45
public class ExplainOperation : IReadOperation < BsonDocument > , IWriteOperation < BsonDocument >
31
46
{
32
47
// fields
33
48
private readonly DatabaseNamespace _databaseNamespace ;
34
- private readonly BsonDocument _command ;
49
+ private readonly IExplainableOperation _explainableOperation ;
35
50
private readonly MessageEncoderSettings _messageEncoderSettings ;
36
51
private ExplainVerbosity _verbosity ;
37
52
@@ -40,12 +55,12 @@ public class ExplainOperation : IReadOperation<BsonDocument>, IWriteOperation<Bs
40
55
/// Initializes a new instance of the <see cref="ExplainOperation"/> class.
41
56
/// </summary>
42
57
/// <param name="databaseNamespace">The database namespace.</param>
43
- /// <param name="command ">The command .</param>
58
+ /// <param name="explainableOperation ">The explainable operation .</param>
44
59
/// <param name="messageEncoderSettings">The message encoder settings.</param>
45
- public ExplainOperation ( DatabaseNamespace databaseNamespace , BsonDocument command , MessageEncoderSettings messageEncoderSettings )
60
+ public ExplainOperation ( DatabaseNamespace databaseNamespace , IExplainableOperation explainableOperation , MessageEncoderSettings messageEncoderSettings )
46
61
{
47
62
_databaseNamespace = Ensure . IsNotNull ( databaseNamespace , nameof ( databaseNamespace ) ) ;
48
- _command = Ensure . IsNotNull ( command , nameof ( command ) ) ;
63
+ _explainableOperation = Ensure . IsNotNull ( explainableOperation , nameof ( explainableOperation ) ) ;
49
64
_messageEncoderSettings = Ensure . IsNotNull ( messageEncoderSettings , nameof ( messageEncoderSettings ) ) ;
50
65
_verbosity = ExplainVerbosity . QueryPlanner ;
51
66
}
@@ -63,14 +78,14 @@ public DatabaseNamespace DatabaseNamespace
63
78
}
64
79
65
80
/// <summary>
66
- /// Gets the command to be explained.
81
+ /// Gets the operation to be explained.
67
82
/// </summary>
68
83
/// <value>
69
- /// The command to be explained.
84
+ /// The operation to be explained.
70
85
/// </value>
71
- public BsonDocument Command
86
+ public IExplainableOperation ExplainableOperation
72
87
{
73
- get { return _command ; }
88
+ get { return _explainableOperation ; }
74
89
}
75
90
76
91
/// <summary>
@@ -100,29 +115,49 @@ public ExplainVerbosity Verbosity
100
115
/// <inheritdoc/>
101
116
public BsonDocument Execute ( IReadBinding binding , CancellationToken cancellationToken )
102
117
{
103
- var operation = CreateReadOperation ( ) ;
104
- return operation . Execute ( binding , cancellationToken ) ;
118
+ using ( var channelSource = binding . GetReadChannelSource ( cancellationToken ) )
119
+ using ( var channel = channelSource . GetChannel ( cancellationToken ) )
120
+ using ( var channelBinding = new ChannelReadBinding ( channelSource . Server , channel , binding . ReadPreference , binding . Session ) )
121
+ {
122
+ var operation = CreateReadOperation ( channel . ConnectionDescription , binding . Session ) ;
123
+ return operation . Execute ( channelBinding , cancellationToken ) ;
124
+ }
105
125
}
106
126
107
127
/// <inheritdoc/>
108
128
public BsonDocument Execute ( IWriteBinding binding , CancellationToken cancellationToken )
109
129
{
110
- var operation = CreateWriteOperation ( ) ;
111
- return operation . Execute ( binding , cancellationToken ) ;
130
+ using ( var channelSource = binding . GetWriteChannelSource ( cancellationToken ) )
131
+ using ( var channel = channelSource . GetChannel ( cancellationToken ) )
132
+ using ( var channelBinding = new ChannelReadWriteBinding ( channelSource . Server , channel , binding . Session ) )
133
+ {
134
+ var operation = CreateWriteOperation ( channel . ConnectionDescription , binding . Session ) ;
135
+ return operation . Execute ( channelBinding , cancellationToken ) ;
136
+ }
112
137
}
113
138
114
139
/// <inheritdoc/>
115
- public Task < BsonDocument > ExecuteAsync ( IReadBinding binding , CancellationToken cancellationToken )
140
+ public async Task < BsonDocument > ExecuteAsync ( IReadBinding binding , CancellationToken cancellationToken )
116
141
{
117
- var operation = CreateReadOperation ( ) ;
118
- return operation . ExecuteAsync ( binding , cancellationToken ) ;
142
+ using ( var channelSource = await binding . GetReadChannelSourceAsync ( cancellationToken ) . ConfigureAwait ( false ) )
143
+ using ( var channel = await channelSource . GetChannelAsync ( cancellationToken ) . ConfigureAwait ( false ) )
144
+ using ( var channelBinding = new ChannelReadBinding ( channelSource . Server , channel , binding . ReadPreference , binding . Session ) )
145
+ {
146
+ var operation = CreateReadOperation ( channel . ConnectionDescription , binding . Session ) ;
147
+ return await operation . ExecuteAsync ( channelBinding , cancellationToken ) . ConfigureAwait ( false ) ;
148
+ }
119
149
}
120
150
121
151
/// <inheritdoc/>
122
- public Task < BsonDocument > ExecuteAsync ( IWriteBinding binding , CancellationToken cancellationToken )
152
+ public async Task < BsonDocument > ExecuteAsync ( IWriteBinding binding , CancellationToken cancellationToken )
123
153
{
124
- var operation = CreateWriteOperation ( ) ;
125
- return operation . ExecuteAsync ( binding , cancellationToken ) ;
154
+ using ( var channelSource = await binding . GetWriteChannelSourceAsync ( cancellationToken ) . ConfigureAwait ( false ) )
155
+ using ( var channel = await channelSource . GetChannelAsync ( cancellationToken ) . ConfigureAwait ( false ) )
156
+ using ( var channelBinding = new ChannelReadWriteBinding ( channelSource . Server , channel , binding . Session ) )
157
+ {
158
+ var operation = CreateWriteOperation ( channel . ConnectionDescription , binding . Session ) ;
159
+ return await operation . ExecuteAsync ( channelBinding , cancellationToken ) . ConfigureAwait ( false ) ;
160
+ }
126
161
}
127
162
128
163
// private methods
@@ -142,34 +177,35 @@ private static string ConvertVerbosityToString(ExplainVerbosity verbosity)
142
177
}
143
178
}
144
179
145
- internal BsonDocument CreateCommand ( )
180
+ internal BsonDocument CreateExplainCommand ( ConnectionDescription connectionDescription , ICoreSession session )
146
181
{
182
+ var explainableCommand = _explainableOperation . CreateCommand ( connectionDescription , session ) ;
147
183
return new BsonDocument
148
184
{
149
- { "explain" , _command } ,
185
+ { "explain" , explainableCommand } ,
150
186
{ "verbosity" , ConvertVerbosityToString ( _verbosity ) }
151
187
} ;
152
188
}
153
189
154
- private ReadCommandOperation < BsonDocument > CreateReadOperation ( )
190
+ private ReadCommandOperation < BsonDocument > CreateReadOperation ( ConnectionDescription connectionDescription , ICoreSession session )
155
191
{
156
- var command = CreateCommand ( ) ;
192
+ var explainCommand = CreateExplainCommand ( connectionDescription , session ) ;
157
193
return new ReadCommandOperation < BsonDocument > (
158
194
_databaseNamespace ,
159
- command ,
195
+ explainCommand ,
160
196
BsonDocumentSerializer . Instance ,
161
197
_messageEncoderSettings )
162
198
{
163
199
RetryRequested = false
164
200
} ;
165
201
}
166
202
167
- private WriteCommandOperation < BsonDocument > CreateWriteOperation ( )
203
+ private WriteCommandOperation < BsonDocument > CreateWriteOperation ( ConnectionDescription connectionDescription , ICoreSession session )
168
204
{
169
- var command = CreateCommand ( ) ;
205
+ var explainCommand = CreateExplainCommand ( connectionDescription , session ) ;
170
206
return new WriteCommandOperation < BsonDocument > (
171
207
_databaseNamespace ,
172
- command ,
208
+ explainCommand ,
173
209
BsonDocumentSerializer . Instance ,
174
210
_messageEncoderSettings ) ;
175
211
}
0 commit comments