MongoCollectionSettings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 System.Collections.Generic;
  17. using System.Text;
  18. using MongoDB.Bson;
  19. using MongoDB.Bson.Serialization;
  20. using MongoDB.Driver.Core.Misc;
  21. namespace MongoDB.Driver
  22. {
  23. /// <summary>
  24. /// The settings used to access a collection.
  25. /// </summary>
  26. public class MongoCollectionSettings
  27. {
  28. // private fields
  29. private Setting<bool> _assignIdOnInsert;
  30. private Setting<GuidRepresentation> _guidRepresentation;
  31. private Setting<ReadConcern> _readConcern;
  32. private Setting<UTF8Encoding> _readEncoding;
  33. private Setting<ReadPreference> _readPreference;
  34. private Setting<WriteConcern> _writeConcern;
  35. private Setting<UTF8Encoding> _writeEncoding;
  36. // the following fields are set when Freeze is called
  37. private bool _isFrozen;
  38. private int _frozenHashCode;
  39. private string _frozenStringRepresentation;
  40. // constructors
  41. /// <summary>
  42. /// Initializes a new instance of the MongoCollectionSettings class.
  43. /// </summary>
  44. public MongoCollectionSettings()
  45. {
  46. }
  47. // public properties
  48. /// <summary>
  49. /// Gets or sets a value indicating whether the driver should assign Id values when missing.
  50. /// </summary>
  51. public bool AssignIdOnInsert
  52. {
  53. get { return _assignIdOnInsert.Value; }
  54. set
  55. {
  56. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  57. _assignIdOnInsert.Value = value;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets the representation used for Guids.
  62. /// </summary>
  63. public GuidRepresentation GuidRepresentation
  64. {
  65. get { return _guidRepresentation.Value; }
  66. set
  67. {
  68. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  69. _guidRepresentation.Value = value;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets a value indicating whether the settings have been frozen to prevent further changes.
  74. /// </summary>
  75. public bool IsFrozen
  76. {
  77. get { return _isFrozen; }
  78. }
  79. /// <summary>
  80. /// Gets or sets the read concern.
  81. /// </summary>
  82. public ReadConcern ReadConcern
  83. {
  84. get { return _readConcern.Value; }
  85. set
  86. {
  87. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  88. _readConcern.Value = Ensure.IsNotNull(value, nameof(value));
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the Read Encoding.
  93. /// </summary>
  94. public UTF8Encoding ReadEncoding
  95. {
  96. get { return _readEncoding.Value; }
  97. set
  98. {
  99. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  100. _readEncoding.Value = value;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets the read preference to use.
  105. /// </summary>
  106. public ReadPreference ReadPreference
  107. {
  108. get { return _readPreference.Value; }
  109. set
  110. {
  111. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  112. if (value == null)
  113. {
  114. throw new ArgumentNullException("value");
  115. }
  116. _readPreference.Value = value;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets the serializer registry.
  121. /// </summary>
  122. public IBsonSerializerRegistry SerializerRegistry
  123. {
  124. get { return BsonSerializer.SerializerRegistry; }
  125. }
  126. /// <summary>
  127. /// Gets or sets the WriteConcern to use.
  128. /// </summary>
  129. public WriteConcern WriteConcern
  130. {
  131. get { return _writeConcern.Value; }
  132. set
  133. {
  134. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  135. if (value == null)
  136. {
  137. throw new ArgumentNullException("value");
  138. }
  139. _writeConcern.Value = value;
  140. }
  141. }
  142. /// <summary>
  143. /// Gets or sets the Write Encoding.
  144. /// </summary>
  145. public UTF8Encoding WriteEncoding
  146. {
  147. get { return _writeEncoding.Value; }
  148. set
  149. {
  150. if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  151. _writeEncoding.Value = value;
  152. }
  153. }
  154. // public methods
  155. /// <summary>
  156. /// Creates a clone of the settings.
  157. /// </summary>
  158. /// <returns>A clone of the settings.</returns>
  159. public virtual MongoCollectionSettings Clone()
  160. {
  161. var clone = new MongoCollectionSettings();
  162. clone._assignIdOnInsert = _assignIdOnInsert.Clone();
  163. clone._guidRepresentation = _guidRepresentation.Clone();
  164. clone._readConcern = _readConcern.Clone();
  165. clone._readEncoding = _readEncoding.Clone();
  166. clone._readPreference = _readPreference.Clone();
  167. clone._writeConcern = _writeConcern.Clone();
  168. clone._writeEncoding = _writeEncoding.Clone();
  169. return clone;
  170. }
  171. /// <summary>
  172. /// Compares two MongoCollectionSettings instances.
  173. /// </summary>
  174. /// <param name="obj">The other instance.</param>
  175. /// <returns>True if the two instances are equal.</returns>
  176. public override bool Equals(object obj)
  177. {
  178. var rhs = obj as MongoCollectionSettings;
  179. if (rhs == null)
  180. {
  181. return false;
  182. }
  183. else
  184. {
  185. if (_isFrozen && rhs._isFrozen)
  186. {
  187. return _frozenStringRepresentation == rhs._frozenStringRepresentation;
  188. }
  189. else
  190. {
  191. return
  192. _assignIdOnInsert.Value == rhs._assignIdOnInsert.Value &&
  193. _guidRepresentation.Value == rhs._guidRepresentation.Value &&
  194. object.Equals(_readConcern.Value, rhs._readConcern.Value) &&
  195. object.Equals(_readEncoding, rhs._readEncoding) &&
  196. _readPreference.Value == rhs._readPreference.Value &&
  197. _writeConcern.Value == rhs._writeConcern.Value &&
  198. object.Equals(_writeEncoding, rhs._writeEncoding);
  199. }
  200. }
  201. }
  202. /// <summary>
  203. /// Freezes the settings.
  204. /// </summary>
  205. /// <returns>The frozen settings.</returns>
  206. public MongoCollectionSettings Freeze()
  207. {
  208. if (!_isFrozen)
  209. {
  210. _frozenHashCode = GetHashCode();
  211. _frozenStringRepresentation = ToString();
  212. _isFrozen = true;
  213. }
  214. return this;
  215. }
  216. /// <summary>
  217. /// Returns a frozen copy of the settings.
  218. /// </summary>
  219. /// <returns>A frozen copy of the settings.</returns>
  220. public MongoCollectionSettings FrozenCopy()
  221. {
  222. if (_isFrozen)
  223. {
  224. return this;
  225. }
  226. else
  227. {
  228. return Clone().Freeze();
  229. }
  230. }
  231. /// <summary>
  232. /// Gets the hash code.
  233. /// </summary>
  234. /// <returns>The hash code.</returns>
  235. public override int GetHashCode()
  236. {
  237. if (_isFrozen)
  238. {
  239. return _frozenHashCode;
  240. }
  241. // see Effective Java by Joshua Bloch
  242. int hash = 17;
  243. hash = 37 * hash + _assignIdOnInsert.Value.GetHashCode();
  244. hash = 37 * hash + _guidRepresentation.Value.GetHashCode();
  245. hash = 37 * hash + ((_readConcern.Value == null) ? 0 : _readConcern.Value.GetHashCode());
  246. hash = 37 * hash + ((_readEncoding.Value == null) ? 0 : _readEncoding.Value.GetHashCode());
  247. hash = 37 * hash + ((_readPreference.Value == null) ? 0 : _readPreference.Value.GetHashCode());
  248. hash = 37 * hash + ((_writeConcern.Value == null) ? 0 : _writeConcern.Value.GetHashCode());
  249. hash = 37 * hash + ((_writeEncoding.Value == null) ? 0 : _writeEncoding.Value.GetHashCode());
  250. return hash;
  251. }
  252. /// <summary>
  253. /// Returns a string representation of the settings.
  254. /// </summary>
  255. /// <returns>A string representation of the settings.</returns>
  256. public override string ToString()
  257. {
  258. if (_isFrozen)
  259. {
  260. return _frozenStringRepresentation;
  261. }
  262. var parts = new List<string>();
  263. parts.Add(string.Format("AssignIdOnInsert={0}", _assignIdOnInsert));
  264. parts.Add(string.Format("GuidRepresentation={0}", _guidRepresentation));
  265. parts.Add(string.Format("ReadConcern={0}", _readConcern.Value));
  266. if (_readEncoding.HasBeenSet)
  267. {
  268. parts.Add(string.Format("ReadEncoding={0}", (_readEncoding.Value == null) ? "null" : "UTF8Encoding"));
  269. }
  270. parts.Add(string.Format("ReadPreference={0}", _readPreference));
  271. parts.Add(string.Format("WriteConcern={0}", _writeConcern));
  272. if (_writeEncoding.HasBeenSet)
  273. {
  274. parts.Add(string.Format("WriteEncoding={0}", (_writeEncoding.Value == null) ? "null" : "UTF8Encoding"));
  275. }
  276. return string.Join(";", parts.ToArray());
  277. }
  278. // internal methods
  279. internal void ApplyDefaultValues(MongoDatabaseSettings databaseSettings)
  280. {
  281. if (!_assignIdOnInsert.HasBeenSet)
  282. {
  283. AssignIdOnInsert = MongoDefaults.AssignIdOnInsert;
  284. }
  285. if (!_guidRepresentation.HasBeenSet)
  286. {
  287. GuidRepresentation = databaseSettings.GuidRepresentation;
  288. }
  289. if (!_readConcern.HasBeenSet)
  290. {
  291. ReadConcern = databaseSettings.ReadConcern;
  292. }
  293. if (!_readEncoding.HasBeenSet)
  294. {
  295. ReadEncoding = databaseSettings.ReadEncoding;
  296. }
  297. if (!_readPreference.HasBeenSet)
  298. {
  299. ReadPreference = databaseSettings.ReadPreference;
  300. }
  301. if (!_writeConcern.HasBeenSet)
  302. {
  303. WriteConcern = databaseSettings.WriteConcern;
  304. }
  305. if (!_writeEncoding.HasBeenSet)
  306. {
  307. WriteEncoding = databaseSettings.WriteEncoding;
  308. }
  309. }
  310. }
  311. }