Skip to content

Commit d930018

Browse files
CSHARP-3918: Support authorizedCollections option for listCollections helpers.
1 parent 28ed947 commit d930018

File tree

7 files changed

+139
-526
lines changed

7 files changed

+139
-526
lines changed

src/MongoDB.Driver.Core/Core/Operations/ListCollectionsOperation.cs

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2013-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -13,9 +13,11 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Linq;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819
using MongoDB.Bson;
20+
using MongoDB.Bson.Serialization.Serializers;
1921
using MongoDB.Driver.Core.Bindings;
2022
using MongoDB.Driver.Core.Events;
2123
using MongoDB.Driver.Core.Misc;
@@ -26,9 +28,10 @@ namespace MongoDB.Driver.Core.Operations
2628
/// <summary>
2729
/// Represents a list collections operation.
2830
/// </summary>
29-
public class ListCollectionsOperation : IReadOperation<IAsyncCursor<BsonDocument>>
31+
public class ListCollectionsOperation : IReadOperation<IAsyncCursor<BsonDocument>>, IExecutableInRetryableReadContext<IAsyncCursor<BsonDocument>>
3032
{
3133
// fields
34+
private bool? _authorizedCollections;
3235
private int? _batchSize;
3336
private BsonDocument _filter;
3437
private readonly DatabaseNamespace _databaseNamespace;
@@ -51,6 +54,18 @@ public ListCollectionsOperation(
5154
}
5255

5356
// 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+
5469
/// <summary>
5570
/// Gets or sets the batch size.
5671
/// </summary>
@@ -127,11 +142,22 @@ public IAsyncCursor<BsonDocument> Execute(IReadBinding binding, CancellationToke
127142
{
128143
Ensure.IsNotNull(binding, nameof(binding));
129144

130-
using (EventContext.BeginOperation())
131145
using (var context = RetryableReadContext.Create(binding, _retryRequested, cancellationToken))
132146
{
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);
135161
}
136162
}
137163

@@ -140,24 +166,58 @@ public async Task<IAsyncCursor<BsonDocument>> ExecuteAsync(IReadBinding binding,
140166
{
141167
Ensure.IsNotNull(binding, nameof(binding));
142168

143-
using (EventContext.BeginOperation())
144169
using (var context = await RetryableReadContext.CreateAsync(binding, _retryRequested, cancellationToken).ConfigureAwait(false))
145170
{
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);
148185
}
149186
}
150187

151188
// private methods
152-
private IExecutableInRetryableReadContext<IAsyncCursor<BsonDocument>> CreateOperation(IChannel channel)
189+
private ReadCommandOperation<BsonDocument> CreateOperation()
153190
{
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)
155200
{
156-
BatchSize = _batchSize,
157-
Filter = _filter,
158-
NameOnly = _nameOnly,
159201
RetryRequested = _retryRequested // might be overridden by retryable read context
160202
};
161203
}
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+
}
162222
}
163223
}

src/MongoDB.Driver.Core/Core/Operations/ListCollectionsUsingCommandOperation.cs

Lines changed: 0 additions & 209 deletions
This file was deleted.

src/MongoDB.Driver/ListCollectionNamesOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ namespace MongoDB.Driver
2323
public sealed class ListCollectionNamesOptions
2424
{
2525
// fields
26+
private bool? authorizedCollections;
2627
private FilterDefinition<BsonDocument> _filter;
2728

2829
// properties
30+
/// <summary>
31+
/// Gets or sets the AuthorizedCollections flag.
32+
/// </summary>
33+
public bool? AuthorizedCollections
34+
{
35+
get { return authorizedCollections; }
36+
set { authorizedCollections = value; }
37+
}
38+
2939
/// <summary>
3040
/// Gets or sets the filter.
3141
/// </summary>

src/MongoDB.Driver/MongoDatabaseImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ private ListCollectionsOperation CreateListCollectionNamesOperation(ListCollecti
693693
var messageEncoderSettings = GetMessageEncoderSettings();
694694
return new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
695695
{
696+
AuthorizedCollections = options?.AuthorizedCollections,
696697
Filter = options?.Filter?.Render(_settings.SerializerRegistry.GetSerializer<BsonDocument>(), _settings.SerializerRegistry, _linqProvider),
697698
NameOnly = true,
698699
RetryRequested = _client.Settings.RetryReads

0 commit comments

Comments
 (0)