Documentation
¶
Overview ¶
Package tree implements a tree serialiser and reader.
Example ¶
package main import ( "bytes" "fmt" "vimagination.zapto.org/tree" ) func main() { var ( buf, readBuf bytes.Buffer root tree.Branch branch tree.Branch ) root.Add("child1", tree.Leaf([]byte("Hello"))) root.Add("child2", tree.Leaf([]byte("World"))) root.Add("branch1", &branch) branch.Add("childA", tree.Leaf([]byte("Foo"))) branch.Add("childB", tree.Leaf([]byte("Bar"))) tree.Serialise(&buf, root) t := tree.OpenAt(bytes.NewReader(buf.Bytes()), int64(buf.Len())) child1, _ := t.Child("child1") child1.WriteTo(&readBuf) fmt.Printf("child 1 data: %q\n", readBuf.Bytes()) readBuf.Reset() branch1, _ := t.Child("branch1") childB, _ := branch1.Child("childB") childB.WriteTo(&readBuf) fmt.Printf("child B data: %q\n", readBuf.Bytes()) }
Output: child 1 data: "Hello" child B data: "Bar"
Index ¶
- func Serialise(w io.Writer, root Node) error
- type Branch
- type ChildNotFoundError
- type ChildrenError
- type DuplicateChildError
- type Leaf
- type MemTree
- func (m *MemTree) Child(name string) (*MemTree, error)
- func (m *MemTree) ChildNames() iter.Seq[string]
- func (m *MemTree) Children() iter.Seq2[string, Node]
- func (m *MemTree) Data() []byte
- func (m *MemTree) DataLen() int64
- func (m *MemTree) NumChildren() int
- func (m *MemTree) WriteTo(w io.Writer) (int64, error)
- type Node
- type OffsetReaderAt
- type OffsetWriter
- type Roots
- type Tree
- type TreeCloser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Serialise ¶
Serialise writes a tree structure to the given writer.
The byte-format for each node is as follows:
Names []string (stored in lexical order) Pointers []int64 (pointer to the end (&Size + 1) of each child node record, stored as variable-length integers; length of pointer stored in NameSizes) NameSizes []uint64 (lengths of each name and pointer, stored as variable-length integers; bottom three bits are the length of the pointer - 1, remaining bits are name length) Data []byte Sizes []uint64 (size of NamesSizes and Data sections, stored as variable-length integers; zeros are omitted) Size uint8 (lower 5 bits: size of the Sizes field, bit 6: size Data > 0, bit 7: size NameSizes > 0)
NB: All slices are stored without separators.
If the given Writer implements the io.Seeker interface it will be used to determine the current writer position, and offset all pointer accordingly.
The OffsetWriter type can be used to wrap an io.Writer to provide a custom offset, or to have Serialise ignore the underlying Writers position.
Types ¶
type Branch ¶ added in v1.2.0
type Branch []nameNode
Branch is a collection of named Nodes.
func (*Branch) Add ¶ added in v1.2.0
Add adds a named Node to the branch.
No locking takes place, so all children should be added before using the Branch Node.
func (Branch) Child ¶ added in v1.2.0
Child attempts to retrieve a child Node corresponding to the given name.
If no child matches the given name, the returned error will be of type ChildNotFoundError.
func (Branch) Children ¶ added in v1.2.0
Children returns an iterator that loops through all of the child Nodes.
func (Branch) NumChildren ¶ added in v1.2.0
NumChildren returns the number of child Nodes that are attached to this Node.
type ChildNotFoundError ¶
type ChildNotFoundError string
ChildNotFoundError contains the name of the child that could not be found.
func (ChildNotFoundError) Error ¶
func (c ChildNotFoundError) Error() string
Error implements the error interface.
type ChildrenError ¶
type ChildrenError struct {
// contains filtered or unexported fields
}
ChildrenError is a Node and error type that is returned from the Children iterator.
It has no children and any attempt to retrieve the data will result in the underlying error to be returned.
func NewChildrenError ¶ added in v1.1.0
func NewChildrenError(err error) ChildrenError
NewChildrenError wraps an error to give it the methods of a Node.
func (ChildrenError) Children ¶
func (ChildrenError) Children() iter.Seq2[string, Node]
Children always returns an empty iterator.
func (ChildrenError) Unwrap ¶ added in v1.0.1
func (c ChildrenError) Unwrap() error
Unwrap returns the wrapped error.
type DuplicateChildError ¶
type DuplicateChildError []string
DuplicateChildError is an error that records the duplicated child name.
func (DuplicateChildError) Error ¶
func (d DuplicateChildError) Error() string
Error implements the error interface.
type Leaf ¶ added in v1.2.0
type Leaf []byte
Leaf represents a childless Node that contains only data.
The Leaf itself is a byte-slice.
func (Leaf) Child ¶ added in v1.2.0
Child will always return nil with a ChildNotFoundError error for a Leaf Node.
func (Leaf) NumChildren ¶ added in v1.2.0
NumChildren will always return 0 for a Leaf Node.
type MemTree ¶
type MemTree struct {
// contains filtered or unexported fields
}
MemTree represents a tree backed by an in-memory byte slice.
func OpenMemAt ¶
OpenMemAt opens a Tree from the given byte slice, using the given Node pointer instead of using the length of the data.
func (*MemTree) Child ¶
Child attempts to retrieve a child Node corresponding to the given name.
If no child matches the given name, the returned error will be of type ChildNotFoundError.
func (*MemTree) ChildNames ¶ added in v1.3.0
ChildNames returns an iterator that loops through the names of the child Nodes.
func (*MemTree) Children ¶
Children returns an iterator that loops through all of the child Nodes.
Read errors will be expressed with a final Node of underlying type ChildrenError.
func (*MemTree) DataLen ¶ added in v1.1.0
DataLen returns the length of the data stored on this Node.
func (*MemTree) NumChildren ¶ added in v1.1.0
NumChildren returns the number of child Nodes that are attached to this Node.
type Node ¶
type Node interface { // Children returns an iterator that yields a (unique) name and Node for each // of the child nodes. // // Yielding the children in a lexically sorted order is recommended, // but not required. // // If an error occurs, the Node may be of type ChildrenError, which in // addition to being a Node also implements the error interface. Children() iter.Seq2[string, Node] // WriterTo accepts an io.Writer to which any data stored on the node will be // passed. io.WriterTo }
Node represents a single node in a Tree.
type OffsetReaderAt ¶ added in v1.3.1
OffsetReaderAt is a wrapper around the io.ReaderAt interface that will shift the read position by the set Offset.
type OffsetWriter ¶ added in v1.3.1
OffsetWriter is a wrapper around io.Writer that provides an io.Seeker interface that can be used by Serialise to offset the pointers in the tree.
type Roots ¶ added in v1.2.0
type Roots []multiNode
func Merge ¶ added in v1.2.0
Merge combines the children from multiple nodes, merging same named children similarly.
Changes made to the Nodes after merging will not be recognised.
func (Roots) Child ¶ added in v1.2.0
Child attempts to retrieve a child Node corresponding to the given name.
If no child matches the given name, the returned error will be of type ChildNotFoundError.
func (Roots) Children ¶ added in v1.2.0
Children returns an iterator that loops through all of the child Nodes.
Any errors will be expressed with a final Node of underlying type ChildrenError.
func (Roots) NumChildren ¶ added in v1.2.0
NumChildren returns the number of child Nodes that are attached to this Node.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree represents a Node of a tree backed by an io.ReaderAt.
func OpenAt ¶
OpenAt opens a Tree from the given io.ReaderAt.
The pos should be the length of the data underlying the io.ReaderAt, or a specific Node pointer address within the data.
func (*Tree) Child ¶
Child attempts to retrieve a child Node corresponding to the given name.
If no child matches the given name, the returned error will be of type ChildNotFoundError.
func (*Tree) Children ¶
Children returns an iterator that loops through all of the child Nodes.
Read errors will be expressed with a final Node of underlying type ChildrenError.
func (*Tree) NumChildren ¶ added in v1.1.0
NumChildren returns the number of child Nodes that are attached to this Node.
type TreeCloser ¶
TreeCloser is a tree that includes a Close method for an opened file.
func OpenFile ¶
func OpenFile(path string) (*TreeCloser, error)
OpenFile opens a Tree from the given filename.