AggregateQueryableExecutionModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2015-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. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.Serialization;
  23. using MongoDB.Driver.Core.Misc;
  24. namespace MongoDB.Driver.Linq
  25. {
  26. /// <summary>
  27. /// A model for a queryable to be executed using the aggregation framework.
  28. /// </summary>
  29. /// <typeparam name="TOutput">The type of the output.</typeparam>
  30. public sealed class AggregateQueryableExecutionModel<TOutput> : QueryableExecutionModel
  31. {
  32. private readonly IReadOnlyList<BsonDocument> _stages;
  33. private readonly IBsonSerializer<TOutput> _outputSerializer;
  34. internal AggregateQueryableExecutionModel(IEnumerable<BsonDocument> stages, IBsonSerializer<TOutput> outputSerializer)
  35. {
  36. _stages = (Ensure.IsNotNull(stages, nameof(stages)) as IReadOnlyList<BsonDocument>) ?? stages.ToList();
  37. _outputSerializer = Ensure.IsNotNull(outputSerializer, nameof(outputSerializer));
  38. }
  39. /// <summary>
  40. /// Gets the stages.
  41. /// </summary>
  42. public IEnumerable<BsonDocument> Stages
  43. {
  44. get { return _stages; }
  45. }
  46. /// <summary>
  47. /// Gets the output serializer.
  48. /// </summary>
  49. public IBsonSerializer<TOutput> OutputSerializer
  50. {
  51. get { return _outputSerializer; }
  52. }
  53. /// <summary>
  54. /// Gets the type of the output.
  55. /// </summary>
  56. public override Type OutputType
  57. {
  58. get { return _outputSerializer.ValueType; }
  59. }
  60. /// <summary>
  61. /// Returns a <see cref="System.String" /> that represents this instance.
  62. /// </summary>
  63. /// <returns>
  64. /// A <see cref="System.String" /> that represents this instance.
  65. /// </returns>
  66. public override string ToString()
  67. {
  68. var sb = new StringBuilder("aggregate([");
  69. if (_stages.Count > 0)
  70. {
  71. sb.Append(string.Join(", ", _stages.Select(x => x.ToString())));
  72. }
  73. sb.Append("])");
  74. return sb.ToString();
  75. }
  76. internal override object Execute<TInput>(IMongoCollection<TInput> collection, AggregateOptions options)
  77. {
  78. var pipeline = CreatePipeline<TInput>();
  79. return collection.Aggregate(pipeline, options, CancellationToken.None);
  80. }
  81. internal override Task ExecuteAsync<TInput>(IMongoCollection<TInput> collection, AggregateOptions options, CancellationToken cancellationToken)
  82. {
  83. var pipeline = CreatePipeline<TInput>();
  84. return collection.AggregateAsync(pipeline, options, cancellationToken);
  85. }
  86. private BsonDocumentStagePipelineDefinition<TInput, TOutput> CreatePipeline<TInput>()
  87. {
  88. return new BsonDocumentStagePipelineDefinition<TInput, TOutput>(
  89. _stages,
  90. _outputSerializer);
  91. }
  92. }
  93. }