1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

Remove Batch (#2274)

This commit is contained in:
Justin Baur 2022-09-14 14:57:05 -04:00 committed by GitHub
parent f848eb2477
commit 735ad264f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 59 deletions

View File

@ -8,7 +8,6 @@ using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterpri
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.Extensions.Logging;
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.SelfHosted;
@ -71,7 +70,7 @@ public class SelfHostedSyncSponsorshipsCommand : BaseIdentityClientService, ISel
}
var syncedSponsorships = new List<OrganizationSponsorshipData>();
foreach (var orgSponsorshipsBatch in CoreHelpers.Batch(organizationSponsorshipsDict.Values, 1000))
foreach (var orgSponsorshipsBatch in organizationSponsorshipsDict.Values.Chunk(1000))
{
var response = await SendAsync<OrganizationSponsorshipSyncRequestModel, OrganizationSponsorshipSyncResponseModel>(HttpMethod.Post, "organization/sponsorship/sync", new OrganizationSponsorshipSyncRequestModel
{

View File

@ -403,7 +403,7 @@ public class CipherService : ICipherService
var events = deletingCiphers.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Deleted, null));
foreach (var eventsBatch in events.Batch(100))
foreach (var eventsBatch in events.Chunk(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
@ -574,7 +574,7 @@ public class CipherService : ICipherService
var events = cipherInfos.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c.cipher, EventType.Cipher_Shared, null));
foreach (var eventsBatch in events.Batch(100))
foreach (var eventsBatch in events.Chunk(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
@ -791,7 +791,7 @@ public class CipherService : ICipherService
var events = deletingCiphers.Select(c =>
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_SoftDeleted, null));
foreach (var eventsBatch in events.Batch(100))
foreach (var eventsBatch in events.Chunk(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}
@ -840,7 +840,7 @@ public class CipherService : ICipherService
c.DeletedDate = null;
return new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Restored, null);
});
foreach (var eventsBatch in events.Batch(100))
foreach (var eventsBatch in events.Chunk(100))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}

View File

@ -70,32 +70,6 @@ public static class CoreHelpers
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 string CleanCertificateThumbprint(string thumbprint)
{
// Clean possible garbage characters from thumbprint copy/paste

View File

@ -3,7 +3,6 @@ using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Bit.Events.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -95,7 +94,7 @@ public class CollectController : Controller
}
if (cipherEvents.Any())
{
foreach (var eventsBatch in cipherEvents.Batch(50))
foreach (var eventsBatch in cipherEvents.Chunk(50))
{
await _eventService.LogCipherEventsAsync(eventsBatch);
}

View File

@ -70,31 +70,6 @@ public class CoreHelpersTests
Assert.Equal(expectedComb, comb);
}
[Theory]
[InlineData(2, 5, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 })]
[InlineData(2, 3, new[] { 1, 2, 3, 4, 5 })]
[InlineData(2, 1, new[] { 1, 2 })]
[InlineData(1, 1, new[] { 1 })]
[InlineData(2, 2, new[] { 1, 2, 3 })]
public void Batch_Success(int batchSize, int totalBatches, int[] collection)
{
// Arrange
var remainder = collection.Length % batchSize;
// Act
var batches = collection.Batch(batchSize);
// Assert
Assert.Equal(totalBatches, batches.Count());
foreach (var batch in batches.Take(totalBatches - 1))
{
Assert.Equal(batchSize, batch.Count());
}
Assert.Equal(batches.Last().Count(), remainder == 0 ? batchSize : remainder);
}
/*
[Fact]
public void ToGuidIdArrayTVP_Success()