Index tests
This commit is contained in:
@@ -12,20 +12,20 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateTextIndexTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> _fieldExpression = t => t.SomeContent2;
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Creates_Index()
|
||||
public async Task WhenFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression);
|
||||
await Sut.CreateTextIndexAsync(fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(_fieldExpression, null, null));
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -36,12 +36,12 @@ public class CreateTextIndexTests : BaseIndexTests
|
||||
var options = new IndexCreationOptions { Name = "theIndexName" };
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, options);
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
_fieldExpression, options, null));
|
||||
fieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -53,16 +53,15 @@ public class CreateTextIndexTests : BaseIndexTests
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, partitionKey: partitionKey);
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
_fieldExpression, null, partitionKey));
|
||||
fieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
/*
|
||||
[Fact]
|
||||
/*[Fact]
|
||||
public async Task Ensure_Creates_Index_With_CancellationToken()
|
||||
{
|
||||
// Arrange
|
||||
@@ -70,13 +69,14 @@ public class CreateTextIndexTests : BaseIndexTests
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, token);
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(_fieldExpression, null, null, token));
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Options_With_CancellationToken()
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ public class GetIndexNamesTests : BaseIndexTests
|
||||
const string indexName = "theIndexName";
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null))
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
@@ -28,7 +28,7 @@ public class GetIndexNamesTests : BaseIndexTests
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null), Times.Once());
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null, CancellationToken.None), Times.Once());
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -61,7 +61,7 @@ public class GetIndexNamesTests : BaseIndexTests
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey))
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class BaseIndexTests : GenericTestContext<MongoDbIndexHandler>
|
||||
{
|
||||
protected (Mock<IAsyncCursor<BsonDocument>>, Mock<IMongoIndexManager<TDocument>>) SetupIndexes<TDocument>(
|
||||
List<BsonDocument> indexes,
|
||||
Mock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var asyncCursor = MockOf<IAsyncCursor<BsonDocument>>();
|
||||
var moveNextSequence = asyncCursor
|
||||
.SetupSequence(x => x.MoveNextAsync(It.IsAny<CancellationToken>()));
|
||||
|
||||
var currentSequence = asyncCursor
|
||||
.SetupSequence(x => x.Current);
|
||||
|
||||
foreach (var bsonDocument in indexes)
|
||||
{
|
||||
moveNextSequence.ReturnsAsync(true);
|
||||
currentSequence.Returns(new[] {bsonDocument});
|
||||
}
|
||||
|
||||
moveNextSequence.ReturnsAsync(false);
|
||||
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(x => x.ListAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(asyncCursor.Object);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return (asyncCursor, indexManager);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class CreateTextIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WhenFieldExpression_ThenCreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}")),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
SetupContext(collection);
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") ),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") ),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(Mock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class GetIndexNamesAsyncTests : BaseIndexTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithNoParameters_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null));
|
||||
manager.Verify(x => x.ListAsync(CancellationToken.None));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionKey_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey));
|
||||
manager.Verify(x => x.ListAsync(CancellationToken.None));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCancellationToken_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken();
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>(cancellationToken:token);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null));
|
||||
manager.Verify(x => x.ListAsync(token));
|
||||
cursor.Verify(x => x.MoveNextAsync(token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionKeyCancellationToken_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken();
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey));
|
||||
manager.Verify(x => x.ListAsync(token));
|
||||
cursor.Verify(x => x.MoveNextAsync(token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public static class IndexExtensions
|
||||
{
|
||||
public static bool EqualToJson<TDocument>(this IndexKeysDefinition<TDocument> keys, string json)
|
||||
{
|
||||
var indexModelRendered = RenderIndexModelKeys(keys);
|
||||
return indexModelRendered.Equals(json, System.StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) =>
|
||||
x.Unique == y.Unique &&
|
||||
x.TextIndexVersion == y.TextIndexVersion &&
|
||||
x.SphereIndexVersion == y.SphereIndexVersion &&
|
||||
x.Sparse == y.Sparse &&
|
||||
x.Name == y.Name &&
|
||||
x.Min == y.Min &&
|
||||
x.Max == y.Max &&
|
||||
x.LanguageOverride == y.LanguageOverride &&
|
||||
x.ExpireAfter == y.ExpireAfter &&
|
||||
x.DefaultLanguage == y.DefaultLanguage &&
|
||||
x.Bits == y.Bits &&
|
||||
x.Background == y.Background &&
|
||||
x.Version == y.Version;
|
||||
|
||||
public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) =>
|
||||
x.Unique == y.Unique &&
|
||||
x.TextIndexVersion == y.TextIndexVersion &&
|
||||
x.SphereIndexVersion == y.SphereIndexVersion &&
|
||||
x.Sparse == y.Sparse &&
|
||||
x.Name == y.Name &&
|
||||
x.Min == y.Min &&
|
||||
x.Max == y.Max &&
|
||||
x.LanguageOverride == y.LanguageOverride &&
|
||||
x.ExpireAfter == y.ExpireAfter &&
|
||||
x.DefaultLanguage == y.DefaultLanguage &&
|
||||
x.Bits == y.Bits &&
|
||||
x.Background == y.Background &&
|
||||
x.Version == y.Version;
|
||||
|
||||
private static string RenderIndexModelKeys<TDocument>(IndexKeysDefinition<TDocument> keys)
|
||||
{
|
||||
var indexModelRendered = keys.Render(
|
||||
BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(),
|
||||
BsonSerializer.SerializerRegistry);
|
||||
|
||||
var result = indexModelRendered.ToString();
|
||||
return result.Replace(" ", "");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
@@ -18,8 +19,9 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">An optional Cancellation Token.</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)
|
||||
Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -33,8 +35,13 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">An optional cancellation token</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)
|
||||
Task<string> CreateTextIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -48,8 +55,13 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">An optional cancellation token.</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)
|
||||
Task<string> CreateAscendingIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -63,8 +75,13 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">An optional cancellation token.</param>
|
||||
/// <returns>The result of the create index operation.</returns>
|
||||
Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
Task<string> CreateDescendingIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -78,8 +95,13 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">An optional cancellation token.</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)
|
||||
Task<string> CreateHashedIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -93,8 +115,13 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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>
|
||||
/// <param name="cancellationToken">An optional Cancellation token.</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)
|
||||
Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(
|
||||
IEnumerable<Expression<Func<TDocument, object>>> fields,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -105,7 +132,8 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
/// <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)
|
||||
/// <param name="cancellationToken">An optional cancellation token,</param>
|
||||
Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Index
|
||||
{
|
||||
@@ -20,57 +21,60 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
{
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual async Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null)
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<List<string>> GetIndexesNamesAsync<TDocument, TKey>(string partitionKey = null, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync();
|
||||
var indexes = await indexCursor.ToListAsync();
|
||||
var indexCursor = await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.ListAsync(cancellationToken);
|
||||
var indexes = await indexCursor.ToListAsync(cancellationToken);
|
||||
return indexes.Select(e => e["name"].ToString()).ToList();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual async Task<string> CreateTextIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<string> CreateTextIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(
|
||||
var model = new CreateIndexModel<TDocument>(
|
||||
Builders<TDocument>.IndexKeys.Text(field),
|
||||
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
|
||||
));
|
||||
);
|
||||
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
|
||||
.CreateOneAsync(
|
||||
model,
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in ascending order.
|
||||
/// 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>
|
||||
public virtual async Task<string> CreateAscendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<string> CreateAscendingIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
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),
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<string> CreateDescendingIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
@@ -79,21 +83,16 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
|
||||
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions),
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an index on the given field in descending order.
|
||||
/// 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>
|
||||
public virtual async Task<string> CreateDescendingIndexAsync<TDocument, TKey>(Expression<Func<TDocument, object>> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<string> CreateHashedIndexAsync<TDocument, TKey>(
|
||||
Expression<Func<TDocument, object>> field,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
@@ -102,44 +101,16 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
var indexKey = Builders<TDocument>.IndexKeys;
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
|
||||
new CreateIndexModel<TDocument>(indexKey.Hashed(field), createOptions),
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a hashed 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>
|
||||
public virtual 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.
|
||||
/// 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="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 virtual async Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(IEnumerable<Expression<Func<TDocument, object>>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null)
|
||||
/// <inheritdoc />
|
||||
public virtual async Task<string> CreateCombinedTextIndexAsync<TDocument, TKey>(
|
||||
IEnumerable<Expression<Func<TDocument, object>>> fields,
|
||||
IndexCreationOptions indexCreationOptions = null,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
@@ -150,22 +121,19 @@ namespace MongoDbGenericRepository.DataAccess.Index
|
||||
{
|
||||
listOfDefs.Add(Builders<TDocument>.IndexKeys.Text(field));
|
||||
}
|
||||
|
||||
return await collection.Indexes
|
||||
.CreateOneAsync(new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions));
|
||||
.CreateOneAsync(
|
||||
new CreateIndexModel<TDocument>(Builders<TDocument>.IndexKeys.Combine(listOfDefs), createOptions),
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public virtual async Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null)
|
||||
/// <inheritdoc />
|
||||
public virtual async Task DropIndexAsync<TDocument, TKey>(string indexName, string partitionKey = null, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName);
|
||||
await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes.DropOneAsync(indexName, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user