Skip to content

Commit

Permalink
Implementation of Bulk Operations to MonboDbRepository
Browse files Browse the repository at this point in the history
- InsertMany
- UpdateMany
- DeleteMany
- Related with #6654
  • Loading branch information
enisn committed Dec 16, 2020
1 parent 0a96808 commit 7d39d8a
Showing 1 changed file with 145 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ await Collection.InsertOneAsync(
return entity;
}

public override async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
foreach (var entity in entities)
{
await ApplyAbpConceptsForAddedEntityAsync(entity);
}

if (SessionHandle != null)
{
await Collection.InsertManyAsync(
SessionHandle,
entities,
cancellationToken: cancellationToken);
}
else
{
await Collection.InsertManyAsync(
entities,
cancellationToken: cancellationToken);
}
}

public async override Task<TEntity> UpdateAsync(
TEntity entity,
bool autoSave = false,
Expand Down Expand Up @@ -132,6 +154,55 @@ public async override Task<TEntity> UpdateAsync(
return entity;
}

public override async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
var isSoftDeleteEntity = typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity));

foreach (var entity in entities)
{
SetModificationAuditProperties(entity);

if (isSoftDeleteEntity)
{
SetDeletionAuditProperties(entity);
await TriggerEntityDeleteEventsAsync(entity);
}
else
{
await TriggerEntityUpdateEventsAsync(entity);
}

await TriggerDomainEventsAsync(entity);

SetNewConcurrencyStamp(entity);
}

var entitiesCount = entities.Count();
BulkWriteResult result;

if (SessionHandle != null)
{
result = await Collection.BulkWriteAsync(SessionHandle, GetReplaceRequests());
}
else
{
result = await Collection.BulkWriteAsync(GetReplaceRequests());
}

if (result.MatchedCount < entitiesCount)
{
ThrowOptimisticConcurrencyException();
}

IEnumerable<WriteModel<TEntity>> GetReplaceRequests()
{
foreach (var entity in entities)
{
yield return new ReplaceOneModel<TEntity>(CreateEntityFilter(entity), entity);
}
}
}

public async override Task DeleteAsync(
TEntity entity,
bool autoSave = false,
Expand Down Expand Up @@ -195,6 +266,67 @@ public async override Task DeleteAsync(
}
}

public override async Task DeleteManyAsync(
IEnumerable<TEntity> entities,
bool autoSave = false,
CancellationToken cancellationToken = default)
{
foreach (var entity in entities)
{
await ApplyAbpConceptsForDeletedEntityAsync(entity);
var oldConcurrencyStamp = SetNewConcurrencyStamp(entity);
}

var entitiesCount = entities.Count();

if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{
UpdateResult updateResult;
if (SessionHandle != null)
{
updateResult = await Collection.UpdateManyAsync(
SessionHandle,
CreateEntitiesFilter(entities),
Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true)
);
}
else
{
updateResult = await Collection.UpdateManyAsync(
CreateEntitiesFilter(entities),
Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true)
);
}

if (updateResult.MatchedCount < entitiesCount)
{
ThrowOptimisticConcurrencyException();
}
}
else
{
DeleteResult deleteResult;
if (SessionHandle != null)
{
deleteResult = await Collection.DeleteManyAsync(
SessionHandle,
CreateEntitiesFilter(entities)
);
}
else
{
deleteResult = await Collection.DeleteManyAsync(
CreateEntitiesFilter(entities)
);
}

if (deleteResult.DeletedCount < entitiesCount)
{
ThrowOptimisticConcurrencyException();
}
}
}

public async override Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().ToListAsync(GetCancellationToken(cancellationToken));
Expand Down Expand Up @@ -271,6 +403,13 @@ protected virtual FilterDefinition<TEntity> CreateEntityFilter(TEntity entity, b
);
}

protected virtual FilterDefinition<TEntity> CreateEntitiesFilter(IEnumerable<TEntity> entities, bool withConcurrencyStamp = false)
{
throw new NotImplementedException(
$"{nameof(CreateEntitiesFilter)} is not implemented for MongoDB by default. It should be overriden and implemented by the deriving class!"
);
}

protected virtual async Task ApplyAbpConceptsForAddedEntityAsync(TEntity entity)
{
CheckAndSetId(entity);
Expand Down Expand Up @@ -483,9 +622,14 @@ protected override FilterDefinition<TEntity> CreateEntityFilter(TEntity entity,
return RepositoryFilterer.CreateEntityFilter(entity, withConcurrencyStamp, concurrencyStamp);
}

protected override FilterDefinition<TEntity> CreateEntitiesFilter(IEnumerable<TEntity> entities, bool withConcurrencyStamp = false)
{
return RepositoryFilterer.CreateEntitiesFilter(entities, withConcurrencyStamp);
}

public async Task DeleteManyAsync([NotNull] IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();

}
}
}

0 comments on commit 7d39d8a

Please sign in to comment.