MongoDatabaseSettings.cs 10 KB

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