zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
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 
17  client(connection::connect(params))
18 { }
19 
20 client::client(string_view conn_string) :
21  client(connection::connect(conn_string))
22 { }
23 
24 client::client(std::shared_ptr<connection> conn) noexcept :
25  _conn(std::move(conn))
26 { }
27 
28 future<client> client::connect(string_view conn_string)
29 {
30  return connect(connection_params::parse(conn_string));
31 }
32 
33 future<client> client::connect(connection_params conn_params)
34 {
35  try
36  {
37  auto conn = connection::connect(conn_params);
38  auto state_change_fut = conn->watch_state();
39  if (conn->state() == state::connected)
40  {
41  promise<client> p;
42  p.set_value(client(std::move(conn)));
43  return p.get_future();
44  }
45  else
46  {
47  // TODO: Test if future::then can be relied on and use that instead of std::async
48  return std::async
49  (
50  std::launch::async,
51  [state_change_fut = std::move(state_change_fut), conn = std::move(conn)] () mutable -> client
52  {
53  state s(state_change_fut.get());
54  if (s == state::connected)
55  return client(conn);
56  else
57  throw std::runtime_error(std::string("Unexpected state: ") + to_string(s));
58  }
59  );
60  }
61  }
62  catch (...)
63  {
64  promise<client> p;
65  p.set_exception(std::current_exception());
66  return p.get_future();
67  }
68 }
69 
70 client::~client() noexcept = default;
71 
72 void client::close()
73 {
74  _conn->close();
75 }
76 
77 future<get_result> client::get(string_view path) const
78 {
79  return _conn->get(path);
80 }
81 
82 future<watch_result> client::watch(string_view path) const
83 {
84  return _conn->watch(path);
85 }
86 
87 future<get_children_result> client::get_children(string_view path) const
88 {
89  return _conn->get_children(path);
90 }
91 
92 future<watch_children_result> client::watch_children(string_view path) const
93 {
94  return _conn->watch_children(path);
95 }
96 
97 future<exists_result> client::exists(string_view path) const
98 {
99  return _conn->exists(path);
100 }
101 
102 future<watch_exists_result> client::watch_exists(string_view path) const
103 {
104  return _conn->watch_exists(path);
105 }
106 
107 future<create_result> client::create(string_view path,
108  const buffer& data,
109  const acl& rules,
110  create_mode mode
111  )
112 {
113  return _conn->create(path, data, rules, mode);
114 }
115 
116 future<create_result> client::create(string_view path,
117  const buffer& data,
118  create_mode mode
119  )
120 {
121  return create(path, data, acls::open_unsafe(), mode);
122 }
123 
124 future<set_result> client::set(string_view path, const buffer& data, version check)
125 {
126  return _conn->set(path, data, check);
127 }
128 
129 future<get_acl_result> client::get_acl(string_view path) const
130 {
131  return _conn->get_acl(path);
132 }
133 
134 future<void> client::set_acl(string_view path, const acl& rules, acl_version check)
135 {
136  return _conn->set_acl(path, rules, check);
137 }
138 
139 future<void> client::erase(string_view path, version check)
140 {
141  return _conn->erase(path, check);
142 }
143 
144 future<void> client::load_fence() const
145 {
146  return _conn->load_fence();
147 }
148 
149 future<multi_result> client::commit(multi_op txn)
150 {
151  return _conn->commit(std::move(txn));
152 }
153 
154 }
static connection_params parse(string_view conn_string)
Create an instance from a connection string.
Definition: connection.cpp:186
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:385
future< multi_result > commit(multi_op txn)
Commit the transaction specified by txn.
Definition: client.cpp:149
future< void > erase(string_view path, version check=version::any())
Erase the entry at the given path.
Definition: client.cpp:139
client(const connection_params &params)
Create a client connected to the cluster specified by params.
Definition: client.cpp:16
Represents a version of the ACL of an entry.
Definition: types.hpp:171
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 ent...
Definition: client.cpp:82
future< exists_result > exists(string_view path) const
Return the stat of the entry of the given path or nullopt if it does not exist.
Definition: client.cpp:97
future< create_result > create(string_view path, const buffer &data, const acl &rules, create_mode mode=create_mode::normal)
Definition: client.cpp:107
create_mode
When used in client::set, this value determines how the entry is created on the server.
Definition: types.hpp:283
static future< client > connect(string_view conn_string)
Definition: client.cpp:28
void close()
Close the underlying connection.
Definition: client.cpp:72
Used to specify parameters for a connection.
Definition: connection.hpp:84
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:144
The client is in the connected state – it is connected to a server in the ensemble (one of the serve...
A collection of operations that will be performed atomically.
Definition: multi.hpp:162
future< get_acl_result > get_acl(string_view path) const
Return the ACL and stat of the entry of the given path.
Definition: client.cpp:129
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:92
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:139
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 e...
Definition: client.cpp:102
future< void > set_acl(string_view path, const acl &rules, acl_version check=acl_version::any())
Set the ACL for the entry of the given path if such an entry exists and the given version check match...
Definition: client.cpp:134
future< set_result > set(string_view path, const buffer &data, version check=version::any())
Set the data for the entry of the given path if such an entry exists and the given version matches th...
Definition: client.cpp:124
future< get_result > get(string_view path) const
Return the data and the stat of the entry of the given path.
Definition: client.cpp:77
future< get_children_result > get_children(string_view path) const
Return the list of the children of the entry of the given path.
Definition: client.cpp:87
Represents a version of the data.
Definition: types.hpp:158
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:79
A ZooKeeper client connection.
Definition: client.hpp:25
An actual connection to the server.
Definition: connection.hpp:26