AggregateFacetResults.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright 2016-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.Reflection;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.IO;
  21. using MongoDB.Bson.Serialization;
  22. using MongoDB.Bson.Serialization.Serializers;
  23. using MongoDB.Driver.Core.Misc;
  24. namespace MongoDB.Driver
  25. {
  26. /// <summary>
  27. /// Represents the results of a $facet stage with an arbitrary number of facets.
  28. /// </summary>
  29. public class AggregateFacetResults
  30. {
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="AggregateFacetResults"/> class.
  33. /// </summary>
  34. /// <param name="facets">The facets.</param>
  35. public AggregateFacetResults(AggregateFacetResult[] facets)
  36. {
  37. Facets = Ensure.IsNotNull(facets, nameof(facets));
  38. }
  39. /// <summary>
  40. /// Gets the facets.
  41. /// </summary>
  42. public IReadOnlyList<AggregateFacetResult> Facets { get; private set; }
  43. }
  44. internal class AggregateFacetResultsSerializer : SerializerBase<AggregateFacetResults>
  45. {
  46. private readonly string[] _names;
  47. private readonly IBsonSerializer[] _serializers;
  48. public AggregateFacetResultsSerializer(IEnumerable<string> names, IEnumerable<IBsonSerializer> serializers)
  49. {
  50. _names = names.ToArray();
  51. _serializers = serializers.ToArray();
  52. }
  53. public override AggregateFacetResults Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  54. {
  55. var facets = new AggregateFacetResult[_names.Length];
  56. var reader = context.Reader;
  57. reader.ReadStartDocument();
  58. while (reader.ReadBsonType() != 0)
  59. {
  60. var name = reader.ReadName();
  61. var index = Array.IndexOf(_names, name);
  62. if (index != -1)
  63. {
  64. var itemSerializer = _serializers[index];
  65. var itemType = itemSerializer.ValueType;
  66. var itemSerializerType = typeof(IBsonSerializer<>).MakeGenericType(itemType);
  67. var arraySerializerType = typeof(ArraySerializer<>).MakeGenericType(itemType);
  68. var arraySerializerConstructor = arraySerializerType.GetTypeInfo().GetConstructor(new[] { itemSerializerType });
  69. var arraySerializer = (IBsonSerializer)arraySerializerConstructor.Invoke(new object[] { itemSerializer });
  70. var output = (Array)arraySerializer.Deserialize(context);
  71. var facetType = typeof(AggregateFacetResult<>).MakeGenericType(itemType);
  72. var ienumerableItemType = typeof(IEnumerable<>).MakeGenericType(itemType);
  73. var facetConstructor = facetType.GetTypeInfo().GetConstructor(new[] { typeof(string), ienumerableItemType });
  74. var facet = (AggregateFacetResult)facetConstructor.Invoke(new object[] { name, output });
  75. facets[index] = facet;
  76. }
  77. else
  78. {
  79. throw new BsonSerializationException($"Unexpected field name '{name}' in $facet result.");
  80. }
  81. }
  82. reader.ReadEndDocument();
  83. var missingIndex = Array.IndexOf(facets, null);
  84. if (missingIndex != -1)
  85. {
  86. var missingName = _names[missingIndex];
  87. throw new BsonSerializationException($"Field name '{missingName}' in $facet result.");
  88. }
  89. return new AggregateFacetResults(facets);
  90. }
  91. }
  92. }