Index tests

This commit is contained in:
Sean Garrett
2023-06-20 22:06:30 +01:00
parent ed2eddd33e
commit 25766febd4
8 changed files with 553 additions and 146 deletions
@@ -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
{
@@ -13,64 +14,67 @@ namespace MongoDbGenericRepository.DataAccess.Index
public class MongoDbIndexHandler : DataAccessBase, IMongoDbIndexHandler
{
/// <summary>
/// The MongoDbIndexHandler constructor.
/// The MongoDbIndexHandler constructor.
/// </summary>
/// <param name="mongoDbContext">The mongo db context</param>
public MongoDbIndexHandler(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
/// <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>
{
var model = new CreateIndexModel<TDocument>(
Builders<TDocument>.IndexKeys.Text(field),
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
);
return await HandlePartitioned<TDocument, TKey>(partitionKey).Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(
Builders<TDocument>.IndexKeys.Text(field),
indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions)
));
.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>
{
@@ -78,22 +82,17 @@ namespace MongoDbGenericRepository.DataAccess.Index
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Ascending(field), createOptions));
.CreateOneAsync(
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>
{
@@ -101,45 +100,17 @@ namespace MongoDbGenericRepository.DataAccess.Index
var createOptions = indexCreationOptions == null ? null : MapIndexOptions(indexCreationOptions);
var indexKey = Builders<TDocument>.IndexKeys;
return await collection.Indexes
.CreateOneAsync(
new CreateIndexModel<TDocument>(indexKey.Descending(field), createOptions));
.CreateOneAsync(
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);
}
}
}
}