mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
minor Snapshot refactors
This commit is contained in:
parent
79ed112474
commit
ad0ab03b77
4 changed files with 43 additions and 26 deletions
|
|
@ -1,13 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace FilterLists.Services.Extensions
|
||||
{
|
||||
public static class CollectionExtensions
|
||||
{
|
||||
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> source)
|
||||
{
|
||||
foreach (var item in source)
|
||||
destination.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/FilterLists.Services/Extensions/EnumerableExtensions.cs
Normal file
29
src/FilterLists.Services/Extensions/EnumerableExtensions.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace FilterLists.Services.Extensions
|
||||
{
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> source)
|
||||
{
|
||||
foreach (var item in source)
|
||||
destination.Add(item);
|
||||
}
|
||||
|
||||
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int batchSize)
|
||||
{
|
||||
var batches = new List<T>(batchSize);
|
||||
foreach (var item in source)
|
||||
{
|
||||
batches.Add(item);
|
||||
if (batches.Count != batchSize)
|
||||
continue;
|
||||
yield return batches.AsReadOnly();
|
||||
batches = new List<T>(batchSize);
|
||||
}
|
||||
|
||||
if (batches.Count > 0)
|
||||
yield return batches.AsReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ public static class RawRuleLinterExtensions
|
|||
public static string LintStringForMySql(this string rule)
|
||||
{
|
||||
rule = rule.TrimLeadingAndTrailingWhitespace();
|
||||
rule = rule.DropIfEmpty();
|
||||
rule = rule.TrimSingleBackslashFromEnd();
|
||||
rule = rule.DropIfContainsBackslashSingleQuote();
|
||||
rule = rule.DropIfTooLong();
|
||||
|
|
@ -18,6 +19,11 @@ private static string TrimLeadingAndTrailingWhitespace(this string rule)
|
|||
return rule.Trim(charsToTrim);
|
||||
}
|
||||
|
||||
private static string DropIfEmpty(this string rule)
|
||||
{
|
||||
return rule == "" ? null : rule;
|
||||
}
|
||||
|
||||
private static string TrimSingleBackslashFromEnd(this string rule)
|
||||
{
|
||||
if (rule != null)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
using System.Threading.Tasks;
|
||||
using FilterLists.Data;
|
||||
using FilterLists.Data.Entities;
|
||||
using FilterLists.Services.Extensions;
|
||||
|
||||
namespace FilterLists.Services.SnapshotService
|
||||
{
|
||||
|
|
@ -75,24 +76,18 @@ private void SaveSnapshotInBatches(string content)
|
|||
SaveSnapshotBatches(snapshotBatches);
|
||||
}
|
||||
|
||||
private static List<string> GetRawRules(string content)
|
||||
private static IEnumerable<string> GetRawRules(string content)
|
||||
{
|
||||
var rawRules = content.Split(new[] {"\r\n", "\r", "\n"}, StringSplitOptions.RemoveEmptyEntries).ToList();
|
||||
for (var i = 0; i < rawRules.Count; i++)
|
||||
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 List<string>(new HashSet<string>(rawRules.Where(x => !string.IsNullOrWhiteSpace(x))));
|
||||
return new HashSet<string>(rawRules);
|
||||
}
|
||||
|
||||
private IEnumerable<SnapshotBatchDe> GetSnapshotBatches(List<string> rawRules)
|
||||
private IEnumerable<SnapshotBatchDe> GetSnapshotBatches(IEnumerable<string> rawRules)
|
||||
{
|
||||
var rawRuleBatches = GetRawRuleBatches(rawRules);
|
||||
return rawRuleBatches.Select(rawRuleBatch => new SnapshotBatchDe(dbContext, snapshot, rawRuleBatch));
|
||||
}
|
||||
|
||||
public static IEnumerable<IEnumerable<string>> GetRawRuleBatches(List<string> rawRules)
|
||||
{
|
||||
for (var i = 0; i < rawRules.Count; i += BatchSize)
|
||||
yield return rawRules.GetRange(i, Math.Min(BatchSize, rawRules.Count - i));
|
||||
return rawRules.Batch(BatchSize)
|
||||
.Select(rawRuleBatch => new SnapshotBatchDe(dbContext, snapshot, rawRuleBatch));
|
||||
}
|
||||
|
||||
private static void SaveSnapshotBatches(IEnumerable<SnapshotBatchDe> snapshotBatches)
|
||||
|
|
|
|||
Loading…
Reference in a new issue