Repository navigation
Expand file tree
/
Copy pathChangeAwareCache.cs
More file actions
216 lines (188 loc) · 6.57 KB
/
Copy pathChangeAwareCache.cs
File metadata and controls
216 lines (188 loc) · 6.57 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using DynamicData.Cache;
using DynamicData.Kernel;
// ReSharper disable once CheckNamespace
namespace DynamicData;
/// <summary>
/// A cache which captures all changes which are made to it. These changes are recorded until CaptureChanges() at which point thw changes are cleared.
/// Used for creating custom operators.
/// </summary>
/// <seealso cref="ICache{TObject, TKey}" />
/// <typeparam name="TObject">The value of the cache.</typeparam>
/// <typeparam name="TKey">The key of the cache.</typeparam>
public sealed class ChangeAwareCache<TObject, TKey> : ICache<TObject, TKey>
where TObject : notnull
where TKey : notnull
{
private readonly Dictionary<TKey, TObject> _data;
private ChangeSet<TObject, TKey> _changes;
/// <summary>
/// Initializes a new instance of the <see cref="ChangeAwareCache{TObject, TKey}"/> class.
/// </summary>
public ChangeAwareCache()
{
_changes = [];
_data = [];
}
/// <summary>
/// Initializes a new instance of the <see cref="ChangeAwareCache{TObject, TKey}"/> class.
/// </summary>
/// <param name="capacity">The capacity of the initial items.</param>
public ChangeAwareCache(int capacity)
{
_changes = new ChangeSet<TObject, TKey>(capacity);
_data = new Dictionary<TKey, TObject>(capacity);
}
/// <summary>
/// Initializes a new instance of the <see cref="ChangeAwareCache{TObject, TKey}"/> class.
/// </summary>
/// <param name="data">Data to populate the cache with.</param>
public ChangeAwareCache(Dictionary<TKey, TObject> data)
{
_data = data ?? throw new ArgumentNullException(nameof(data));
_changes = [];
}
/// <inheritdoc />
public int Count => _data.Count;
/// <inheritdoc />
public IEnumerable<TObject> Items => _data.Values;
/// <inheritdoc />
public IEnumerable<TKey> Keys => _data.Keys;
/// <inheritdoc />
public IEnumerable<KeyValuePair<TKey, TObject>> KeyValues => _data;
/// <summary>
/// Adds the item to the cache without checking whether there is an existing value in the cache.
/// </summary>
/// <param name="item">The item to add.</param>
/// <param name="key">The key to add.</param>
public void Add(TObject item, TKey key)
{
_changes.Add(new Change<TObject, TKey>(ChangeReason.Add, key, item));
_data.Add(key, item);
}
/// <inheritdoc />
public void AddOrUpdate(TObject item, TKey key)
{
_changes.Add(_data.TryGetValue(key, out var existingItem) ? new Change<TObject, TKey>(ChangeReason.Update, key, item, existingItem) : new Change<TObject, TKey>(ChangeReason.Add, key, item));
_data[key] = item;
}
/// <summary>
/// Create a change set from recorded changes and clears known changes.
/// </summary>
/// <returns>A change set with the key/value changes.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0301:Simplify collection initialization", Justification = "This would result in differing operation")]
public ChangeSet<TObject, TKey> CaptureChanges()
{
if (_changes.Count == 0)
{
return ChangeSet<TObject, TKey>.Empty;
}
var copy = _changes;
_changes = [];
return copy;
}
/// <inheritdoc />
public void Clear()
{
var toRemove = _data.Select(kvp => new Change<TObject, TKey>(ChangeReason.Remove, kvp.Key, kvp.Value));
_changes.AddRange(toRemove);
_data.Clear();
}
/// <inheritdoc />
public void Clone(IChangeSet<TObject, TKey> changes)
{
changes.ThrowArgumentNullExceptionIfNull(nameof(changes));
foreach (var change in changes.ToConcreteType())
{
switch (change.Reason)
{
case ChangeReason.Add:
case ChangeReason.Update:
AddOrUpdate(change.Current, change.Key);
break;
case ChangeReason.Remove:
Remove(change.Key);
break;
case ChangeReason.Refresh:
Refresh(change.Key);
break;
case ChangeReason.Moved:
break;
default:
throw new ArgumentOutOfRangeException(nameof(changes));
}
}
}
/// <inheritdoc />
public Optional<TObject> Lookup(TKey key) => _data.Lookup(key);
/// <summary>
/// Raises an evaluate change for the specified keys.
/// </summary>
/// <param name="keys">The keys to refresh.</param>
public void Refresh(IEnumerable<TKey> keys)
{
keys.ThrowArgumentNullExceptionIfNull(nameof(keys));
if (keys is IList<TKey> list)
{
foreach (var key in EnumerableIList.Create(list))
{
Refresh(key);
}
}
else
{
foreach (var key in keys)
{
Refresh(key);
}
}
}
/// <summary>
/// Raises an evaluate change for all items in the cache.
/// </summary>
public void Refresh() => _changes.AddRange(_data.Select(t => new Change<TObject, TKey>(ChangeReason.Refresh, t.Key, t.Value)));
/// <summary>
/// Raises an evaluate change for the specified key.
/// </summary>
/// <param name="key">The key.</param>
public void Refresh(TKey key)
{
if (_data.TryGetValue(key, out var existingItem))
{
_changes.Add(new Change<TObject, TKey>(ChangeReason.Refresh, key, existingItem));
}
}
/// <summary>
/// Removes the item matching the specified keys.
/// </summary>
/// <param name="keys">The keys.</param>
public void Remove(IEnumerable<TKey> keys)
{
keys.ThrowArgumentNullExceptionIfNull(nameof(keys));
if (keys is IList<TKey> list)
{
foreach (var item in EnumerableIList.Create(list))
{
Remove(item);
}
}
else
{
foreach (var key in keys)
{
Remove(key);
}
}
}
/// <inheritdoc />
public void Remove(TKey key)
{
if (_data.TryGetValue(key, out var existingItem))
{
_changes.Add(new Change<TObject, TKey>(ChangeReason.Remove, key, existingItem));
_data.Remove(key);
}
}
}