1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 13:38:13 -05:00

batch events

This commit is contained in:
Kyle Spearrin 2019-07-25 15:50:13 -04:00
parent 675b22cc9f
commit 6a91fd6be9
3 changed files with 41 additions and 4 deletions

View File

@ -10,6 +10,7 @@ using Bit.Core.Models.Data;
using Newtonsoft.Json;
using System.IO;
using Bit.Core.Enums;
using Bit.Core.Utilities;
namespace Bit.Core.Services
{
@ -295,9 +296,12 @@ namespace Bit.Core.Services
await _cipherRepository.DeleteAsync(cipherIds, deletingUserId);
var events = deletingCiphers.Select(c =>
var events = deletingCiphers.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Deleted, null));
await _eventService.LogCipherEventsAsync(events);
foreach(var eventsBatch in events.Batch(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
// push
await _pushService.PushSyncCiphersAsync(deletingUserId);
@ -509,7 +513,10 @@ namespace Bit.Core.Services
var events = ciphers.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Shared, null));
await _eventService.LogCipherEventsAsync(events);
foreach(var eventsBatch in events.Batch(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
// push
await _pushService.PushSyncCiphersAsync(sharingUserId);

View File

@ -65,6 +65,32 @@ namespace Bit.Core.Utilities
return new Guid(guidArray);
}
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int size)
{
T[] bucket = null;
var count = 0;
foreach(var item in source)
{
if(bucket == null)
{
bucket = new T[size];
}
bucket[count++] = item;
if(count != size)
{
continue;
}
yield return bucket.Select(x => x);
bucket = null;
count = 0;
}
// Return the last bucket with all remaining elements
if(bucket != null && count > 0)
{
yield return bucket.Take(count);
}
}
public static DataTable ToGuidIdArrayTVP(this IEnumerable<Guid> ids)
{
return ids.ToArrayTVP("GuidId");

View File

@ -7,6 +7,7 @@ using Bit.Core.Enums;
using Bit.Core.Models.Table;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Bit.Events.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -74,7 +75,10 @@ namespace Bit.Events.Controllers
}
if(cipherEvents.Any())
{
await _eventService.LogCipherEventsAsync(cipherEvents);
foreach(var eventsBatch in cipherEvents.Batch(50))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
}
return new OkResult();
}