Skip to content

Commit 0317906

Browse files
committed
changed return type of IMongoIndexManager.CreateOneAsync per the index management specification.
1 parent 577d6da commit 0317906

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

src/MongoDB.Driver.Core/Core/Operations/CreateIndexRequest.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,24 @@ public BsonDocument Weights
272272
set { _weights = value; }
273273
}
274274

275+
// publuc methods
276+
/// <summary>
277+
/// Gets the name of the index.
278+
/// </summary>
279+
/// <returns>The name of the index.</returns>
280+
public string GetIndexName()
281+
{
282+
var additionalOptionsName = _additionalOptions == null ? null : (string)_additionalOptions.GetValue("name", null);
283+
return _name ?? additionalOptionsName ?? IndexNameHelper.GetIndexName(_keys);
284+
}
285+
275286
// methods
276287
internal BsonDocument CreateIndexDocument()
277288
{
278-
var additionalOptionsName = _additionalOptions == null ? null : _additionalOptions.GetValue("name", null);
279-
var name = _name ?? additionalOptionsName ?? IndexNameHelper.GetIndexName(_keys);
280289
var document = new BsonDocument
281290
{
282291
{ "key", _keys },
283-
{ "name", name },
292+
{ "name", GetIndexName() },
284293
{ "background", () => _background.Value, _background.HasValue },
285294
{ "bits", () => _bits.Value, _bits.HasValue },
286295
{ "bucketSize", () => _bucketSize.Value, _bucketSize.HasValue },

src/MongoDB.Driver.Tests/MongoCollectionImplTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public async Task CountAsync_should_execute_the_CountOperation()
320320
operation.MaxTime.Should().Be(options.MaxTime);
321321
operation.Skip.Should().Be(options.Skip);
322322
}
323-
323+
324324
[Test]
325325
public async Task DeleteManyAsync_should_execute_the_BulkMixedOperation()
326326
{
@@ -686,6 +686,7 @@ public async Task Indexes_CreateAsync_should_execute_the_CreateIndexesOperation(
686686
request.Unique.Should().Be(options.Unique);
687687
request.Version.Should().Be(options.Version);
688688
request.Weights.Should().Be(weights);
689+
request.GetIndexName().Should().Be(options.Name);
689690
}
690691

691692
[Test]

src/MongoDB.Driver/IMongoIndexManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public interface IMongoIndexManager<TDocument>
5151
/// <param name="keys">The keys.</param>
5252
/// <param name="options">The options.</param>
5353
/// <param name="cancellationToken">The cancellation token.</param>
54-
/// <returns>A task.</returns>
55-
Task CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
54+
/// <returns>
55+
/// A task whos result is the name of the index that was created.
56+
/// </returns>
57+
Task<string> CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
5658

5759
/// <summary>
5860
/// Drops all the indexes.

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public override MongoCollectionSettings Settings
541541
get { return _collection._settings; }
542542
}
543543

544-
public override Task CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options, CancellationToken cancellationToken)
544+
public override async Task<string> CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options, CancellationToken cancellationToken)
545545
{
546546
Ensure.IsNotNull(keys, "keys");
547547

@@ -569,7 +569,8 @@ public override Task CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateI
569569
};
570570

571571
var operation = new CreateIndexesOperation(_collection._collectionNamespace, new[] { request }, _collection._messageEncoderSettings);
572-
return _collection.ExecuteWriteOperation(operation, cancellationToken);
572+
await _collection.ExecuteWriteOperation(operation, cancellationToken);
573+
return request.GetIndexName();
573574
}
574575

575576
public override Task DropAllAsync(CancellationToken cancellationToken)

src/MongoDB.Driver/MongoIndexManagerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public abstract class MongoIndexManagerBase<TDocument> : IMongoIndexManager<TDoc
3535
public abstract MongoCollectionSettings Settings { get; }
3636

3737
/// <inheritdoc />
38-
public abstract Task CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
38+
public abstract Task<string> CreateOneAsync(IndexKeysDefinition<TDocument> keys, CreateIndexOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
3939

4040
/// <inheritdoc />
4141
public abstract Task DropAllAsync(CancellationToken cancellationToken = default(CancellationToken));

0 commit comments

Comments
 (0)