コード例 #1
0
        public void TestSafeModeTrueFSyncTrueW2WTimeout()
        {
            var connectionString = "mongodb://localhost/?safe=true;fsync=true;w=2;wtimeout=2s";
            var builder          = new MongoUrlBuilder("mongodb://localhost")
            {
                SafeMode = SafeMode.Create(true, true, 2, TimeSpan.FromSeconds(2))
            };

            Assert.AreEqual(SafeMode.Create(true, true, 2, TimeSpan.FromSeconds(2)), builder.SafeMode);
            Assert.AreEqual(connectionString, builder.ToString());
            Assert.AreEqual(connectionString, new MongoUrlBuilder(connectionString).ToString());
        }
コード例 #2
0
        public void TestSafeModeTrueFSyncTrueW2()
        {
            var connectionString = "mongodb://localhost/?safe=true;fsync=true;w=2";
            var builder          = new MongoUrlBuilder("mongodb://localhost")
            {
                SafeMode = SafeMode.Create(true, true, 2)
            };

            Assert.AreEqual(SafeMode.Create(true, true, 2), builder.SafeMode);
            Assert.AreEqual(connectionString, builder.ToString());
            Assert.AreEqual(connectionString, new MongoUrlBuilder(connectionString).ToString());
        }
コード例 #3
0
        public void TestAll()
        {
            var settings = new MongoServerSettings();

            settings.ConnectionMode     = ConnectionMode.ReplicaSet;
            settings.ConnectTimeout     = TimeSpan.FromSeconds(1);
            settings.DefaultCredentials = MongoCredentials.Create("username", "password");
            settings.GuidRepresentation = GuidRepresentation.CSharpLegacy;
            settings.IPv6 = true;
            settings.MaxConnectionIdleTime = TimeSpan.FromSeconds(2);
            settings.MaxConnectionLifeTime = TimeSpan.FromSeconds(3);
            settings.MaxConnectionPoolSize = 99;
            settings.MinConnectionPoolSize = 11;
            settings.ReadPreference        = ReadPreference.Primary;
            settings.ReplicaSetName        = "replicaname";
            settings.SafeMode         = SafeMode.Create(5, TimeSpan.FromSeconds(4));
            settings.Server           = new MongoServerAddress("server");
            settings.SocketTimeout    = TimeSpan.FromSeconds(5);
            settings.UseSsl           = true;
            settings.WaitQueueSize    = 55;
            settings.WaitQueueTimeout = TimeSpan.FromSeconds(6);

            Assert.AreEqual(ConnectionMode.ReplicaSet, settings.ConnectionMode);
            Assert.AreEqual(TimeSpan.FromSeconds(1), settings.ConnectTimeout);
            Assert.AreEqual(MongoCredentials.Create("username", "password"), settings.DefaultCredentials);
            Assert.AreEqual(GuidRepresentation.CSharpLegacy, settings.GuidRepresentation);
            Assert.AreEqual(true, settings.IPv6);
            Assert.AreEqual(TimeSpan.FromSeconds(2), settings.MaxConnectionIdleTime);
            Assert.AreEqual(TimeSpan.FromSeconds(3), settings.MaxConnectionLifeTime);
            Assert.AreEqual(99, settings.MaxConnectionPoolSize);
            Assert.AreEqual(11, settings.MinConnectionPoolSize);
            Assert.AreEqual(ReadPreference.Primary, settings.ReadPreference);
            Assert.AreEqual("replicaname", settings.ReplicaSetName);
            Assert.AreEqual(SafeMode.Create(5, TimeSpan.FromSeconds(4)), settings.SafeMode);
            Assert.AreEqual(new MongoServerAddress("server"), settings.Server);
            Assert.IsTrue((new[] { new MongoServerAddress("server") }).SequenceEqual(settings.Servers));
            Assert.AreEqual(TimeSpan.FromSeconds(5), settings.SocketTimeout);
            Assert.AreEqual(true, settings.UseSsl);
            Assert.AreEqual(55, settings.WaitQueueSize);
            Assert.AreEqual(TimeSpan.FromSeconds(6), settings.WaitQueueTimeout);

            Assert.IsFalse(settings.IsFrozen);
            var hashCode             = settings.GetHashCode();
            var stringRepresentation = settings.ToString();

            Assert.AreEqual(settings, settings);

            settings.Freeze();
            Assert.IsTrue(settings.IsFrozen);
            Assert.AreEqual(hashCode, settings.GetHashCode());
            Assert.AreEqual(stringRepresentation, settings.ToString());
        }
