public async Task <Record[]> LoadRecordsAsync(IStatsSourceSelector provider) { var records = new Dictionary <string, int>(); foreach (var path in provider.GetFilePathes(_fileLoader)) { await ReadRecordsXxx(path); } return(records.Select(r => new Record(r.Key, r.Value)) .ToArray()); async Task ReadRecordsXxx(string path) { using var reader = _readLineStreamFactory.Create(path); while (await reader.ReadLineAsync() is { } line) { ApplyRecord(line); } } void ApplyRecord(string line) { var(count, key) = provider.ExtractRecord(line); if (records !.ContainsKey(key)) { records[key] += count; }
private async Task <Record[]> LoadRecordsAsync_refactor(IStatsSourceSelector provider) { var deserializer = new StatsDeserializer(provider.ExtractRecord); var aggregations = new List <IDictionary <string, int> >(); foreach (var path in provider.GetFilePathes(_fileLoader)) { using var reader = _readLineStreamFactory.Create(path); aggregations.Add(await deserializer.ReadRecordsAsync(reader)); } return(aggregations.SelectMany(x => x) .GroupBy(pair => pair.Key) .Select(group => new Record(group.Key, group.Sum(x => x.Value))) .ToArray()); }