public bool GetIdByHash(string nodeHash, out ulong id) { id = 0; if (nodeHash.Equals(EmptyHash)) { return(true); } if (_idCache.TryGetValue(nodeHash, out id)) { return(true); } var idByte = _dbContext.Get(EntryPrefix.VersionByHash.BuildPrefix(HexUtils.HexToBytes(nodeHash))); if (!(idByte is null)) { id = UInt64Utils.FromBytes(idByte); return(true); } id = _versionFactory.NewVersion(); _idCache[nodeHash] = id; if (_idCache.Count >= _idCacheCapacity) { CommitIds(); } return(false); }
public ulong InsertAllNodes(ulong root, IDictionary <ulong, IHashTrieNode> allTrieNodes) { if (root == 0) { return(0); } if (allTrieNodes.TryGetValue(root, out var node)) { switch (node) { case InternalNode internalNode: List <byte[]> childrenHash = new List <byte[]>(); List <ulong> children = new List <ulong>(); var childrenMask = internalNode.ChildrenMask; foreach (var child in internalNode.Children) { ulong childRoot = InsertAllNodes(child, allTrieNodes); var childNode = GetNodeById(childRoot); children.Add(childRoot); childrenHash.Add(childNode.Hash); } var newInternalNodeId = _versionFactory.NewVersion(); _nodeCache[newInternalNodeId] = new InternalNode(childrenMask, children, childrenHash); return(newInternalNodeId); case LeafNode leafNode: var newLeafNodeId = _versionFactory.NewVersion(); _nodeCache[newLeafNodeId] = leafNode; return(newLeafNodeId); } } else { throw new InvalidOperationException(); } return(0); }