コード例 #4
0
 public void TestAll()
 {
     string connectionString = "mongodb://localhost/?connect=replicaSet;replicaSet=name;slaveOk=true;safe=true;fsync=true;w=2;wtimeout=2s;guids=PythonLegacy";
     MongoUrl url = new MongoUrl(connectionString);
     Assert.IsNull(url.DefaultCredentials);
     Assert.AreEqual(1, url.Servers.Count());
     Assert.AreEqual("localhost", url.Server.Host);
     Assert.AreEqual(27017, url.Server.Port);
     Assert.AreEqual(null, url.DatabaseName);
     Assert.AreEqual(ConnectionMode.ReplicaSet, url.ConnectionMode);
     Assert.AreEqual("name", url.ReplicaSetName);
     Assert.AreEqual(GuidRepresentation.PythonLegacy, url.GuidRepresentation);
     Assert.AreEqual(SafeMode.Create(true, true, 2, TimeSpan.FromSeconds(2)), url.SafeMode);
     Assert.AreEqual(true, url.SlaveOk);
     Assert.AreEqual(connectionString, url.ToString());
 }
コード例 #5
0
        public void TestAll()
        {
            string connectionString = "server=localhost;connect=replicaSet;replicaSet=name;slaveOk=true;safe=true;fsync=true;w=2;wtimeout=2s";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.IsNull(builder.Username);
            Assert.IsNull(builder.Password);
            Assert.AreEqual(1, builder.Servers.Count());
            Assert.AreEqual("localhost", builder.Server.Host);
            Assert.AreEqual(27017, builder.Server.Port);
            Assert.AreEqual(null, builder.DatabaseName);
            Assert.AreEqual(ConnectionMode.ReplicaSet, builder.ConnectionMode);
            Assert.AreEqual("name", builder.ReplicaSetName);
            Assert.AreEqual(SafeMode.Create(true, true, 2, TimeSpan.FromSeconds(2)), builder.SafeMode);
            Assert.AreEqual(true, builder.SlaveOk);
            Assert.AreEqual(connectionString, builder.ToString());
        }
コード例 #6
0
        public MongoDataProvider(string joinParentId, string mongoConnectionString, string databaseName, string safeMode)
        {
            bool parsedSafeMode;

            SafeMode = SafeMode.Create(bool.TryParse(safeMode, out parsedSafeMode) ? parsedSafeMode : false);

            JoinParentId = new ID(joinParentId);

            Server = MongoServer.Create(mongoConnectionString);

            Db = Server.GetDatabase(databaseName);

            Items = Db.GetCollection <Item>("items", SafeMode);

            Items.EnsureIndex(IndexKeys.Ascending(new[] { "ParentID" }));
            Items.EnsureIndex(IndexKeys.Ascending(new[] { "TemplateID" }));

            EnsureNotEmpty();
        }
コード例 #7
0
        public override void Initialize(string name, NameValueCollection config)
        {
            this._SessionStateSection = ConfigurationManager.GetSection("system.web/sessionState") as SessionStateSection;
            this._MongoWebSection     = ConfigurationManager.GetSection("mongoDbWeb") as MongoDbWebSection;

            this._MongoCollection = ConnectionHelper.GetDatabase(config)
                                    .GetCollection <T>(config["collection"] ?? _MongoWebSection.SessionState.MongoCollectionName);

            _SessionDataClassMap = new BsonClassMap <T>();
            _SessionDataClassMap.AutoMap();

            _ApplicationVirtualPathField = MapBsonMember(t => t.ApplicationVirtualPath);
            _IdField                  = MapBsonMember(t => t.SessionId);
            _LockIdField              = MapBsonMember(t => t.LockId);
            _LockedField              = MapBsonMember(t => t.Locked);
            _ExpiresField             = MapBsonMember(t => t.Expires);
            _LockDateField            = MapBsonMember(t => t.LockDate);
            _SessionStateActionsField = MapBsonMember(t => t.SessionStateActions);
            _SessionItemsField        = MapBsonMember(t => t.SessionStateItems);

            // Ideally, we'd just use the object's bson id - however, serialization gets a bit wonky
            // when trying to have an Id property which isn't a simple type in the base class
            // This also provides better backwards compatibility with the old BsonDocument implementation (field names match)
            this._MongoCollection.EnsureIndex(
                IndexKeys.Ascending(_ApplicationVirtualPathField, _IdField), IndexOptions.SetUnique(true));

            if (_Cache == null)
            {
                lock (_CacheGuarantee)
                {
                    if (_Cache == null)
                    {
                        _Cache = new Cache <string, T> .Builder()
                        {
                            EntryExpiration = new TimeSpan(0, 0, _MongoWebSection.SessionState.MemoryCacheExpireSeconds),
                            MaxEntries      = _MongoWebSection.SessionState.MaxInMemoryCachedSessions
                        }

                        .Cache;
                    }
                }
            }

            // Initialise safe mode options. Defaults to Safe Mode=true, fsynch=false, w=0 (replicas to write to before returning)
            bool safeModeEnabled = true;

            bool fsync = _MongoWebSection.FSync;

            if (config["fsync"] != null)
            {
                bool.TryParse(config["fsync"], out fsync);
            }

            int replicasToWrite = _MongoWebSection.ReplicasToWrite;

            if ((config["replicasToWrite"] != null) && (!int.TryParse(config["replicasToWrite"], out replicasToWrite)))
            {
                throw new ProviderException("replicasToWrite must be a valid integer");
            }

            _SafeMode = SafeMode.Create(safeModeEnabled, fsync, replicasToWrite);

            base.Initialize(name, config);
        }
