Skip to content
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

Fix DataCache.Find #1673

Merged
merged 7 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/neo/IO/Caching/DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public void Delete(TKey key)
public IEnumerable<(TKey Key, TValue Value)> Find(byte[] key_prefix = null)
{
IEnumerable<(byte[], TKey, TValue)> cached;
HashSet<TKey> cachedKeySet;
lock (dictionary)
{
cached = dictionary
Expand All @@ -164,9 +165,10 @@ public void Delete(TKey key)
))
.OrderBy(p => p.KeyBytes, ByteArrayComparer.Default)
.ToArray();
cachedKeySet = new HashSet<TKey>(dictionary.Keys);
}
var uncached = FindInternal(key_prefix ?? Array.Empty<byte>())
.Where(p => !dictionary.ContainsKey(p.Key))
.Where(p => !cachedKeySet.Contains(p.Key))
.Select(p =>
(
KeyBytes: p.Key.ToArray(),
Expand Down
25 changes: 25 additions & 0 deletions tests/neo.UnitTests/IO/Caching/UT_DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,5 +326,30 @@ public void TestTryGet()
myDataCache.TryGet(new MyKey("key2")).Should().Be(new MyValue("value2"));
myDataCache.TryGet(new MyKey("key3")).Should().BeNull();
}

[TestMethod]
public void TestFindInvalid()
{
var myDataCache = new MyDataCache<MyKey, MyValue>();
myDataCache.Add(new MyKey("key1"), new MyValue("value1"));

myDataCache.InnerDict.Add(new MyKey("key2"), new MyValue("value2"));
myDataCache.InnerDict.Add(new MyKey("key3"), new MyValue("value3"));
myDataCache.InnerDict.Add(new MyKey("key4"), new MyValue("value3"));

var items = myDataCache.Find().GetEnumerator();
items.MoveNext().Should().Be(true);
items.Current.Key.Should().Be(new MyKey("key1"));

myDataCache.TryGet(new MyKey("key3")); // GETLINE

items.MoveNext().Should().Be(true);
items.Current.Key.Should().Be(new MyKey("key2"));
items.MoveNext().Should().Be(true);
items.Current.Key.Should().Be(new MyKey("key3"));
items.MoveNext().Should().Be(true);
items.Current.Key.Should().Be(new MyKey("key4"));
items.MoveNext().Should().Be(false);
}
}
}