Documentation ¶
Overview ¶
Package node defines MKVS tree nodes.
Index ¶
- Constants
- Variables
- func ToMapKey(k []byte) string
- type Depth
- type InternalNode
- func (n *InternalNode) CompactMarshalBinary() (data []byte, err error)
- func (n *InternalNode) Equal(other Node) bool
- func (n *InternalNode) Extract() Node
- func (n *InternalNode) ExtractUnchecked() Node
- func (n *InternalNode) GetHash() hash.Hash
- func (n *InternalNode) IsClean() bool
- func (n *InternalNode) MarshalBinary() (data []byte, err error)
- func (n *InternalNode) Size() uint64
- func (n *InternalNode) SizedUnmarshalBinary(data []byte) (int, error)
- func (n *InternalNode) UnmarshalBinary(data []byte) error
- func (n *InternalNode) UpdateHash()
- type Key
- func (k Key) AppendBit(keyLen Depth, val bool) Key
- func (k Key) BitLength() Depth
- func (k Key) CommonPrefixLen(keyBitLen Depth, k2 Key, k2bitLen Depth) (bitLength Depth)
- func (k Key) Compare(other Key) int
- func (k Key) Equal(other Key) bool
- func (k Key) GetBit(bit Depth) bool
- func (k Key) MarshalBinary() (data []byte, err error)
- func (k Key) Merge(keyLen Depth, k2 Key, k2Len Depth) Key
- func (k Key) SetBit(bit Depth, val bool) Key
- func (k *Key) SizedUnmarshalBinary(data []byte) (int, error)
- func (k Key) Split(splitPoint, keyLen Depth) (prefix, suffix Key)
- func (k Key) String() string
- func (k *Key) UnmarshalBinary(data []byte) error
- type LeafNode
- func (n *LeafNode) CompactMarshalBinary() (data []byte, err error)
- func (n *LeafNode) Equal(other Node) bool
- func (n *LeafNode) Extract() Node
- func (n *LeafNode) ExtractUnchecked() Node
- func (n *LeafNode) GetHash() hash.Hash
- func (n *LeafNode) IsClean() bool
- func (n *LeafNode) MarshalBinary() ([]byte, error)
- func (n *LeafNode) Size() uint64
- func (n *LeafNode) SizedUnmarshalBinary(data []byte) (int, error)
- func (n *LeafNode) UnmarshalBinary(data []byte) error
- func (n *LeafNode) UpdateHash()
- type Node
- type Pointer
- func (p *Pointer) Equal(other *Pointer) bool
- func (p *Pointer) Extract() *Pointer
- func (p *Pointer) ExtractUnchecked() *Pointer
- func (p *Pointer) ExtractWithNode() *Pointer
- func (p *Pointer) ExtractWithNodeUnchecked() *Pointer
- func (p *Pointer) GetHash() hash.Hash
- func (p *Pointer) IsClean() bool
- func (p *Pointer) Size() uint64
- type Root
- type RootType
Constants ¶
const ( // PrefixLeafNode is the prefix used in hash computations of leaf nodes. PrefixLeafNode byte = 0x00 // PrefixInternalNode is the prefix used in hash computations of internal nodes. PrefixInternalNode byte = 0x01 // PrefixNilNode is the prefix used to mark a nil pointer in a subtree serialization. PrefixNilNode byte = 0x02 // PointerSize is the size of a node pointer in memory. PointerSize = uint64(unsafe.Sizeof(Pointer{})) // InternalNodeSize is the minimum size of an internal node in memory. InternalNodeSize = uint64(unsafe.Sizeof(InternalNode{})) // LeafNodeSize is the minimum size of a leaf node in memory. LeafNodeSize = uint64(unsafe.Sizeof(LeafNode{})) // ValueLengthSize is the size of the encoded value length. ValueLengthSize = int(unsafe.Sizeof(uint32(0))) )
const DepthSize = int(unsafe.Sizeof(Depth(0)))
DepthSize is the size of Depth in bytes.
Variables ¶
var ( // ErrMalformedNode is the error when a malformed node is encountered // during deserialization. ErrMalformedNode = errors.New("mkvs: malformed node") // ErrMalformedKey is the error when a malformed key is encountered // during deserialization. ErrMalformedKey = errors.New("mkvs: malformed key") )
Functions ¶
Types ¶
type Depth ¶
type Depth uint16
Depth determines the maximum length of the key in bits.
maxKeyLengthInBits = 2^size_of(Depth)*8
func (Depth) MarshalBinary ¶
MarshalBinary encodes a Depth into binary form.
type InternalNode ¶
type InternalNode struct { Hash hash.Hash // Label is the label on the incoming edge. Label Key // LabelBitLength is the length of the label in bits. LabelBitLength Depth Clean bool // LeafNode is for the key ending at this depth. LeafNode *Pointer Left *Pointer Right *Pointer }
InternalNode is an internal node with two children and possibly a leaf.
Note that Label and LabelBitLength can only be empty iff the internal node is the root of the tree.
func (*InternalNode) CompactMarshalBinary ¶
func (n *InternalNode) CompactMarshalBinary() (data []byte, err error)
CompactMarshalBinary encodes an internal node into binary form without any hash pointers (e.g., for proofs).
func (*InternalNode) Equal ¶
func (n *InternalNode) Equal(other Node) bool
Equal compares a node with some other node.
func (*InternalNode) Extract ¶
func (n *InternalNode) Extract() Node
Extract makes a copy of the node containing only hash references.
For LeafNode, it makes a deep copy so that the parent internal node always ships it since we cannot address the LeafNode uniquely with NodeID (both the internal node and LeafNode have the same path and bit depth).
func (*InternalNode) ExtractUnchecked ¶
func (n *InternalNode) ExtractUnchecked() Node
ExtractUnchecked makes a copy of the node containing only hash references without checking the dirty flag.
For LeafNode, it makes a deep copy so that the parent internal node always ships it since we cannot address the LeafNode uniquely with NodeID (both the internal node and LeafNode have the same path and bit depth).
func (*InternalNode) GetHash ¶
func (n *InternalNode) GetHash() hash.Hash
GetHash returns the node's cached hash.
func (*InternalNode) IsClean ¶
func (n *InternalNode) IsClean() bool
IsClean returns true if the node is non-dirty.
func (*InternalNode) MarshalBinary ¶
func (n *InternalNode) MarshalBinary() (data []byte, err error)
MarshalBinary encodes an internal node into binary form.
func (*InternalNode) Size ¶
func (n *InternalNode) Size() uint64
Size returns the size of this internal node in bytes.
func (*InternalNode) SizedUnmarshalBinary ¶
func (n *InternalNode) SizedUnmarshalBinary(data []byte) (int, error)
SizedUnmarshalBinary decodes a binary marshaled internal node.
func (*InternalNode) UnmarshalBinary ¶
func (n *InternalNode) UnmarshalBinary(data []byte) error
UnmarshalBinary decodes a binary marshaled internal node.
func (*InternalNode) UpdateHash ¶
func (n *InternalNode) UpdateHash()
UpdateHash updates the node's cached hash by recomputing it.
Does not mark the node as clean.
type Key ¶
type Key []byte
Key holds variable-length key.
func (Key) AppendBit ¶
AppendBit appends the given bit to the key.
This function is immutable and returns a new instance of Key.
func (Key) CommonPrefixLen ¶
CommonPrefixLen computes length of common prefix of k and k2.
Additionally, keyBitLen and k2bitLen are key lengths in bits of k and k2 respectively.
func (Key) Compare ¶
Compare compares the key with some other key and returns 0 if both keys are equal, -1 if the the key is smaller and 1 if the key is larger.
func (Key) MarshalBinary ¶
MarshalBinary encodes a key length in bytes + key into binary form.
func (Key) Merge ¶
Merge bit-wise merges key of given length with another key of given length.
keyLen is the length of the original key in bits and k2Len is the length of another key in bits. This function is immutable and returns a new instance of Key.
func (Key) SetBit ¶
SetBit sets the bit at the given position bit to value val.
This function is immutable and returns a new instance of Key
func (*Key) SizedUnmarshalBinary ¶
SizedUnmarshalBinary decodes a binary marshaled key incl. length in bytes.
func (Key) Split ¶
Split performs bit-wise split of the key.
keyLen is the length of the key in bits and splitPoint is the index of the first suffix bit. This function is immutable and returns two new instances of Key.
func (*Key) UnmarshalBinary ¶
UnmarshalBinary decodes a binary marshaled key including the length in bytes.
type LeafNode ¶
LeafNode is a leaf node containing a key/value pair.
func (*LeafNode) CompactMarshalBinary ¶
CompactMarshalBinary encodes a leaf node into binary form.
func (*LeafNode) ExtractUnchecked ¶
ExtractUnchecked makes a copy of the node containing only hash references without checking the dirty flag.
func (*LeafNode) MarshalBinary ¶
MarshalBinary encodes a leaf node into binary form.
func (*LeafNode) SizedUnmarshalBinary ¶
SizedUnmarshalBinary decodes a binary marshaled leaf node.
func (*LeafNode) UnmarshalBinary ¶
UnmarshalBinary decodes a binary marshaled leaf node.
func (*LeafNode) UpdateHash ¶
func (n *LeafNode) UpdateHash()
UpdateHash updates the node's cached hash by recomputing it.
Does not mark the node as clean.
type Node ¶
type Node interface { encoding.BinaryMarshaler encoding.BinaryUnmarshaler // IsClean returns true if the node is non-dirty. IsClean() bool // CompactMarshalBinary encodes a node into binary form without any hash // pointers (e.g., for proofs). CompactMarshalBinary() ([]byte, error) // GetHash returns the node's cached hash. GetHash() hash.Hash // UpdateHash updates the node's cached hash by recomputing it. // // Does not mark the node as clean. UpdateHash() // Extract makes a copy of the node containing only hash references. Extract() Node // ExtractUnchecked makes a copy of the node containing only hash // references without checking the dirty flag. ExtractUnchecked() Node // Equal compares a node with another node. Equal(other Node) bool // Size returns the size of this pointer in bytes. Size() uint64 }
Node is either an InternalNode or a LeafNode.
func UnmarshalBinary ¶
UnmarshalBinary unmarshals a node of arbitrary type.
type Pointer ¶
type Pointer struct { Clean bool Hash hash.Hash Node Node LRU *list.Element // DBInternal contains NodeDB-specific internal metadata to aid // pointer resolution. DBInternal interface{} }
Pointer is a pointer to another node.
func (*Pointer) ExtractUnchecked ¶
ExtractUnchecked makes a copy of the pointer containing only hash references without checking the dirty flag.
func (*Pointer) ExtractWithNode ¶
ExtractWithNode makes a copy of the pointer containing hash references and an extracted copy of the node pointed to.
func (*Pointer) ExtractWithNodeUnchecked ¶
ExtractWithNodeUnchecked makes a copy of the pointer containing hash references and an extracted copy of the node pointed to without checking the dirty flag.
type Root ¶
type Root struct { // Namespace is the namespace under which the root is stored. Namespace common.Namespace `json:"ns"` // Version is the monotonically increasing version number in which the root is stored. Version uint64 `json:"version"` // Type is the type of storage this root is used for. Type RootType `json:"root_type"` // Hash is the merkle root hash. Hash hash.Hash `json:"hash"` }
Root is a storage root.
func (*Root) EncodedHash ¶
EncodedHash returns the encoded cryptographic hash of the storage root.
func (*Root) Follows ¶
Follows checks if another root follows the given root. A root follows another iff the namespace matches and the version is either equal or exactly one higher.
It is the responsibility of the caller to check if the merkle roots follow each other.
type RootType ¶ added in v0.2100.0
type RootType uint8
RootType is a storage root type.
const ( // RootTypeInvalid is an invalid/uninitialized root type. RootTypeInvalid RootType = 0 // RootTypeState is the type for state storage roots. RootTypeState RootType = 1 // RootTypeIO is the type for IO storage roots. RootTypeIO RootType = 2 // RootTypeMax is the number of different root types and should be kept at the last one. RootTypeMax RootType = 2 )