GridFSBucketOptions.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 MongoDB.Bson.Serialization;
  16. using MongoDB.Driver.Core.Misc;
  17. namespace MongoDB.Driver.GridFS
  18. {
  19. /// <summary>
  20. /// Represents mutable options for a GridFS instance.
  21. /// </summary>
  22. public class GridFSBucketOptions
  23. {
  24. // fields
  25. private string _bucketName;
  26. private int _chunkSizeBytes;
  27. private bool _disableMD5 = false;
  28. private ReadConcern _readConcern;
  29. private ReadPreference _readPreference;
  30. private WriteConcern _writeConcern;
  31. // constructors
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="GridFSBucketOptions"/> class.
  34. /// </summary>
  35. public GridFSBucketOptions()
  36. : this(ImmutableGridFSBucketOptions.Defaults)
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="GridFSBucketOptions"/> class.
  41. /// </summary>
  42. /// <param name="other">The other <see cref="GridFSBucketOptions"/> from which to copy the values.</param>
  43. public GridFSBucketOptions(GridFSBucketOptions other)
  44. {
  45. Ensure.IsNotNull(other, nameof(other));
  46. _bucketName = other.BucketName;
  47. _chunkSizeBytes = other.ChunkSizeBytes;
  48. _disableMD5 = other.DisableMD5;
  49. _readConcern = other.ReadConcern;
  50. _readPreference = other.ReadPreference;
  51. _writeConcern = other.WriteConcern;
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="GridFSBucketOptions"/> class.
  55. /// </summary>
  56. /// <param name="other">The other <see cref="ImmutableGridFSBucketOptions"/> from which to copy the values.</param>
  57. public GridFSBucketOptions(ImmutableGridFSBucketOptions other)
  58. {
  59. Ensure.IsNotNull(other, nameof(other));
  60. _bucketName = other.BucketName;
  61. _chunkSizeBytes = other.ChunkSizeBytes;
  62. _disableMD5 = other.DisableMD5;
  63. _readConcern = other.ReadConcern;
  64. _readPreference = other.ReadPreference;
  65. _writeConcern = other.WriteConcern;
  66. }
  67. // properties
  68. /// <summary>
  69. /// Gets or sets the bucket name.
  70. /// </summary>
  71. /// <value>
  72. /// The bucket name.
  73. /// </value>
  74. public string BucketName
  75. {
  76. get { return _bucketName; }
  77. set
  78. {
  79. Ensure.IsNotNullOrEmpty(value, nameof(value));
  80. _bucketName = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets the chunk size in bytes.
  85. /// </summary>
  86. /// <value>
  87. /// The chunk size in bytes.
  88. /// </value>
  89. public int ChunkSizeBytes
  90. {
  91. get { return _chunkSizeBytes; }
  92. set
  93. {
  94. Ensure.IsGreaterThanZero(value, nameof(value));
  95. _chunkSizeBytes = value;
  96. }
  97. }
  98. /// <summary>
  99. /// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
  100. /// </summary>
  101. /// <value>
  102. /// Whether MD5 checksum computation is disabled when uploading a GridFS file.
  103. /// </value>
  104. public bool DisableMD5
  105. {
  106. get { return _disableMD5; }
  107. set { _disableMD5 = value; }
  108. }
  109. /// <summary>
  110. /// Gets or sets the read concern.
  111. /// </summary>
  112. /// <value>
  113. /// The read concern.
  114. /// </value>
  115. public ReadConcern ReadConcern
  116. {
  117. get { return _readConcern; }
  118. set { _readConcern = value; }
  119. }
  120. /// <summary>
  121. /// Gets or sets the read preference.
  122. /// </summary>
  123. /// <value>
  124. /// The read preference.
  125. /// </value>
  126. public ReadPreference ReadPreference
  127. {
  128. get { return _readPreference; }
  129. set { _readPreference = value; }
  130. }
  131. /// <summary>
  132. /// Gets or sets the write concern.
  133. /// </summary>
  134. /// <value>
  135. /// The write concern.
  136. /// </value>
  137. public WriteConcern WriteConcern
  138. {
  139. get { return _writeConcern; }
  140. set { _writeConcern = value; }
  141. }
  142. }
  143. /// <summary>
  144. /// Represents immutable options for a GridFS instance.
  145. /// </summary>
  146. public class ImmutableGridFSBucketOptions
  147. {
  148. #region static
  149. // static fields
  150. private static readonly ImmutableGridFSBucketOptions __defaults = new ImmutableGridFSBucketOptions();
  151. // static properties
  152. /// <summary>
  153. /// Gets the default GridFSBucketOptions.
  154. /// </summary>
  155. /// <value>
  156. /// The default GridFSBucketOptions.
  157. /// </value>
  158. public static ImmutableGridFSBucketOptions Defaults
  159. {
  160. get { return __defaults; }
  161. }
  162. #endregion
  163. // fields
  164. private readonly string _bucketName;
  165. private readonly int _chunkSizeBytes;
  166. private readonly bool _disableMD5 = false;
  167. private readonly ReadConcern _readConcern;
  168. private readonly ReadPreference _readPreference;
  169. private readonly WriteConcern _writeConcern;
  170. // constructors
  171. /// <summary>
  172. /// Initializes a new instance of the <see cref="ImmutableGridFSBucketOptions"/> class.
  173. /// </summary>
  174. public ImmutableGridFSBucketOptions()
  175. {
  176. _bucketName = "fs";
  177. _chunkSizeBytes = 255 * 1024;
  178. }
  179. /// <summary>
  180. /// Initializes a new instance of the <see cref="ImmutableGridFSBucketOptions" /> class.
  181. /// </summary>
  182. /// <param name="other">The other <see cref="GridFSBucketOptions"/> from which to copy the values.</param>
  183. public ImmutableGridFSBucketOptions(GridFSBucketOptions other)
  184. {
  185. Ensure.IsNotNull(other, nameof(other));
  186. _bucketName = other.BucketName;
  187. _chunkSizeBytes = other.ChunkSizeBytes;
  188. _disableMD5 = other.DisableMD5;
  189. _readConcern = other.ReadConcern;
  190. _readPreference = other.ReadPreference;
  191. _writeConcern = other.WriteConcern;
  192. }
  193. // properties
  194. /// <summary>
  195. /// Gets the bucket name.
  196. /// </summary>
  197. /// <value>
  198. /// The bucket name.
  199. /// </value>
  200. public string BucketName
  201. {
  202. get { return _bucketName; }
  203. }
  204. /// <summary>
  205. /// Gets the chunk size in bytes.
  206. /// </summary>
  207. /// <value>
  208. /// The chunk size in bytes.
  209. /// </value>
  210. public int ChunkSizeBytes
  211. {
  212. get { return _chunkSizeBytes; }
  213. }
  214. /// <summary>
  215. /// Gets or sets whether to disable MD5 checksum computation when uploading a GridFS file.
  216. /// </summary>
  217. /// <value>
  218. /// Whether MD5 checksum computation is disabled when uploading a GridFS file.
  219. /// </value>
  220. public bool DisableMD5
  221. {
  222. get { return _disableMD5; }
  223. }
  224. /// <summary>
  225. /// Gets the read concern.
  226. /// </summary>
  227. /// <value>
  228. /// The read concern.
  229. /// </value>
  230. public ReadConcern ReadConcern
  231. {
  232. get { return _readConcern; }
  233. }
  234. /// <summary>
  235. /// Gets the read preference.
  236. /// </summary>
  237. /// <value>
  238. /// The read preference.
  239. /// </value>
  240. public ReadPreference ReadPreference
  241. {
  242. get { return _readPreference; }
  243. }
  244. /// <summary>
  245. /// Gets the serializer registry.
  246. /// </summary>
  247. /// <value>
  248. /// The serializer registry.
  249. /// </value>
  250. public IBsonSerializerRegistry SerializerRegistry
  251. {
  252. get { return BsonSerializer.SerializerRegistry; }
  253. }
  254. /// <summary>
  255. /// Gets the write concern.
  256. /// </summary>
  257. /// <value>
  258. /// The write concern.
  259. /// </value>
  260. public WriteConcern WriteConcern
  261. {
  262. get { return _writeConcern; }
  263. }
  264. }
  265. }