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 <vector>
14 
15 namespace zk::server
16 {
17 
20 
28 struct server_id :
29  strong_id<server_id, std::size_t>
30 {
34  server_id(std::size_t value) :
35  strong_id<server_id, std::size_t>(value)
36  {
37  ensure_valid();
38  }
39 
43  void ensure_valid() const;
44 
46  friend std::ostream& operator<<(std::ostream&, const server_id&);
47 };
48 
70 class configuration final
71 {
72 public:
73  using duration_type = std::chrono::milliseconds;
74 
75 public:
77  static std::uint16_t default_client_port;
78 
80  static std::uint16_t default_peer_port;
81 
83  static std::uint16_t default_leader_port;
84 
86  static duration_type default_tick_time;
87 
89  static std::size_t default_init_limit;
90 
92  static std::size_t default_sync_limit;
93 
94 public:
97  static configuration make_minimal(std::string data_directory, std::uint16_t client_port = default_client_port);
98 
100  static configuration from_file(std::string filename);
101 
103  static configuration from_stream(std::istream& stream);
104 
106  static configuration from_lines(std::vector<std::string> lines);
107 
109  static configuration from_string(string_view value);
110 
111  ~configuration() noexcept;
112 
114  const optional<std::string>& source_file() const { return _source_file; }
115 
118  bool is_minimal() const;
119 
122  std::uint16_t client_port() const;
123  configuration& client_port(optional<std::uint16_t> port);
125 
128  const optional<std::string>& data_directory() const;
129  configuration& data_directory(optional<std::string> path);
131 
135  duration_type tick_time() const;
136  configuration& tick_time(optional<duration_type> time);
138 
142  std::size_t init_limit() const;
143  configuration& init_limit(optional<std::size_t> limit);
145 
148  std::size_t sync_limit() const;
149  configuration& sync_limit(optional<std::size_t> limit);
151 
156  optional<bool> leader_serves() const;
157  configuration& leader_serves(optional<bool> serve);
159 
161  std::map<server_id, std::string> servers() const;
162 
172  std::string hostname,
173  std::uint16_t peer_port = default_peer_port,
174  std::uint16_t leader_port = default_leader_port
175  );
176 
179  std::map<std::string, std::string> unknown_settings() const;
180 
185  configuration& add_setting(std::string key, std::string value);
186 
190  void save(std::ostream& stream) const;
191 
194  void save_file(std::string filename);
195 
200  friend bool operator==(const configuration& lhs, const configuration& rhs);
201  friend bool operator!=(const configuration& lhs, const configuration& rhs);
203 
204 private:
205  using line_list = std::vector<std::string>;
206 
207  template <typename T>
208  struct setting
209  {
210  setting() noexcept;
211  setting(T value, std::size_t line) noexcept;
212 
213  optional<T> value;
214  std::size_t line;
215  };
216 
217  template <typename T, typename FEncode>
218  void set(setting<T>& target, optional<T> value, string_view key, const FEncode& encode);
219 
220  template <typename T>
221  void set(setting<T>& target, optional<T> value, string_view key);
222 
223 private:
224  explicit configuration();
225 
226 private:
227  optional<std::string> _source_file;
228  line_list _lines;
229  setting<std::uint16_t> _client_port;
230  setting<std::string> _data_directory;
231  setting<duration_type> _tick_time;
232  setting<std::size_t> _init_limit;
233  setting<std::size_t> _sync_limit;
234  setting<bool> _leader_serves;
235  std::map<server_id, setting<std::string>> _server_paths;
236  std::map<std::string, setting<std::string>> _unknown_settings;
237 };
238 
240 
241 }
242 
243 namespace std
244 {
245 
246 template <>
247 struct hash<zk::server::server_id>
248 {
250  using result_type = std::size_t;
251 
252  result_type operator()(const argument_type& x) const
253  {
254  return zk::hash(x);
255  }
256 };
257 
258 }
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
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 std::uint16_t default_leader_port
The default value for leader_port.
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.
static std::uint16_t default_client_port
The default value for client_port.
server_id(std::size_t value)
Create an instance from the given value.
static std::size_t default_sync_limit
The default value for sync_limit.
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 configuration from_stream(std::istream &stream)
Load configuration from the provided stream.
duration_type tick_time() const
static configuration from_string(string_view value)
Load configuration directly from the in-memory value.
std::size_t init_limit() const
static std::size_t default_init_limit
The default value for init_limit.
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.
static duration_type default_tick_time
The default value for tick_time.
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)
void save_file(std::string filename)
Save this configuration to filename.
static std::uint16_t default_peer_port
The default value for peer_port.
std::size_t sync_limit() const
std::uint16_t client_port() const
optional< bool > leader_serves() 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
configuration & add_setting(std::string key, std::string value)
Add an arbitrary setting with the key and value.