-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatcher.cs
More file actions
95 lines (75 loc) · 3.14 KB
/
Copy pathBatcher.cs
File metadata and controls
95 lines (75 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Bitdiff.Utils
{
public class Batcher : IBatcher
{
private int _batchNumber;
public event EventHandler<BatchEventArgs> Complete = (s, e) => { };
public event EventHandler<BatchCompleteEventArgs> SingleBatchComplete = (s, e) => { };
private void OnComplete(BatchEventArgs e)
{
var handler = Complete;
handler?.Invoke(this, e);
}
private void OnSingleBatchComplete(BatchCompleteEventArgs e)
{
var handler = SingleBatchComplete;
handler?.Invoke(this, e);
}
public void Batch<T>(IEnumerable<T> items, int batchSize, Action<IEnumerable<T>, int> action)
{
var itemsToProcess = new List<T>();
var index = 1;
var totalItemsProcessed = 0;
foreach (var item in items)
{
itemsToProcess.Add(item);
if (index % batchSize == 0)
Process(itemsToProcess, action, ref totalItemsProcessed);
index++;
}
if (itemsToProcess.Any())
Process(itemsToProcess, action, ref totalItemsProcessed);
OnComplete(new BatchEventArgs(totalItemsProcessed));
}
public async Task AsyncBatch<T>(IEnumerable<T> items, int batchSize, Func<IEnumerable<T>, int, Task> action)
{
var itemsToProcess = new List<T>();
var index = 1;
var totalItemsProcessed = 0;
foreach (var item in items)
{
itemsToProcess.Add(item);
if (index % batchSize == 0)
totalItemsProcessed = await ProcessAsync(itemsToProcess, action, totalItemsProcessed);
index++;
}
if (itemsToProcess.Any())
totalItemsProcessed = await ProcessAsync(itemsToProcess, action, totalItemsProcessed);
OnComplete(new BatchEventArgs(totalItemsProcessed));
}
private void Process<T>(ICollection<T> itemsToProcess, Action<IEnumerable<T>, int> action, ref int totalItemsProcessed)
{
var stopwatch = Stopwatch.StartNew();
action(itemsToProcess, ++_batchNumber);
stopwatch.Stop();
totalItemsProcessed += itemsToProcess.Count;
OnSingleBatchComplete(new BatchCompleteEventArgs(itemsToProcess.Count, stopwatch.Elapsed, totalItemsProcessed));
itemsToProcess.Clear();
}
private async Task<int> ProcessAsync<T>(ICollection<T> itemsToProcess, Func<IEnumerable<T>, int, Task> action, int totalItemsProcessed)
{
var stopwatch = Stopwatch.StartNew();
await action(itemsToProcess, ++_batchNumber);
stopwatch.Stop();
totalItemsProcessed += itemsToProcess.Count;
OnSingleBatchComplete(new BatchCompleteEventArgs(itemsToProcess.Count, stopwatch.Elapsed, totalItemsProcessed));
itemsToProcess.Clear();
return totalItemsProcessed;
}
}
}