cache

package
v0.0.0-...-8a961ea Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTreeCacheSelector = NewTreeCacheSelector(
	func(p string) bool { return true },
	func(p string) bool { return true },
)

DefaultTreeCacheSelector returns true for all methods

View Source
var ErrNodeNotFound = errors.New("node not found")

ErrNodeNotFound indicates a node can not be found.

View Source
var ErrNodeNotLive = errors.New("node state is not LIVE")

ErrNodeNotLive indicates the state of node is not LIVE.

View Source
var ErrRootNotMatch = errors.New("root path not match")

ErrRootNotMatch indicates the root path does not match.

View Source
var IgnoreBuiltinTreeCacheSelector = NewTreeCacheSelector(
	func(path string) bool {
		return !strings.HasPrefix(path, "/zookeeper")
	},
	func(path string) bool {
		return !strings.HasPrefix(path, "/zookeeper")
	},
)

IgnoreBuiltinTreeCacheSelector ignores path starts with /zookeeper This could be useful if you use / as your root

Functions

This section is empty.

Types

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 DummyLogger

type DummyLogger struct{}

DummyLogger is a Logger does nothing.

func (DummyLogger) Debugf

func (l DummyLogger) Debugf(string, ...interface{})

Debugf does nothing.

func (DummyLogger) Printf

func (l DummyLogger) Printf(string, ...interface{})

Printf does nothing.

type Logger

type Logger interface {
	Printf(string, ...interface{})
	Debugf(string, ...interface{})
}

Logger provides customized logging within TreeCache.

type NodeCache

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

A utility that attempts to keep the data from a node locally cached. This class will watch the node, respond to update/create/delete events, pull down the data, etc. You can register a listener that will get notified when changes occur.

func NewNodeCache

func NewNodeCache(client curator.CuratorFramework, path string, dataIsCompressed bool) *NodeCache

func (*NodeCache) Close

func (c *NodeCache) Close() error

func (*NodeCache) NodeCacheListenable

func (c *NodeCache) NodeCacheListenable() NodeCacheListenable

func (*NodeCache) Start

func (c *NodeCache) Start() error

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

func (*NodeCache) StartAndInitalize

func (c *NodeCache) StartAndInitalize(buildInitial bool) error

Same as Start() but gives the option of doing an initial build

type NodeCacheListenable

type NodeCacheListenable interface {
	curator.Listenable /* [T] */

	AddListener(listener NodeCacheListener)

	RemoveListener(listener NodeCacheListener)
}

type NodeCacheListener

type NodeCacheListener interface {
	// Called when a change has occurred
	NodeChanged() error
}

type NodeCacheListenerContainer

type NodeCacheListenerContainer struct {
	*curator.ListenerContainer
}

func (*NodeCacheListenerContainer) AddListener

func (c *NodeCacheListenerContainer) AddListener(listener NodeCacheListener)

func (*NodeCacheListenerContainer) RemoveListener

func (c *NodeCacheListenerContainer) RemoveListener(listener NodeCacheListener)

type NodeState

type NodeState int32

NodeState represents state of TreeNode. TODO: make this a private type?

const (
	NodeStatePENDING NodeState = iota
	NodeStateLIVE
	NodeStateDEAD
)

Available node states.

func (*NodeState) CompareAndSwap

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

CompareAndSwap set the state to new if value is old atomatically.

func (*NodeState) Load

func (s *NodeState) Load() NodeState

Load loads then returns the state atomically.

func (*NodeState) Store

func (s *NodeState) Store(new NodeState)

Store sets the state to given value atomically.

func (*NodeState) Swap

func (s *NodeState) Swap(new NodeState) NodeState

Swap sets the state to given value and returns the old state atomically.

type PathChildrenCache

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

A utility that attempts to keep all data from all children of a ZK path locally cached. This class 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.

func NewPathChildrenCache

func NewPathChildrenCache(client curator.CuratorFramework, path string, cacheData, dataIsCompressed bool) *PathChildrenCache

func (*PathChildrenCache) RefreshMode

func (c *PathChildrenCache) RefreshMode(mode RefreshMode)

type RefreshMode

type RefreshMode int
const (
	STANDARD RefreshMode = iota
	FORCE_GET_DATA_AND_STAT
	POST_INITIALIZED
)

type TreeCache

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

TreeCache 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 of this class 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 NewTreeCache

func NewTreeCache(client curator.CuratorFramework, root string, selector TreeCacheSelector) *TreeCache

NewTreeCache creates a TreeCache 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 (*TreeCache) CurrentChildren

func (tc *TreeCache) 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 (*TreeCache) CurrentData

func (tc *TreeCache) 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 (*TreeCache) Listenable

func (tc *TreeCache) Listenable() TreeCacheListenable

Listenable returns the cache listeners.

func (*TreeCache) SetCacheData

func (tc *TreeCache) SetCacheData(cacheData bool) *TreeCache

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 (*TreeCache) SetCreateParentNodes

