Skip to content

Commit

Permalink
Implemented MongoDb server disable table scan option. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
nscheibe authored Mar 21, 2020
1 parent a68972b commit 740cf56
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/Context.Tests/MongoDatabaseBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,24 @@ public void RegisterSerializer_RegisterNull_ThrowsException()
// Assert
Assert.Throws<ArgumentNullException>(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

Expand Down
4 changes: 3 additions & 1 deletion src/Context/IMongoDatabaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ IMongoDatabaseBuilder RegisterConventionPack(
string name, IConventionPack conventions, Func<Type, bool> filter);

IMongoDatabaseBuilder RegisterCamelCaseConventionPack();

IMongoDatabaseBuilder ConfigureCollection<TDocument>(
IMongoCollectionConfiguration<TDocument> configuration) where TDocument : class;

IMongoDatabaseBuilder ConfigureConnection(
Action<MongoClientSettings> mongoClientSettingsAction);

IMongoDatabaseBuilder ConfigureDatabase(Action<IMongoDatabase> configureDatabase);
}
}
12 changes: 12 additions & 0 deletions src/Context/Internal/MongoDatabaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class MongoDatabaseBuilder : IMongoDatabaseBuilder
private readonly List<Action> _registrationConventionActions;
private readonly List<Action> _registrationSerializerActions;
private readonly List<Action<MongoClientSettings>> _mongoClientSettingsActions;
private readonly List<Action<IMongoDatabase>> _databaseConfigurationActions;
private readonly List<Action<IMongoDatabase, Dictionary<Type, object>>> _builderActions;

private static readonly object _lockObject = new object();
Expand All @@ -32,6 +33,7 @@ public MongoDatabaseBuilder(MongoOptions mongoOptions)
_registrationConventionActions = new List<Action>();
_registrationSerializerActions = new List<Action>();
_mongoClientSettingsActions = new List<Action<MongoClientSettings>>();
_databaseConfigurationActions = new List<Action<IMongoDatabase>>();
_builderActions = new List<Action<IMongoDatabase, Dictionary<Type, object>>>();
}

Expand Down Expand Up @@ -96,6 +98,13 @@ public IMongoDatabaseBuilder RegisterSerializer<T>(IBsonSerializer<T> serializer
return this;
}

public IMongoDatabaseBuilder ConfigureDatabase(Action<IMongoDatabase> configureDatabase)
{
_databaseConfigurationActions.Add(configureDatabase);

return this;
}

internal MongoDbContextData Build()
{
// synchronize registration
Expand Down Expand Up @@ -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<Type, object>();
_builderActions.ForEach(
Expand Down
6 changes: 4 additions & 2 deletions src/Context/Internal/MongoDbContextData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public MongoDbContextData(
public IMongoClient Client { get; }
public IMongoDatabase Database { get; }

internal IMongoCollection<TDocument> CreateCollection<TDocument>() where TDocument : class
internal IMongoCollection<TDocument> CreateCollection<TDocument>()
where TDocument : class
{
MongoCollectionBuilder<TDocument> collectionBuilder =
TryGetCollectionBuilder<TDocument>();
Expand All @@ -37,7 +38,8 @@ internal IMongoCollection<TDocument> CreateCollection<TDocument>() where TDocume
return collectionBuilder.Build();
}

private MongoCollectionBuilder<TDocument> TryGetCollectionBuilder<TDocument>() where TDocument : class
private MongoCollectionBuilder<TDocument> TryGetCollectionBuilder<TDocument>()
where TDocument : class
{
MongoCollectionBuilder<TDocument> collectionBuilder = null;

Expand Down
33 changes: 33 additions & 0 deletions src/Context/MongoServerExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<BsonDocument>(command);
}

public static bool IsTableScanDisabled(this IMongoClient mongoClient)
{
var getNoTableScanCommand =
new BsonDocument { { "getParameter", 1 }, { "notablescan", 1 } };

BsonDocument parameterResult = mongoClient.GetDatabase("admin")
.RunCommand<BsonDocument>(getNoTableScanCommand);

BsonElement notablescanElement = parameterResult.GetElement("notablescan");

if(bool.TryParse(notablescanElement.Value.ToString(), out bool isDisabled))
{
return isDisabled;
}

return false;
}
}
}

0 comments on commit 740cf56

Please sign in to comment.