diff --git a/src/Context.Tests/MongoDatabaseBuilderTests.cs b/src/Context.Tests/MongoDatabaseBuilderTests.cs index 9ea734e..76377fd 100644 --- a/src/Context.Tests/MongoDatabaseBuilderTests.cs +++ b/src/Context.Tests/MongoDatabaseBuilderTests.cs @@ -358,6 +358,24 @@ public void RegisterSerializer_RegisterNull_ThrowsException() // Assert Assert.Throws(registerSerializers); } + + #endregion + + #region DisableTableScan Tests + + [Fact] + public void DisableTableScan_SetMongoServerTableScanToDisabled_MongoServerTableScanIsDisabled() + { + // Arrange + var mongoDatabaseBuilder = new MongoDatabaseBuilder(_mongoOptions); + + // Act + mongoDatabaseBuilder.ConfigureDatabase(db => db.Client.DisableTableScan()); + MongoDbContextData result = mongoDatabaseBuilder.Build(); + + // Assert + Assert.True(result.Client.IsTableScanDisabled()); + } #endregion diff --git a/src/Context/IMongoDatabaseBuilder.cs b/src/Context/IMongoDatabaseBuilder.cs index 3890a8d..f05b9d8 100644 --- a/src/Context/IMongoDatabaseBuilder.cs +++ b/src/Context/IMongoDatabaseBuilder.cs @@ -13,11 +13,13 @@ IMongoDatabaseBuilder RegisterConventionPack( string name, IConventionPack conventions, Func filter); IMongoDatabaseBuilder RegisterCamelCaseConventionPack(); - + IMongoDatabaseBuilder ConfigureCollection( IMongoCollectionConfiguration configuration) where TDocument : class; IMongoDatabaseBuilder ConfigureConnection( Action mongoClientSettingsAction); + + IMongoDatabaseBuilder ConfigureDatabase(Action configureDatabase); } } diff --git a/src/Context/Internal/MongoDatabaseBuilder.cs b/src/Context/Internal/MongoDatabaseBuilder.cs index e634619..76e6a0d 100644 --- a/src/Context/Internal/MongoDatabaseBuilder.cs +++ b/src/Context/Internal/MongoDatabaseBuilder.cs @@ -14,6 +14,7 @@ internal class MongoDatabaseBuilder : IMongoDatabaseBuilder private readonly List _registrationConventionActions; private readonly List _registrationSerializerActions; private readonly List> _mongoClientSettingsActions; + private readonly List> _databaseConfigurationActions; private readonly List>> _builderActions; private static readonly object _lockObject = new object(); @@ -32,6 +33,7 @@ public MongoDatabaseBuilder(MongoOptions mongoOptions) _registrationConventionActions = new List(); _registrationSerializerActions = new List(); _mongoClientSettingsActions = new List>(); + _databaseConfigurationActions = new List>(); _builderActions = new List>>(); } @@ -96,6 +98,13 @@ public IMongoDatabaseBuilder RegisterSerializer(IBsonSerializer serializer return this; } + public IMongoDatabaseBuilder ConfigureDatabase(Action configureDatabase) + { + _databaseConfigurationActions.Add(configureDatabase); + + return this; + } + internal MongoDbContextData Build() { // synchronize registration @@ -127,6 +136,9 @@ internal MongoDbContextData Build() IMongoDatabase mongoDatabase = mongoClient .GetDatabase(_mongoOptions.DatabaseName); + // configure mongo database + _databaseConfigurationActions.ForEach(configure => configure(mongoDatabase)); + // create mongo collection builders var mongoCollectionBuilders = new Dictionary(); _builderActions.ForEach( diff --git a/src/Context/Internal/MongoDbContextData.cs b/src/Context/Internal/MongoDbContextData.cs index a5579cb..6d3e797 100644 --- a/src/Context/Internal/MongoDbContextData.cs +++ b/src/Context/Internal/MongoDbContextData.cs @@ -24,7 +24,8 @@ public MongoDbContextData( public IMongoClient Client { get; } public IMongoDatabase Database { get; } - internal IMongoCollection CreateCollection() where TDocument : class + internal IMongoCollection CreateCollection() + where TDocument : class { MongoCollectionBuilder collectionBuilder = TryGetCollectionBuilder(); @@ -37,7 +38,8 @@ internal IMongoCollection CreateCollection() where TDocume return collectionBuilder.Build(); } - private MongoCollectionBuilder TryGetCollectionBuilder() where TDocument : class + private MongoCollectionBuilder TryGetCollectionBuilder() + where TDocument : class { MongoCollectionBuilder collectionBuilder = null; diff --git a/src/Context/MongoServerExtensions.cs b/src/Context/MongoServerExtensions.cs new file mode 100644 index 0000000..ed13e01 --- /dev/null +++ b/src/Context/MongoServerExtensions.cs @@ -0,0 +1,33 @@ +using MongoDB.Bson; +using MongoDB.Driver; + +namespace MongoDB.Extensions.Context +{ + public static class MongoServerExtensions + { + public static void DisableTableScan(this IMongoClient mongoClient) + { + var command = new BsonDocument { { "setParameter", 1 }, { "notablescan", 1 } }; + + mongoClient.GetDatabase("admin").RunCommand(command); + } + + public static bool IsTableScanDisabled(this IMongoClient mongoClient) + { + var getNoTableScanCommand = + new BsonDocument { { "getParameter", 1 }, { "notablescan", 1 } }; + + BsonDocument parameterResult = mongoClient.GetDatabase("admin") + .RunCommand(getNoTableScanCommand); + + BsonElement notablescanElement = parameterResult.GetElement("notablescan"); + + if(bool.TryParse(notablescanElement.Value.ToString(), out bool isDisabled)) + { + return isDisabled; + } + + return false; + } + } +}