func (tc *TreeCache) SetCreateParentNodes(yes bool) *TreeCache

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 (*TreeCache) SetLogger

func (tc *TreeCache) SetLogger(l Logger) *TreeCache

SetLogger sets the inner Logger of TreeCache.

func (*TreeCache) SetMaxDepth

func (tc *TreeCache) SetMaxDepth(depth int) *TreeCache

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 (*TreeCache) Start

func (tc *TreeCache) Start() error

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

func (*TreeCache) Stop

func (tc *TreeCache) Stop()

Stop stops the cache.

func (*TreeCache) UnhandledErrorListenable

func (tc *TreeCache) UnhandledErrorListenable() curator.UnhandledErrorListenable

UnhandledErrorListenable allows catching unhandled errors in asynchornous operations.

type TreeCacheEvent

type TreeCacheEvent struct {
	Type TreeCacheEventType
	Data *ChildData
}

TreeCacheEvent represents a change to a path

func (TreeCacheEvent) String

func (e TreeCacheEvent) String() string

String returns the string representation of TreeCacheEvent

type TreeCacheEventType

type TreeCacheEventType int

TreeCacheEventType represents the type of change to a path

const (
	// TreeCacheEventNodeAdded indicates a node was added
	TreeCacheEventNodeAdded TreeCacheEventType = iota
	// TreeCacheEventNodeUpdated indicates a node's data was changed
	TreeCacheEventNodeUpdated
	// TreeCacheEventNodeRemoved indicates a node was removed from the tree
	TreeCacheEventNodeRemoved

	// TreeCacheEventConnSuspended is called when the connection has changed to SUSPENDED
	TreeCacheEventConnSuspended
	// TreeCacheEventConnReconnected is called when the connection has changed to RECONNECTED
	TreeCacheEventConnReconnected
	// TreeCacheEventConnLost is called when the connection has changed to LOST
	TreeCacheEventConnLost
	// TreeCacheEventInitialized is posted after the initial cache has been fully populated
	TreeCacheEventInitialized
)

func (TreeCacheEventType) String

func (et TreeCacheEventType) String() string

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

type TreeCacheListenable

type TreeCacheListenable interface {
	curator.Listenable

	AddListener(TreeCacheListener)
	RemoveListener(TreeCacheListener)
}

TreeCacheListenable represents a container of TreeCacheListener(s).

type TreeCacheListener

type TreeCacheListener interface {
	// Called when a change has occurred
	ChildEvent(client curator.CuratorFramework, event TreeCacheEvent) error
}

TreeCacheListener represents listener for TreeCache changes

func NewTreeCacheListener

func NewTreeCacheListener(cb childEventCallback) TreeCacheListener

NewTreeCacheListener creates TreeCacheListener with given function

type TreeCacheListenerContainer

type TreeCacheListenerContainer struct {
	curator.ListenerContainer
}

TreeCacheListenerContainer is a container of TreeCacheListener.

func (*TreeCacheListenerContainer) AddListener

func (c *TreeCacheListenerContainer) AddListener(listener TreeCacheListener)

AddListener adds a listener to the container.

func (*TreeCacheListenerContainer) RemoveListener

func (c *TreeCacheListenerContainer) RemoveListener(listener TreeCacheListener)

RemoveListener removes a listener to the container.

type TreeCacheSelector

type TreeCacheSelector interface {
	// TraverseChildren returns true if children of this path should be cached.
	// i.e. if false is returned, this node is not queried to
	// determine if it has children or not
	TraverseChildren(fullPath string) bool

	// AcceptChild returns true if this node should be returned from the cache
	AcceptChild(fullPath string) bool
}

TreeCacheSelector controls which nodes a TreeCache 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 NewTreeCacheSelector

func NewTreeCacheSelector(traverseChildren, acceptChild func(string) bool) TreeCacheSelector

NewTreeCacheSelector creates a new TreeCacheSelector with given functions

type TreeNode

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

TreeNode represents a node in a tree of znodes.

func NewTreeNode

func NewTreeNode(tree *TreeCache, path string, parent *TreeNode) *TreeNode

NewTreeNode creates a TreeNode with given path and parent. NOTE: parent should be nil if the node is root.

func (*TreeNode) ChildData

func (tn *TreeNode) ChildData() *ChildData

ChildData returns the ChildData.

func (*TreeNode) Children

func (tn *TreeNode) Children() map[string]*TreeNode

Children returns the children of current node.

func (*TreeNode) FindChild

func (tn *TreeNode) FindChild(path string) (*TreeNode, bool)

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

func (*TreeNode) RemoveChild

func (tn *TreeNode) RemoveChild(path string)

RemoveChild removes child by path.

func (*TreeNode) SwapChildData

func (tn *TreeNode) SwapChildData(d *ChildData) *ChildData

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

Jump to

Keyboard shortcuts

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