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

     1  // Copyright (c) 2019 IoTeX Foundation
     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 trie
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  var (
    15  	// ErrInvalidTrie indicates something wrong causing invalid operation
    16  	ErrInvalidTrie = errors.New("invalid trie operation")
    17  
    18  	// ErrNotExist indicates entry does not exist
    19  	ErrNotExist = errors.New("not exist in trie")
    20  
    21  	// ErrEndOfIterator defines an error which will be returned
    22  	ErrEndOfIterator = errors.New("hit the end of the iterator, no more item")
    23  )
    24  
    25  type (
    26  	// Iterator iterates a trie
    27  	Iterator interface {
    28  		Next() ([]byte, []byte, error)
    29  	}
    30  
    31  	// Trie is the interface of Merkle Patricia Trie
    32  	Trie interface {
    33  		// Start starts the trie and the corresponding dependencies
    34  		Start(context.Context) error
    35  		// Stop stops the trie
    36  		Stop(context.Context) error
    37  		// Upsert inserts a new entry
    38  		Upsert([]byte, []byte) error
    39  		// Get retrieves an existing entry
    40  		Get([]byte) ([]byte, error)
    41  		// Delete deletes an entry
    42  		Delete([]byte) error
    43  		// RootHash returns trie's root hash
    44  		RootHash() ([]byte, error)
    45  		// SetRootHash sets a new root to trie
    46  		SetRootHash([]byte) error
    47  		// IsEmpty returns true is this is an empty trie
    48  		IsEmpty() bool
    49  		// Clone clones a trie with a new kvstore
    50  		Clone(KVStore) (Trie, error)
    51  	}
    52  	// TwoLayerTrie is a trie data structure with two layers
    53  	TwoLayerTrie interface {
    54  		// Start starts the layer one trie
    55  		Start(context.Context) error
    56  		// Stop stops the layer one trie
    57  		Stop(context.Context) error
    58  		// RootHash returns the layer one trie root
    59  		RootHash() ([]byte, error)
    60  		// SetRootHash sets root hash for layer one trie
    61  		SetRootHash([]byte) error
    62  		// Get returns the value in layer two
    63  		Get([]byte, []byte) ([]byte, error)
    64  		// Upsert upserts an item in layer two
    65  		Upsert([]byte, []byte, []byte) error
    66  		// Delete deletes an item in layer two
    67  		Delete([]byte, []byte) error
    68  	}
    69  )