Пример #1
0
        /// <summary>
        /// Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code. Managed and unmanaged resources
        /// can be disposed.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. Only unmanaged resources can be disposed.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // Note disposing has been done.
                _disposed = true;

                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    if (_server != null)
                    {
                        try
                        {
                            // Disconnect from the server.
                            _server.Disconnect();
                        }
                        catch { }
                    }
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                _server         = null;
                _client         = null;
                _serverAddress  = null;
                _clientSettings = null;
            }
        }
Пример #2
0
 /// <summary>
 /// Gets the server instance.
 /// </summary>
 /// <param name="address">The address.</param>
 /// <returns>The server instance.</returns>
 public virtual MongoServerInstance GetServerInstance(MongoServerAddress address)
 {
     lock (_serverLock)
     {
         return(_serverInstances.FirstOrDefault(i => i.Address.Equals(address)));
     }
 }
Пример #3
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="address">The address.</param>
        internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address)
        {
            _settings     = settings;
            _address      = address;
            _sequentialId = Interlocked.Increment(ref __nextSequentialId);
            _state        = MongoServerState.Disconnected;
            _serverInfo   = new ServerInformation
            {
                MaxDocumentSize  = MongoDefaults.MaxDocumentSize,
                MaxMessageLength = MongoDefaults.MaxMessageLength,
                InstanceType     = MongoServerInstanceType.Unknown
            };
            _connectionPool          = new MongoConnectionPool(this);
            _pingTimeAggregator      = new PingTimeAggregator(5);
            _permanentlyDisconnected = false;
            // Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);

            _stateVerificationAcquireConnectionOptions = new MongoConnectionPool.AcquireConnectionOptions
            {
                OkToAvoidWaitingByCreatingNewConnection = false,
                OkToExceedMaxConnectionPoolSize         = true,
                OkToExceedWaitQueueSize = true,
                WaitQueueTimeout        = TimeSpan.FromSeconds(2)
            };
        }
Пример #4
0
        static void Main(string[] args)
        {
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            var settings = new MongoDB.Driver.MongoServerSettings();

            var address = new MongoDB.Driver.MongoServerAddress(Properties.Settings.Default.Server, Properties.Settings.Default.Port);

            settings.Server = address;

            MongoDB.Driver.MongoServer server = new MongoDB.Driver.MongoServer(settings);


            var           client   = new MongoDB.Driver.MongoClient();
            MongoDatabase database = server.GetDatabase("mc2db");

            Console.WriteLine("Aloitetaan...");
            var now    = MC2DateTimeValue.Now().ToLocalTime();
            var nowStr = string.Format("{0:0000}-{1:00}-{2:00}-{3:00}-{4:00}-{5:00}",
                                       now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);

            var logFile = Path.Combine(string.Format("{0}_{1}.txt", Properties.Settings.Default.ExcelPath + "\\Log", nowStr));

            if (!Directory.Exists(Path.GetDirectoryName(logFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(logFile));
            }
            var x = new PayrollExport(logFile, database);

            x.ExportDocuments();
            Console.WriteLine("Valmis...aikaa kului {0} minuuttia", sw.Elapsed.TotalMinutes);
        }
Пример #5
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoServerInstance" /> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="address">The address.</param>
 /// <param name="cluster">The cluster.</param>
 /// <param name="endPoint">The end point.</param>
 internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address, ICluster cluster, EndPoint endPoint)
 {
     _settings     = settings;
     _address      = address;
     _cluster      = cluster;
     _sequentialId = Interlocked.Increment(ref __nextSequentialId);
     _endPoint     = endPoint;
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="address">The address.</param>
 /// <param name="cluster">The cluster.</param>
 internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address, ICluster cluster)
 {
     _settings     = settings;
     _address      = address;
     _cluster      = cluster;
     _sequentialId = Interlocked.Increment(ref __nextSequentialId);
     _endPoint     = new DnsEndPoint(address.Host, address.Port);
 }
        private IEnumerable <MongoServerAddress> ParseServersString(string value)
        {
            var servers = new List <MongoServerAddress>();

            foreach (var server in value.Split(','))
            {
                servers.Add(MongoServerAddress.Parse(server));
            }
            return(servers);
        }
