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.Delete
{
///
public class MongoDbEraser : DataAccessBase, IMongoDbEraser
{
///
/// The MongoDbEraser constructor.
///
/// the MongoDb Context
public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
#region Delete TKey
///
public virtual long DeleteOne(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
var filter = Builders.Filter.Eq("Id", document.Id);
return HandlePartitioned(document).DeleteOne(filter, cancellationToken).DeletedCount;
}
///
public virtual long DeleteOne(Expression> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
return HandlePartitioned(partitionKey).DeleteOne(filter, cancellationToken).DeletedCount;
}
///
public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
var filter = Builders.Filter.Eq("Id", document.Id);
return (await HandlePartitioned(document).DeleteOneAsync(filter, cancellationToken)).DeletedCount;
}
///
public virtual async Task DeleteOneAsync(
Expression> filter,
string partitionKey,
CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter, cancellationToken)).DeletedCount;
}
///
public virtual async Task DeleteManyAsync(
Expression> filter,
string partitionKey,
CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter, cancellationToken)).DeletedCount;
}
///
public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
var documentList = documents.ToList();
if (!documentList.Any())
{
return 0;
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documentList.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += (await HandlePartitioned(group.FirstOrDefault())
.DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken))
.DeletedCount;
}
return deleteCount;
}
var idsToDelete = documentList.Select(e => e.Id).ToArray();
return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken))
.DeletedCount;
}
///
public virtual long DeleteMany(IEnumerable documents, CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
var documentList = documents.ToList();
if (!documentList.Any())
{
return 0;
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documentList.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += HandlePartitioned(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id), cancellationToken).DeletedCount;
}
return deleteCount;
}
var idsToDelete = documentList.Select(e => e.Id).ToArray();
return HandlePartitioned(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id), cancellationToken).DeletedCount;
}
///
public virtual long DeleteMany(Expression> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument
where TKey : IEquatable
{
return HandlePartitioned(partitionKey).DeleteMany(filter, cancellationToken).DeletedCount;
}
#endregion
}
}