Documentation ¶
Overview ¶
Package ctree implements a Tree container whose methods are all thread-safe allowing concurrent access for multiple goroutines. This container was designed to support concurrent reads using sync.RWLock and are optimized for situations where reads and updates to previously Added values are dominant. The acquisition of write locks is avoided wherever possible.
Index ¶
- type Leaf
- type Tree
- func (t *Tree) Add(path []string, value interface{}) error
- func (t *Tree) Children() map[string]*Tree
- func (t *Tree) Delete(subpath []string) [][]string
- func (t *Tree) DeleteConditional(subpath []string, condition func(interface{}) bool) [][]string
- func (t *Tree) Get(path []string) *Tree
- func (t *Tree) GetLeaf(path []string) *Leaf
- func (t *Tree) GetLeafValue(path []string) interface{}
- func (t *Tree) IsBranch() bool
- func (t *Tree) Query(path []string, f VisitFunc) error
- func (t *Tree) String() string
- func (t *Tree) Value() interface{}
- func (t *Tree) Walk(f VisitFunc) error
- func (t *Tree) WalkSorted(f VisitFunc) error
- type VisitFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Leaf ¶
type Leaf Tree
Leaf is a Tree node that represents a leaf.
Leaf is safe for use from multiple goroutines and will return the latest value. This means that if value in this leaf was updated after Leaf was retrieved, Value will return the updated content, not the original one. This also means that multiple calls to Value may return different results.
func DetachedLeaf ¶
func DetachedLeaf(val interface{}) *Leaf
DetachedLeaf returns a Leaf that's not attached to any tree.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree is a thread-safe container.
func (*Tree) Children ¶
Children returns a mapping of child nodes if current node represents a branch, nil otherwise.
func (*Tree) Delete ¶
Delete removes all leaves at or below subpath as well as any ancestors with no children, returning the list of all leaves removed. Deletes prevent all other concurrent access.
func (*Tree) DeleteConditional ¶
DeleteConditional removes all leaves at or below subpath as well as any ancestors with no children for those leaves which the given conditional function returns true, returning the list of all leaves removed. DeleteConditional prevents all other concurrent access.
func (*Tree) Get ¶
Get returns the Tree node if path points to it, nil otherwise. All nodes in path must be fully specified with no globbing (*).
func (*Tree) GetLeaf ¶
GetLeaf returns the leaf node if path points to a leaf in t, nil otherwise. All nodes in path must be fully specified with no globbing (*).
func (*Tree) GetLeafValue ¶
GetLeafValue returns the leaf value if path points to a leaf in t, nil otherwise. All nodes in path must be fully specified with no globbing (*).
func (*Tree) IsBranch ¶
IsBranch returns whether the Tree node represents a branch. Returns false if called on a nil node.
func (*Tree) Query ¶
Query calls f for all leaves that match a given query where zero or more nodes in path may be specified by globs (*). Results and their full paths are passed to f as they are found in the Tree. No ordering of paths is guaranteed.
func (*Tree) String ¶
String implements the string interface for Tree returning a stable output sorting keys at each level.
func (*Tree) Value ¶
func (t *Tree) Value() interface{}
Value returns the latest value stored in node t if it represents a leaf, nil otherwise. Value is safe to call on nil Tree.
func (*Tree) WalkSorted ¶
WalkSorted calls f for all leaves in string sorted order.
type VisitFunc ¶
VisitFunc is a callback func triggered on leaf values by Query and Walk.
The provided Leaf is the leaf node of the tree, val is the value stored inside of it. l can be retained after VisitFunc returns and l.Value() can be called to get the latest value for that leaf.
Note that l.Value can *not* be called inside VisitFunc, because the node is already locked by Query/Walk.
If the function returns an error, the Walk or Query will terminate early and return the supplied error.