Beispiel #1
0
        private ulong ModifyInternalNode(ulong id, InternalNode node, byte h, ulong value, byte[]?valueHash)
        {
            if (value == 0 && node.GetChildByHash(h) != 0 && node.Children.Count() == 2)
            {
                // we have to handle case when one of two children is deleted and internal node is folded to leaf
                var secondChild = node.Children.First(child => child != node.GetChildByHash(h));
                // fold only if secondChild is also a leaf
                if (GetNodeById(secondChild).Type == NodeType.Leaf)
                {
                    return(secondChild);
                }
            }

            if (value != 0 && node.GetChildByHash(h) != 0 && node.Children.Count() == 1 && GetNodeById(value).Type == NodeType.Leaf)
            {
                return(value);
            }

            var modified = InternalNode.ModifyChildren(
                node, h, value,
                node.Children.Select(id => GetNodeById(id)?.Hash ?? throw new InvalidOperationException()),
                valueHash
                );

            if (modified == null)
            {
                return(0u);
            }
            var newId = _versionFactory.NewVersion();

            _nodeCache[newId] = modified;
            return(newId);
        }