コード例 #8
0
        public override void Initialize(string name, NameValueCollection config)
        {
            this._SessionStateSection = ConfigurationManager.GetSection("system.web/sessionState") as SessionStateSection;
            this._MongoWebSection     = ConfigurationManager.GetSection("mongoDbWeb") as MongoDbWebSection;

            var connectionString = ConnectionHelper.GetDatabaseConnectionString(_MongoWebSection, config);
            var databaseName     = ConnectionHelper.GetDatabaseName(_MongoWebSection, config);
            var collectionName   =
                _MongoWebSection != null &&
                _MongoWebSection.SessionState != null &&
                _MongoWebSection.SessionState.MongoCollectionName != null
                                ? _MongoWebSection.SessionState.MongoCollectionName
                                : config["collection"];

            this._MongoCollection = MongoServer.Create(connectionString)
                                    .GetDatabase(databaseName)
                                    .GetCollection <T>(collectionName);

            _UseLock = config["useLock"] == "true" ? true : _MongoWebSection.SessionState.UseLock;

            _DotNetMemoryCacheName = config["dotNetMemoryCacheName"] == null
                                ? _MongoWebSection.SessionState.DotNetMemoryCacheName
                                : config["dotNetMemoryCacheName"];

            _SessionDataClassMap = new BsonClassMap <T>();
            _SessionDataClassMap.AutoMap();

            _ApplicationVirtualPathField = MapBsonMember(t => t.ApplicationVirtualPath);
            _IdField                  = MapBsonMember(t => t.SessionId);
            _LockIdField              = MapBsonMember(t => t.LockId);
            _LockedField              = MapBsonMember(t => t.Locked);
            _AccessedField            = MapBsonMember(t => t.Accessed);
            _ExpiresField             = MapBsonMember(t => t.Expires);
            _LockDateField            = MapBsonMember(t => t.LockDate);
            _SessionStateActionsField = MapBsonMember(t => t.SessionStateActions);
            _SessionItemsField        = MapBsonMember(t => t.SessionStateItems);

            // Ideally, we'd just use the object's bson id - however, serialization gets a bit wonky
            // when trying to have an Id property which isn't a simple type in the base class
            // This also provides better backwards compatibility with the old BsonDocument implementation (field names match)
            this._MongoCollection.EnsureIndex(
                IndexKeys.Ascending(_ApplicationVirtualPathField, _IdField), IndexOptions.SetUnique(true));

            // MongoDB TTL collection http://docs.mongodb.org/manual/tutorial/expire-data/
            this._MongoCollection.EnsureIndex(IndexKeys.Ascending(_AccessedField), IndexOptions.SetTimeToLive(_SessionStateSection.Timeout));

            bool useDotNetMemoryCache = !string.IsNullOrWhiteSpace(_DotNetMemoryCacheName);

            if (useDotNetMemoryCache && _MemoryCache == null)
            {
                lock (_CacheGuarantee)
                {
                    if (_MemoryCache == null)
                    {
                        _MemoryCache = new MemoryCache(_DotNetMemoryCacheName);
                    }
                }
            }

            // Initialise safe mode options. Defaults to Safe Mode=true, fsynch=false, w=0 (replicas to write to before returning)
            bool safeModeEnabled = true;

            bool fsync = _MongoWebSection.FSync;

            if (config["fsync"] != null)
            {
                bool.TryParse(config["fsync"], out fsync);
            }

            int replicasToWrite = _MongoWebSection.ReplicasToWrite;

            if ((config["replicasToWrite"] != null) && (!int.TryParse(config["replicasToWrite"], out replicasToWrite)))
            {
                throw new ProviderException("replicasToWrite must be a valid integer");
            }

            _SafeMode = SafeMode.Create(safeModeEnabled, fsync, replicasToWrite);

            base.Initialize(name, config);
        }