Пример #8
0
 private void StartTrackingServerInstances()
 {
     _serverInstances.AddRange(_cluster.Description.Servers.Select(serverDescription =>
     {
         var endPoint = (DnsEndPoint)serverDescription.EndPoint;
         var address  = new MongoServerAddress(endPoint.Host, endPoint.Port);
         return(new MongoServerInstance(_settings, address, _cluster));
     }));
     _serverInstances.Sort(ServerInstanceAddressComparer.Instance);
     _cluster.DescriptionChanged += OnClusterDescriptionChanged;
 }
        private MongoServerState state; // always use property to set value so event gets raised
        #endregion

        #region constructors
        internal MongoServerInstance(
            MongoServer server,
            MongoServerAddress address
            )
        {
            this.server           = server;
            this.address          = address;
            this.maxDocumentSize  = MongoDefaults.MaxDocumentSize;
            this.maxMessageLength = MongoDefaults.MaxMessageLength;
            this.state            = MongoServerState.Disconnected;
        }
Пример #10
0
        private MongoServerState _state; // always use property to set value so event gets raised

        // constructors
        internal MongoServerInstance(MongoServer server, MongoServerAddress address)
        {
            _server           = server;
            _address          = address;
            _sequentialId     = Interlocked.Increment(ref __nextSequentialId);
            _maxDocumentSize  = MongoDefaults.MaxDocumentSize;
            _maxMessageLength = MongoDefaults.MaxMessageLength;
            _state            = MongoServerState.Disconnected;
            _connectionPool   = new MongoConnectionPool(this);
            // Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
        }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="address">The address.</param>
 internal MongoServerInstance(MongoServer server, MongoServerAddress address)
 {
     _server       = server;
     _address      = address;
     _sequentialId = Interlocked.Increment(ref __nextSequentialId);
     _state        = MongoServerState.Disconnected;
     _serverInfo   = new ServerInformation
     {
         MaxDocumentSize  = MongoDefaults.MaxDocumentSize,
         MaxMessageLength = MongoDefaults.MaxMessageLength,
         InstanceType     = MongoServerInstanceType.Unknown
     };
     _connectionPool          = new MongoConnectionPool(this);
     _pingTimeAggregator      = new PingTimeAggregator(5);
     _permanentlyDisconnected = false;
     // Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
 }
Пример #12
0
        // private methods
        private IEnumerable <MongoServerAddress> GetInstanceAddressesFromNamedResponseElement(string elementName)
        {
            if (!Response.Contains(elementName))
            {
                return(Enumerable.Empty <MongoServerAddress>());
            }

            List <MongoServerAddress> instanceAddresses = new List <MongoServerAddress>();

            foreach (var hostName in Response[elementName].AsBsonArray)
            {
                var address = MongoServerAddress.Parse(hostName.AsString);
                instanceAddresses.Add(address);
            }

            return(instanceAddresses);
        }
        private TCommandResult RunWriteCommandAs <TCommandResult>(
            BsonDocument command,
            IBsonSerializer <TCommandResult> resultSerializer,
            out MongoServerInstance serverInstance)
            where TCommandResult : CommandResult
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new WriteCommandOperation <TCommandResult>(new DatabaseNamespace(_name), command, resultSerializer, messageEncoderSettings);

            using (var binding = _server.GetWriteBinding())
                using (var connectionSource = binding.GetWriteConnectionSource())
                {
                    var endPoint = (DnsEndPoint)connectionSource.ServerDescription.EndPoint;
                    var address  = new MongoServerAddress(endPoint.Host, endPoint.Port);
                    serverInstance = _server.GetServerInstance(address);
                    return(operation.Execute(connectionSource, Timeout.InfiniteTimeSpan, CancellationToken.None));
                }
        }
