-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
AggregateOperation.cs
541 lines (487 loc) · 20.2 KB
/
AggregateOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* Copyright 2013-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Core.Bindings;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Events;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
namespace MongoDB.Driver.Core.Operations
{
/// <summary>
/// Represents an aggregate operation.
/// </summary>
/// <typeparam name="TResult">The type of the result values.</typeparam>
public class AggregateOperation<TResult> : IReadOperation<IAsyncCursor<TResult>>, IExecutableInRetryableReadContext<IAsyncCursor<TResult>>
{
// fields
private bool? _allowDiskUse;
private int? _batchSize;
private Collation _collation;
private readonly CollectionNamespace _collectionNamespace;
private BsonValue _comment;
private readonly DatabaseNamespace _databaseNamespace;
private BsonValue _hint;
private BsonDocument _let;
private TimeSpan? _maxAwaitTime;
private TimeSpan? _maxTime;
private readonly MessageEncoderSettings _messageEncoderSettings;
private readonly IReadOnlyList<BsonDocument> _pipeline;
private ReadConcern _readConcern = ReadConcern.Default;
private readonly IBsonSerializer<TResult> _resultSerializer;
private bool _retryRequested;
private bool? _useCursor;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="AggregateOperation{TResult}"/> class.
/// </summary>
/// <param name="databaseNamespace">The database namespace.</param>
/// <param name="pipeline">The pipeline.</param>
/// <param name="resultSerializer">The result value serializer.</param>
/// <param name="messageEncoderSettings">The message encoder settings.</param>
public AggregateOperation(DatabaseNamespace databaseNamespace, IEnumerable<BsonDocument> pipeline, IBsonSerializer<TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
: this(pipeline, resultSerializer, messageEncoderSettings)
{
_databaseNamespace = Ensure.IsNotNull(databaseNamespace, nameof(databaseNamespace));
}
/// <summary>
/// Initializes a new instance of the <see cref="AggregateOperation{TResult}"/> class.
/// </summary>
/// <param name="collectionNamespace">The collection namespace.</param>
/// <param name="pipeline">The pipeline.</param>
/// <param name="resultSerializer">The result value serializer.</param>
/// <param name="messageEncoderSettings">The message encoder settings.</param>
public AggregateOperation(CollectionNamespace collectionNamespace, IEnumerable<BsonDocument> pipeline, IBsonSerializer<TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
: this(pipeline, resultSerializer, messageEncoderSettings)
{
_collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace));
}
private AggregateOperation(IEnumerable<BsonDocument> pipeline, IBsonSerializer<TResult> resultSerializer, MessageEncoderSettings messageEncoderSettings)
{
_pipeline = Ensure.IsNotNull(pipeline, nameof(pipeline)).ToList();
_resultSerializer = Ensure.IsNotNull(resultSerializer, nameof(resultSerializer));
_messageEncoderSettings = Ensure.IsNotNull(messageEncoderSettings, nameof(messageEncoderSettings));
}
// properties
/// <summary>
/// Gets or sets a value indicating whether the server is allowed to use the disk.
/// </summary>
/// <value>
/// A value indicating whether the server is allowed to use the disk.
/// </value>
public bool? AllowDiskUse
{
get { return _allowDiskUse; }
set { _allowDiskUse = value; }
}
/// <summary>
/// Gets or sets the size of a batch.
/// </summary>
/// <value>
/// The size of a batch.
/// </value>
public int? BatchSize
{
get { return _batchSize; }
set { _batchSize = Ensure.IsNullOrGreaterThanOrEqualToZero(value, nameof(value)); }
}
/// <summary>
/// Gets or sets the collation.
/// </summary>
public Collation Collation
{
get { return _collation; }
set { _collation = value; }
}
/// <summary>
/// Gets the collection namespace.
/// </summary>
/// <value>
/// The collection namespace.
/// </value>
public CollectionNamespace CollectionNamespace
{
get { return _collectionNamespace; }
}
/// <summary>
/// Gets or sets the comment.
/// </summary>
/// <value>
/// The comment.
/// </value>
public BsonValue Comment
{
get { return _comment; }
set { _comment = value; }
}
/// <summary>
/// Gets the database namespace.
/// </summary>
/// <value>
/// The database namespace.
/// </value>
public DatabaseNamespace DatabaseNamespace
{
get { return _databaseNamespace; }
}
/// <summary>
/// Gets or sets the hint. This must either be a BsonString representing the index name or a BsonDocument representing the key pattern of the index.
/// </summary>
/// <value>
/// The hint.
/// </value>
public BsonValue Hint
{
get { return _hint; }
set { _hint = value; }
}
/// <summary>
/// Gets or sets the "let" definition.
/// </summary>
/// <value>
/// The "let" definition.
/// </value>
public BsonDocument Let
{
get { return _let; }
set { _let = value; }
}
/// <summary>
/// Gets or sets the maximum await time.
/// </summary>
/// <value>
/// The maximum await time.
/// </value>
public TimeSpan? MaxAwaitTime
{
get { return _maxAwaitTime; }
set { _maxAwaitTime = value; }
}
/// <summary>
/// Gets or sets the maximum time the server should spend on this operation.
/// </summary>
/// <value>
/// The maximum time the server should spend on this operation.
/// </value>
public TimeSpan? MaxTime
{
get { return _maxTime; }
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
}
/// <summary>
/// Gets the message encoder settings.
/// </summary>
/// <value>
/// The message encoder settings.
/// </value>
public MessageEncoderSettings MessageEncoderSettings
{
get { return _messageEncoderSettings; }
}
/// <summary>
/// Gets the pipeline.
/// </summary>
/// <value>
/// The pipeline.
/// </value>
public IReadOnlyList<BsonDocument> Pipeline
{
get { return _pipeline; }
}
/// <summary>
/// Gets or sets the read concern.
/// </summary>
/// <value>
/// The read concern.
/// </value>
public ReadConcern ReadConcern
{
get { return _readConcern; }
set { _readConcern = Ensure.IsNotNull(value, nameof(value)); }
}
/// <summary>
/// Gets the result value serializer.
/// </summary>
/// <value>
/// The result value serializer.
/// </value>
public IBsonSerializer<TResult> ResultSerializer
{
get { return _resultSerializer; }
}
/// <summary>
/// Gets or sets a value indicating whether to retry.
/// </summary>
/// <value>Whether to retry.</value>
public bool RetryRequested
{
get => _retryRequested;
set => _retryRequested = value;
}
/// <summary>
/// Gets or sets a value indicating whether the server should use a cursor to return the results.
/// </summary>
/// <value>
/// A value indicating whether the server should use a cursor to return the results.
/// </value>
[Obsolete("Server versions 3.6 and newer always use a cursor.")]
public bool? UseCursor
{
get { return _useCursor; }
set { _useCursor = value; }
}
// methods
/// <inheritdoc/>
public IAsyncCursor<TResult> Execute(IReadBinding binding, CancellationToken cancellationToken)
{
Ensure.IsNotNull(binding, nameof(binding));
using (BeginOperation())
using (var context = RetryableReadContext.Create(binding, _retryRequested, cancellationToken))
{
return Execute(context, cancellationToken);
}
}
/// <inheritdoc/>
public IAsyncCursor<TResult> Execute(RetryableReadContext context, CancellationToken cancellationToken)
{
Ensure.IsNotNull(context, nameof(context));
EnsureIsReadOnlyPipeline();
using (EventContext.BeginOperation())
{
var operation = CreateOperation(context);
var result = operation.Execute(context, cancellationToken);
context.ChannelSource.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
return CreateCursor(context.ChannelSource, context.Channel, result);
}
}
/// <inheritdoc/>
public async Task<IAsyncCursor<TResult>> ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
{
Ensure.IsNotNull(binding, nameof(binding));
using (BeginOperation())
using (var context = await RetryableReadContext.CreateAsync(binding, _retryRequested, cancellationToken).ConfigureAwait(false))
{
return await ExecuteAsync(context, cancellationToken).ConfigureAwait(false);
}
}
/// <inheritdoc/>
public async Task<IAsyncCursor<TResult>> ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
{
Ensure.IsNotNull(context, nameof(context));
EnsureIsReadOnlyPipeline();
using (EventContext.BeginOperation())
{
var operation = CreateOperation(context);
var result = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false);
context.ChannelSource.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
return CreateCursor(context.ChannelSource, context.Channel, result);
}
}
/// <summary>
/// Returns an AggregateExplainOperation for this AggregateOperation.
/// </summary>
/// <param name="verbosity">The verbosity.</param>
/// <returns>An AggregateExplainOperation.</returns>
public IReadOperation<BsonDocument> ToExplainOperation(ExplainVerbosity verbosity)
{
return new AggregateExplainOperation(_collectionNamespace, _pipeline, _messageEncoderSettings)
{
AllowDiskUse = _allowDiskUse,
Collation = _collation,
MaxTime = _maxTime
};
}
internal BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
{
var readConcern = ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern);
var command = new BsonDocument
{
{ "aggregate", _collectionNamespace == null ? (BsonValue)1 : _collectionNamespace.CollectionName },
{ "pipeline", new BsonArray(_pipeline) },
{ "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
{ "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
{ "collation", () => _collation.ToBsonDocument(), _collation != null },
{ "hint", _hint, _hint != null },
{ "let", _let, _let != null },
{ "comment", _comment, _comment != null },
{ "readConcern", readConcern, readConcern != null },
{
"cursor",
new BsonDocument
{
{ "batchSize", () => _batchSize.Value, _batchSize.HasValue }
}
}
};
return command;
}
private IDisposable BeginOperation() => EventContext.BeginOperation(null, "aggregate");
private ReadCommandOperation<AggregateResult> CreateOperation(RetryableReadContext context)
{
var databaseNamespace = _collectionNamespace == null ? _databaseNamespace : _collectionNamespace.DatabaseNamespace;
var command = CreateCommand(context.Channel.ConnectionDescription, context.Binding.Session);
var serializer = new AggregateResultDeserializer(_resultSerializer);
return new ReadCommandOperation<AggregateResult>(databaseNamespace, command, serializer, MessageEncoderSettings)
{
RetryRequested = _retryRequested // might be overridden by retryable read context
};
}
private AsyncCursor<TResult> CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, AggregateResult result)
{
if (result.CursorId.HasValue)
{
return CreateCursorFromCursorResult(channelSource, channel, result);
}
else
{
// don't need connection pinning
return CreateCursorFromInlineResult(result);
}
}
private AsyncCursor<TResult> CreateCursorFromCursorResult(IChannelSourceHandle channelSource, IChannelHandle channel, AggregateResult result)
{
var cursorId = result.CursorId.GetValueOrDefault(0);
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, channel, cursorId);
return new AsyncCursor<TResult>(
getMoreChannelSource,
result.CollectionNamespace,
_comment,
result.Results,
cursorId,
result.PostBatchResumeToken,
_batchSize,
null, // limit
_resultSerializer,
MessageEncoderSettings,
_maxAwaitTime);
}
private AsyncCursor<TResult> CreateCursorFromInlineResult(AggregateResult result)
{
return new AsyncCursor<TResult>(
null, // channelSource
CollectionNamespace,
_comment,
result.Results,
0, // cursorId
null, // postBatchResumeToken
null, // batchSize
null, // limit
_resultSerializer,
MessageEncoderSettings,
_maxAwaitTime);
}
private void EnsureIsReadOnlyPipeline()
{
if (Pipeline.Any(s => { var n = s.GetElement(0).Name; return n == "$out" || n == "$merge"; }))
{
throw new ArgumentException("The pipeline for an AggregateOperation contains a $out or $merge operator. Use AggregateOutputToCollectionOperation instead.", "pipeline");
}
}
private class AggregateResult
{
public BsonTimestamp AtClusterTime;
public long? CursorId;
public CollectionNamespace CollectionNamespace;
public BsonDocument PostBatchResumeToken;
public TResult[] Results;
}
private class AggregateResultDeserializer : SerializerBase<AggregateResult>
{
private readonly IBsonSerializer<TResult> _resultSerializer;
public AggregateResultDeserializer(IBsonSerializer<TResult> resultSerializer)
{
_resultSerializer = resultSerializer;
}
public override AggregateResult Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var reader = context.Reader;
AggregateResult result = null;
reader.ReadStartDocument();
while (reader.ReadBsonType() != 0)
{
var elementName = reader.ReadName();
if (elementName == "cursor")
{
var cursorDeserializer = new CursorDeserializer(_resultSerializer);
result = cursorDeserializer.Deserialize(context);
}
else if (elementName == "result")
{
var arraySerializer = new ArraySerializer<TResult>(_resultSerializer);
result = new AggregateResult();
result.Results = arraySerializer.Deserialize(context);
}
else
{
reader.SkipValue();
}
}
reader.ReadEndDocument();
return result;
}
}
private class CursorDeserializer : SerializerBase<AggregateResult>
{
private readonly IBsonSerializer<TResult> _resultSerializer;
public CursorDeserializer(IBsonSerializer<TResult> resultSerializer)
{
_resultSerializer = resultSerializer;
}
public override AggregateResult Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var reader = context.Reader;
var result = new AggregateResult();
reader.ReadStartDocument();
while (reader.ReadBsonType() != 0)
{
var elementName = reader.ReadName();
switch (elementName)
{
case "atClusterTime":
result.AtClusterTime = BsonTimestampSerializer.Instance.Deserialize(context);
break;
case "id":
result.CursorId = new Int64Serializer().Deserialize(context);
break;
case "ns":
var ns = reader.ReadString();
result.CollectionNamespace = CollectionNamespace.FromFullName(ns);
break;
case "firstBatch":
var arraySerializer = new ArraySerializer<TResult>(_resultSerializer);
result.Results = arraySerializer.Deserialize(context);
break;
case "postBatchResumeToken":
result.PostBatchResumeToken = BsonDocumentSerializer.Instance.Deserialize(context);
break;
default:
reader.SkipValue();
break;
}
}
reader.ReadEndDocument();
return result;
}
}
}
}