tree

package
v0.0.0-...-a660477 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2017 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNodeNotFound indicates a node can not be found.
	ErrNodeNotFound = errors.New("node not found")
	// ErrRootNotMatch indicates the root path does not match.
	ErrRootNotMatch = errors.New("root path not match")
	// ErrNodeNotLive indicates the state of node is not LIVE.
	ErrNodeNotLive = errors.New("node state is not LIVE")
)

DefaultSelector always returns true.

View Source
var FuncAcceptAll = func(string) bool { return true }

FuncAcceptAll is a function that always returns true.

Functions

This section is empty.

Types

type Cache

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

Cache is a a utility that attempts to keep all data from all children of a ZK path locally cached. It will watch the ZK path, respond to update/create/delete events, pull down the data, etc. You can register a listener that will get notified when changes occur.

NOTE: It's not possible to stay transactionally in sync. Users must be prepared for false-positives and false-negatives. Additionally, always use the version number when updating data to avoid overwriting another process' change.

func NewCache

func NewCache(client *enhanced.Client, root string, selector Selector) *Cache

NewCache creates a Cache for the given client and path with default options.

If the client is namespaced, all operations on the resulting TreeCache will be in terms of the namespace, including all published events. The given path is the root at which the TreeCache will watch and explore. If no node exists at the given path, the TreeCache will be initially empty.

func (*Cache) AddErrorListener

func (c *Cache) AddErrorListener(l *ErrorListener)

AddErrorListener adds an error listener.

func (*Cache) AddEventListener

func (c *Cache) AddEventListener(l *CacheEventListener)

AddEventListener adds a CacheEventListener.

func (*Cache) CurrentChildren

func (c *Cache) CurrentChildren(fullPath string) (map[string]*ChildData, error)

CurrentChildren returns the current set of children at the given full path, mapped by child name. There are no guarantees of accuracy; this is merely the most recent view of the data. If there is no node at this path, ErrNodeNotFound is returned.

func (*Cache) CurrentData

func (c *Cache) CurrentData(fullPath string) (*ChildData, error)

CurrentData returns the current data for the given full path. There are no guarantees of accuracy. This is merely the most recent view of the data. If there is no node at the given path, ErrNodeNotFound is returned.

func (*Cache) DelErrorListener

func (c *Cache) DelErrorListener(l *ErrorListener)

DelErrorListener deletes the first occurrence of l.

func (*Cache) DelEventListener

func (c *Cache) DelEventListener(l *CacheEventListener)

DelEventListener deletes the first occurrence of l.

func (*Cache) SetCacheData

func (c *Cache) SetCacheData(cacheData bool) *Cache

SetCacheData sets whether or not to cache byte data per node, default true. NOTE: When this set to false, the events still contain data of znode but you can't query them by TreeCache.CurrentData/CurrentChildren

func (*Cache) SetCreateParentNodes

func (c *Cache) SetCreateParentNodes(yes bool) *Cache

SetCreateParentNodes sets whether to auto-create parent nodes for the cached path. By default, TreeCache does not do this. Note: Parent nodes is only created when Start() is called.

func (*Cache) SetMaxDepth

func (c *Cache) SetMaxDepth(depth int) *Cache

SetMaxDepth sets the maximum depth to explore/watch. Set to 0 will watch only the root node. Set to 1 will watch the root node and its immediate children. Default to math.MaxInt32.

func (*Cache) Start

func (c *Cache) Start() error

Start starts the TreeCache. The cache is not started automatically. You must call this method.

func (*Cache) Stop

func (c *Cache) Stop()

Stop stops the cache.

type CacheEvent

type CacheEvent struct {
	Type CacheEventType
	Data *ChildData
}

CacheEvent represents a change to a path

func (CacheEvent) String

func (e CacheEvent) String() string

String returns the string representation of CacheEvent

type CacheEventListener

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

CacheEventListener is a handler of cache event.

func NewCacheEventListener

func NewCacheEventListener(fn func(CacheEvent)) *CacheEventListener

NewCacheEventListener creates CacheEventListener with fn.

func (*CacheEventListener) Handle

func (l *CacheEventListener) Handle(e CacheEvent)

Handle calls the function with e.

type CacheEventListeners

type CacheEventListeners struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CacheEventListeners is a set of CacheEventListeners.

func NewCacheEventListeners

func NewCacheEventListeners() *CacheEventListeners

NewCacheEventListeners creates empty CacheEventListeners.

func (*CacheEventListeners) Add

func (l *CacheEventListeners) Add(listener *CacheEventListener)

Add adds a CacheEventListener.

func (*CacheEventListeners) Broadcast

func (l *CacheEventListeners) Broadcast(e CacheEvent)

Broadcast calls every listener with given CacheEvent.

func (*CacheEventListeners) Clear

func (l *CacheEventListeners) Clear()

Clear deletes all listeners.

func (*CacheEventListeners) Count

func (l *CacheEventListeners) Count() int

Count returns the number of listeners.

func (*CacheEventListeners) Del

func (l *CacheEventListeners) Del(listener *CacheEventListener)

Del deletes the first occurrence of listener.

type CacheEventType

type CacheEventType int

CacheEventType represents the type of change to a path.