Пример #14
0
        private IDisposable RequestStart(IServerSelector serverSelector, ReadPreference readPreference)
        {
            var request = __threadStaticRequest;

            if (request != null)
            {
                var selected = serverSelector.SelectServers(_cluster.Description, new[] { request.ServerDescription }).ToList();
                if (selected.Count == 0)
                {
                    throw new InvalidOperationException("A nested call to RequestStart was made that is not compatible with the existing request.");
                }
                request.NestingLevel++;
                return(new RequestStartResult(this));
            }

            IReadBindingHandle connectionBinding;
            var server = _cluster.SelectServer(serverSelector);

            using (var connection = server.GetConnection())
            {
                if (readPreference.ReadPreferenceMode == ReadPreferenceMode.Primary)
                {
                    connectionBinding = new ReadWriteBindingHandle(new ConnectionReadWriteBinding(server, connection.Fork()));
                }
                else
                {
                    connectionBinding = new ReadBindingHandle(new ConnectionReadBinding(server, connection.Fork(), readPreference));
                }
            }

            var serverDescription = server.Description;
            var endPoint          = (DnsEndPoint)serverDescription.EndPoint;
            var serverAddress     = new MongoServerAddress(endPoint.Host, endPoint.Port);
            var serverInstance    = _serverInstances.Single(i => i.Address == serverAddress);

            __threadStaticRequest = new Request(serverDescription, serverInstance, connectionBinding);

            return(new RequestStartResult(this));
        }
Пример #15
0
        internal void VerifyState(MongoConnection connection)
        {
            CommandResult isMasterResult = null;
            bool          ok             = false;

            try
            {
                var isMasterCommand = new CommandDocument("ismaster", 1);
                isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand, false);
                if (!isMasterResult.Ok)
                {
                    throw new MongoCommandException(isMasterResult);
                }

                var isPrimary   = isMasterResult.Response["ismaster", false].ToBoolean();
                var isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                var isPassive   = isMasterResult.Response["passive", false].ToBoolean();
                var isArbiter   = isMasterResult.Response["arbiterOnly", false].ToBoolean();
                // workaround for CSHARP-273
                if (isPassive && isArbiter)
                {
                    isPassive = false;
                }

                var maxDocumentSize  = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                var maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize

                MongoServerBuildInfo buildInfo;
                var buildInfoCommand = new CommandDocument("buildinfo", 1);
                var buildInfoResult  = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, buildInfoCommand, false);
                if (buildInfoResult.Ok)
                {
                    buildInfo = new MongoServerBuildInfo(
                        buildInfoResult.Response["bits"].ToInt32(),      // bits
                        buildInfoResult.Response["gitVersion"].AsString, // gitVersion
                        buildInfoResult.Response["sysInfo"].AsString,    // sysInfo
                        buildInfoResult.Response["version"].AsString     // versionString
                        );
                }
                else
                {
                    // short term fix: if buildInfo fails due to auth we don't know the server version; see CSHARP-324
                    if (buildInfoResult.ErrorMessage != "need to login")
                    {
                        throw new MongoCommandException(buildInfoResult);
                    }
                    buildInfo = null;
                }

                _isMasterResult   = isMasterResult;
                _maxDocumentSize  = maxDocumentSize;
                _maxMessageLength = maxMessageLength;
                _buildInfo        = buildInfo;
                this.SetState(MongoServerState.Connected, isPrimary, isSecondary, isPassive, isArbiter);

                // if this is the primary of a replica set check to see if any instances have been added or removed
                if (isPrimary && _server.Settings.ConnectionMode == ConnectionMode.ReplicaSet)
                {
                    var instanceAddresses = new List <MongoServerAddress>();
                    if (isMasterResult.Response.Contains("hosts"))
                    {
                        foreach (var hostName in isMasterResult.Response["hosts"].AsBsonArray)
                        {
                            var address = MongoServerAddress.Parse(hostName.AsString);
                            instanceAddresses.Add(address);
                        }
                    }
                    if (isMasterResult.Response.Contains("passives"))
                    {
                        foreach (var hostName in isMasterResult.Response["passives"].AsBsonArray)
                        {
                            var address = MongoServerAddress.Parse(hostName.AsString);
                            instanceAddresses.Add(address);
                        }
                    }
                    if (isMasterResult.Response.Contains("arbiters"))
                    {
                        foreach (var hostName in isMasterResult.Response["arbiters"].AsBsonArray)
                        {
                            var address = MongoServerAddress.Parse(hostName.AsString);
                            instanceAddresses.Add(address);
                        }
                    }
                    _server.VerifyInstances(instanceAddresses);
                }

                ok = true;
            }
            finally
            {
                if (!ok)
                {
                    _isMasterResult   = isMasterResult;
                    _maxDocumentSize  = MongoDefaults.MaxDocumentSize;
                    _maxMessageLength = MongoDefaults.MaxMessageLength;
                    _buildInfo        = null;
                    this.SetState(MongoServerState.Disconnected, false, false, false, false);
                }
            }
        }
