MapReduceOptions.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright 2010-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 MongoDB.Bson;
  17. using MongoDB.Bson.Serialization;
  18. using MongoDB.Driver.Core.Misc;
  19. namespace MongoDB.Driver
  20. {
  21. /// <summary>
  22. /// Represents the options for a map-reduce operation.
  23. /// </summary>
  24. /// <typeparam name="TDocument">The type of the document.</typeparam>
  25. /// <typeparam name="TResult">The type of the result.</typeparam>
  26. public sealed class MapReduceOptions<TDocument, TResult>
  27. {
  28. // fields
  29. private bool? _bypassDocumentValidation;
  30. private Collation _collation;
  31. private FilterDefinition<TDocument> _filter;
  32. private BsonJavaScript _finalize;
  33. private bool? _javaScriptMode;
  34. private long? _limit;
  35. private TimeSpan? _maxTime;
  36. private MapReduceOutputOptions _outputOptions;
  37. private IBsonSerializer<TResult> _resultSerializer;
  38. private BsonDocument _scope;
  39. private SortDefinition<TDocument> _sort;
  40. private bool? _verbose;
  41. // properties
  42. /// <summary>
  43. /// Gets or sets a value indicating whether to bypass document validation.
  44. /// </summary>
  45. public bool? BypassDocumentValidation
  46. {
  47. get { return _bypassDocumentValidation; }
  48. set { _bypassDocumentValidation = value; }
  49. }
  50. /// <summary>
  51. /// Gets or sets the collation.
  52. /// </summary>
  53. public Collation Collation
  54. {
  55. get { return _collation; }
  56. set { _collation = value; }
  57. }
  58. /// <summary>
  59. /// Gets or sets the filter.
  60. /// </summary>
  61. public FilterDefinition<TDocument> Filter
  62. {
  63. get { return _filter; }
  64. set { _filter = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the finalize function.
  68. /// </summary>
  69. public BsonJavaScript Finalize
  70. {
  71. get { return _finalize; }
  72. set { _finalize = value; }
  73. }
  74. /// <summary>
  75. /// Gets or sets the java script mode.
  76. /// </summary>
  77. public bool? JavaScriptMode
  78. {
  79. get { return _javaScriptMode; }
  80. set { _javaScriptMode = value; }
  81. }
  82. /// <summary>
  83. /// Gets or sets the limit.
  84. /// </summary>
  85. public long? Limit
  86. {
  87. get { return _limit; }
  88. set { _limit = value; }
  89. }
  90. /// <summary>
  91. /// Gets or sets the maximum time.
  92. /// </summary>
  93. public TimeSpan? MaxTime
  94. {
  95. get { return _maxTime; }
  96. set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
  97. }
  98. /// <summary>
  99. /// Gets or sets the output options.
  100. /// </summary>
  101. public MapReduceOutputOptions OutputOptions
  102. {
  103. get { return _outputOptions; }
  104. set { _outputOptions = value; }
  105. }
  106. /// <summary>
  107. /// Gets or sets the result serializer.
  108. /// </summary>
  109. public IBsonSerializer<TResult> ResultSerializer
  110. {
  111. get { return _resultSerializer; }
  112. set { _resultSerializer = value; }
  113. }
  114. /// <summary>
  115. /// Gets or sets the scope.
  116. /// </summary>
  117. public BsonDocument Scope
  118. {
  119. get { return _scope; }
  120. set { _scope = value; }
  121. }
  122. /// <summary>
  123. /// Gets or sets the sort.
  124. /// </summary>
  125. public SortDefinition<TDocument> Sort
  126. {
  127. get { return _sort; }
  128. set { _sort = value; }
  129. }
  130. /// <summary>
  131. /// Gets or sets whether to include timing information.
  132. /// </summary>
  133. public bool? Verbose
  134. {
  135. get { return _verbose; }
  136. set { _verbose = value; }
  137. }
  138. }
  139. /// <summary>
  140. /// Represents the output options for a map-reduce operation.
  141. /// </summary>
  142. public abstract class MapReduceOutputOptions
  143. {
  144. private static MapReduceOutputOptions __inline = new InlineOutput();
  145. private MapReduceOutputOptions()
  146. { }
  147. /// <summary>
  148. /// An inline map-reduce output options.
  149. /// </summary>
  150. public static MapReduceOutputOptions Inline
  151. {
  152. get { return __inline; }
  153. }
  154. /// <summary>
  155. /// A merge map-reduce output options.
  156. /// </summary>
  157. /// <param name="collectionName">The name of the collection.</param>
  158. /// <param name="databaseName">The name of the database.</param>
  159. /// <param name="sharded">Whether the output collection should be sharded.</param>
  160. /// <param name="nonAtomic">Whether the server should not lock the database for the duration of the merge.</param>
  161. /// <returns>A merge map-reduce output options.</returns>
  162. public static MapReduceOutputOptions Merge(string collectionName, string databaseName = null, bool? sharded = null, bool? nonAtomic = null)
  163. {
  164. Ensure.IsNotNull(collectionName, nameof(collectionName));
  165. return new CollectionOutput(collectionName, Core.Operations.MapReduceOutputMode.Merge, databaseName, sharded, nonAtomic);
  166. }
  167. /// <summary>
  168. /// A reduce map-reduce output options.
  169. /// </summary>
  170. /// <param name="collectionName">The name of the collection.</param>
  171. /// <param name="databaseName">The name of the database.</param>
  172. /// <param name="sharded">Whether the output collection should be sharded.</param>
  173. /// <param name="nonAtomic">Whether the server should not lock the database for the duration of the reduce.</param>
  174. /// <returns>A reduce map-reduce output options.</returns>
  175. public static MapReduceOutputOptions Reduce(string collectionName, string databaseName = null, bool? sharded = null, bool? nonAtomic = null)
  176. {
  177. Ensure.IsNotNull(collectionName, nameof(collectionName));
  178. return new CollectionOutput(collectionName, Core.Operations.MapReduceOutputMode.Reduce, databaseName, sharded, nonAtomic);
  179. }
  180. /// <summary>
  181. /// A replace map-reduce output options.
  182. /// </summary>
  183. /// <param name="collectionName">The name of the collection.</param>
  184. /// <param name="databaseName">Name of the database.</param>
  185. /// <param name="sharded">Whether the output collection should be sharded.</param>
  186. /// <returns>A replace map-reduce output options.</returns>
  187. public static MapReduceOutputOptions Replace(string collectionName, string databaseName = null, bool? sharded = null)
  188. {
  189. Ensure.IsNotNull(collectionName, nameof(collectionName));
  190. return new CollectionOutput(collectionName, Core.Operations.MapReduceOutputMode.Replace, databaseName, sharded, null);
  191. }
  192. internal sealed class InlineOutput : MapReduceOutputOptions
  193. {
  194. internal InlineOutput()
  195. { }
  196. }
  197. internal sealed class CollectionOutput : MapReduceOutputOptions
  198. {
  199. private readonly string _collectionName;
  200. private readonly string _databaseName;
  201. private readonly bool? _nonAtomic;
  202. private readonly Core.Operations.MapReduceOutputMode _outputMode;
  203. private readonly bool? _sharded;
  204. internal CollectionOutput(string collectionName, Core.Operations.MapReduceOutputMode outputMode, string databaseName = null, bool? sharded = null, bool? nonAtomic = null)
  205. {
  206. _collectionName = collectionName;
  207. _outputMode = outputMode;
  208. _databaseName = databaseName;
  209. _sharded = sharded;
  210. _nonAtomic = nonAtomic;
  211. }
  212. public string CollectionName
  213. {
  214. get { return _collectionName; }
  215. }
  216. public string DatabaseName
  217. {
  218. get { return _databaseName; }
  219. }
  220. public bool? NonAtomic
  221. {
  222. get { return _nonAtomic; }
  223. }
  224. public Core.Operations.MapReduceOutputMode OutputMode
  225. {
  226. get { return _outputMode; }
  227. }
  228. public bool? Sharded
  229. {
  230. get { return _sharded; }
  231. }
  232. }
  233. }
  234. }