CreateCollectionOptions.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. namespace MongoDB.Driver
  19. {
  20. /// <summary>
  21. /// Options for creating a collection.
  22. /// </summary>
  23. public class CreateCollectionOptions
  24. {
  25. // fields
  26. private bool? _autoIndexId;
  27. private bool? _capped;
  28. private Collation _collation;
  29. private IndexOptionDefaults _indexOptionDefaults;
  30. private long? _maxDocuments;
  31. private long? _maxSize;
  32. private bool? _noPadding;
  33. private BsonDocument _storageEngine;
  34. private bool? _usePowerOf2Sizes;
  35. private IBsonSerializerRegistry _serializerRegistry;
  36. private DocumentValidationAction? _validationAction;
  37. private DocumentValidationLevel? _validationLevel;
  38. // properties
  39. /// <summary>
  40. /// Gets or sets the collation.
  41. /// </summary>
  42. public Collation Collation
  43. {
  44. get { return _collation; }
  45. set { _collation = value; }
  46. }
  47. /// <summary>
  48. /// Gets or sets a value indicating whether to automatically create an index on the _id.
  49. /// </summary>
  50. [Obsolete("AutoIndexId has been deprecated since server version 3.2.")]
  51. public bool? AutoIndexId
  52. {
  53. get { return _autoIndexId; }
  54. set { _autoIndexId = value; }
  55. }
  56. /// <summary>
  57. /// Gets or sets a value indicating whether the collection is capped.
  58. /// </summary>
  59. public bool? Capped
  60. {
  61. get { return _capped; }
  62. set { _capped = value; }
  63. }
  64. /// <summary>
  65. /// Gets or sets the index option defaults.
  66. /// </summary>
  67. /// <value>
  68. /// The index option defaults.
  69. /// </value>
  70. public IndexOptionDefaults IndexOptionDefaults
  71. {
  72. get { return _indexOptionDefaults; }
  73. set { _indexOptionDefaults = value; }
  74. }
  75. /// <summary>
  76. /// Gets or sets the maximum number of documents (used with capped collections).
  77. /// </summary>
  78. public long? MaxDocuments
  79. {
  80. get { return _maxDocuments; }
  81. set { _maxDocuments = value; }
  82. }
  83. /// <summary>
  84. /// Gets or sets the maximum size of the collection (used with capped collections).
  85. /// </summary>
  86. public long? MaxSize
  87. {
  88. get { return _maxSize; }
  89. set { _maxSize = value; }
  90. }
  91. /// <summary>
  92. /// Gets or sets whether padding should not be used.
  93. /// </summary>
  94. public bool? NoPadding
  95. {
  96. get { return _noPadding; }
  97. set { _noPadding = value; }
  98. }
  99. /// <summary>
  100. /// Gets or sets the serializer registry.
  101. /// </summary>
  102. public IBsonSerializerRegistry SerializerRegistry
  103. {
  104. get { return _serializerRegistry; }
  105. set { _serializerRegistry = value; }
  106. }
  107. /// <summary>
  108. /// Gets or sets the storage engine options.
  109. /// </summary>
  110. public BsonDocument StorageEngine
  111. {
  112. get { return _storageEngine; }
  113. set { _storageEngine = value; }
  114. }
  115. /// <summary>
  116. /// Gets or sets a value indicating whether to use power of 2 sizes.
  117. /// </summary>
  118. public bool? UsePowerOf2Sizes
  119. {
  120. get { return _usePowerOf2Sizes; }
  121. set { _usePowerOf2Sizes = value; }
  122. }
  123. /// <summary>
  124. /// Gets or sets the validation action.
  125. /// </summary>
  126. /// <value>
  127. /// The validation action.
  128. /// </value>
  129. public DocumentValidationAction? ValidationAction
  130. {
  131. get { return _validationAction; }
  132. set { _validationAction = value; }
  133. }
  134. /// <summary>
  135. /// Gets or sets the validation level.
  136. /// </summary>
  137. /// <value>
  138. /// The validation level.
  139. /// </value>
  140. public DocumentValidationLevel? ValidationLevel
  141. {
  142. get { return _validationLevel; }
  143. set { _validationLevel = value; }
  144. }
  145. }
  146. /// <summary>
  147. /// Options for creating a collection.
  148. /// </summary>
  149. /// <typeparam name="TDocument">The type of the document.</typeparam>
  150. public sealed class CreateCollectionOptions<TDocument> : CreateCollectionOptions
  151. {
  152. #region static
  153. // internal static methods
  154. /// <summary>
  155. /// Coerces a generic CreateCollectionOptions{TDocument} from a non-generic CreateCollectionOptions.
  156. /// </summary>
  157. /// <param name="options">The options.</param>
  158. /// <returns>The generic options.</returns>
  159. internal static CreateCollectionOptions<TDocument> CoercedFrom(CreateCollectionOptions options)
  160. {
  161. if (options == null)
  162. {
  163. return null;
  164. }
  165. if (options.GetType() == typeof(CreateCollectionOptions))
  166. {
  167. #pragma warning disable 618
  168. return new CreateCollectionOptions<TDocument>
  169. {
  170. AutoIndexId = options.AutoIndexId,
  171. Capped = options.Capped,
  172. Collation = options.Collation,
  173. IndexOptionDefaults = options.IndexOptionDefaults,
  174. MaxDocuments = options.MaxDocuments,
  175. MaxSize = options.MaxSize,
  176. NoPadding = options.NoPadding,
  177. SerializerRegistry = options.SerializerRegistry,
  178. StorageEngine = options.StorageEngine,
  179. UsePowerOf2Sizes = options.UsePowerOf2Sizes,
  180. ValidationAction = options.ValidationAction,
  181. ValidationLevel = options.ValidationLevel
  182. };
  183. #pragma warning restore
  184. }
  185. return (CreateCollectionOptions<TDocument>)options;
  186. }
  187. #endregion
  188. // private fields
  189. private IBsonSerializer<TDocument> _documentSerializer;
  190. private FilterDefinition<TDocument> _validator;
  191. // public properties
  192. /// <summary>
  193. /// Gets or sets the document serializer.
  194. /// </summary>
  195. public IBsonSerializer<TDocument> DocumentSerializer
  196. {
  197. get { return _documentSerializer; }
  198. set { _documentSerializer = value; }
  199. }
  200. /// <summary>
  201. /// Gets or sets the validator.
  202. /// </summary>
  203. /// <value>
  204. /// The validator.
  205. /// </value>
  206. public FilterDefinition<TDocument> Validator
  207. {
  208. get { return _validator; }
  209. set { _validator = value; }
  210. }
  211. }
  212. }