Пример #16
0
        /// <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));
            }
        }
        /// <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 serverPattern = @"((\[[^]]+?\]|[^:,/]+)(:\d+)?)";
            const string pattern       =
                @"^mongodb://" +
                @"((?<username>[^:]+):(?<password>[^@]+)@)?" +
                @"(?<servers>" + serverPattern + "(," + serverPattern + ")*)?" +
                @"(/(?<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 != "")
                {
                    _defaultCredentials = new MongoCredentials(username, password);
                }
                else
                {
                    _defaultCredentials = null;
                }

                if (servers != "")
                {
                    List <MongoServerAddress> addresses = new List <MongoServerAddress>();
                    foreach (string server in servers.Split(','))
                    {
                        var address = MongoServerAddress.Parse(server);
                        addresses.Add(address);
                    }
                    _servers = addresses;
                }

                _databaseName = (databaseName != "") ? databaseName : null;

                if (!string.IsNullOrEmpty(query))
                {
                    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":
                            FSync = ParseBoolean(name, value);
                            break;

                        case "guids":
                        case "uuidrepresentation":
                            GuidRepresentation = (GuidRepresentation)Enum.Parse(typeof(GuidRepresentation), value, true);     // ignoreCase
                            break;

                        case "ipv6":
                            IPv6 = ParseBoolean(name, value);
                            break;

                        case "j":
                        case "journal":
                            Journal = 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 "readpreference":
                            if (_readPreference == null)
                            {
                                _readPreference = new ReadPreference();
                            }
                            ReadPreference.ReadPreferenceMode = ParseReadPreferenceMode(name, value);
                            break;

                        case "readpreferencetags":
                            if (_readPreference == null)
                            {
                                _readPreference = new ReadPreference {
                                    ReadPreferenceMode = ReadPreferenceMode.Primary
                                };
                            }
                            ReadPreference.AddTagSet(ParseReplicaSetTagSet(name, value));
                            break;

                        case "replicaset":
                            ReplicaSetName = value;
                            break;

                        case "safe":
                            var safe = Convert.ToBoolean(value);
                            if (_w == null)
                            {
                                W = safe ? 1 : 0;
                            }
                            else
                            {
                                if (safe)
                                {
                                    // don't overwrite existing W value unless it's 0
                                    var wCount = _w as WriteConcern.WCount;
                                    if (wCount != null && wCount.Value == 0)
                                    {
                                        W = 1;
                                    }
                                }
                                else
                                {
                                    W = 0;
                                }
                            }
                            break;

                        case "secondaryacceptablelatency":
                        case "secondaryacceptablelatencyms":
                            SecondaryAcceptableLatency = ParseTimeSpan(name, value);
                            break;

                        case "slaveok":
#pragma warning disable 618
                            SlaveOk = ParseBoolean(name, value);
#pragma warning restore
                            break;

                        case "sockettimeout":
                        case "sockettimeoutms":
                            SocketTimeout = ParseTimeSpan(name, value);
                            break;

                        case "ssl":
                            UseSsl = ParseBoolean(name, value);
                            break;

                        case "sslverifycertificate":
                            VerifySslCertificate = ParseBoolean(name, value);
                            break;

                        case "w":
                            W = WriteConcern.WValue.Parse(value);
                            break;

                        case "waitqueuemultiple":
                            WaitQueueMultiple = ParseDouble(name, value);
                            break;

                        case "waitqueuesize":
                            WaitQueueSize = ParseInt32(name, value);
                            break;

                        case "waitqueuetimeout":
                        case "waitqueuetimeoutms":
                            WaitQueueTimeout = ParseTimeSpan(name, value);
                            break;

                        case "wtimeout":
                        case "wtimeoutms":
                            WTimeout = ParseTimeSpan(name, value);
                            break;

                        default:
                            var message = string.Format("Invalid option '{0}'.", name);
                            throw new ArgumentException(message, "url");
                        }
                    }
                }
            }
            else
            {
                throw new FormatException(string.Format("Invalid connection string '{0}'.", url));
            }
        }
