Skip to content

Remove ChangeSet from StoreView #3739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2b7040a
Speed up Read only storage
shargon Feb 11, 2025
ff69413
Add ut
shargon Feb 11, 2025
aaedc8b
Fix ut
shargon Feb 11, 2025
f06cf0b
Format
shargon Feb 11, 2025
10790c1
Fix CloneCache
shargon Feb 11, 2025
9610824
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 11, 2025
89a1ec1
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 12, 2025
66c3f67
Merge branch 'master' into avoid-change-set-from-readonly
Jim8y Feb 12, 2025
fcd947c
Merge remote-tracking branch 'origin/master' into avoid-change-set-fr…
shargon Feb 12, 2025
7ffe985
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 13, 2025
911a79e
Rename
shargon Feb 13, 2025
5eff62b
change error message
shargon Feb 13, 2025
ede8ca9
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 13, 2025
46f92c4
Fix clone cache tests
shargon Feb 13, 2025
500e242
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 14, 2025
0941a85
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 17, 2025
f9cb69f
Merge remote-tracking branch 'origin/master' into avoid-change-set-fr…
shargon Feb 22, 2025
df37bb6
Fix merge
shargon Feb 22, 2025
1ce2865
Update UT_MemoryStore.cs
shargon Feb 22, 2025
6d5bb90
Fix build
shargon Feb 22, 2025
ec841a4
Merge branch 'master' into avoid-change-set-from-readonly
Jim8y Feb 25, 2025
8bc5ae8
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 26, 2025
6421d7b
Merge branch 'master' into avoid-change-set-from-readonly
shargon Feb 28, 2025
5f4c2ae
Update src/Neo/Persistence/StoreCache.cs
shargon Feb 28, 2025
ecdc2b4
Merge branch 'master' into avoid-change-set-from-readonly
Jim8y Mar 4, 2025
67f11c8
Merge branch 'master' into avoid-change-set-from-readonly
cschuchardt88 Mar 4, 2025
c4cd0e2
Merge branch 'master' into avoid-change-set-from-readonly
NGDAdmin Mar 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class NeoSystem : IDisposable
/// <remarks>
/// It doesn't need to be disposed because the <see cref="IStoreSnapshot"/> inside it is null.
/// </remarks>
public DataCache StoreView => new StoreCache(store);
public StoreCache StoreView => new(store);

/// <summary>
/// The memory pool of the <see cref="NeoSystem"/>.
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/Persistence/ClonedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ClonedCache : DataCache
{
private readonly DataCache _innerCache;

public ClonedCache(DataCache innerCache)
public ClonedCache(DataCache innerCache) : base(false)
{
_innerCache = innerCache;
}
Expand Down
71 changes: 48 additions & 23 deletions src/Neo/Persistence/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public class Trackable(StorageItem item, TrackState state)
}

private readonly Dictionary<StorageKey, Trackable> _dictionary = [];
private readonly HashSet<StorageKey> _changeSet = [];
private readonly HashSet<StorageKey>? _changeSet;

/// <summary>
/// True if DataCache is readOnly
/// </summary>
public bool IsReadOnly => _changeSet == null;

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

/// <summary>
/// Data cache constructor
/// </summary>
/// <param name="readOnly">True if you don't want to allow writes</param>
protected DataCache(bool readOnly)
{
if (!readOnly)
_changeSet = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the same as _changeSet = null; ?

cause you have public bool IsReadOnly => _changeSet == null;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the same as _changeSet = null; ?

cause you have public bool IsReadOnly => _changeSet == null;

Line 87 creates an empty set

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting being null

}

/// <summary>
/// Adds a new entry to the cache.
/// </summary>
Expand All @@ -97,7 +112,7 @@ public void Add(StorageKey key, StorageItem value)
{
_dictionary[key] = new Trackable(value, TrackState.Added);
}
_changeSet.Add(key);
_changeSet?.Add(key);
}
}

Expand All @@ -113,6 +128,11 @@ public void Add(StorageKey key, StorageItem value)
/// </summary>
public virtual void Commit()
{
if (_changeSet is null)
{
throw new InvalidOperationException("DataCache is read only");
}

lock (_dictionary)
{
foreach (var key in _changeSet)
Expand All @@ -138,6 +158,24 @@ public virtual void Commit()
}
}

