PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Driver/Core/MongoUrl.cs

https://github.com/afinnell/mongo-csharp-driver
C# | 346 lines | 171 code | 36 blank | 139 comment | 10 complexity | 9eadd9b4c6ca18aff5eab38cde6d20e4 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 System.Text.RegularExpressions;
  20. using MongoDB.Bson;
  21. using MongoDB.Driver.Internal;
  22. namespace MongoDB.Driver {
  23. /// <summary>
  24. /// Server connection mode.
  25. /// </summary>
  26. [Serializable]
  27. public enum ConnectionMode {
  28. /// <summary>
  29. /// Connect directly to a server.
  30. /// </summary>
  31. Direct,
  32. /// <summary>
  33. /// Connect to a replica set.
  34. /// </summary>
  35. ReplicaSet
  36. }
  37. /// <summary>
  38. /// Represents an immutable URL style connection string. See also MongoUrlBuilder.
  39. /// </summary>
  40. [Serializable]
  41. public class MongoUrl {
  42. #region private static fields
  43. private static object staticLock = new object();
  44. private static Dictionary<string, MongoUrl> cache = new Dictionary<string, MongoUrl>();
  45. #endregion
  46. #region private fields
  47. private MongoServerSettings serverSettings;
  48. private double waitQueueMultiple;
  49. private int waitQueueSize;
  50. private string databaseName;
  51. private string url;
  52. #endregion
  53. #region constructors
  54. /// <summary>
  55. /// Creates a new instance of MongoUrl.
  56. /// </summary>
  57. /// <param name="url">The URL containing the settings.</param>
  58. public MongoUrl(
  59. string url
  60. ) {
  61. var builder = new MongoUrlBuilder(url); // parses url
  62. serverSettings = builder.ToServerSettings().Freeze();
  63. this.waitQueueMultiple = builder.WaitQueueMultiple;
  64. this.waitQueueSize = builder.WaitQueueSize;
  65. this.databaseName = builder.DatabaseName;
  66. this.url = builder.ToString(); // keep canonical form
  67. }
  68. #endregion
  69. #region public properties
  70. /// <summary>
  71. /// Gets the actual wait queue size (either WaitQueueSize or WaitQueueMultiple x MaxConnectionPoolSize).
  72. /// </summary>
  73. public int ComputedWaitQueueSize {
  74. get {
  75. if (waitQueueMultiple == 0.0) {
  76. return waitQueueSize;
  77. } else {
  78. return (int) (waitQueueMultiple * serverSettings.MaxConnectionPoolSize);
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Gets the connection mode.
  84. /// </summary>
  85. public ConnectionMode ConnectionMode {
  86. get { return serverSettings.ConnectionMode; }
  87. }
  88. /// <summary>
  89. /// Gets the connect timeout.
  90. /// </summary>
  91. public TimeSpan ConnectTimeout {
  92. get { return serverSettings.ConnectTimeout; }
  93. }
  94. /// <summary>
  95. /// Gets the optional database name.
  96. /// </summary>
  97. public string DatabaseName {
  98. get { return databaseName; }
  99. }
  100. /// <summary>
  101. /// Gets the default credentials.
  102. /// </summary>
  103. public MongoCredentials DefaultCredentials {
  104. get { return serverSettings.DefaultCredentials; }
  105. }
  106. /// <summary>
  107. /// Gets the representation to use for Guids.
  108. /// </summary>
  109. public GuidRepresentation GuidRepresentation {
  110. get { return serverSettings.GuidRepresentation; }
  111. }
  112. /// <summary>
  113. /// Gets whether to use IPv6.
  114. /// </summary>
  115. public bool IPv6 {
  116. get { return serverSettings.IPv6; }
  117. }
  118. /// <summary>
  119. /// Gets the max connection idle time.
  120. /// </summary>
  121. public TimeSpan MaxConnectionIdleTime {
  122. get { return serverSettings.MaxConnectionIdleTime; }
  123. }
  124. /// <summary>
  125. /// Gets the max connection life time.
  126. /// </summary>
  127. public TimeSpan MaxConnectionLifeTime {
  128. get { return serverSettings.MaxConnectionLifeTime; }
  129. }
  130. /// <summary>
  131. /// Gets the max connection pool size.
  132. /// </summary>
  133. public int MaxConnectionPoolSize {
  134. get { return serverSettings.MaxConnectionPoolSize; }
  135. }
  136. /// <summary>
  137. /// Gets the min connection pool size.
  138. /// </summary>
  139. public int MinConnectionPoolSize {
  140. get { return serverSettings.MinConnectionPoolSize; }
  141. }
  142. /// <summary>
  143. /// Gets the name of the replica set.
  144. /// </summary>
  145. public string ReplicaSetName {
  146. get { return serverSettings.ReplicaSetName; }
  147. }
  148. /// <summary>
  149. /// Gets the SafeMode to use.
  150. /// </summary>
  151. public SafeMode SafeMode {
  152. get { return serverSettings.SafeMode; }
  153. }
  154. /// <summary>
  155. /// Gets the address of the server (see also Servers if using more than one address).
  156. /// </summary>
  157. public MongoServerAddress Server {
  158. get { return serverSettings.Server; }
  159. }
  160. /// <summary>
  161. /// Gets the list of server addresses (see also Server if using only one address).
  162. /// </summary>
  163. public IEnumerable<MongoServerAddress> Servers {
  164. get { return serverSettings.Servers; }
  165. }
  166. /// <summary>
  167. /// Gets whether queries should be sent to secondary servers.
  168. /// </summary>
  169. public bool SlaveOk {
  170. get { return serverSettings.SlaveOk; }
  171. }
  172. /// <summary>
  173. /// Gets the socket timeout.
  174. /// </summary>
  175. public TimeSpan SocketTimeout {
  176. get { return serverSettings.SocketTimeout; }
  177. }
  178. /// <summary>
  179. /// Gets the URL (in canonical form).
  180. /// </summary>
  181. public string Url {
  182. get { return url; }
  183. }
  184. /// <summary>
  185. /// Gets the wait queue multiple (the actual wait queue size will be WaitQueueMultiple x MaxConnectionPoolSize).
  186. /// </summary>
  187. public double WaitQueueMultiple {
  188. get { return waitQueueMultiple; }
  189. }
  190. /// <summary>
  191. /// Gets the wait queue size.
  192. /// </summary>
  193. public int WaitQueueSize {
  194. get { return waitQueueSize; }
  195. }
  196. /// <summary>
  197. /// Gets the wait queue timeout.
  198. /// </summary>
  199. public TimeSpan WaitQueueTimeout {
  200. get { return serverSettings.WaitQueueTimeout; }
  201. }
  202. #endregion
  203. #region public operators
  204. /// <summary>
  205. /// Compares two MongoUrls.
  206. /// </summary>
  207. /// <param name="lhs">The first URL.</param>
  208. /// <param name="rhs">The other URL.</param>
  209. /// <returns>True if the two URLs are equal (or both null).</returns>
  210. public static bool operator ==(
  211. MongoUrl lhs,
  212. MongoUrl rhs
  213. ) {
  214. return object.Equals(lhs, rhs);
  215. }
  216. /// <summary>
  217. /// Compares two MongoUrls.
  218. /// </summary>
  219. /// <param name="lhs">The first URL.</param>
  220. /// <param name="rhs">The other URL.</param>
  221. /// <returns>True if the two URLs are not equal (or one is null and the other is not).</returns>
  222. public static bool operator !=(
  223. MongoUrl lhs,
  224. MongoUrl rhs
  225. ) {
  226. return !(lhs == rhs);
  227. }
  228. #endregion
  229. #region public static methods
  230. /// <summary>
  231. /// Clears the URL cache. When a URL is parsed it is stored in the cache so that it doesn't have to be
  232. /// parsed again. There is rarely a need to call this method.
  233. /// </summary>
  234. public static void ClearCache() {
  235. cache.Clear();
  236. }
  237. /// <summary>
  238. /// Creates an instance of MongoUrl (might be an existing existence if the same URL has been used before).
  239. /// </summary>
  240. /// <param name="url">The URL containing the settings.</param>
  241. /// <returns>An instance of MongoUrl.</returns>
  242. public static MongoUrl Create(
  243. string url
  244. ) {
  245. // cache previously seen urls to avoid repeated parsing
  246. lock (staticLock) {
  247. MongoUrl mongoUrl;
  248. if (!cache.TryGetValue(url, out mongoUrl)) {
  249. mongoUrl = new MongoUrl(url);
  250. var canonicalUrl = mongoUrl.ToString();
  251. if (canonicalUrl != url) {
  252. if (cache.ContainsKey(canonicalUrl)) {
  253. mongoUrl = cache[canonicalUrl]; // use existing MongoUrl
  254. } else {
  255. cache[canonicalUrl] = mongoUrl; // cache under canonicalUrl also
  256. }
  257. }
  258. cache[url] = mongoUrl;
  259. }
  260. return mongoUrl;
  261. }
  262. }
  263. #endregion
  264. #region public methods
  265. /// <summary>
  266. /// Compares two MongoUrls.
  267. /// </summary>
  268. /// <param name="rhs">The other URL.</param>
  269. /// <returns>True if the two URLs are equal.</returns>
  270. public bool Equals(
  271. MongoUrl rhs
  272. ) {
  273. // this works because URL is in canonical form
  274. return this.url == rhs.url;
  275. }
  276. /// <summary>
  277. /// Compares two MongoUrls.
  278. /// </summary>
  279. /// <param name="obj">The other URL.</param>
  280. /// <returns>True if the two URLs are equal.</returns>
  281. public override bool Equals(
  282. object obj
  283. ) {
  284. return Equals(obj as MongoUrl);
  285. }
  286. /// <summary>
  287. /// Gets the hash code.
  288. /// </summary>
  289. /// <returns>The hash code.</returns>
  290. public override int GetHashCode() {
  291. // this works because URL is in canonical form
  292. return url.GetHashCode();
  293. }
  294. /// <summary>
  295. /// Creates a new instance of MongoServerSettings based on the settings in this MongoUrlBuilder.
  296. /// </summary>
  297. /// <returns>A new instance of MongoServerSettings.</returns>
  298. public MongoServerSettings ToServerSettings() {
  299. return serverSettings;
  300. }
  301. /// <summary>
  302. /// Returns the canonical URL based on the settings in this MongoUrlBuilder.
  303. /// </summary>
  304. /// <returns>The canonical URL.</returns>
  305. public override string ToString() {
  306. return url;
  307. }
  308. #endregion
  309. }
  310. }