From 735ad264f1add66c3954444333f940d46e8cccaf Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Wed, 14 Sep 2022 14:57:05 -0400 Subject: [PATCH] Remove Batch (#2274) --- .../SelfHostedSyncSponsorshipsCommand.cs | 3 +-- .../Services/Implementations/CipherService.cs | 8 +++--- src/Core/Utilities/CoreHelpers.cs | 26 ------------------- src/Events/Controllers/CollectController.cs | 3 +-- test/Core.Test/Utilities/CoreHelpersTests.cs | 25 ------------------ 5 files changed, 6 insertions(+), 59 deletions(-) diff --git a/src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SelfHosted/SelfHostedSyncSponsorshipsCommand.cs b/src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SelfHosted/SelfHostedSyncSponsorshipsCommand.cs index df293c3a7b..be588cd4eb 100644 --- a/src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SelfHosted/SelfHostedSyncSponsorshipsCommand.cs +++ b/src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/SelfHosted/SelfHostedSyncSponsorshipsCommand.cs @@ -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(); - foreach (var orgSponsorshipsBatch in CoreHelpers.Batch(organizationSponsorshipsDict.Values, 1000)) + foreach (var orgSponsorshipsBatch in organizationSponsorshipsDict.Values.Chunk(1000)) { var response = await SendAsync(HttpMethod.Post, "organization/sponsorship/sync", new OrganizationSponsorshipSyncRequestModel { diff --git a/src/Core/Services/Implementations/CipherService.cs b/src/Core/Services/Implementations/CipherService.cs index d114ba4164..21bb4a8579 100644 --- a/src/Core/Services/Implementations/CipherService.cs +++ b/src/Core/Services/Implementations/CipherService.cs @@ -403,7 +403,7 @@ public class CipherService : ICipherService var events = deletingCiphers.Select(c => new Tuple(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(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(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(c, EventType.Cipher_Restored, null); }); - foreach (var eventsBatch in events.Batch(100)) + foreach (var eventsBatch in events.Chunk(100)) { await _eventService.LogCipherEventsAsync(eventsBatch); } diff --git a/src/Core/Utilities/CoreHelpers.cs b/src/Core/Utilities/CoreHelpers.cs index 0185281b85..4878109ce9 100644 --- a/src/Core/Utilities/CoreHelpers.cs +++ b/src/Core/Utilities/CoreHelpers.cs @@ -70,32 +70,6 @@ public static class CoreHelpers return new Guid(guidArray); } - public static IEnumerable> Batch(this IEnumerable 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 diff --git a/src/Events/Controllers/CollectController.cs b/src/Events/Controllers/CollectController.cs index aaed0b3584..2dc996cf26 100644 --- a/src/Events/Controllers/CollectController.cs +++ b/src/Events/Controllers/CollectController.cs @@ -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); } diff --git a/test/Core.Test/Utilities/CoreHelpersTests.cs b/test/Core.Test/Utilities/CoreHelpersTests.cs index 9b3909385a..f66f9ca01a 100644 --- a/test/Core.Test/Utilities/CoreHelpersTests.cs +++ b/test/Core.Test/Utilities/CoreHelpersTests.cs @@ -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()