zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
multi.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 
5 #include <initializer_list>
6 #include <iosfwd>
7 #include <string>
8 #include <variant>
9 #include <vector>
10 
11 #include "acl.hpp"
12 #include "buffer.hpp"
13 #include "forwards.hpp"
14 #include "results.hpp"
15 #include "types.hpp"
16 
17 namespace zk
18 {
19 
22 
24 enum class op_type : int
25 {
26  check,
27  create,
28  erase,
29  set,
30 };
31 
32 std::ostream& operator<<(std::ostream&, const op_type&);
33 
34 std::string to_string(const op_type&);
35 
37 class op final
38 {
39 public:
41  struct check_data
42  {
43  std::string path;
44  version check;
45 
46  explicit check_data(std::string path, version check);
47 
48  op_type type() const { return op_type::check; }
49  };
50 
52  static op check(std::string path, version check = version::any());
53 
55  struct create_data
56  {
57  std::string path;
58  buffer data;
59  acl rules;
60  create_mode mode;
61 
62  explicit create_data(std::string path, buffer data, acl rules, create_mode mode);
63 
64  op_type type() const { return op_type::create; }
65  };
66 
71  static op create(std::string path, buffer data, acl rules, create_mode mode = create_mode::normal);
72  static op create(std::string path, buffer data, create_mode mode = create_mode::normal);
74 
76  struct erase_data
77  {
78  std::string path;
79  version check;
80 
81  explicit erase_data(std::string path, version check);
82 
83  op_type type() const { return op_type::erase; }
84  };
85 
89  static op erase(std::string path, version check = version::any());
90 
92  struct set_data
93  {
94  std::string path;
95  buffer data;
96  version check;
97 
98  explicit set_data(std::string path, buffer data, version check);
99 
100  op_type type() const { return op_type::set; }
101  };
102 
106  static op set(std::string path, buffer data, version check = version::any());
107 
108 public:
109  op(const op&);
110  op(op&&) noexcept;
111 
112  op& operator=(const op&) = delete;
113  op& operator=(op&&) = delete;
114 
115  ~op() noexcept;
116 
118  op_type type() const;
119 
123  const check_data& as_check() const;
124 
128  const create_data& as_create() const;
129 
133  const erase_data& as_erase() const;
134 
138  const set_data& as_set() const;
139 
140 private:
141  using any_data = std::variant<check_data, create_data, erase_data, set_data>;
142 
143  explicit op(any_data&&) noexcept;
144 
145  template <typename T>
146  const T& as(ptr<const char> operation) const;
147 
148  friend std::ostream& operator<<(std::ostream&, const op&);
149 
150 private:
151  any_data _storage;
152 };
153 
154 std::ostream& operator<<(std::ostream&, const op&);
155 
156 std::string to_string(const op&);
157 
162 class multi_op final
163 {
164 public:
165  using iterator = std::vector<op>::iterator;
166  using const_iterator = std::vector<op>::const_iterator;
167  using size_type = std::vector<op>::size_type;
168 
169 public:
171  multi_op() noexcept
172  { }
173 
175  multi_op(std::vector<op> ops) noexcept;
176 
178  multi_op(std::initializer_list<op> ops) :
179  multi_op(std::vector<op>(ops))
180  { }
181 
182  ~multi_op() noexcept;
183 
185  size_type size() const { return _ops.size(); }
186 
189  const op& operator[](size_type idx) const { return _ops[idx]; }
190  op& operator[](size_type idx) { return _ops[idx]; }
192 
197  const op& at(size_type idx) const { return _ops.at(idx); }
198  op& at(size_type idx) { return _ops.at(idx); }
200 
203  iterator begin() { return _ops.begin(); }
204  const_iterator begin() const { return _ops.begin(); }
205  const_iterator cbegin() const { return _ops.begin(); }
207 
210  iterator end() { return _ops.end(); }
211  const_iterator end() const { return _ops.end(); }
212  const_iterator cend() const { return _ops.end(); }
214 
216  void reserve(size_type capacity) { _ops.reserve(capacity); }
217 
221  template <typename... TArgs>
222  void emplace_back(TArgs&&... args)
223  {
224  _ops.emplace_back(std::forward<TArgs>(args)...);
225  }
226 
229  void push_back(op&& x) { emplace_back(std::move(x)); }
230  void push_back(const op& x) { emplace_back(x); }
232 
233 private:
234  std::vector<op> _ops;
235 };
236 
237 std::ostream& operator<<(std::ostream&, const multi_op&);
238 
239 std::string to_string(const multi_op&);
240 
242 class multi_result final
243 {
244 public:
246  class part final
247  {
248  public:
249  explicit part(op_type, std::nullptr_t) noexcept;
250  explicit part(create_result) noexcept;
251  explicit part(set_result) noexcept;
252 
253  part(const part&);
254  part(part&&) noexcept;
255 
256  part& operator=(const part&) = delete;
257  part& operator=(part&&) = delete;
258 
259  ~part() noexcept;
260 
262  op_type type() const { return _type; }
263 
267  const create_result& as_create() const;
268 
272  const set_result& as_set() const;
273 
274  private:
275  using any_result = std::variant<std::monostate, create_result, set_result>;
276 
277  template <typename T>
278  const T& as(ptr<const char> operation) const;
279 
280  private:
281  op_type _type;
282  any_result _storage;
283  };
284 
285  using iterator = std::vector<part>::iterator;
286  using const_iterator = std::vector<part>::const_iterator;
287  using size_type = std::vector<part>::size_type;
288 
289 public:
290  multi_result() noexcept
291  { }
292 
293  multi_result(std::vector<part> parts) noexcept;
294 
295  ~multi_result() noexcept;
296 
298  size_type size() const { return _parts.size(); }
299 
304  part& operator[](size_type idx) { return _parts[idx]; }
305  const part& operator[](size_type idx) const { return _parts[idx]; }
307 
312  const part& at(size_type idx) const { return _parts.at(idx); }
313  part& at(size_type idx) { return _parts.at(idx); }
315 
318  iterator begin() { return _parts.begin(); }
319  const_iterator begin() const { return _parts.begin(); }
320  const_iterator cbegin() const { return _parts.begin(); }
322 
325  iterator end() { return _parts.end(); }
326  const_iterator end() const { return _parts.end(); }
327  const_iterator cend() const { return _parts.end(); }
329 
331  void reserve(size_type capacity) { _parts.reserve(capacity); }
332 
336  template <typename... TArgs>
337  void emplace_back(TArgs&&... args)
338  {
339  _parts.emplace_back(std::forward<TArgs>(args)...);
340  }
341 
344  void push_back(part&& x) { emplace_back(std::move(x)); }
345  void push_back(const part& x) { emplace_back(x); }
347 
348 private:
349  std::vector<part> _parts;
350 };
351 
352 std::ostream& operator<<(std::ostream&, const multi_result::part&);
353 std::ostream& operator<<(std::ostream&, const multi_result&);
354 
355 std::string to_string(const multi_result::part&);
356 std::string to_string(const multi_result&);
357 
360 }
T * ptr
A simple, unowned pointer.
Definition: config.hpp:37
Data for a op::set operation.
Definition: multi.hpp:92
Describes the various result types of client operations.
void reserve(size_type capacity)
Increase the reserved memory block so it can store at least capacity operations without reallocating...
Definition: multi.hpp:216
const set_data & as_set() const
Get the set-specific data.
Definition: multi.cpp:178
iterator end()
Definition: multi.hpp:210
iterator begin()
Definition: multi.hpp:203
part & operator[](size_type idx)
Definition: multi.hpp:304
size_type size() const
The number of operations in this transaction bundle.
Definition: multi.hpp:185
Represents a single operation of a multi_op.
Definition: multi.hpp:37
The result type of client::create.
Definition: results.hpp:116
iterator end()
Definition: multi.hpp:325
const part & at(size_type idx) const
Definition: multi.hpp:312
op_type
Describes the type of an op.
Definition: multi.hpp:24
const create_data & as_create() const
Get the create-specific data.
Definition: multi.cpp:129
create_mode
When used in client::set, this value determines how the entry is created on the server.
Definition: types.hpp:283
void reserve(size_type capacity)
Increase the reserved memory block so it can store at least capacity results without reallocating...
Definition: multi.hpp:331
multi_op(std::initializer_list< op > ops)
Create an instance from the provided ops.
Definition: multi.hpp:178
Standard behavior of an entry – the opposite of doing any of the options.
Data for a op::check operation.
Definition: multi.hpp:41
op_type type() const
Get the underlying type of this operation.
Definition: multi.cpp:56
A collection of operations that will be performed atomically.
Definition: multi.hpp:162
void emplace_back(TArgs &&...args)
Construct an operation emplace on the end of the list using args.
Definition: multi.hpp:222
void push_back(op &&x)
Definition: multi.hpp:229
The result type of client::set.
Definition: results.hpp:141
const op & at(size_type idx) const
Definition: multi.hpp:197
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:139
multi_op() noexcept
Create an empty operation set.
Definition: multi.hpp:171
const erase_data & as_erase() const
Get the erase-specific data.
Definition: multi.cpp:153
Controls the buffer type.
op_type type() const
The op_type of the op that caused this result.
Definition: multi.hpp:262
static op check(std::string path, version check=version::any())
Check that the given path exists with the provided version check (which can be version::any).
Definition: multi.cpp:92
static constexpr version any()
When specified in an operation, this version specifier will always pass.
Definition: types.hpp:150
const op & operator[](size_type idx) const
Definition: multi.hpp:189
static op create(std::string path, buffer data, acl rules, create_mode mode=create_mode::normal)
Definition: multi.cpp:119
A part of a result. The behavior depends on the type of op provided to the original transaction...
Definition: multi.hpp:246
The result of a successful client::commit operation.
Definition: multi.hpp:242
Represents a version of the data.
Definition: types.hpp:158
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:79
size_type size() const
The number of results in this transaction bundle.
Definition: multi.hpp:298
Data for a op::erase operation.
Definition: multi.hpp:76
const check_data & as_check() const
Get the check-specific data.
Definition: multi.cpp:97
void emplace_back(TArgs &&...args)
Construct a result emplace on the end of the list using args.
Definition: multi.hpp:337
Data for a op::create operation.
Definition: multi.hpp:55
void push_back(part &&x)
Definition: multi.hpp:344
static op erase(std::string path, version check=version::any())
Delete the entry at the given path if it matches the version check.
Definition: multi.cpp:148
static op set(std::string path, buffer data, version check=version::any())
Set the data for the entry at path if it matches the version check.
Definition: multi.cpp:173
iterator begin()
Definition: multi.hpp:318