Skip to content

Commit be1c7da

Browse files
shargonJimmycschuchardt88NGDAdmin
authored
Remove ChangeSet from StoreView (#3739)
* Speed up Read only storage * Add ut * Fix ut * Format * Fix CloneCache * Rename * change error message * Fix clone cache tests * Fix merge * Update UT_MemoryStore.cs * Fix build * Update src/Neo/Persistence/StoreCache.cs --------- Co-authored-by: Jimmy <[email protected]> Co-authored-by: Christopher Schuchardt <[email protected]> Co-authored-by: NGD Admin <[email protected]>
1 parent 6c9312a commit be1c7da

File tree

9 files changed

+155
-100
lines changed

9 files changed

+155
-100
lines changed

src/Neo/NeoSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class NeoSystem : IDisposable
8484
/// <remarks>
8585
/// It doesn't need to be disposed because the <see cref="IStoreSnapshot"/> inside it is null.
8686
/// </remarks>
87-
public DataCache StoreView => new StoreCache(store);
87+
public StoreCache StoreView => new(store);
8888

8989
/// <summary>
9090
/// The memory pool of the <see cref="NeoSystem"/>.

src/Neo/Persistence/ClonedCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ClonedCache : DataCache
2020
{
2121
private readonly DataCache _innerCache;
2222

23-
public ClonedCache(DataCache innerCache)
23+
public ClonedCache(DataCache innerCache) : base(false)
2424
{
2525
_innerCache = innerCache;
2626
}

src/Neo/Persistence/DataCache.cs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ public class Trackable(StorageItem item, TrackState state)
4343
}
4444

4545
private readonly Dictionary<StorageKey, Trackable> _dictionary = [];
46-
private readonly HashSet<StorageKey> _changeSet = [];
46+
private readonly HashSet<StorageKey>? _changeSet;
47+
48+
/// <summary>
49+
/// True if DataCache is readOnly
50+
/// </summary>
51+
public bool IsReadOnly => _changeSet == null;
4752

4853
/// <summary>
4954
/// Reads a specified entry from the cache. If the entry is not in the cache, it will be automatically loaded from the underlying storage.
@@ -72,6 +77,16 @@ public StorageItem this[StorageKey key]
7277
}
7378
}
7479

80+
/// <summary>
81+
/// Data cache constructor
82+
/// </summary>
83+
/// <param name="readOnly">True if you don't want to allow writes</param>
84+
protected DataCache(bool readOnly)
85+
{
86+
if (!readOnly)
87+
_changeSet = [];
88+
}
89+
7590
/// <summary>
7691
/// Adds a new entry to the cache.
7792
/// </summary>
@@ -97,7 +112,7 @@ public void Add(StorageKey key, StorageItem value)
97112
{
98113
_dictionary[key] = new Trackable(value, TrackState.Added);
99114
}
100-
_changeSet.Add(key);
115+
_changeSet?.Add(key);
101116
}
102117
}
103118

@@ -113,6 +128,11 @@ public void Add(StorageKey key, StorageItem value)
113128
/// </summary>
114129
public virtual void Commit()
115130
{
131+
if (_changeSet is null)
132+
{
133+
throw new InvalidOperationException("DataCache is read only");
134+
}
135+
116136
lock (_dictionary)
117137
{
118138
foreach (var key in _changeSet)
@@ -138,6 +158,24 @@ public virtual void Commit()
138158
}
139159
}
140160

161+
/// <summary>
162+
/// Gets the change set in the cache.
163+
/// </summary>
164+
/// <returns>The change set.</returns>
165+
public IEnumerable<KeyValuePair<StorageKey, Trackable>> GetChangeSet()
166+
{
167+
if (_changeSet is null)
168+
{
169+
throw new InvalidOperationException("DataCache is read only");
170+
}
171+
172+
lock (_dictionary)
173+
{
174+
foreach (var key in _changeSet)
175+
yield return new(key, _dictionary[key]);
176+
}
177+
}
178+
141179
/// <summary>
142180
/// Creates a snapshot, which uses this instance as the underlying storage.
143181
/// </summary>
@@ -170,20 +208,20 @@ public void Delete(StorageKey key)
170208
if (trackable.State == TrackState.Added)
171209
{
172210
trackable.State = TrackState.NotFound;
173-
_changeSet.Remove(key);
211+
_changeSet?.Remove(key);
174212
}
175213
else if (trackable.State != TrackState.NotFound)
176214
{
177215
trackable.State = TrackState.Deleted;
178-
_changeSet.Add(key);
216+
_changeSet?.Add(key);
179217
}
180218
}
181219
else
182220
{
183221
var item = TryGetInternal(key);
184222
if (item == null) return;
185223
_dictionary.Add(key, new Trackable(item, TrackState.Deleted));
186-
_changeSet.Add(key);
224+
_changeSet?.Add(key);
187225
}
188226
}
189227
}
@@ -262,19 +300,6 @@ public void Delete(StorageKey key)
262300
yield break;
263301
}
264302