const (
	// CacheEventNodeAdded indicates a node was added.
	CacheEventNodeAdded CacheEventType = iota
	// CacheEventNodeUpdated indicates a node's data was changed.
	CacheEventNodeUpdated
	// CacheEventNodeRemoved indicates a node was removed from the tree.
	CacheEventNodeRemoved

	// CacheEventConnSuspended is called when the connection has changed to SUSPENDED.
	CacheEventConnSuspended
	// CacheEventConnReconnected is called when the connection has changed to RECONNECTED.
	CacheEventConnReconnected
	// CacheEventConnLost is called when the connection has changed to LOST.
	CacheEventConnLost
	// CacheEventInitialized is posted after the initial cache has been fully populated.
	CacheEventInitialized
)

func (CacheEventType) String

func (et CacheEventType) String() string

String returns the string representation of CacheEventType. "Unknown" is returned when event type is unknown

type CacheState

type CacheState int32

CacheState represents the state of a Cache.

const (
	// CacheStateLatent indicates Cache.Start() has not yet been called.
	CacheStateLatent CacheState = iota
	// CacheStateStarted indicates Cache.Start() has been called.
	CacheStateStarted
	// CacheStateStopped indicates Cache.Close() has been called.
	CacheStateStopped
)

func (*CacheState) EqualTo

func (s *CacheState) EqualTo(x CacheState) bool

EqualTo returns true if value equals to given CacheState.

func (*CacheState) SetValueIf

func (s *CacheState) SetValueIf(old, new CacheState) bool

SetValueIf does a CAS operation.

func (*CacheState) Value

func (s *CacheState) Value() CacheState

Value returns the state.

type ChildData

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

ChildData contains data of a node including: stat, data, path

func NewChildData

func NewChildData(path string, stat *zk.Stat, data []byte) *ChildData

NewChildData creates ChildData.

func (ChildData) Data

func (cd ChildData) Data() []byte

Data returns the node data for this child when the cache mode is set to cache data

func (ChildData) Path

func (cd ChildData) Path() string

Path returns the full path of this child

func (*ChildData) SetStat

func (cd *ChildData) SetStat(s *zk.Stat)

SetStat sets the stat data for this child

func (ChildData) Stat

func (cd ChildData) Stat() *zk.Stat

Stat returns the stat data for this child

type ErrorListener

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

ErrorListener is a handler of error.

func NewErrorListener

func NewErrorListener(fn func(error)) *ErrorListener

NewErrorListener creates ErrorListener from fn.

func (*ErrorListener) Handle

func (l *ErrorListener) Handle(e error)

Handle calls the function with e.

type ErrorListeners

type ErrorListeners struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ErrorListeners is a set of listeners.

func NewErrorListeners

func NewErrorListeners() *ErrorListeners

NewErrorListeners creates empty ErrorListeners.

func (*ErrorListeners) Add

func (l *ErrorListeners) Add(listener *ErrorListener)

Add adds a ErrorListener.

func (*ErrorListeners) Broadcast

func (l *ErrorListeners) Broadcast(err error)

Broadcast calls every listener with given error.

func (*ErrorListeners) Clear

func (l *ErrorListeners) Clear()

Clear removes all listeners.

func (*ErrorListeners) Count

func (l *ErrorListeners) Count() int

Count returns the number of listeners.

func (*ErrorListeners) Del

func (l *ErrorListeners) Del(listener *ErrorListener)

Del deletes the first occurrence of listener.

type Node

type Node struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Node represents a node within a tree of ZNodes.

func NewNode

func NewNode(cache *Cache, path string, parent *Node) *Node

NewNode creates a Node with given path and parent. NOTE: Parent should be nil if the node is root.

func (*Node) ChildData

func (n *Node) ChildData() *ChildData

ChildData returns the ChildData.

func (*Node) Children

func (n *Node) Children() map[string]*Node

Children returns the children of current node.

func (*Node) FindChild

func (n *Node) FindChild(path string) (*Node, bool)

FindChild finds a child of current node by its relative path. NOTE: path should contain no slash.

func (*Node) RemoveChild

func (n *Node) RemoveChild(path string)

RemoveChild removes child by path.

func (*Node) SwapChildData

func (n *Node) SwapChildData(d *ChildData) *ChildData

SwapChildData sets ChildData to given value and returns the old ChildData.

type NodeState

type NodeState int32

NodeState represents state of a Node.

const (
	NodeStatePending NodeState = iota
	NodeStateLive
	NodeStateDead
)

Available node states.

func (*NodeState) EqualTo

func (s *NodeState) EqualTo(x NodeState) bool

EqualTo returns true if value equals to given NodeState.

func (*NodeState) SetValue

func (s *NodeState) SetValue(new NodeState)

SetValue sets the state to given value.

func (*NodeState) SetValueIf

func (s *NodeState) SetValueIf(old, new NodeState) bool

SetValueIf does a CAS operation.

func (*NodeState) SwapValue

func (s *NodeState) SwapValue(new NodeState) NodeState

SwapValue sets the state to new returns the old one.

func (*NodeState) Value

func (s *NodeState) Value() NodeState

Value returns the current state.

type Selector

type Selector interface {
	TraverseChildren(string) bool
	AcceptChildData(string) bool
}

Selector controls the nodes a Cache processes. When iterating over the children of a parent node, a given node's children are queried only if TraverseChildren returns true. When caching the list of nodes for a parent node, a given node is stored only if AcceptChild returns true.

func NewSelector

func NewSelector(traverseChildren, acceptChild func(string) bool) Selector

NewSelector creates Selector.

Jump to

Keyboard shortcuts

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