node

package
v0.0.0-...-c69f244 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2022 License: LGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ChildrenCapacity is the maximum number of children in a branch node.
	ChildrenCapacity = 16
)

Variables

View Source
var (
	// DefaultCopySettings contains the following copy settings:
	// - children are NOT deep copied recursively
	// - the HashDigest field is left empty on the copy
	// - the Encoding field is left empty on the copy
	// - the key field is deep copied
	// - the value field is deep copied
	DefaultCopySettings = CopySettings{
		CopyKey:   true,
		CopyValue: true,
	}

	// DeepCopySettings returns the following copy settings:
	// - children are deep copied recursively
	// - the HashDigest field is deep copied
	// - the Encoding field is deep copied
	// - the key field is deep copied
	// - the value field is deep copied
	DeepCopySettings = CopySettings{
		CopyChildren: true,
		CopyCached:   true,
		CopyKey:      true,
		CopyValue:    true,
	}
)
View Source
var (
	ErrReadHeaderByte     = errors.New("cannot read header byte")
	ErrUnknownNodeType    = errors.New("unknown node type")
	ErrDecodeValue        = errors.New("cannot decode value")
	ErrReadChildrenBitmap = errors.New("cannot read children bitmap")
	ErrDecodeChildHash    = errors.New("cannot decode child hash")
)
View Source
var (
	ErrPartialKeyTooBig = errors.New("partial key length cannot be larger than or equal to 2^16")
	ErrReadKeyLength    = errors.New("cannot read key length")
	ErrReadKeyData      = errors.New("cannot read key data")
)

Functions

This section is empty.

Types

type Buffer

type Buffer interface {
	io.Writer
	Len() int
	Bytes() []byte
}

Buffer is an interface with some methods of *bytes.Buffer.

type CopySettings

type CopySettings struct {
	// CopyChildren can be set to true to recursively deep copy the eventual
	// children of the node. This is false by default and should only be used
	// in tests since it is quite inefficient.
	CopyChildren bool
	// CopyCached can be set to true to deep copy the cached digest
	// and encoding fields on the current node copied.
	// This is false by default because in production, a node is copied
	// when it is about to be mutated, hence making its cached fields
	// no longer valid.
	CopyCached bool
	// CopyKey can be set to true to deep copy the key field of
	// the node. This is useful when false if the key is about to
	// be assigned after the Copy operation, to save a memory operation.
	CopyKey bool
	// CopyValue can be set to true to deep copy the value field of
	// the node. This is useful when false if the value is about to
	// be assigned after the Copy operation, to save a memory operation.
	CopyValue bool
}

CopySettings contains settings to configure the deep copy of a node.

type Node

type Node struct {
	// Key is the partial key bytes in nibbles (0 to f in hexadecimal)
	Key   []byte
	Value []byte
	// Generation is incremented on every trie Snapshot() call.
	// Each node also contain a certain Generation number,
	// which is updated to match the trie Generation once they are
	// inserted, moved or iterated over.
	Generation uint64
	// Children is a slice of length 16 for branches.
	// It is left to nil for leaves to reduce memory usage.
	Children []*Node

	// Dirty is true when the branch differs
	// from the node stored in the database.
	Dirty bool
	// HashDigest is the cached hash digest of the
	// node encoding.
	HashDigest []byte
	// Encoding is the cached encoding of the node.
	Encoding []byte

	// Descendants is the number of descendant nodes for
	// this particular node.
	Descendants uint32
}

Node is a node in the trie and can be a leaf or a branch.

func Decode

func Decode(reader io.Reader) (n *Node, err error)

Decode decodes a node from a reader. For branch decoding, see the comments on decodeBranch. For leaf decoding, see the comments on decodeLeaf.

func (*Node) ChildrenBitmap

func (n *Node) ChildrenBitmap() (bitmap uint16)

ChildrenBitmap returns the 16 bit bitmap of the children in the branch node.

func (*Node) Copy

func (n *Node) Copy(settings CopySettings) *Node

Copy deep copies the node. Setting copyChildren to true will deep copy children as well.

func (*Node) Encode

func (n *Node) Encode(buffer Buffer) (err error)

Encode encodes the node to the buffer given. The encoding format is documented in encode_doc.go.

func (*Node) EncodeAndHash

func (n *Node) EncodeAndHash(isRoot bool) (encoding, hash []byte, err error)

EncodeAndHash returns the encoding of the node and the blake2b hash digest of the encoding of the node. If the encoding is less than 32 bytes, the hash returned is the encoding and not the hash of the encoding.

func (*Node) NumChildren

func (n *Node) NumChildren() (count int)

NumChildren returns the total number of children in the branch node.

func (*Node) SetDirty

func (n *Node) SetDirty(dirty bool)

SetDirty sets the dirty status to the node.

func (*Node) String

func (n *Node) String() string

func (Node) StringNode

func (n Node) StringNode() (stringNode *gotree.Node)

StringNode returns a gotree compatible node for String methods.

func (*Node) Type

func (n *Node) Type() Type

Type returns Leaf or Branch depending on what type the node is.

type Type

type Type byte

Type is the type of the node.

const (
	// Leaf type for leaf nodes.
	Leaf Type = iota
	// Branch type for branches (with or without value).
	Branch
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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