265-
/// <summary>
266-
/// Gets the change set in the cache.
267-
/// </summary>
268-
/// <returns>The change set.</returns>
269-
public IEnumerable<KeyValuePair<StorageKey, Trackable>> GetChangeSet()
270-
{
271-
lock (_dictionary)
272-
{
273-
foreach (var key in _changeSet)
274-
yield return new(key, _dictionary[key]);
275-
}
276-
}
277-
278303
/// <summary>
279304
/// Determines whether the cache contains the specified entry.
280305
/// </summary>
@@ -337,13 +362,13 @@ public bool Contains(StorageKey key)
337362
else
338363
{
339364
trackable.State = TrackState.Added;
340-
_changeSet.Add(key);
365+
_changeSet?.Add(key);
341366
}
342367
}
343368
else if (trackable.State == TrackState.None)
344369
{
345370
trackable.State = TrackState.Changed;
346-
_changeSet.Add(key);
371+
_changeSet?.Add(key);
347372
}
348373
}
349374
else
@@ -359,7 +384,7 @@ public bool Contains(StorageKey key)
359384
trackable = new Trackable(item, TrackState.Changed);
360385
}
361386
_dictionary.Add(key, trackable);
362-
_changeSet.Add(key);
387+
_changeSet?.Add(key);
363388
}
364389
return trackable.Item;
365390
}
@@ -392,7 +417,7 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory)
392417
else
393418
{
394419
trackable.State = TrackState.Added;
395-
_changeSet.Add(key);
420+
_changeSet?.Add(key);
396421
}
397422
}
398423
}
@@ -402,7 +427,7 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory)
402427
if (item == null)
403428
{
404429
trackable = new Trackable(factory(), TrackState.Added);
405-
_changeSet.Add(key);
430+
_changeSet?.Add(key);
406431
}
407432
else
408433
{

src/Neo/Persistence/StoreCache.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public class StoreCache : DataCache, IDisposable
3131
/// Initializes a new instance of the <see cref="StoreCache"/> class.
3232
/// </summary>
3333
/// <param name="store">An <see cref="IStore"/> to create a readonly cache.</param>
34-
public StoreCache(IStore store)
34+
/// <param name="readOnly">True if you don't want to track write changes</param>
35+
public StoreCache(IStore store, bool readOnly = true) : base(readOnly)
3536
{
3637
_store = store;
3738
}
@@ -40,7 +41,7 @@ public StoreCache(IStore store)
4041
/// Initializes a new instance of the <see cref="StoreCache"/> class.
4142
/// </summary>
4243
/// <param name="snapshot">An <see cref="IStoreSnapshot"/> to create a snapshot cache.</param>
43-
public StoreCache(IStoreSnapshot snapshot)
44+
public StoreCache(IStoreSnapshot snapshot) : base(false)
4445
{
4546
_store = snapshot;
4647
_snapshot = snapshot;

tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Wallet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public void TestListAddress_WhenWalletNotOpen()
342342
public void TestCancelTransaction()
343343
{
344344
TestUtilOpenWallet();
345-
var snapshot = _neoSystem.GetSnapshot();
345+
var snapshot = _neoSystem.GetSnapshotCache();
346346
var tx = TestUtils.CreateValidTx(snapshot, _wallet, _walletAccount);
347347
snapshot.Commit();
348348
var paramsArray = new JArray(tx.Hash.ToString(), new JArray(_walletAccount.Address));

0 commit comments

Comments
 (0)