/// <summary> /// Gets or sets individual settings by keyword. /// </summary> /// <param name="keyword">The keyword.</param> /// <returns>The value of the setting.</returns> public override object this[ string keyword ] { get { if (keyword == null) { throw new ArgumentNullException("keyword"); } return(base[canonicalKeywords[keyword.ToLower()]]); } set { if (keyword == null) { throw new ArgumentNullException("keyword"); } bool fsync; int w; TimeSpan wtimeout; switch (keyword.ToLower()) { case "connect": if (value is string) { ConnectionMode = (ConnectionMode)Enum.Parse(typeof(ConnectionMode), (string)value, true); } else { ConnectionMode = (ConnectionMode)value; } break; case "connecttimeout": case "connecttimeoutms": ConnectTimeout = ToTimeSpan(keyword, value); break; case "database": DatabaseName = (string)value; break; case "fsync": fsync = Convert.ToBoolean(value); w = (safeMode != null) ? safeMode.W : 0; wtimeout = (safeMode != null) ? safeMode.WTimeout : TimeSpan.Zero; SafeMode = SafeMode.Create(true, fsync, w, wtimeout); break; case "ipv6": IPv6 = Convert.ToBoolean(value); break; case "maxidletime": case "maxidletimems": MaxConnectionIdleTime = ToTimeSpan(keyword, value); break; case "maxlifetime": case "maxlifetimems": MaxConnectionLifeTime = ToTimeSpan(keyword, value); break; case "maxpoolsize": MaxConnectionPoolSize = Convert.ToInt32(value); break; case "minpoolsize": MinConnectionPoolSize = Convert.ToInt32(value); break; case "password": Password = (string)value; break; case "replicaset": ReplicaSetName = (string)value; ConnectionMode = ConnectionMode.ReplicaSet; break; case "safe": if (Convert.ToBoolean(value)) { fsync = (safeMode == null) ? false : safeMode.FSync; w = (safeMode == null) ? 0 : safeMode.W; wtimeout = (w == 0 || safeMode == null) ? TimeSpan.Zero : safeMode.WTimeout; SafeMode = SafeMode.Create(true, fsync, w, wtimeout); } else { SafeMode = SafeMode.False; } break; case "server": case "servers": Servers = ParseServersString((string)value); break; case "slaveok": SlaveOk = Convert.ToBoolean(value); break; case "sockettimeout": case "sockettimeoutms": SocketTimeout = ToTimeSpan(keyword, value); break; case "username": Username = (string)value; break; case "w": w = Convert.ToInt32(value); fsync = (safeMode == null) ? false : safeMode.FSync; wtimeout = (w == 0 || safeMode == null) ? TimeSpan.Zero : safeMode.WTimeout; SafeMode = SafeMode.Create(true, fsync, w, wtimeout); break; case "waitqueuemultiple": WaitQueueMultiple = Convert.ToDouble(value); WaitQueueSize = 0; break; case "waitqueuesize": WaitQueueSize = Convert.ToInt32(value); WaitQueueMultiple = 0; break; case "waitqueuetimeout": case "waitqueuetimeoutms": WaitQueueTimeout = ToTimeSpan(keyword, value); break; case "wtimeout": wtimeout = ToTimeSpan(keyword, value); fsync = (safeMode == null) ? false : safeMode.FSync; w = (safeMode == null) ? 0 : safeMode.W; SafeMode = SafeMode.Create(true, fsync, w, wtimeout); break; default: var message = string.Format("Invalid keyword: {0}", keyword); throw new ArgumentException(message); } } }
/// <summary> /// Parses a URL and sets all settings to match the URL. /// </summary> /// <param name="url">The URL.</param> public void Parse( string url ) { ResetValues(); const string pattern = @"^mongodb://" + @"((?<username>[^:]+):(?<password>[^@]+)@)?" + @"(?<servers>[^:,/]+(:\d+)?(,[^:,/]+(:\d+)?)*)" + @"(/(?<database>[^?]+)?(\?(?<query>.*))?)?$"; Match match = Regex.Match(url, pattern); if (match.Success) { string username = Uri.UnescapeDataString(match.Groups["username"].Value); string password = Uri.UnescapeDataString(match.Groups["password"].Value); string servers = match.Groups["servers"].Value; string databaseName = match.Groups["database"].Value; string query = match.Groups["query"].Value; if (username != "" && password != "") { this.defaultCredentials = new MongoCredentials(username, password); } else { this.defaultCredentials = null; } if (servers != "") { List <MongoServerAddress> addresses = new List <MongoServerAddress>(); foreach (string server in servers.Split(',')) { var address = MongoServerAddress.Parse(server); addresses.Add(address); } if (addresses.Count == 1) { this.connectionMode = ConnectionMode.Direct; } else if (addresses.Count > 1) { this.connectionMode = ConnectionMode.ReplicaSet; } this.servers = addresses; } else { throw new FormatException("Invalid connection string: server missing"); } this.databaseName = (databaseName != "") ? databaseName : null; if (!string.IsNullOrEmpty(query)) { var safeModeChanged = false; var safe = false; var w = 0; var wtimeout = TimeSpan.Zero; var fsync = false; foreach (var pair in query.Split('&', ';')) { var parts = pair.Split('='); if (parts.Length != 2) { throw new FormatException(string.Format("Invalid connection string: {0}", parts)); } var name = parts[0]; var value = parts[1]; switch (name.ToLower()) { case "connect": connectionMode = ParseConnectionMode(name, value); break; case "connecttimeout": case "connecttimeoutms": connectTimeout = ParseTimeSpan(name, value); break; case "fsync": safeModeChanged = true; safe = true; fsync = ParseBoolean(name, value); break; case "ipv6": ipv6 = ParseBoolean(name, value); break; case "maxidletime": case "maxidletimems": maxConnectionIdleTime = ParseTimeSpan(name, value); break; case "maxlifetime": case "maxlifetimems": maxConnectionLifeTime = ParseTimeSpan(name, value); break; case "maxpoolsize": maxConnectionPoolSize = ParseInt32(name, value); break; case "minpoolsize": minConnectionPoolSize = ParseInt32(name, value); break; case "replicaset": this.replicaSetName = value; this.connectionMode = ConnectionMode.ReplicaSet; break; case "safe": safeModeChanged = true; safe = ParseBoolean(name, value); break; case "slaveok": slaveOk = ParseBoolean(name, value); break; case "sockettimeout": case "sockettimeoutms": socketTimeout = ParseTimeSpan(name, value); break; case "w": safeModeChanged = true; safe = true; w = ParseInt32(name, value); break; case "waitqueuemultiple": waitQueueMultiple = ParseDouble(name, value); waitQueueSize = 0; break; case "waitqueuesize": waitQueueMultiple = 0; waitQueueSize = ParseInt32(name, value); break; case "waitqueuetimeout": case "waitqueuetimeoutms": waitQueueTimeout = ParseTimeSpan(name, value); break; case "wtimeout": case "wtimeoutms": safeModeChanged = true; safe = true; wtimeout = ParseTimeSpan(name, value); break; } } if (safeModeChanged) { this.safeMode = SafeMode.Create(safe, fsync, w, wtimeout); } } } else { throw new FormatException(string.Format("Invalid connection string: {0}", url)); } }