github.com/beyonderyue/gochain@v2.2.26+incompatible/light/nodeset.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package light
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/gochain-io/gochain/common"
    23  	"github.com/gochain-io/gochain/crypto"
    24  	"github.com/gochain-io/gochain/log"
    25  	"github.com/gochain-io/gochain/rlp"
    26  )
    27  
    28  // NodeSet stores a set of trie nodes. It implements trie.Database and can also
    29  // act as a cache for another trie.Database.
    30  type NodeSet struct {
    31  	nodes map[string][]byte
    32  	order []string
    33  
    34  	dataSize int
    35  	lock     sync.RWMutex
    36  }
    37  
    38  // NewNodeSet creates an empty node set
    39  func NewNodeSet() *NodeSet {
    40  	return &NodeSet{
    41  		nodes: make(map[string][]byte),
    42  	}
    43  }
    44  
    45  // Put stores a new node in the set
    46  func (db *NodeSet) Put(key []byte, value []byte) error {
    47  	db.lock.Lock()
    48  	defer db.lock.Unlock()
    49  
    50  	if _, ok := db.nodes[string(key)]; ok {
    51  		return nil
    52  	}
    53  	keystr := string(key)
    54  
    55  	db.nodes[keystr] = common.CopyBytes(value)
    56  	db.order = append(db.order, keystr)
    57  	db.dataSize += len(value)
    58  
    59  	return nil
    60  }
    61  
    62  // Get returns a stored node
    63  func (db *NodeSet) Get(key []byte) ([]byte, error) {
    64  	db.lock.RLock()
    65  	defer db.lock.RUnlock()
    66  
    67  	if entry, ok := db.nodes[string(key)]; ok {
    68  		return entry, nil
    69  	}
    70  	return nil, common.ErrNotFound
    71  }
    72  
    73  // Has returns true if the node set contains the given key
    74  func (db *NodeSet) Has(key []byte) (bool, error) {
    75  	_, err := db.Get(key)
    76  	return err == nil, nil
    77  }
    78  
    79  // KeyCount returns the number of nodes in the set
    80  func (db *NodeSet) KeyCount() int {
    81  	db.lock.RLock()
    82  	defer db.lock.RUnlock()
    83  
    84  	return len(db.nodes)
    85  }
    86  
    87  // DataSize returns the aggregated data size of nodes in the set
    88  func (db *NodeSet) DataSize() int {
    89  	db.lock.RLock()
    90  	defer db.lock.RUnlock()
    91  
    92  	return db.dataSize
    93  }
    94  
    95  // NodeList converts the node set to a NodeList
    96  func (db *NodeSet) NodeList() NodeList {
    97  	db.lock.RLock()
    98  	defer db.lock.RUnlock()
    99  
   100  	var values NodeList
   101  	for _, key := range db.order {
   102  		values = append(values, db.nodes[key])
   103  	}
   104  	return values
   105  }
   106  
   107  // Store writes the contents of the set to the given database
   108  func (db *NodeSet) Store(target common.Putter) {
   109  	db.lock.RLock()
   110  	defer db.lock.RUnlock()
   111  
   112  	for key, value := range db.nodes {
   113  		if err := target.Put([]byte(key), value); err != nil {
   114  			log.Error("Cannot write node set", "err", err)
   115  		}
   116  	}
   117  }
   118  
   119  // NodeList stores an ordered list of trie nodes. It implements common.Putter.
   120  type NodeList []rlp.RawValue
   121  
   122  // Store writes the contents of the list to the given database
   123  func (n NodeList) Store(db common.Putter) {
   124  	for _, node := range n {
   125  		if err := db.Put(crypto.Keccak256(node), node); err != nil {
   126  			log.Error("Cannot write node list", "err", err)
   127  		}
   128  	}
   129  }
   130  
   131  // NodeSet converts the node list to a NodeSet
   132  func (n NodeList) NodeSet() *NodeSet {
   133  	db := NewNodeSet()
   134  	n.Store(db)
   135  	return db
   136  }
   137  
   138  // Put stores a new node at the end of the list
   139  func (n *NodeList) Put(key []byte, value []byte) error {
   140  	*n = append(*n, value)
   141  	return nil
   142  }
   143  
   144  // DataSize returns the aggregated data size of nodes in the list
   145  func (n NodeList) DataSize() int {
   146  	var size int
   147  	for _, node := range n {
   148  		size += len(node)
   149  	}
   150  	return size
   151  }