trie

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidTrie indicates something wrong causing invalid operation
	ErrInvalidTrie = errors.New("invalid trie operation")

	// ErrNotExist indicates entry does not exist
	ErrNotExist = errors.New("not exist in trie")
)
View Source
var ErrEndOfIterator = errors.New("hit the end of the iterator, no more item")

ErrEndOfIterator defines an error which will be returned

Functions

func DefaultHashFunc

func DefaultHashFunc(data []byte) []byte

DefaultHashFunc implements a default hash function

Types

type HashFunc

type HashFunc func([]byte) []byte

HashFunc defines a function to generate the hash which will be used as key in db

type Iterator

type Iterator interface {
	Next() ([]byte, []byte, error)
}

Iterator iterates a trie

func NewLeafIterator

func NewLeafIterator(tr Trie) (Iterator, error)

NewLeafIterator returns a new leaf iterator

type KVStore

type KVStore interface {
	// Start starts the KVStore
	Start(context.Context) error
	// Stop stops the KVStore
	Stop(context.Context) error
	// Put puts key, value pair into KVStore
	Put([]byte, []byte) error
	// Delete deletes record from KVStore by key
	Delete([]byte) error
	// Get gets the value from KVStore by key
	Get([]byte) ([]byte, error)
}

KVStore defines an interface for storing trie data as key-value pair

type LeafIterator

type LeafIterator struct {
	// contains filtered or unexported fields
}

LeafIterator defines an iterator to go through all the leaves under given node

func (*LeafIterator) Next

func (li *LeafIterator) Next() ([]byte, []byte, error)

Next moves iterator to next node

type Node

type Node interface {
	// Type returns the type of a node
	Type() NodeType
	// Key returns the key of a node, only leaf has key
	Key() []byte
	// Value returns the value of a node, only leaf has value
	Value() []byte
	// contains filtered or unexported methods
}

Node defines the interface of a trie node Note: all the key-value pairs should be of the same length of keys

type NodeType

type NodeType int

NodeType is the type of a trie node

const (
	// BRANCH is an internal node type of length 1 and multiple child
	BRANCH NodeType = iota + 1
	// LEAF is a leaf node with value
	LEAF
	// EXTENSION is a spefic type of branch with only one child
	EXTENSION
)

type Option

type Option func(Trie) error

Option sets parameters for SameKeyLenTrieContext construction parameter

func HashFuncOption

func HashFuncOption(hashFunc HashFunc) Option

HashFuncOption sets the hash func for the trie

func KVStoreOption

func KVStoreOption(kvStore KVStore) Option

KVStoreOption sets the kvstore for the trie

func KeyLengthOption

func KeyLengthOption(len int) Option

KeyLengthOption sets the length of the keys saved in trie

func RootHashOption

func RootHashOption(h []byte) Option

RootHashOption sets the root hash for the trie

func RootKeyOption

func RootKeyOption(key string) Option

RootKeyOption sets the root key for the trie

type Trie

type Trie interface {
	// Start starts the trie and the corresponding dependencies
	Start(context.Context) error
	// Stop stops the trie
	Stop(context.Context) error
	// Upsert inserts a new entry
	Upsert([]byte, []byte) error
	// Get retrieves an existing entry
	Get([]byte) ([]byte, error)
	// Delete deletes an entry
	Delete([]byte) error
	// RootHash returns trie's root hash
	RootHash() []byte
	// SetRootHash sets a new root to trie
	SetRootHash([]byte) error
	// DB returns the KVStore storing the node data
	DB() KVStore
	// contains filtered or unexported methods
}

Trie is the interface of Merkle Patricia Trie

func NewTrie

func NewTrie(options ...Option) (Trie, error)

NewTrie creates a trie with DB filename

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL