Implementation of index management.CreateTextIndexWithOptionAsync is failing.

This commit is contained in:
alexandre-spieser
2018-09-30 19:07:38 +01:00
parent ad18170804
commit 0b939592ba
6 changed files with 422 additions and 267 deletions
@@ -9,13 +9,16 @@
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" /> <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="MongoDB.Driver" Version="2.7.0" /> <PackageReference Include="MongoDB.Driver" Version="2.7.0" />
<PackageReference Include="MongoDbGenericRepository" Version="1.3.7" />
<PackageReference Include="xunit" Version="2.4.0" /> <PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.console" Version="2.4.0" /> <PackageReference Include="xunit.runner.console" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" /> <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System.Configuration"> <Reference Include="System.Configuration">
<HintPath>..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll</HintPath> <HintPath>..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll</HintPath>
@@ -11,7 +11,7 @@ namespace CoreIntegrationTests.Infrastructure
{ {
public abstract class MongoDbDocumentTestBase<T> : public abstract class MongoDbDocumentTestBase<T> :
IClassFixture<MongoDbTestFixture<T, Guid>> IClassFixture<MongoDbTestFixture<T, Guid>>
where T: TestDoc, new() where T : TestDoc, new()
{ {
private readonly MongoDbTestFixture<T, Guid> _fixture; private readonly MongoDbTestFixture<T, Guid> _fixture;
@@ -73,7 +73,7 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
SUT.AddOne<T>(document); SUT.AddOne<T>(document);
// Assert // Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id)) long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id))
: SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey); : SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey);
Assert.True(1 == count, GetTestName()); Assert.True(1 == count, GetTestName());
} }
@@ -88,7 +88,7 @@ namespace CoreIntegrationTests.Infrastructure
// Assert // Assert
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id)) long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id))
: SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey); : SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey);
Assert.True (1 == count, GetTestName()); Assert.True(1 == count, GetTestName());
} }
[Fact] [Fact]
@@ -103,7 +103,7 @@ namespace CoreIntegrationTests.Infrastructure
|| e.Id.Equals(documents[1].Id)) || e.Id.Equals(documents[1].Id))
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id) : SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey); || e.Id.Equals(documents[1].Id), PartitionKey);
Assert.True (2 == count, GetTestName()); Assert.True(2 == count, GetTestName());
} }
[Fact] [Fact]
@@ -114,7 +114,7 @@ namespace CoreIntegrationTests.Infrastructure
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T(), new T(), new T() }; var documents = new List<T> { new T(), new T(), new T(), new T() };
if(documents.Any(e => e is IPartitionedDocument)) if (documents.Any(e => e is IPartitionedDocument))
{ {
var secondPartitionKey = $"{PartitionKey}-2"; var secondPartitionKey = $"{PartitionKey}-2";
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey; ((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
@@ -144,7 +144,7 @@ namespace CoreIntegrationTests.Infrastructure
|| e.Id.Equals(documents[1].Id)) || e.Id.Equals(documents[1].Id))
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id) : SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|| e.Id.Equals(documents[1].Id), PartitionKey); || e.Id.Equals(documents[1].Id), PartitionKey);
Assert.True (2 == count, GetTestName()); Assert.True(2 == count, GetTestName());
} }
[Fact] [Fact]
@@ -235,7 +235,7 @@ namespace CoreIntegrationTests.Infrastructure
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey); var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
var count = cursor.Count(); var count = cursor.Count();
// Assert // Assert
Assert.True (1 == count, GetTestName()); Assert.True(1 == count, GetTestName());
} }
[Fact] [Fact]
@@ -490,10 +490,10 @@ namespace CoreIntegrationTests.Infrastructure
Assert.True(result); Assert.True(result);
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey); var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
Assert.True(null != updatedDocument); Assert.True(null != updatedDocument);
Assert.True(childrenToAdd[0].Type== updatedDocument.Children[0].Type, GetTestName()); Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
Assert.True(childrenToAdd[0].Value== updatedDocument.Children[0].Value, GetTestName()); Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
Assert.True(childrenToAdd[1].Type== updatedDocument.Children[1].Type, GetTestName()); Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
Assert.True(childrenToAdd[1].Value== updatedDocument.Children[1].Value, GetTestName()); Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
} }
#endregion Update #endregion Update
@@ -509,7 +509,7 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
var result = SUT.DeleteOne<T>(document); var result = SUT.DeleteOne<T>(document);
// Assert // Assert
Assert.True (1 == result); Assert.True(1 == result);
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
} }
@@ -522,7 +522,7 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
var result = SUT.DeleteOne<T>(e => e.Id.Equals(document.Id), PartitionKey); var result = SUT.DeleteOne<T>(e => e.Id.Equals(document.Id), PartitionKey);
// Assert // Assert
Assert.True (1 == result); Assert.True(1 == result);
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
} }
@@ -535,7 +535,7 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
var result = await SUT.DeleteOneAsync<T>(document); var result = await SUT.DeleteOneAsync<T>(document);
// Assert // Assert
Assert.True (1 == result); Assert.True(1 == result);
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
} }
@@ -548,7 +548,7 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
var result = await SUT.DeleteOneAsync<T>(e => e.Id.Equals(document.Id), PartitionKey); var result = await SUT.DeleteOneAsync<T>(e => e.Id.Equals(document.Id), PartitionKey);
// Assert // Assert
Assert.True (1 == result); Assert.True(1 == result);
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
} }
@@ -765,7 +765,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -787,7 +788,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -809,7 +811,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -831,7 +834,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -853,7 +857,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -875,7 +880,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -897,7 +903,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -919,7 +926,8 @@ namespace CoreIntegrationTests.Infrastructure
var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}"; var criteria = $"{GetTestName()}.{DocumentTypeName}.{Guid.NewGuid()}";
var documents = CreateTestDocuments(5); var documents = CreateTestDocuments(5);
var i = 1; var i = 1;
documents.ForEach(e => { documents.ForEach(e =>
{
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++); e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
e.SomeContent = criteria; e.SomeContent = criteria;
}); });
@@ -936,6 +944,47 @@ namespace CoreIntegrationTests.Infrastructure
#endregion Max / Min Queries #endregion Max / Min Queries
#region Index Management
[Fact]
public async Task CreateTextIndexNoOptionAsync()
{
// Arrange
const string expectedIndexName = "SomeContent_text";
// Act
var result = await SUT.CreateTextIndexAsync<T>(x => x.SomeContent, null, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync<T>(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync<T>(expectedIndexName, PartitionKey);
}
[Fact]
public async Task CreateTextIndexWithOptionAsync()
{
// Arrange
string expectedIndexName = $"SomeContent_text_{Guid.NewGuid()}";
var option = new IndexCreationOptions
{
Name = expectedIndexName
};
// Act
var result = await SUT.CreateTextIndexAsync<T>(x => x.SomeContent, option, PartitionKey);
// Assert
var listOfIndexNames = await SUT.GetIndexesNamesAsync<T>(PartitionKey);
Assert.Contains(expectedIndexName, listOfIndexNames);
// Cleanup
await SUT.DropIndexAsync<T>(expectedIndexName, PartitionKey);
}
#endregion Index Management
#region Test Utils #region Test Utils
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
@@ -12,7 +12,7 @@ namespace MongoDbGenericRepository
/// <summary> /// <summary>
/// The IBaseMongoRepository exposes the CRUD functionality of the BaseMongoRepository. /// The IBaseMongoRepository exposes the CRUD functionality of the BaseMongoRepository.
/// </summary> /// </summary>
public interface IBaseMongoRepository : IReadOnlyMongoRepository public interface IBaseMongoRepository : IReadOnlyMongoRepository, IMongoDbCollectionIndexRepository
{ {
#region Create #region Create
@@ -0,0 +1,332 @@
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq.Expressions;
using MongoDbGenericRepository.Models;
using System.Linq;
namespace MongoDbGenericRepository
{
/// <summary>
/// The repository interface for managing indexes
/// </summary>
public interface IMongoDbCollectionIndexRepository
{
/// <summary>
/// Create a text index on the given field.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Create a text index on the given field.
/// IndexCreationOptions can be supplied to further specify
/// how the creation should be done.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Creates an index on the given field in ascending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument;
/// <summary>
/// Creates an index on the given field in ascending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Creates an index on the given field in descending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>A string containing the result of the operation.</returns>
Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Creates a hashed index on the given field.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>A string containing the result of the operation.</returns>
Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Creates a hashed index on the given field.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Creates a combined text index
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="fields">The fields we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Creates a combined text index
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="fields">The fields we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Drops the index given a field name
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="indexName">The name of the index</param>
/// <param name="partitionKey">An optional partition key</param>
Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Drops the index given a field name
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="indexName">The name of the index</param>
/// <param name="partitionKey">An optional partition key</param>
Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Returns the names of the indexes present on a collection.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
where TDocument : IDocument;
/// <summary>
/// Returns the names of the indexes present on a collection.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>A list containing the names of the indexes on on the concerned collection.</returns>
Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
}
/// <summary>
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
/// Its constructor must be given a connection string and a database name.
/// </summary>
public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository
{
#region Index Management
/// <inheritdoc />
public async Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await CreateTextIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <inheritdoc />
public async Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(
Builders<TDocument>.IndexKeys.Text(field),
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
));
}
/// <inheritdoc />
public async Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
{
return await CreateAscendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <inheritdoc />
public async Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
}
/// <inheritdoc />
public async Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await CreateDescendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <inheritdoc />
public async Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
}
/// <inheritdoc />
public async Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await CreateHashedIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <inheritdoc />
public async Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions));
}
/// <inheritdoc />
public async Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
{
return await CreateCombinedTextIndexAsync<TDocument, Guid>(fields, indexCreationOptions, partitionKey);
}
/// <inheritdoc />
public async Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var listOfDefs = new List<IndexKeysDefinition<TDocument>>();
foreach (var field in fields)
{
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
}
return await collection.Indexes
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
}
/// <inheritdoc />
public async Task DropIndexAsync<TDocument>(string indexName, string partitionKey = null)
where TDocument : IDocument
{
await DropIndexAsync<TDocument, Guid>(indexName, partitionKey);
}
/// <inheritdoc />
public async Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName);
}
/// <inheritdoc />
public async Task<List<string>> GetIndexesNamesAsync<TDocument>(string partitionKey = null)
where TDocument : IDocument
{
return await GetIndexesNamesAsync<TDocument, Guid>(partitionKey);
}
/// <inheritdoc />
public async Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync();
var indexes = await indexCursor.ToListAsync();
return indexes.Select(e => e["name"].ToString()).ToList();
}
#endregion Index Management
private CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
{
return new CreateIndexOptions
{
Unique = indexCreationOptions.Unique,
TextIndexVersion = indexCreationOptions.TextIndexVersion,
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
Sparse = indexCreationOptions.Sparse,
Name = indexCreationOptions.Name,
Min = indexCreationOptions.Min,
Max = indexCreationOptions.Max,
LanguageOverride = indexCreationOptions.LanguageOverride,
ExpireAfter = indexCreationOptions.ExpireAfter,
DefaultLanguage = indexCreationOptions.DefaultLanguage,
BucketSize = indexCreationOptions.BucketSize,
Bits = indexCreationOptions.Bits,
Background = indexCreationOptions.Background,
Version = indexCreationOptions.Version
};
}
}
}
@@ -13,9 +13,8 @@ namespace MongoDbGenericRepository
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
/// Its constructor must be given a connection string and a database name. /// Its constructor must be given a connection string and a database name.
/// </summary> /// </summary>
public abstract class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository
{ {
/// <summary> /// <summary>
/// The constructor taking a connection string and a database name. /// The constructor taking a connection string and a database name.
/// </summary> /// </summary>
@@ -1081,234 +1080,6 @@ namespace MongoDbGenericRepository
#endregion Find And Update #endregion Find And Update
#region Index Management
/// <summary>
/// Create an Index given a field and an optional ascending / descending parameter
/// we want to create them in the background as we want the db to still be available during this process
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateTextIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument
{
return await CreateTextIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <summary>
/// Create an Index given a field and an optional ascending / descending parameter
/// we want to create them in the background as we want the db to still be available during this process
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(
Builders<TDocument>.IndexKeys.Text(field),
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
));
}
/// <summary>
/// Creates an index on the given field in ascending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateAscendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
{
return await CreateAscendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <summary>
/// Creates an index on the given field in ascending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
}
/// <summary>
/// Creates an index on the given field in ascending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns></returns>
public async Task<string> CreateDescendingIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
{
return await CreateDescendingIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <summary>
/// Creates an index on the given field in descending order
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
}
/// <summary>
/// Create an Index given a field and an optional ascending / descending parameter
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns></returns>
public async Task<string> CreateHashedIndexAsync<TDocument>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
{
return await CreateHashedIndexAsync<TDocument, Guid>(field, indexCreationOptions, partitionKey);
}
/// <summary>
/// Creates a hashed index on the given field.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="field">The field we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateHashedIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions));
}
/// <summary>
/// Creates a combined text index
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="fields">The fields we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateCombinedTextIndexAsync<TDocument>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument
{
return await CreateCombinedTextIndexAsync<TDocument, Guid>(fields, indexCreationOptions, partitionKey);
}
/// <summary>
/// Creates a combined text index
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="fields">The fields we want to index</param>
/// <param name="indexCreationOptions">Options for creating an index..</param>
/// <param name="partitionKey">An optional partition key</param>
/// <returns>The result of the create index operation.</returns>
public async Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = HandlePartitioned<TDocument, TKey>(partitionKey);
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var listOfDefs = new List<IndexKeysDefinition<TDocument>>();
foreach (var field in fields)
{
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
}
return await collection.Indexes
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
}
/// <summary>
/// Drops the index given a field name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fieldName"></param>
/// <returns></returns>
public async Task DropIndexAsync<T>(string fieldName) where T : BaseMongoEntity
{
var collection = GetCollection<T>();
await collection.Indexes.DropOneAsync(fieldName);
}
/// <summary>
/// Drops the index given a field name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public async Task<List<string>> GetIndexesNamesAsync<TDocument>() where T : BaseMongoEntity
{
var collection = GetCollection<T>();
var indexCursor = await collection.Indexes.ListAsync();
var indexes = await indexCursor.ToListAsync();
var values = indexes.Select(e => e["name"].ToString()).ToList();
return values;
}
#endregion Index Management
private CreateIndexOptions MapIndexOptions(IndexCreationOptions indexCreationOptions)
{
return new CreateIndexOptions
{
Unique = indexCreationOptions.Unique,
TextIndexVersion = indexCreationOptions.TextIndexVersion,
SphereIndexVersion = indexCreationOptions.SphereIndexVersion,
Sparse = indexCreationOptions.Sparse,
Name = indexCreationOptions.Name,
Min = indexCreationOptions.Min,
Max = indexCreationOptions.Max,
LanguageOverride = indexCreationOptions.LanguageOverride,
ExpireAfter = indexCreationOptions.ExpireAfter,
DefaultLanguage = indexCreationOptions.DefaultLanguage,
BucketSize = indexCreationOptions.BucketSize,
Bits = indexCreationOptions.Bits,
Background = indexCreationOptions.Background,
Version = indexCreationOptions.Version
};
}
/// <summary> /// <summary>
/// Sets the value of the document Id if it is not set already. /// Sets the value of the document Id if it is not set already.
/// </summary> /// </summary>
+12 -12
View File
@@ -76,23 +76,23 @@ namespace MongoDbGenericRepository
.FirstOrDefault() as CollectionNameAttribute)?.Name; .FirstOrDefault() as CollectionNameAttribute)?.Name;
} }
/// <summary> /// <summary>
/// Returns a collection for a document type. Also handles document types with a partition key. /// Returns a collection for a document type. Also handles document types with a partition key.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The optional value of the partition key.</param> /// <param name="partitionKey">The optional value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null)
{ {
return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey)); return Database.GetCollection<TDocument>(GetCollectionName<TDocument>(partitionKey));
} }
/// <summary> /// <summary>
/// Drops a collection, use very carefully. /// Drops a collection, use very carefully.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
public void DropCollection<TDocument>(string partitionKey = null) public void DropCollection<TDocument>(string partitionKey = null)
{ {
Database.DropCollection(GetCollectionName<TDocument>(partitionKey)); Database.DropCollection(GetCollectionName<TDocument>(partitionKey));
} }
/// <summary> /// <summary>