zookeeper-cpp
ZooKeeper Client for C++
client.cpp
1 #include "client.hpp"
2 #include "acl.hpp"
3 #include "connection.hpp"
4 #include "multi.hpp"
5 
6 #include <sstream>
7 #include <ostream>
8 
9 namespace zk
10 {
11 
13 // client //
15 
16 client::client(string_view conn_string) :
17  client(connection::connect(conn_string))
18 { }
19 
20 client::client(std::shared_ptr<connection> conn) noexcept :
21  _conn(std::move(conn))
22 { }
23 
24 future<client> client::connect(string_view conn_string)
25 {
26  try
27  {
28  auto conn = connection::connect(conn_string);
29  auto state_change_fut = conn->watch_state();
30  if (conn->state() == state::connected)
31  {
32  promise<client> p;
33  p.set_value(client(std::move(conn)));
34  return p.get_future();
35  }
36  else
37  {
38  // TODO: Test if future::then can be relied on and use that instead of std::async
39  return std::async
40  (
41  std::launch::async,
42  [state_change_fut = std::move(state_change_fut), conn = std::move(conn)] () mutable -> client
43  {
44  state s(state_change_fut.get());
45  if (s == state::connected)
46  return client(conn);
47  else
48  throw std::runtime_error(std::string("Unexpected state: ") + to_string(s));
49  }
50  );
51  }
52  }
53  catch (...)
54  {
55  promise<client> p;
56  p.set_exception(std::current_exception());
57  return p.get_future();
58  }
59 }
60 
61 client::~client() noexcept = default;
62 
63 void client::close()
64 {
65  _conn->close();
66 }
67 
68 future<get_result> client::get(string_view path) const
69 {
70  return _conn->get(path);
71 }
72 
73 future<watch_result> client::watch(string_view path) const
74 {
75  return _conn->watch(path);
76 }
77 
78 future<get_children_result> client::get_children(string_view path) const
79 {
80  return _conn->get_children(path);
81 }
82 
83 future<watch_children_result> client::watch_children(string_view path) const
84 {
85  return _conn->watch_children(path);
86 }
87 
88 future<exists_result> client::exists(string_view path) const
89 {
90  return _conn->exists(path);
91 }
92 
93 future<watch_exists_result> client::watch_exists(string_view path) const
94 {
95  return _conn->watch_exists(path);
96 }
97 
98 future<create_result> client::create(string_view path,
99  const buffer& data,
100  const acl& rules,
101  create_mode mode
102  )
103 {
104  return _conn->create(path, data, rules, mode);
105 }
106 
107 future<create_result> client::create(string_view path,
108  const buffer& data,
109  create_mode mode
110  )
111 {
112  return create(path, data, acls::open_unsafe(), mode);
113 }
114 
115 future<set_result> client::set(string_view path, const buffer& data, version check)
116 {
117  return _conn->set(path, data, check);
118 }
119 
120 future<get_acl_result> client::get_acl(string_view path) const
121 {
122  return _conn->get_acl(path);
123 }
124 
125 future<void> client::set_acl(string_view path, const acl& rules, acl_version check)
126 {
127  return _conn->set_acl(path, rules, check);
128 }
129 
130 future<void> client::erase(string_view path, version check)
131 {
132  return _conn->erase(path, check);
133 }
134 
135 future<void> client::load_fence() const
136 {
137  return _conn->load_fence();
138 }
139 
140 future<multi_result> client::commit(multi_op txn)
141 {
142  return _conn->commit(std::move(txn));
143 }
144 
145 }
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:320
Definition: acl.cpp:8
future< multi_result > commit(multi_op txn)
Commit the transaction specified by txn.
Definition: client.cpp:140
future< void > erase(string_view path, version check=version::any())
Erase the node with the given path.
Definition: client.cpp:130
Represents a version of the ACL of a ZNode.
Definition: types.hpp:145
future< watch_result > watch(string_view path) const
Similar to get, but if the call is successful (no error is returned), a watch will be left on the nod...
Definition: client.cpp:73
future< exists_result > exists(string_view path) const
Return the stat of the node of the given path or nullopt if no such node exists.
Definition: client.cpp:88
future< create_result > create(string_view path, const buffer &data, const acl &rules, create_mode mode=create_mode::normal)
Create a node with the given path.
Definition: client.cpp:98
create_mode
When used in client::set, this value determines how the znode is created on the server.
Definition: types.hpp:252
static future< client > connect(string_view conn_string)
Create a client connected to the cluster specified by conn_string.
Definition: client.cpp:24
static const acl & open_unsafe()
This is a completely open ACL.
Definition: acl.cpp:161
future< void > load_fence() const
Ensure that all subsequent reads observe the data at the transaction on the server at or past real-ti...
Definition: client.cpp:135
The client is in the connected state – it is connected to a server in the ensemble (one of the serve...
future< get_acl_result > get_acl(string_view path) const
Return the ACL and stat of the node of the given path.
Definition: client.cpp:120
future< watch_children_result > watch_children(string_view path) const
Similar to get_children, but if the call is successful (no error is returned), a watch will be left o...
Definition: client.cpp:83
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:124
future< watch_exists_result > watch_exists(string_view path) const
Similar to watch, but if the call is successful (no error is returned), a watch will be left on the n...
Definition: client.cpp:93
future< void > set_acl(string_view path, const acl &rules, acl_version check=acl_version::any())
Set the ACL for the node of the given path if such a node exists and the given version check matches ...
Definition: client.cpp:125
future< set_result > set(string_view path, const buffer &data, version check=version::any())
Set the data for the node of the given path if such a node exists and the given version matches the v...
Definition: client.cpp:115
future< get_result > get(string_view path) const
Return the data and the stat of the node of the given path.
Definition: client.cpp:68
future< get_children_result > get_children(string_view path) const
Return the list of the children of the node of the given path.
Definition: client.cpp:78
Represents a version of the data.
Definition: types.hpp:131
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:67
A ZooKeeper client connection.
Definition: client.hpp:27