From ee09614db4543f602ba3d7e7fabdc508cf017015 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Wed, 7 Feb 2018 15:01:55 -0600 Subject: [PATCH] async batches --- .../SnapshotService/SnapshotBatchDe.cs | 5 +++-- .../SnapshotService/SnapshotDe.cs | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs b/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs index a4b222c1c..8a3d84813 100644 --- a/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs +++ b/src/FilterLists.Services/SnapshotService/SnapshotBatchDe.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using FilterLists.Data; using FilterLists.Data.Entities; using FilterLists.Data.Entities.Junctions; @@ -22,11 +23,11 @@ public SnapshotBatchDe(FilterListsDbContext dbContext, Snapshot snapshot, IEnume this.rawRules = rawRules; } - public void SaveSnapshotBatch() + public async Task SaveSnapshotBatchAsync() { AddNewRules(); AddSnapshotRules(); - dbContext.SaveChanges(); + await dbContext.SaveChangesAsync(); } private void AddNewRules() diff --git a/src/FilterLists.Services/SnapshotService/SnapshotDe.cs b/src/FilterLists.Services/SnapshotService/SnapshotDe.cs index 9b8d40598..30410349c 100644 --- a/src/FilterLists.Services/SnapshotService/SnapshotDe.cs +++ b/src/FilterLists.Services/SnapshotService/SnapshotDe.cs @@ -29,7 +29,7 @@ public async Task SaveSnapshotAsync() var content = await TryGetContent(); await dbContext.SaveChangesAsync(); if (content != null) - SaveSnapshotInBatches(content); + await SaveSnapshotInBatches(content); } private async Task AddSnapshot() @@ -69,11 +69,11 @@ private async Task GetContent() return null; } - private void SaveSnapshotInBatches(string content) + private async Task SaveSnapshotInBatches(string content) { var rawRules = GetRawRules(content); var snapshotBatches = GetSnapshotBatches(rawRules); - SaveSnapshotBatches(snapshotBatches); + await SaveSnapshotBatches(snapshotBatches); } private static IEnumerable GetRawRules(string content) @@ -81,7 +81,7 @@ private static IEnumerable GetRawRules(string content) var rawRules = content.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries); for (var i = 0; i < rawRules.Length; i++) rawRules[i] = rawRules[i].LintStringForMySql(); - return new HashSet(rawRules); + return new HashSet(rawRules.Where(x => x != null)); } private IEnumerable GetSnapshotBatches(IEnumerable rawRules) @@ -90,10 +90,10 @@ private IEnumerable GetSnapshotBatches(IEnumerable rawR .Select(rawRuleBatch => new SnapshotBatchDe(dbContext, snapshot, rawRuleBatch)); } - private static void SaveSnapshotBatches(IEnumerable snapshotBatches) + private static async Task SaveSnapshotBatches(IEnumerable snapshotBatches) { foreach (var snapshotBatch in snapshotBatches) - snapshotBatch.SaveSnapshotBatch(); + await snapshotBatch.SaveSnapshotBatchAsync(); } } } \ No newline at end of file