Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSHARP-2043: Added SuppressEnsureIndexes option to GridFSBucketOption… #292

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/MongoDB.Driver.GridFS/GridFSBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,16 +814,19 @@ private void EnsureIndexes(IReadWriteBindingHandle binding, CancellationToken ca
{
if (!_ensureIndexesDone)
{
var isFilesCollectionEmpty = IsFilesCollectionEmpty(binding, cancellationToken);
if (isFilesCollectionEmpty)
if (!_options.SuppressEnsureIndexes)
{
if (!FilesCollectionIndexesExist(binding, cancellationToken))
var isFilesCollectionEmpty = IsFilesCollectionEmpty(binding, cancellationToken);
if (isFilesCollectionEmpty)
{
CreateFilesCollectionIndexes(binding, cancellationToken);
}
if (!ChunksCollectionIndexesExist(binding, cancellationToken))
{
CreateChunksCollectionIndexes(binding, cancellationToken);
if (!FilesCollectionIndexesExist(binding, cancellationToken))
{
CreateFilesCollectionIndexes(binding, cancellationToken);
}
if (!ChunksCollectionIndexesExist(binding, cancellationToken))
{
CreateChunksCollectionIndexes(binding, cancellationToken);
}
}
}

Expand All @@ -843,16 +846,19 @@ private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, Cancellat
{
if (!_ensureIndexesDone)
{
var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync(binding, cancellationToken).ConfigureAwait(false);
if (isFilesCollectionEmpty)
if (!_options.SuppressEnsureIndexes)
{
if (!(await FilesCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
{
await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
}
if (!(await ChunksCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync(binding, cancellationToken).ConfigureAwait(false);
if (isFilesCollectionEmpty)
{
await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
if (!(await FilesCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
{
await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
}
if (!(await ChunksCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
{
await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class GridFSBucketOptions
private ReadConcern _readConcern;
private ReadPreference _readPreference;
private WriteConcern _writeConcern;
private bool _suppressEnsureIndexes;

// constructors
/// <summary>
Expand All @@ -51,6 +52,7 @@ public GridFSBucketOptions(GridFSBucketOptions other)
_readConcern = other.ReadConcern;
_readPreference = other.ReadPreference;
_writeConcern = other.WriteConcern;
_suppressEnsureIndexes = other.SuppressEnsureIndexes;
}

/// <summary>
Expand All @@ -65,6 +67,7 @@ public GridFSBucketOptions(ImmutableGridFSBucketOptions other)
_readConcern = other.ReadConcern;
_readPreference = other.ReadPreference;
_writeConcern = other.WriteConcern;
_suppressEnsureIndexes = other.SuppressEnsureIndexes;
}

// properties
Expand Down Expand Up @@ -135,6 +138,18 @@ public WriteConcern WriteConcern
get { return _writeConcern; }
set { _writeConcern = value; }
}

/// <summary>
/// Gets or sets the suppress ensure indexes setting
/// </summary>
/// <value>
/// The suppress ensure indexes setting
/// </value>
public bool SuppressEnsureIndexes
{
get { return _suppressEnsureIndexes; }
set { _suppressEnsureIndexes = value; }
}
}

/// <summary>
Expand Down Expand Up @@ -165,6 +180,7 @@ public static ImmutableGridFSBucketOptions Defaults
private readonly ReadConcern _readConcern;
private readonly ReadPreference _readPreference;
private readonly WriteConcern _writeConcern;
private readonly bool _suppressEnsureIndexes;

// constructors
/// <summary>
Expand All @@ -174,6 +190,7 @@ public ImmutableGridFSBucketOptions()
{
_bucketName = "fs";
_chunkSizeBytes = 255 * 1024;
_suppressEnsureIndexes = false;
}

/// <summary>
Expand All @@ -188,6 +205,7 @@ public ImmutableGridFSBucketOptions(GridFSBucketOptions other)
_readConcern = other.ReadConcern;
_readPreference = other.ReadPreference;
_writeConcern = other.WriteConcern;
_suppressEnsureIndexes = other.SuppressEnsureIndexes;
}

// properties
Expand Down Expand Up @@ -256,5 +274,16 @@ public WriteConcern WriteConcern
{
get { return _writeConcern; }
}

/// <summary>
/// Gets the suppress ensure indexes setting
/// </summary>
/// <value>
/// The suppress ensure indexes setting
/// </value>
public bool SuppressEnsureIndexes
{
get { return _suppressEnsureIndexes; }
}
}
}
39 changes: 37 additions & 2 deletions tests/MongoDB.Driver.GridFS.Tests/GridFSBucketOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void constructor_with_immutable_other_should_initialize_instance()
[Fact]
public void constructor_with_mutable_other_should_initialize_instance()
{
var other = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
var other = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority, SuppressEnsureIndexes = true };

var result = new GridFSBucketOptions(other);

Expand All @@ -126,6 +126,7 @@ public void constructor_with_mutable_other_should_initialize_instance()
result.ReadConcern.Should().Be(other.ReadConcern);
result.ReadPreference.Should().Be(other.ReadPreference);
result.WriteConcern.Should().Be(other.WriteConcern);
result.SuppressEnsureIndexes.Should().Be(other.SuppressEnsureIndexes);
}

[Fact]
Expand All @@ -137,6 +138,7 @@ public void constructor_with_no_arguments_should_initialize_instance_with_defaul
result.ChunkSizeBytes.Should().Be(255 * 1024);
result.ReadPreference.Should().BeNull();
result.WriteConcern.Should().BeNull();
result.SuppressEnsureIndexes.Should().BeFalse();
}

[Fact]
Expand Down Expand Up @@ -198,6 +200,26 @@ public void WriteConcern_set_should_have_expected_result()

subject.WriteConcern.Should().Be(WriteConcern.WMajority);
}

[Fact]
public void SuppressEnsureIndexes_get_should_return_expected_result()
{
var subject = new GridFSBucketOptions { SuppressEnsureIndexes = true };

var result = subject.SuppressEnsureIndexes;

result.Should().BeTrue();
}

[Fact]
public void SuppressEnsureIndexes_set_should_have_expected_result()
{
var subject = new GridFSBucketOptions();

subject.SuppressEnsureIndexes = true;

subject.SuppressEnsureIndexes.Should().BeTrue();
}
}

public class ImmutableGridFSBucketOptionsTests
Expand Down Expand Up @@ -225,7 +247,7 @@ public void ChunkSizeBytes_get_should_return_expected_result()
[Fact]
public void constructor_with_arguments_should_initialize_instance()
{
var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority };
var mutable = new GridFSBucketOptions { BucketName = "bucket", ChunkSizeBytes = 123, ReadConcern = ReadConcern.Majority, ReadPreference = ReadPreference.Secondary, WriteConcern = WriteConcern.WMajority, SuppressEnsureIndexes = true };

var result = new ImmutableGridFSBucketOptions(mutable);

Expand All @@ -234,6 +256,7 @@ public void constructor_with_arguments_should_initialize_instance()
result.ReadConcern.Should().Be(ReadConcern.Majority);
result.ReadPreference.Should().Be(ReadPreference.Secondary);
result.WriteConcern.Should().Be(WriteConcern.WMajority);
result.SuppressEnsureIndexes.Should().BeTrue();
}

[Fact]
Expand All @@ -246,6 +269,7 @@ public void constructor_with_no_arguments_should_initialize_instance_with_defaul
result.ReadConcern.Should().BeNull();
result.ReadPreference.Should().BeNull();
result.WriteConcern.Should().BeNull();
result.SuppressEnsureIndexes.Should().BeFalse();
}

[Fact]
Expand All @@ -267,6 +291,7 @@ public void Defaults_get_should_return_expected_result()
result.ReadConcern.Should().BeNull();
result.ReadPreference.Should().BeNull();
result.WriteConcern.Should().BeNull();
result.SuppressEnsureIndexes.Should().BeFalse();
}

[Fact]
Expand Down Expand Up @@ -298,5 +323,15 @@ public void WriteConcern_get_should_return_expected_result()

result.Should().Be(WriteConcern.WMajority);
}

[Fact]
public void SuppressEnsureIndexes_get_should_return_expected_result()
{
var subject = new ImmutableGridFSBucketOptions(new GridFSBucketOptions { SuppressEnsureIndexes = true });

var result = subject.SuppressEnsureIndexes;

result.Should().BeTrue();
}
}
}