Skip to content

Commit 4183b7c

Browse files
committed
wip
1 parent 8e54670 commit 4183b7c

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

src/Libplanet.Store/Trie/NodeExtensions.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ public static IEnumerable<INode> SelfAndDescendants(this INode @this)
1010
{
1111
yield return @this;
1212

13-
foreach (var child in @this.Children)
13+
foreach (var child in GetChildren(@this))
1414
{
15-
foreach (var descendant in child.SelfAndDescendants())
16-
{
17-
yield return descendant;
18-
}
15+
yield return child;
1916
}
2017
}
2118

@@ -43,6 +40,13 @@ private static IEnumerable<KeyValuePair<KeyBytes, IValue>> GetNodes(INode node,
4340
{
4441
if (node is FullNode fullNode)
4542
{
43+
if (fullNode.Value is ValueNode valueNode)
44+
{
45+
var key = nibbles.ToKeyBytes();
46+
var value = valueNode.Value;
47+
yield return new(key, value);
48+
}
49+
4650
foreach (var (key, value) in fullNode.Children)
4751
{
4852
var nodeNibble = nibbles.Append(key);
@@ -74,6 +78,7 @@ private static IEnumerable<INode> GetChildren(INode node)
7478
{
7579
foreach (var child in node.Children)
7680
{
81+
yield return child;
7782
foreach (var item in GetChildren(child))
7883
{
7984
yield return item;

src/Libplanet.Store/Trie/Nodes/FullNode.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ public sealed record class FullNode(ImmutableDictionary<byte, INode> Children, I
1616

1717
public ImmutableDictionary<byte, INode> Children { get; } = ValidateChildren(Children);
1818

19-
IEnumerable<INode> INode.Children => Children.Values;
19+
IEnumerable<INode> INode.Children
20+
{
21+
get
22+
{
23+
if (Value is not null)
24+
{
25+
yield return Value;
26+
}
27+
28+
foreach (var child in Children)
29+
{
30+
yield return child.Value;
31+
}
32+
}
33+
}
2034

2135
public INode? GetChild(byte index)
2236
{

src/Libplanet.Store/Trie/TrieExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using Bencodex.Types;
4-
using Libplanet.Store.Trie.Nodes;
54

65
namespace Libplanet.Store.Trie;
76

test/Libplanet.Tests/Store/Trie/MerkleTrieTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void IterateSubTrie(bool commit, string extraKey)
106106

107107
trie = trie.Set(extraKey, new Text(extraKey));
108108
trie = commit ? stateStore.Commit(trie) : trie;
109-
Assert.Equal(3, trie.GetNode(prefixKey).Descendants().Count());
109+
Assert.Equal(3, trie.GetNode(prefixKey).SelfAndDescendants().OfType<ValueNode>().Count());
110110
Assert.Equal(3, trie.GetNode(prefixKey).KeyValues().Count());
111111
}
112112

0 commit comments

Comments
 (0)