1
- /* Copyright 2013-present MongoDB Inc.
1
+ /* Copyright 2013-present MongoDB Inc.
2
2
*
3
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
4
* you may not use this file except in compliance with the License.
13
13
* limitations under the License.
14
14
*/
15
15
16
+ using System . Linq ;
16
17
using System . Threading ;
17
18
using System . Threading . Tasks ;
18
19
using MongoDB . Bson ;
20
+ using MongoDB . Bson . Serialization . Serializers ;
19
21
using MongoDB . Driver . Core . Bindings ;
20
22
using MongoDB . Driver . Core . Events ;
21
23
using MongoDB . Driver . Core . Misc ;
@@ -26,9 +28,10 @@ namespace MongoDB.Driver.Core.Operations
26
28
/// <summary>
27
29
/// Represents a list collections operation.
28
30
/// </summary>
29
- public class ListCollectionsOperation : IReadOperation < IAsyncCursor < BsonDocument > >
31
+ public class ListCollectionsOperation : IReadOperation < IAsyncCursor < BsonDocument > > , IExecutableInRetryableReadContext < IAsyncCursor < BsonDocument > >
30
32
{
31
33
// fields
34
+ private bool ? _authorizedCollections ;
32
35
private int ? _batchSize ;
33
36
private BsonDocument _filter ;
34
37
private readonly DatabaseNamespace _databaseNamespace ;
@@ -51,6 +54,18 @@ public ListCollectionsOperation(
51
54
}
52
55
53
56
// properties
57
+ /// <summary>
58
+ /// Gets or sets the AuthorizedCollections flag.
59
+ /// </summary>
60
+ /// <value>
61
+ /// Whether authorizedCollections flag is set.
62
+ /// </value>
63
+ public bool ? AuthorizedCollections
64
+ {
65
+ get => _authorizedCollections ;
66
+ set => _authorizedCollections = value ;
67
+ }
68
+
54
69
/// <summary>
55
70
/// Gets or sets the batch size.
56
71
/// </summary>
@@ -127,11 +142,22 @@ public IAsyncCursor<BsonDocument> Execute(IReadBinding binding, CancellationToke
127
142
{
128
143
Ensure . IsNotNull ( binding , nameof ( binding ) ) ;
129
144
130
- using ( EventContext . BeginOperation ( ) )
131
145
using ( var context = RetryableReadContext . Create ( binding , _retryRequested , cancellationToken ) )
132
146
{
133
- var operation = CreateOperation ( context . Channel ) ;
134
- return operation . Execute ( context , cancellationToken ) ;
147
+ return Execute ( context , cancellationToken ) ;
148
+ }
149
+ }
150
+
151
+ /// <inheritdoc/>
152
+ public IAsyncCursor < BsonDocument > Execute ( RetryableReadContext context , CancellationToken cancellationToken )
153
+ {
154
+ Ensure . IsNotNull ( context , nameof ( context ) ) ;
155
+
156
+ using ( EventContext . BeginOperation ( ) )
157
+ {
158
+ var operation = CreateOperation ( ) ;
159
+ var result = operation . Execute ( context , cancellationToken ) ;
160
+ return CreateCursor ( context . ChannelSource , context . Channel , result ) ;
135
161
}
136
162
}
137
163
@@ -140,24 +166,58 @@ public async Task<IAsyncCursor<BsonDocument>> ExecuteAsync(IReadBinding binding,
140
166
{
141
167
Ensure . IsNotNull ( binding , nameof ( binding ) ) ;
142
168
143
- using ( EventContext . BeginOperation ( ) )
144
169
using ( var context = await RetryableReadContext . CreateAsync ( binding , _retryRequested , cancellationToken ) . ConfigureAwait ( false ) )
145
170
{
146
- var operation = CreateOperation ( context . Channel ) ;
147
- return await operation . ExecuteAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
171
+ return await ExecuteAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
172
+ }
173
+ }
174
+
175
+ /// <inheritdoc/>
176
+ public async Task < IAsyncCursor < BsonDocument > > ExecuteAsync ( RetryableReadContext context , CancellationToken cancellationToken )
177
+ {
178
+ Ensure . IsNotNull ( context , nameof ( context ) ) ;
179
+
180
+ using ( EventContext . BeginOperation ( ) )
181
+ {
182
+ var operation = CreateOperation ( ) ;
183
+ var result = await operation . ExecuteAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
184
+ return CreateCursor ( context . ChannelSource , context . Channel , result ) ;
148
185
}
149
186
}
150
187
151
188
// private methods
152
- private IExecutableInRetryableReadContext < IAsyncCursor < BsonDocument > > CreateOperation ( IChannel channel )
189
+ private ReadCommandOperation < BsonDocument > CreateOperation ( )
153
190
{
154
- return new ListCollectionsUsingCommandOperation ( _databaseNamespace , _messageEncoderSettings )
191
+ var command = new BsonDocument
192
+ {
193
+ { "listCollections" , 1 } ,
194
+ { "filter" , _filter , _filter != null } ,
195
+ { "nameOnly" , ( ) => _nameOnly . Value , _nameOnly . HasValue } ,
196
+ { "cursor" , ( ) => new BsonDocument ( "batchSize" , _batchSize . Value ) , _batchSize . HasValue } ,
197
+ { "authorizedCollections" , ( ) => _authorizedCollections . Value , _authorizedCollections . HasValue }
198
+ } ;
199
+ return new ReadCommandOperation < BsonDocument > ( _databaseNamespace , command , BsonDocumentSerializer . Instance , _messageEncoderSettings )
155
200
{
156
- BatchSize = _batchSize ,
157
- Filter = _filter ,
158
- NameOnly = _nameOnly ,
159
201
RetryRequested = _retryRequested // might be overridden by retryable read context
160
202
} ;
161
203
}
204
+
205
+ private IAsyncCursor < BsonDocument > CreateCursor ( IChannelSourceHandle channelSource , IChannelHandle channel , BsonDocument result )
206
+ {
207
+ var cursorDocument = result [ "cursor" ] . AsBsonDocument ;
208
+ var cursorId = cursorDocument [ "id" ] . ToInt64 ( ) ;
209
+ var getMoreChannelSource = ChannelPinningHelper . CreateGetMoreChannelSource ( channelSource , channel , cursorId ) ;
210
+ var cursor = new AsyncCursor < BsonDocument > (
211
+ getMoreChannelSource ,
212
+ CollectionNamespace . FromFullName ( cursorDocument [ "ns" ] . AsString ) ,
213
+ cursorDocument [ "firstBatch" ] . AsBsonArray . OfType < BsonDocument > ( ) . ToList ( ) ,
214
+ cursorId ,
215
+ batchSize : _batchSize ?? 0 ,
216
+ 0 ,
217
+ BsonDocumentSerializer . Instance ,
218
+ _messageEncoderSettings ) ;
219
+
220
+ return cursor ;
221
+ }
162
222
}
163
223
}
0 commit comments