CreateCollectionOptions.cs 7.1 KB

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