added cancellation tokens to sync deletes and added more unit tests

This commit is contained in:
Sean Garrett
2023-06-17 17:49:49 +01:00
parent b285c7d919
commit 1b8f795e00
13 changed files with 861 additions and 192 deletions
@@ -21,7 +21,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <param name="document">The document you want to delete.</param>
/// <param name="cancellationToken">An optional cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -34,7 +34,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null, CancellationToken cancellationToken = default)
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -94,8 +94,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -106,8 +107,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The Cancellation Token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
}
@@ -24,7 +24,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
#region Delete TKey
/// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -33,7 +33,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
}
/// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null, CancellationToken cancellationToken = default)
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -41,7 +41,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
}
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -68,7 +68,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter)).DeletedCount;
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter, cancellationToken)).DeletedCount;
}
/// <inheritdoc />
@@ -103,14 +103,8 @@ namespace MongoDbGenericRepository.DataAccess.Delete
.DeletedCount;
}
/// <summary>
/// Deletes a list of documents.
/// </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="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
/// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -128,29 +122,22 @@ namespace MongoDbGenericRepository.DataAccess.Delete
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id)).DeletedCount;
deleteCount += HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id), cancellationToken).DeletedCount;
}
return deleteCount;
}
var idsToDelete = documentList.Select(e => e.Id).ToArray();
return HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id)).DeletedCount;
return HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id), cancellationToken).DeletedCount;
}
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
/// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteMany(filter).DeletedCount;
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteMany(filter, cancellationToken).DeletedCount;
}
#endregion