예제 #1
0
        public bool Put(byte[] key, byte[] value)
        {
            if (key.Length > ExtensionNode.MaxKeyLength)
            {
                throw new ArgumentOutOfRangeException("key out of mpt limit");
            }
            if (value.Length > LeafNode.MaxValueLength)
            {
                throw new ArgumentOutOfRangeException("value out of mpt limit");
            }
            var path = key.ToNibbles();

            if (value.Length == 0)
            {
                return(TryDelete(ref root, path));
            }
            var n = new LeafNode(value);

            return(Put(ref root, path, n));
        }
예제 #2
0
        public static MPTNode Decode(byte[] data)
        {
            if (data is null || data.Length == 0)
            {
                return(null);
            }

            MPTNode node;

            using (BinaryReader reader = new BinaryReader(new MemoryStream(data, false), Encoding.UTF8))
            {
                var nodeType = (NodeType)reader.ReadByte();
                switch (nodeType)
                {
                case NodeType.BranchNode:
                {
                    node = new BranchNode();
                    break;
                }

                case NodeType.ExtensionNode:
                {
                    node = new ExtensionNode();
                    break;
                }

                case NodeType.LeafNode:
                {
                    node = new LeafNode();
                    break;
                }

                default:
                    throw new System.InvalidOperationException();
                }

                node.DecodeSpecific(reader);
            }
            return(node);
        }