Repository navigation
Expand file tree
/
Copy pathAbstractFilter.cs
More file actions
174 lines (149 loc) · 6.23 KB
/
Copy pathAbstractFilter.cs
File metadata and controls
174 lines (149 loc) · 6.23 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using DynamicData.Kernel;
namespace DynamicData.Internal
{
internal abstract class AbstractFilter<TObject, TKey> : IFilter<TObject, TKey>
{
private readonly ICache<TObject, TKey> _cache;
private readonly Func<TObject, bool> _filter;
protected AbstractFilter(ICache<TObject, TKey> cache, Func<TObject, bool> filter)
{
if (cache == null) throw new ArgumentNullException(nameof(cache));
_cache = cache;
if (filter==null)
{
_filter = t => true;
}
else
{
_filter = filter;
}
}
public Func<TObject, bool> Filter => _filter;
public IChangeSet<TObject, TKey> Evaluate(IEnumerable<KeyValuePair<TKey, TObject>> items)
{
//this is an internal method only so we can be sure there are no duplicate keys in the result
//(therefore safe to parallelise)
Func<KeyValuePair<TKey,TObject>, Optional<Change<TObject, TKey>>> factory = kv =>
{
var exisiting = _cache.Lookup(kv.Key);
var matches = _filter(kv.Value);
if (matches)
{
if (!exisiting.HasValue)
return new Change<TObject, TKey>(ChangeReason.Add, kv.Key, kv.Value);
}
else
{
if (exisiting.HasValue)
return new Change<TObject, TKey>(ChangeReason.Remove, kv.Key, kv.Value, exisiting);
}
return Optional.None<Change<TObject, TKey>>();
};
var result = Evaluate(items, factory);
var changes = new ChangeSet<TObject, TKey>(result);
_cache.Clone(changes);
return changes;
}
protected abstract IEnumerable<Change<TObject, TKey>> Evaluate(IEnumerable<KeyValuePair<TKey, TObject>> items, Func<KeyValuePair<TKey,TObject>, Optional<Change<TObject, TKey>>> factory);
public IChangeSet<TObject, TKey> Update(IChangeSet<TObject, TKey> updates)
{
var withfilter = GetChangesWithFilter(updates);
return ProcessResult(withfilter);
}
protected abstract IEnumerable<UpdateWithFilter> GetChangesWithFilter(IChangeSet<TObject, TKey> updates);
private IChangeSet<TObject, TKey> ProcessResult(IEnumerable<UpdateWithFilter> result)
{
//alas, have to process one item at a time as an item can be included multiple
//times in any batch
var updates = new List<Change<TObject, TKey>>();
foreach (var item in result)
{
var matches = item.IsMatch;
var key = item.Change.Key;
var exisiting = _cache.Lookup(key);
var u = item.Change;
Optional<Change<TObject, TKey>> change = Optional.None<Change<TObject, TKey>>();
switch (item.Change.Reason)
{
case ChangeReason.Add:
{
if (matches)
{
_cache.AddOrUpdate(u.Current,u.Key);
change = new Change<TObject, TKey>(ChangeReason.Add, key, u.Current);
}
}
break;
case ChangeReason.Update:
{
if (matches)
{
_cache.AddOrUpdate(u.Current, u.Key);
change = exisiting.HasValue
? new Change<TObject, TKey>(ChangeReason.Update, key, u.Current, exisiting)
: new Change<TObject, TKey>(ChangeReason.Add, key, u.Current);
}
else
{
if (exisiting.HasValue)
{
_cache.Remove(u.Key);
change = new Change<TObject, TKey>(ChangeReason.Remove, key, u.Current, exisiting);
}
}
}
break;
case ChangeReason.Remove:
if (exisiting.HasValue)
{
_cache.Remove(u.Key);
change = u; //new Change<TObject, TKey>(ChangeReason.Remove, key, u.Current, exisiting);
}
break;
case ChangeReason.Evaluate:
if (matches)
{
if (!exisiting.HasValue)
{
_cache.AddOrUpdate(u.Current, u.Key);
change = new Change<TObject, TKey>(ChangeReason.Add, key, u.Current);
}
else
{
change = u;
}
}
else
{
if (exisiting.HasValue)
{
_cache.Remove(u.Key);
change = new Change<TObject, TKey>(ChangeReason.Remove, key, u.Current, exisiting);
}
}
break;
}
if (change.HasValue)
{
updates.Add(change.Value);
}
}
return new ChangeSet<TObject, TKey>(updates);
}
protected struct UpdateWithFilter
{
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object"/> class.
/// </summary>
public UpdateWithFilter(bool isMatch, Change<TObject, TKey> change)
{
IsMatch = isMatch;
Change = change;
}
public Change<TObject, TKey> Change { get; }
public bool IsMatch { get; }
}
}
}