pub struct ZooKeeper { /* private fields */ }
Expand description
The client interface for interacting with a ZooKeeper cluster.
Implementations§
source§impl ZooKeeper
impl ZooKeeper
sourcepub fn connect<W>(
connect_string: &str,
timeout: Duration,
watcher: W
) -> ZkResult<ZooKeeper>where
W: Watcher + 'static,
pub fn connect<W>( connect_string: &str, timeout: Duration, watcher: W ) -> ZkResult<ZooKeeper>where W: Watcher + 'static,
Connect to a ZooKeeper cluster.
connect_string
: comma separated host:port pairs, each corresponding to a zk server, e.g."127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
If the optional chroot suffix is used the example would look like:"127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
where the client would be rooted at"/app/a"
and all paths would be relative to this root – ie getting/setting/etc…"/foo/bar"
would result in operations being run on"/app/a/foo/bar"
(from the server perspective).timeout
: session timeout – how long should a client go without receiving communication from a server before considering it connection loss?watcher
: a watcher object to be notified of connection state changes.
sourcepub fn add_auth<S: ToString>(&self, scheme: S, auth: Vec<u8>) -> ZkResult<()>
pub fn add_auth<S: ToString>(&self, scheme: S, auth: Vec<u8>) -> ZkResult<()>
Add the specified scheme
:auth
information to this connection.
See Acl
for more information.
sourcepub fn create(
&self,
path: &str,
data: Vec<u8>,
acl: Vec<Acl>,
mode: CreateMode
) -> ZkResult<String>
pub fn create( &self, path: &str, data: Vec<u8>, acl: Vec<Acl>, mode: CreateMode ) -> ZkResult<String>
Create a node with the given path
. The node data will be the given data
, and node ACL
will be the given acl
. The mode
argument specifies the behavior of the created node (see
CreateMode
for more information).
This operation, if successful, will trigger all the watches left on the node of the given
path by exists
and get_data
API calls, and the watches left on the parent node by
get_children
API calls.
Errors
If a node with the same actual path already exists in the ZooKeeper, the result will have
Err(ZkError::NodeExists)
. Note that since a different actual path is used for each
invocation of creating sequential node with the same path argument, the call should never
error in this manner.
If the parent node does not exist in the ZooKeeper, Err(ZkError::NoNode)
will be returned.
An ephemeral node cannot have children. If the parent node of the given path is ephemeral,
Err(ZkError::NoChildrenForEphemerals)
will be returned.
If the acl
is invalid or empty, Err(ZkError::InvalidACL)
is returned.
The maximum allowable size of the data array is 1 MiB (1,048,576 bytes). Arrays larger than
this will return Err(ZkError::BadArguments)
.
sourcepub fn delete(&self, path: &str, version: Option<i32>) -> ZkResult<()>
pub fn delete(&self, path: &str, version: Option<i32>) -> ZkResult<()>
Delete the node with the given path
. The call will succeed if such a node exists, and the
given version
matches the node’s version (if the given version is None
, it matches any
node’s versions).
This operation, if successful, will trigger all the watches on the node of the given path
left by exists
API calls, watches left by get_data
API calls, and the watches on the
parent node left by get_children
API calls.
Errors
If the nodes does not exist, Err(ZkError::NoNode)
will be returned.
If the given version
does not match the node’s version, Err(ZkError::BadVersion)
will be
returned.
If the node has children, Err(ZkError::NotEmpty)
will be returned.
sourcepub fn exists(&self, path: &str, watch: bool) -> ZkResult<Option<Stat>>
pub fn exists(&self, path: &str, watch: bool) -> ZkResult<Option<Stat>>
Return the Stat
of the node of the given path
or None
if no such node exists.
If the watch
is true
and the call is successful (no error is returned), a watch will be
left on the node with the given path. The watch will be triggered by a successful operation
that creates/delete the node or sets the data on the node.
sourcepub fn exists_w<W: Watcher + 'static>(
&self,
path: &str,
watcher: W
) -> ZkResult<Option<Stat>>
pub fn exists_w<W: Watcher + 'static>( &self, path: &str, watcher: W ) -> ZkResult<Option<Stat>>
Return the Stat
of the node of the given path
or None
if no such node exists.
Similar to exists
, but sets an explicit Watcher
instead of relying on the client’s base
Watcher
.
sourcepub fn get_acl(&self, path: &str) -> ZkResult<(Vec<Acl>, Stat)>
pub fn get_acl(&self, path: &str) -> ZkResult<(Vec<Acl>, Stat)>
Return the ACL and Stat
of the node of the given path.
Errors
If no node with the given path exists, Err(ZkError::NoNode)
will be returned.
sourcepub fn set_acl(
&self,
path: &str,
acl: Vec<Acl>,
version: Option<i32>
) -> ZkResult<Stat>
pub fn set_acl( &self, path: &str, acl: Vec<Acl>, version: Option<i32> ) -> ZkResult<Stat>
Set the ACL for the node of the given path if such a node exists and the given version
matches the version of the node. Return the Stat
of the node.
Errors
If no node with the given path exists, Err(ZkError::NoNode)
will be returned.
If the given version does not match the node’s version, Err(ZkError::BadVersion)
will be
returned.
sourcepub fn get_children(&self, path: &str, watch: bool) -> ZkResult<Vec<String>>
pub fn get_children(&self, path: &str, watch: bool) -> ZkResult<Vec<String>>
Return the list of the children of the node of the given path
. The returned values are not
prefixed with the provided path
; i.e. if the database contains /path/a
and /path/b
,
the result of get_children
for "/path"
will be ["a", "b"]
.
If the watch
is true
and the call is successful (no error is returned), a watch will be
left on the node with the given path. The watch will be triggered by a successful operation
that deletes the node of the given path or creates/delete a child under the node.
The list of children returned is not sorted and no guarantee is provided as to its natural or lexical order.
Errors
If no node with the given path exists, Err(ZkError::NoNode)
will be returned.
sourcepub fn get_children_w<W: Watcher + 'static>(
&self,
path: &str,
watcher: W
) -> ZkResult<Vec<String>>
pub fn get_children_w<W: Watcher + 'static>( &self, path: &str, watcher: W ) -> ZkResult<Vec<String>>
Return the list of the children of the node of the given path
.
Similar to get_children
, but sets an explicit Watcher
instead of relying on the client’s
base Watcher
.
sourcepub fn get_data(&self, path: &str, watch: bool) -> ZkResult<(Vec<u8>, Stat)>
pub fn get_data(&self, path: &str, watch: bool) -> ZkResult<(Vec<u8>, Stat)>
Return the data and the Stat
of the node of the given path.
If watch
is true
and the call is successful (no error is returned), a watch will be left
on the node with the given path. The watch will be triggered by a successful operation that
sets data on the node, or deletes the node.
Errors
If no node with the given path exists, Err(ZkError::NoNode)
will be returned.
sourcepub fn get_data_w<W: Watcher + 'static>(
&self,
path: &str,
watcher: W
) -> ZkResult<(Vec<u8>, Stat)>
pub fn get_data_w<W: Watcher + 'static>( &self, path: &str, watcher: W ) -> ZkResult<(Vec<u8>, Stat)>
Return the data and the Stat
of the node of the given path.
Similar to get_data
, but sets an explicit Watcher
instead of relying on the client’s
base Watcher
.
sourcepub fn set_data(
&self,
path: &str,
data: Vec<u8>,
version: Option<i32>
) -> ZkResult<Stat>
pub fn set_data( &self, path: &str, data: Vec<u8>, version: Option<i32> ) -> ZkResult<Stat>
Set the data for the node of the given path
if such a node exists and the given version
matches the version of the node (if the given version is None
, it matches any node’s
versions). Return the Stat
of the node.
This operation, if successful, will trigger all the watches on the node of the given path
left by get_data
calls.
Errors
If no node with the given path
exists, Err(ZkError::NoNode)
will be returned.
If the given version does not match the node’s version, Err(ZkError::BadVersion)
will be
returned.
The maximum allowable size of the data
array is 1 MiB (1,048,576 bytes). Arrays larger
than this will return Err(ZkError::BadArguments)
.
sourcepub fn add_watch<W: Watcher + 'static>(
&self,
path: &str,
mode: AddWatchMode,
watcher: W
) -> ZkResult<()>
pub fn add_watch<W: Watcher + 'static>( &self, path: &str, mode: AddWatchMode, watcher: W ) -> ZkResult<()>
Add a watch to the given znode using the given mode. Note: not all watch types can be set with this method. Only the modes available in WatchMode can be set with this method. Requires Zookeeper 3.6.0
sourcepub fn remove_watches(
&self,
path: &str,
watcher_type: WatcherType
) -> ZkResult<()>
pub fn remove_watches( &self, path: &str, watcher_type: WatcherType ) -> ZkResult<()>
Remove watches of a given type for a path.
sourcepub fn add_listener<Listener: Fn(ZkState) + Send + 'static>(
&self,
listener: Listener
) -> Subscription
pub fn add_listener<Listener: Fn(ZkState) + Send + 'static>( &self, listener: Listener ) -> Subscription
Adds a state change Listener
, which will be notified of changes to the client’s ZkState
.
A unique identifier is returned, which is used in remove_listener
to un-subscribe.
sourcepub fn remove_listener(&self, sub: Subscription)
pub fn remove_listener(&self, sub: Subscription)
Removes a state change Listener
and closes the channel.
Trait Implementations§
source§impl ZooKeeperExt for ZooKeeper
impl ZooKeeperExt for ZooKeeper
source§fn ensure_path(&self, path: &str) -> ZkResult<()>
fn ensure_path(&self, path: &str) -> ZkResult<()>
path
exists and create all potential paths leading up to it if it does not.
This operates in a manner similar to mkdir -p
.