Пример #18
0
        // public methods
        /// <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 serverPattern = @"((\[[^]]+?\]|[^:,/]+)(:\d+)?)";
            const string pattern       =
                @"^mongodb://" +
                @"((?<username>[^:]+):(?<password>[^@]+)@)?" +
                @"(?<servers>" + serverPattern + "(," + serverPattern + ")*)" +
                @"(/(?<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 != "")
                {
                    _defaultCredentials = new MongoCredentials(username, password);
                }
                else
                {
                    _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)
                    {
                        _connectionMode = ConnectionMode.Direct;
                    }
                    else if (addresses.Count > 1)
                    {
                        _connectionMode = ConnectionMode.ReplicaSet;
                    }
                    _servers = addresses;
                }
                else
                {
                    throw new FormatException("Invalid connection string. Server missing.");
                }

                _databaseName = (databaseName != "") ? databaseName : null;

                if (!string.IsNullOrEmpty(query))
                {
                    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":
                            if (_safeMode == null)
                            {
                                _safeMode = new SafeMode(false);
                            }
                            _safeMode.FSync = ParseBoolean(name, value);
                            break;

                        case "guids":
                            _guidRepresentation = (GuidRepresentation)Enum.Parse(typeof(GuidRepresentation), value, true);     // ignoreCase
                            break;

                        case "ipv6":
                            _ipv6 = ParseBoolean(name, value);
                            break;

                        case "j":
                            if (_safeMode == null)
                            {
                                _safeMode = new SafeMode(false);
                            }
                            SafeMode.J = 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":
                            _replicaSetName = value;
                            _connectionMode = ConnectionMode.ReplicaSet;
                            break;

                        case "safe":
                            if (_safeMode == null)
                            {
                                _safeMode = new SafeMode(false);
                            }
                            SafeMode.Enabled = ParseBoolean(name, value);
                            break;

                        case "slaveok":
                            _slaveOk = ParseBoolean(name, value);
                            break;

                        case "sockettimeout":
                        case "sockettimeoutms":
                            _socketTimeout = ParseTimeSpan(name, value);
                            break;

                        case "w":
                            if (_safeMode == null)
                            {
                                _safeMode = new SafeMode(false);
                            }
                            try
                            {
                                SafeMode.W = ParseInt32(name, value);
                            }
                            catch (FormatException)
                            {
                                SafeMode.WMode = 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":
                            if (_safeMode == null)
                            {
                                _safeMode = new SafeMode(false);
                            }
                            SafeMode.WTimeout = ParseTimeSpan(name, value);
                            break;
                        }
                    }
                }
            }
            else
            {
                throw new FormatException(string.Format("Invalid connection string '{0}'.", url));
            }
        }