PageRenderTime 137ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Driver/Core/MongoCollectionSettings.cs

https://github.com/zxy050/mongo-csharp-driver
C# | 304 lines | 195 code | 23 blank | 86 comment | 27 complexity | 7edec082459cb37c5038755191cdd3ef MD5 | raw file
  1. /* Copyright 2010-2011 10gen 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.Linq;
  18. using System.Text;
  19. using MongoDB.Bson;
  20. namespace MongoDB.Driver {
  21. /// <summary>
  22. /// The settings used to access a collection (an abstract class, see MongoCollectionSettings{TDefaultDocument}).
  23. /// </summary>
  24. public abstract class MongoCollectionSettings {
  25. #pragma warning disable 1591 // missing XML comment (it's warning about protected members also)
  26. #region protected fields
  27. protected string collectionName;
  28. protected bool assignIdOnInsert;
  29. protected Type defaultDocumentType;
  30. protected GuidRepresentation guidRepresentation;
  31. protected SafeMode safeMode;
  32. protected bool slaveOk;
  33. // the following fields are set when Freeze is called
  34. protected bool isFrozen;
  35. protected int frozenHashCode;
  36. protected string frozenStringRepresentation;
  37. #endregion
  38. #region constructors
  39. protected MongoCollectionSettings(
  40. MongoDatabase database,
  41. string collectionName,
  42. Type defaultDocumentType
  43. ) {
  44. var databaseSettings = database.Settings;
  45. this.collectionName = collectionName;
  46. this.assignIdOnInsert = MongoDefaults.AssignIdOnInsert;
  47. this.defaultDocumentType = defaultDocumentType;
  48. this.guidRepresentation = databaseSettings.GuidRepresentation;
  49. this.safeMode = databaseSettings.SafeMode;
  50. this.slaveOk = databaseSettings.SlaveOk;
  51. }
  52. protected MongoCollectionSettings(
  53. string collectionName,
  54. bool assignIdOnInsert,
  55. Type defaultDocumentType,
  56. GuidRepresentation guidRepresentation,
  57. SafeMode safeMode,
  58. bool slaveOk
  59. ) {
  60. this.collectionName = collectionName;
  61. this.assignIdOnInsert = assignIdOnInsert;
  62. this.defaultDocumentType = defaultDocumentType;
  63. this.guidRepresentation = guidRepresentation;
  64. this.safeMode = safeMode;
  65. this.slaveOk = slaveOk;
  66. }
  67. #endregion
  68. #pragma warning restore
  69. #region public properties
  70. /// <summary>
  71. /// Gets or sets whether the driver should assign Id values when missing.
  72. /// </summary>
  73. public bool AssignIdOnInsert {
  74. get { return assignIdOnInsert; }
  75. set {
  76. if (isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  77. assignIdOnInsert = value;
  78. }
  79. }
  80. /// <summary>
  81. /// Gets the name of the collection.
  82. /// </summary>
  83. public string CollectionName {
  84. get { return collectionName; }
  85. }
  86. /// <summary>
  87. /// Gets the default document type of the collection.
  88. /// </summary>
  89. public Type DefaultDocumentType {
  90. get { return defaultDocumentType; }
  91. }
  92. /// <summary>
  93. /// Gets or sets the representation used for Guids.
  94. /// </summary>
  95. public GuidRepresentation GuidRepresentation {
  96. get { return guidRepresentation; }
  97. set {
  98. if (isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  99. guidRepresentation = value;
  100. }
  101. }
  102. /// <summary>
  103. /// Gets whether the settings have been frozen to prevent further changes.
  104. /// </summary>
  105. public bool IsFrozen {
  106. get { return isFrozen; }
  107. }
  108. /// <summary>
  109. /// Gets or sets the SafeMode to use.
  110. /// </summary>
  111. public SafeMode SafeMode {
  112. get { return safeMode; }
  113. set {
  114. if (isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  115. safeMode = value;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets or sets whether queries should be sent to secondary servers.
  120. /// </summary>
  121. public bool SlaveOk {
  122. get { return slaveOk; }
  123. set {
  124. if (isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); }
  125. slaveOk = value;
  126. }
  127. }
  128. #endregion
  129. #region public methods
  130. /// <summary>
  131. /// Creates a clone of the settings.
  132. /// </summary>
  133. /// <returns>A clone of the settings.</returns>
  134. public abstract MongoCollectionSettings Clone();
  135. /// <summary>
  136. /// Compares two MongoCollectionSettings instances.
  137. /// </summary>
  138. /// <param name="obj">The other instance.</param>
  139. /// <returns>True if the two instances are equal.</returns>
  140. public override bool Equals(object obj) {
  141. var rhs = obj as MongoCollectionSettings;
  142. if (rhs == null) {
  143. return false;
  144. } else {
  145. if (this.isFrozen && rhs.isFrozen) {
  146. return this.frozenStringRepresentation == rhs.frozenStringRepresentation;
  147. } else {
  148. return
  149. this.collectionName == rhs.collectionName &&
  150. this.assignIdOnInsert == rhs.assignIdOnInsert &&
  151. this.defaultDocumentType == rhs.defaultDocumentType &&
  152. this.guidRepresentation == rhs.guidRepresentation &&
  153. this.safeMode == rhs.safeMode &&
  154. this.slaveOk == rhs.slaveOk;
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// Freezes the settings.
  160. /// </summary>
  161. /// <returns>The frozen settings.</returns>
  162. public MongoCollectionSettings Freeze() {
  163. if (!isFrozen) {
  164. safeMode = safeMode.FrozenCopy();
  165. frozenHashCode = GetHashCodeHelper();
  166. frozenStringRepresentation = ToStringHelper();
  167. isFrozen = true;
  168. }
  169. return this;
  170. }
  171. /// <summary>
  172. /// Returns a frozen copy of the settings.
  173. /// </summary>
  174. /// <returns>A frozen copy of the settings.</returns>
  175. public MongoCollectionSettings FrozenCopy() {
  176. if (isFrozen) {
  177. return this;
  178. } else {
  179. return Clone().Freeze();
  180. }
  181. }
  182. /// <summary>
  183. /// Gets the hash code.
  184. /// </summary>
  185. /// <returns>The hash code.</returns>
  186. public override int GetHashCode() {
  187. if (isFrozen) {
  188. return frozenHashCode;
  189. } else {
  190. return GetHashCodeHelper();
  191. }
  192. }
  193. /// <summary>
  194. /// Returns a string representation of the settings.
  195. /// </summary>
  196. /// <returns>A string representation of the settings.</returns>
  197. public override string ToString() {
  198. if (isFrozen) {
  199. return frozenStringRepresentation;
  200. } else {
  201. return ToStringHelper();
  202. }
  203. }
  204. #endregion
  205. #region private methods
  206. private int GetHashCodeHelper() {
  207. // see Effective Java by Joshua Bloch
  208. int hash = 17;
  209. hash = 37 * hash + ((collectionName == null) ? 0 : collectionName.GetHashCode());
  210. hash = 37 * hash + assignIdOnInsert.GetHashCode();
  211. hash = 37 * hash + ((defaultDocumentType == null) ? 0 : defaultDocumentType.GetHashCode());
  212. hash = 37 * hash + guidRepresentation.GetHashCode();
  213. hash = 37 * hash + ((safeMode == null) ? 0 : safeMode.GetHashCode());
  214. hash = 37 * hash + slaveOk.GetHashCode();
  215. return hash;
  216. }
  217. private string ToStringHelper() {
  218. return string.Format(
  219. "CollectionName={0};AssignIdOnInsert={1};DefaultDocumentType={2};GuidRepresentation={3};SafeMode={4};SlaveOk={5}",
  220. collectionName,
  221. assignIdOnInsert,
  222. defaultDocumentType,
  223. guidRepresentation,
  224. safeMode,
  225. slaveOk
  226. );
  227. }
  228. #endregion
  229. }
  230. /// <summary>
  231. /// Settings used to access a collection.
  232. /// </summary>
  233. /// <typeparam name="TDefaultDocument">The default document type of the collection.</typeparam>
  234. public class MongoCollectionSettings<TDefaultDocument> : MongoCollectionSettings {
  235. #region constructors
  236. /// <summary>
  237. /// Creates a new instance of MongoCollectionSettings.
  238. /// </summary>
  239. /// <param name="database">The database to inherit settings from.</param>
  240. /// <param name="collectionName">The name of the collection.</param>
  241. public MongoCollectionSettings(
  242. MongoDatabase database,
  243. string collectionName
  244. )
  245. : base(database, collectionName, typeof(TDefaultDocument)) {
  246. }
  247. /// <summary>
  248. /// Creates a new instance of MongoCollectionSettings.
  249. /// </summary>
  250. /// <param name="collectionName">The name of the collection.</param>
  251. /// <param name="assignIdOnInsert">Whether the driver should assign the Id values if necessary.</param>
  252. /// <param name="guidRepresentation">The representation for Guids.</param>
  253. /// <param name="safeMode">The safe mode to use.</param>
  254. /// <param name="slaveOk">Whether queries should be sent to secondary servers.</param>
  255. private MongoCollectionSettings(
  256. string collectionName,
  257. bool assignIdOnInsert,
  258. GuidRepresentation guidRepresentation,
  259. SafeMode safeMode,
  260. bool slaveOk
  261. )
  262. : base(collectionName, assignIdOnInsert, typeof(TDefaultDocument), guidRepresentation, safeMode, slaveOk) {
  263. }
  264. #endregion
  265. #region public methods
  266. /// <summary>
  267. /// Creates a clone of the settings.
  268. /// </summary>
  269. /// <returns>A clone of the settings.</returns>
  270. public override MongoCollectionSettings Clone() {
  271. return new MongoCollectionSettings<TDefaultDocument>(
  272. collectionName,
  273. assignIdOnInsert,
  274. guidRepresentation,
  275. safeMode,
  276. slaveOk
  277. );
  278. }
  279. #endregion
  280. }
  281. }