zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
configuration.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 #include <zk/optional.hpp>
5 #include <zk/string_view.hpp>
6 #include <zk/types.hpp>
7 
8 #include <chrono>
9 #include <cstdint>
10 #include <iosfwd>
11 #include <string>
12 #include <map>
13 #include <set>
14 #include <vector>
15 
16 namespace zk::server
17 {
18 
21 
29 struct server_id :
30  strong_id<server_id, std::size_t>
31 {
35  server_id(std::size_t value) :
36  strong_id<server_id, std::size_t>(value)
37  {
38  ensure_valid();
39  }
40 
44  void ensure_valid() const;
45 
47  friend std::ostream& operator<<(std::ostream&, const server_id&);
48 };
49 
71 class configuration final
72 {
73 public:
74  using duration_type = std::chrono::milliseconds;
75 
76 public:
78  static const std::uint16_t default_client_port;
79 
81  static const std::uint16_t default_peer_port;
82 
84  static const std::uint16_t default_leader_port;
85 
87  static const duration_type default_tick_time;
88 
90  static const std::size_t default_init_limit;
91 
93  static const std::size_t default_sync_limit;
94 
96  static const std::set<std::string> default_four_letter_word_whitelist;
97 
100  static const std::set<std::string> all_four_letter_word_whitelist;
101 
104  static const std::set<std::string> known_four_letter_word_whitelist;
105 
106 public:
109  static configuration make_minimal(std::string data_directory, std::uint16_t client_port = default_client_port);
110 
112  static configuration from_file(std::string filename);
113 
115  static configuration from_stream(std::istream& stream);
116 
118  static configuration from_lines(std::vector<std::string> lines);
119 
121  static configuration from_string(string_view value);
122 
123  ~configuration() noexcept;
124 
126  const optional<std::string>& source_file() const { return _source_file; }
127 
130  bool is_minimal() const;
131 
134  std::uint16_t client_port() const;
135  configuration& client_port(optional<std::uint16_t> port);
137 
140  const optional<std::string>& data_directory() const;
141  configuration& data_directory(optional<std::string> path);
143 
147  duration_type tick_time() const;
148  configuration& tick_time(optional<duration_type> time);
150 
154  std::size_t init_limit() const;
155  configuration& init_limit(optional<std::size_t> limit);
157 
160  std::size_t sync_limit() const;
161  configuration& sync_limit(optional<std::size_t> limit);
163 
168  bool leader_serves() const;
169  configuration& leader_serves(optional<bool> serve);
171 
191  const std::set<std::string>& four_letter_word_whitelist() const;
192  configuration& four_letter_word_whitelist(optional<std::set<std::string>> words);
194 
196  std::map<server_id, std::string> servers() const;
197 
207  std::string hostname,
208  std::uint16_t peer_port = default_peer_port,
209  std::uint16_t leader_port = default_leader_port
210  );
211 
214  std::map<std::string, std::string> unknown_settings() const;
215 
220  configuration& add_setting(std::string key, std::string value);
221 
225  void save(std::ostream& stream) const;
226 
229  void save_file(std::string filename);
230 
235  friend bool operator==(const configuration& lhs, const configuration& rhs);
236  friend bool operator!=(const configuration& lhs, const configuration& rhs);
238 
239 private:
240  using line_list = std::vector<std::string>;
241 
242  template <typename T>
243  struct setting
244  {
245  setting() noexcept;
246  setting(T value, std::size_t line) noexcept;
247 
248  optional<T> value;
249  std::size_t line;
250  };
251 
252  template <typename T, typename FEncode>
253  void set(setting<T>& target, optional<T> value, string_view key, const FEncode& encode);
254 
255  template <typename T>
256  void set(setting<T>& target, optional<T> value, string_view key);
257 
258 private:
259  explicit configuration();
260 
261 private:
262  optional<std::string> _source_file;
263  line_list _lines;
264  setting<std::uint16_t> _client_port;
265  setting<std::string> _data_directory;
266  setting<duration_type> _tick_time;
267  setting<std::size_t> _init_limit;
268  setting<std::size_t> _sync_limit;
269  setting<bool> _leader_serves;
270  setting<std::set<std::string>> _four_letter_word_whitelist;
271  std::map<server_id, setting<std::string>> _server_paths;
272  std::map<std::string, setting<std::string>> _unknown_settings;
273 };
274 
276 
277 }
278 
279 namespace std
280 {
281 
282 template <>
283 struct hash<zk::server::server_id>
284 {
286  using result_type = std::size_t;
287 
288  result_type operator()(const argument_type& x) const
289  {
290  return zk::hash(x);
291  }
292 };
293 
294 }
auto map(FUnary &&transform, const optional< T > &...x) -> optional< decltype(transform(x.value()...))>
Apply transform with the arguments in x iff all of them have a value. Otherwise, nullopt will be retu...
Definition: optional.hpp:25
std::map< server_id, std::string > servers() const
Get the servers which are part of the ZooKeeper ensemble.
Represents the ID of a server in the ensemble.
Base type for creating strong ID types.
Definition: types.hpp:28
static const std::size_t default_sync_limit
The default value for sync_limit.
const optional< std::string > & source_file() const
Get the source file. This will only have a value if this was created by from_file.
bool is_minimal() const
Check if this is a "minimal" configuration – meaning it only has a data_directory and client_port se...
const optional< std::string > & data_directory() const
Imports of optional and nullopt_t types, as well as the nullopt constexpr.
static configuration from_lines(std::vector< std::string > lines)
Load configuration from the provided lines.
static configuration make_minimal(std::string data_directory, std::uint16_t client_port=default_client_port)
Creates a minimal configuration, setting the four needed values.
server_id(std::size_t value)
Create an instance from the given value.
static const std::set< std::string > all_four_letter_word_whitelist
A value for four_letter_word_whitelist that enables all commands.
const std::set< std::string > & four_letter_word_whitelist() const
std::map< std::string, std::string > unknown_settings() const
Get settings that were in the configuration file (or manually added with add_setting) but unknown to ...
static configuration from_file(std::string filename)
Load the configuration from a file.
static const std::set< std::string > known_four_letter_word_whitelist
All known values allowed in four_letter_word_whitelist.
static configuration from_stream(std::istream &stream)
Load configuration from the provided stream.
static const std::uint16_t default_client_port
The default value for client_port.
static const std::uint16_t default_leader_port
The default value for leader_port.
duration_type tick_time() const
static const std::size_t default_init_limit
The default value for init_limit.
static const std::uint16_t default_peer_port
The default value for peer_port.
static configuration from_string(string_view value)
Load configuration directly from the in-memory value.
std::size_t init_limit() const
friend std::ostream & operator<<(std::ostream &, const server_id &)
Debug print for this instance.
void ensure_valid() const
Check that this ID is a valid one.
Imports the string_view type as std::string_view.
Represents a configuration which should be run by server instance.
void save(std::ostream &stream) const
Write this configuration to the provided stream.
friend bool operator==(const configuration &lhs, const configuration &rhs)
static const duration_type default_tick_time
The default value for tick_time.
void save_file(std::string filename)
Save this configuration to filename.
std::size_t sync_limit() const
std::uint16_t client_port() const
std::size_t hash(const acl_rule &self)
Compute a hash for the given rule.
Definition: acl.cpp:53
configuration & add_server(server_id id, std::string hostname, std::uint16_t peer_port=default_peer_port, std::uint16_t leader_port=default_leader_port)
Add a new server to the configuration.
value_type value
Underlying value of this ID.
Definition: types.hpp:34
static const std::set< std::string > default_four_letter_word_whitelist
The default value for four_letter_word_whitelist.
configuration & add_setting(std::string key, std::string value)
Add an arbitrary setting with the key and value.