|
| 1 | +/* Copyright 2021-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Bson.Serialization.Serializers; |
| 22 | +using MongoDB.Driver.Core.Bindings; |
| 23 | +using MongoDB.Driver.Core.Misc; |
| 24 | +using MongoDB.Driver.Core.WireProtocol.Messages.Encoders; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Core.Operations |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Represents an estimated document count operation. |
| 30 | + /// </summary> |
| 31 | + public class EstimatedDocumentCountOperation : IReadOperation<long> |
| 32 | + { |
| 33 | + // private fields |
| 34 | + private readonly CollectionNamespace _collectionNamespace; |
| 35 | + private TimeSpan? _maxTime; |
| 36 | + private readonly MessageEncoderSettings _messageEncoderSettings; |
| 37 | + private ReadConcern _readConcern = ReadConcern.Default; |
| 38 | + private bool _retryRequested; |
| 39 | + |
| 40 | + // constructors |
| 41 | + /// <summary> |
| 42 | + /// Initializes a new instance of the <see cref="EstimatedDocumentCountOperation"/> class. |
| 43 | + /// </summary> |
| 44 | + /// <param name="collectionNamespace">The collection namespace.</param> |
| 45 | + /// <param name="messageEncoderSettings">The message encoder settings.</param> |
| 46 | + public EstimatedDocumentCountOperation(CollectionNamespace collectionNamespace, MessageEncoderSettings messageEncoderSettings) |
| 47 | + { |
| 48 | + _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); |
| 49 | + _messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings)); |
| 50 | + } |
| 51 | + |
| 52 | + // public properties |
| 53 | + /// <summary> |
| 54 | + /// Gets the collection namespace. |
| 55 | + /// </summary> |
| 56 | + public CollectionNamespace CollectionNamespace => _collectionNamespace; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets or sets the maximum time the server should spend on this operation. |
| 60 | + /// </summary> |
| 61 | + public TimeSpan? MaxTime |
| 62 | + { |
| 63 | + get => _maxTime; |
| 64 | + set => _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets the message encoder settings. |
| 69 | + /// </summary> |
| 70 | + public MessageEncoderSettings MessageEncoderSettings => _messageEncoderSettings; |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets or sets the read concern. |
| 74 | + /// </summary> |
| 75 | + public ReadConcern ReadConcern |
| 76 | + { |
| 77 | + get => _readConcern; |
| 78 | + set => _readConcern = Ensure.IsNotNull(value, nameof(value)); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets or sets a value indicating whether to retry. |
| 83 | + /// </summary> |
| 84 | + public bool RetryRequested |
| 85 | + { |
| 86 | + get => _retryRequested; |
| 87 | + set => _retryRequested = value; |
| 88 | + } |
| 89 | + |
| 90 | + /// <inheritdoc/> |
| 91 | + public long Execute(IReadBinding binding, CancellationToken cancellationToken) |
| 92 | + { |
| 93 | + Ensure.IsNotNull(binding, nameof(binding)); |
| 94 | + |
| 95 | + using (var context = RetryableReadContext.Create(binding, _retryRequested, cancellationToken)) |
| 96 | + { |
| 97 | + if (Feature.EstimatedDocumentCountByCollStats.IsSupported(context.Channel.ConnectionDescription.ServerVersion)) |
| 98 | + { |
| 99 | + var operation = CreateAggregationOperation(context.Channel.ConnectionDescription.ServerVersion); |
| 100 | + IAsyncCursor<BsonDocument> cursor; |
| 101 | + try |
| 102 | + { |
| 103 | + cursor = operation.Execute(context, cancellationToken); |
| 104 | + } |
| 105 | + catch (MongoCommandException ex) when (ex.Code == (int)ServerErrorCode.NamespaceNotFound) |
| 106 | + { |
| 107 | + // In the event this aggregation is run against a non-existent namespace, a NamespaceNotFound(26) error will be returned during execution. |
| 108 | + return 0; |
| 109 | + } |
| 110 | + var results = cursor.ToList(cancellationToken); |
| 111 | + |
| 112 | + return ExtractCountFromAggregationResults(results); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + var operation = CreateCountOperation(); |
| 117 | + |
| 118 | + return operation.Execute(context, cancellationToken); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// <inheritdoc/> |
| 124 | + public async Task<long> ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) |
| 125 | + { |
| 126 | + Ensure.IsNotNull(binding, nameof(binding)); |
| 127 | + |
| 128 | + using (var context = RetryableReadContext.Create(binding, _retryRequested, cancellationToken)) |
| 129 | + { |
| 130 | + if (Feature.EstimatedDocumentCountByCollStats.IsSupported(context.Channel.ConnectionDescription.ServerVersion)) |
| 131 | + { |
| 132 | + var operation = CreateAggregationOperation(context.Channel.ConnectionDescription.ServerVersion); |
| 133 | + IAsyncCursor<BsonDocument> cursor; |
| 134 | + try |
| 135 | + { |
| 136 | + cursor = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); |
| 137 | + } |
| 138 | + catch (MongoCommandException ex) when (ex.Code == (int)ServerErrorCode.NamespaceNotFound) |
| 139 | + { |
| 140 | + // In the event this aggregation is run against a non-existent namespace, a NamespaceNotFound(26) error will be returned during execution. |
| 141 | + return 0; |
| 142 | + } |
| 143 | + var results = await cursor.ToListAsync(cancellationToken).ConfigureAwait(false); |
| 144 | + |
| 145 | + return ExtractCountFromAggregationResults(results); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + var operation = CreateCountOperation(); |
| 150 | + |
| 151 | + return await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // private methods |
| 157 | + private IExecutableInRetryableReadContext<IAsyncCursor<BsonDocument>> CreateAggregationOperation(SemanticVersion serverVersion) |
| 158 | + { |
| 159 | + Feature.ReadConcern.ThrowIfNotSupported(serverVersion, _readConcern); |
| 160 | + |
| 161 | + var pipeline = CreateAggregationPipeline(); |
| 162 | + var aggregateOperation = new AggregateOperation<BsonDocument>(_collectionNamespace, pipeline, BsonDocumentSerializer.Instance, _messageEncoderSettings) |
| 163 | + { |
| 164 | + MaxTime = _maxTime, |
| 165 | + ReadConcern = _readConcern, |
| 166 | + RetryRequested = _retryRequested |
| 167 | + }; |
| 168 | + return aggregateOperation; |
| 169 | + |
| 170 | + IEnumerable<BsonDocument> CreateAggregationPipeline() => |
| 171 | + new BsonDocument[] |
| 172 | + { |
| 173 | + new BsonDocument("$collStats", new BsonDocument("count", new BsonDocument())), |
| 174 | + new BsonDocument( |
| 175 | + "$group", |
| 176 | + new BsonDocument |
| 177 | + { |
| 178 | + { "_id", 1 }, |
| 179 | + { "n", new BsonDocument("$sum", "$count") } |
| 180 | + }) |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + private IExecutableInRetryableReadContext<long> CreateCountOperation() |
| 185 | + { |
| 186 | + var countOperation = new CountOperation(_collectionNamespace, _messageEncoderSettings) |
| 187 | + { |
| 188 | + MaxTime = _maxTime, |
| 189 | + ReadConcern = _readConcern, |
| 190 | + RetryRequested = _retryRequested |
| 191 | + }; |
| 192 | + return countOperation; |
| 193 | + } |
| 194 | + |
| 195 | + private long ExtractCountFromAggregationResults(List<BsonDocument> results) => |
| 196 | + results.Count switch |
| 197 | + { |
| 198 | + 0 => 0, |
| 199 | + 1 => results[0]["n"].ToInt64(), |
| 200 | + _ => throw new MongoClientException($"Expected aggregate command for {nameof(EstimatedDocumentCountOperation)} to return 1 document, but got {results.Count}."), |
| 201 | + }; |
| 202 | + } |
| 203 | +} |
0 commit comments