/// <summary>
/// Gets the change set in the cache.
/// </summary>
/// <returns>The change set.</returns>
public IEnumerable<KeyValuePair<StorageKey, Trackable>> GetChangeSet()
{
if (_changeSet is null)
{
throw new InvalidOperationException("DataCache is read only");
}

lock (_dictionary)
{
foreach (var key in _changeSet)
yield return new(key, _dictionary[key]);
}
}

/// <summary>
/// Creates a snapshot, which uses this instance as the underlying storage.
/// </summary>
Expand Down Expand Up @@ -170,20 +208,20 @@ public void Delete(StorageKey key)
if (trackable.State == TrackState.Added)
{
trackable.State = TrackState.NotFound;
_changeSet.Remove(key);
_changeSet?.Remove(key);
}
else if (trackable.State != TrackState.NotFound)
{
trackable.State = TrackState.Deleted;
_changeSet.Add(key);
_changeSet?.Add(key);
}
}
else
{
var item = TryGetInternal(key);
if (item == null) return;
_dictionary.Add(key, new Trackable(item, TrackState.Deleted));
_changeSet.Add(key);
_changeSet?.Add(key);
}
}
}
Expand Down Expand Up @@ -262,19 +300,6 @@ public void Delete(StorageKey key)
yield break;
}

/// <summary>
/// Gets the change set in the cache.
/// </summary>
/// <returns>The change set.</returns>
public IEnumerable<KeyValuePair<StorageKey, Trackable>> GetChangeSet()
{
lock (_dictionary)
{
foreach (var key in _changeSet)
yield return new(key, _dictionary[key]);
}
}

/// <summary>
/// Determines whether the cache contains the specified entry.
/// </summary>
Expand Down Expand Up @@ -337,13 +362,13 @@ public bool Contains(StorageKey key)
else
{
trackable.State = TrackState.Added;
_changeSet.Add(key);
_changeSet?.Add(key);
}
}
else if (trackable.State == TrackState.None)
{
trackable.State = TrackState.Changed;
_changeSet.Add(key);
_changeSet?.Add(key);
}
}
else
Expand All @@ -359,7 +384,7 @@ public bool Contains(StorageKey key)
trackable = new Trackable(item, TrackState.Changed);
}
_dictionary.Add(key, trackable);
_changeSet.Add(key);
_changeSet?.Add(key);
}
return trackable.Item;
}
Expand Down Expand Up @@ -392,7 +417,7 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory)
else
{
trackable.State = TrackState.Added;
_changeSet.Add(key);
_changeSet?.Add(key);
}
}
}
Expand All @@ -402,7 +427,7 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory)
if (item == null)
{
trackable = new Trackable(factory(), TrackState.Added);
_changeSet.Add(key);
_changeSet?.Add(key);
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/Neo/Persistence/StoreCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class StoreCache : DataCache, IDisposable
/// Initializes a new instance of the <see cref="StoreCache"/> class.
/// </summary>
/// <param name="store">An <see cref="IStore"/> to create a readonly cache.</param>
public StoreCache(IStore store)
/// <param name="readOnly">True if you don't want to track write changes</param>
public StoreCache(IStore store, bool readOnly = true) : base(readOnly)
{
_store = store;
}
Expand All @@ -40,7 +41,7 @@ public StoreCache(IStore store)
/// Initializes a new instance of the <see cref="StoreCache"/> class.
/// </summary>
/// <param name="snapshot">An <see cref="IStoreSnapshot"/> to create a snapshot cache.</param>
public StoreCache(IStoreSnapshot snapshot)
public StoreCache(IStoreSnapshot snapshot) : base(false)
{
_store = snapshot;
_snapshot = snapshot;
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public void TestListAddress_WhenWalletNotOpen()
public void TestCancelTransaction()
{
TestUtilOpenWallet();
var snapshot = _neoSystem.GetSnapshot();
var snapshot = _neoSystem.GetSnapshotCache();
var tx = TestUtils.CreateValidTx(snapshot, _wallet, _walletAccount);
snapshot.Commit();
var paramsArray = new JArray(tx.Hash.ToString(), new JArray(_walletAccount.Address));
Expand Down
Loading