From 740da95e1bf627fe5547737054c9e64ca1def0fa Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Fri, 17 Aug 2018 20:39:37 -0500 Subject: [PATCH] fix duplicate key issue (hashset was removed) ref #343 --- .../FilterLists.Services.csproj | 4 ++++ src/FilterLists.Services/Snapshot/Snapshot.cs | 4 ++-- .../Snapshot/SnapshotBatch.cs | 16 +++++----------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/FilterLists.Services/FilterLists.Services.csproj b/src/FilterLists.Services/FilterLists.Services.csproj index ab115a047..abacb7ccc 100644 --- a/src/FilterLists.Services/FilterLists.Services.csproj +++ b/src/FilterLists.Services/FilterLists.Services.csproj @@ -29,4 +29,8 @@ + + + + \ No newline at end of file diff --git a/src/FilterLists.Services/Snapshot/Snapshot.cs b/src/FilterLists.Services/Snapshot/Snapshot.cs index 0ae3e1969..066d6e7e4 100644 --- a/src/FilterLists.Services/Snapshot/Snapshot.cs +++ b/src/FilterLists.Services/Snapshot/Snapshot.cs @@ -13,7 +13,7 @@ namespace FilterLists.Services.Snapshot { public class Snapshot { - private const int BatchSize = 50; + private const int BatchSize = 1000; private readonly FilterListsDbContext dbContext; private readonly FilterListViewUrlDto list; private readonly Data.Entities.Snapshot snapEntity; @@ -104,7 +104,7 @@ private static IEnumerable ParseRawRules(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].LintRawRule(); - return rawRules.Where(r => r != null); + return new HashSet(rawRules.Where(r => r != null)); } private IEnumerable CreateBatches(IEnumerable rawRules) => diff --git a/src/FilterLists.Services/Snapshot/SnapshotBatch.cs b/src/FilterLists.Services/Snapshot/SnapshotBatch.cs index 708b81052..2a3aa10b4 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotBatch.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotBatch.cs @@ -13,29 +13,23 @@ public class SnapshotBatch private readonly FilterListsDbContext dbContext; private readonly IEnumerable rawRules; private readonly Data.Entities.Snapshot snapEntity; - private IQueryable rules; public SnapshotBatch(FilterListsDbContext dbContext, IEnumerable rawRules, Data.Entities.Snapshot snapEntity) { this.dbContext = dbContext; - this.snapEntity = snapEntity; this.rawRules = rawRules; + this.snapEntity = snapEntity; } public async Task SaveAsync() - { - AddRules(); - AddSnapshotRules(); - await dbContext.SaveChangesAsync(); - } - - private void AddRules() { var existingRules = GetExistingRules(); var newRules = CreateNewRules(existingRules); - rules = existingRules.Concat(newRules); dbContext.Rules.AddRange(newRules); + var rules = existingRules.Concat(newRules); + AddSnapshotRules(rules); + await dbContext.SaveChangesAsync(); } private IQueryable GetExistingRules() => dbContext.Rules.Where(r => rawRules.Contains(r.Raw)); @@ -43,7 +37,7 @@ private void AddRules() private List CreateNewRules(IQueryable existingRules) => rawRules.Except(existingRules.Select(r => r.Raw)).Select(r => new Rule {Raw = r}).ToList(); - private void AddSnapshotRules() => + private void AddSnapshotRules(IQueryable rules) => snapEntity.AddedSnapshotRules.AddRange(rules.Select(r => new SnapshotRule {Rule = r})); } } \ No newline at end of file