github.com/iotexproject/iotex-core@v1.14.1-rc1/db/trie/mptrie/hashnode.go (about)

     1  // Copyright (c) 2020 IoTeX
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package mptrie
     7  
     8  type hashNode struct {
     9  	node
    10  	hashVal []byte
    11  }
    12  
    13  func newHashNode(ha []byte) *hashNode {
    14  	return &hashNode{hashVal: ha}
    15  }
    16  
    17  func (h *hashNode) Flush(_ client) error {
    18  	return nil
    19  }
    20  
    21  func (h *hashNode) Delete(cli client, key keyType, offset uint8) (node, error) {
    22  	n, err := h.loadNode(cli)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	return n.Delete(cli, key, offset)
    28  }
    29  
    30  func (h *hashNode) Upsert(cli client, key keyType, offset uint8, value []byte) (node, error) {
    31  	n, err := h.loadNode(cli)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return n.Upsert(cli, key, offset, value)
    37  }
    38  
    39  func (h *hashNode) Search(cli client, key keyType, offset uint8) (node, error) {
    40  	node, err := h.loadNode(cli)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return node.Search(cli, key, offset)
    46  }
    47  
    48  func (h *hashNode) LoadNode(cli client) (node, error) {
    49  	return h.loadNode(cli)
    50  }
    51  
    52  func (h *hashNode) loadNode(cli client) (node, error) {
    53  	return cli.loadNode(h.hashVal)
    54  }
    55  
    56  func (h *hashNode) Hash(_ client) ([]byte, error) {
    57  	return h.hashVal, nil
    58  }