Merge branch 'master' of https://github.com/resetius/elliptics
[elliptics.git] / include / elliptics / cppdef.h
blob9caf95dbff9623c8500381327c11ecbe42d586ad
1 /*
2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #ifndef __EDEF_H
17 #define __EDEF_H
19 #include <errno.h>
21 #include <elliptics/typedefs.h>
22 #include <elliptics/packet.h>
23 #include <elliptics/interface.h>
25 #include <iostream>
26 #include <fstream>
27 #include <exception>
28 #include <list>
29 #include <stdexcept>
30 #include <string>
31 #include <vector>
33 namespace ioremap { namespace elliptics {
35 class elliptics_error : public std::runtime_error {
36 public:
37 explicit elliptics_error(int err);
38 int error_code() const;
39 private:
40 static std::string convert(int err);
41 int errno_;
44 class not_found_error : public elliptics_error {
45 public:
46 explicit not_found_error();
49 class timeout_error : public elliptics_error {
50 public:
51 explicit timeout_error();
54 void throw_exception(int err);
56 struct addr_tuple {
57 addr_tuple(const std::string &l_host, const int l_port, const int l_family = AF_INET) :
58 host(l_host), port(l_port), family(l_family) {}
60 std::string host;
61 int port;
62 int family;
65 class logger {
66 public:
67 logger(const int level = DNET_LOG_INFO) {
68 ll.log_level = level;
69 ll.log = logger::real_logger;
70 ll.log_private = this;
72 virtual ~logger() {};
74 virtual void log(const int level, const char *msg) = 0;
77 * Clone is used instead of 'virtual' copy constructor, since we have to
78 * hold a reference to object outside of our scope, namely python created
79 * logger. This is also a reason we return 'unsigned long' instead of
80 * 'logger *' - python does not have pointer.
82 virtual unsigned long clone(void) = 0;
84 static void real_logger(void *priv, const int level, const char *msg);
85 int get_log_level(void) { return ll.log_level; };
86 struct dnet_log *get_dnet_log(void) { return &ll; };
87 protected:
88 struct dnet_log ll;
91 class log_file : public logger {
92 public:
93 log_file(const char *file, const int level = DNET_LOG_INFO);
94 virtual ~log_file();
96 virtual unsigned long clone(void);
97 virtual void log(const int level, const char *msg);
99 std::string *file;
100 private:
102 * Oh shi, I put pointer here to avoid boost::python compiler issues,
103 * when it tries to copy stream, which is not allowed
105 std::ofstream *stream;
108 class callback {
109 public:
110 callback();
111 virtual ~callback();
113 virtual int handle(struct dnet_net_state *state, struct dnet_cmd *cmd);
115 static int complete_callback(struct dnet_net_state *st, struct dnet_cmd *cmd, void *priv) {
116 callback *c = reinterpret_cast<callback *>(priv);
118 return c->handle(st, cmd);
121 std::string wait(int completed = 1);
123 protected:
124 std::string data;
125 pthread_cond_t wait_cond;
126 pthread_mutex_t lock;
127 int complete;
130 class node {
131 public:
132 /* we shold use logger and proper copy constructor here, but not this time */
133 node(logger &l);
134 node(logger &l, struct dnet_config &cfg);
135 node(logger &l, const std::string &config_path);
136 virtual ~node();
138 void parse_config(const std::string &path, struct dnet_config &cfg,
139 std::list<addr_tuple> &remotes,
140 std::vector<int> &groups,
141 int &log_level);
143 void add_remote(const char *addr, const int port, const int family = AF_INET);
145 void set_timeouts(const int wait_timeout, const int check_timeout);
147 protected:
148 int write_data_ll(struct dnet_id *id, void *remote, unsigned int remote_len,
149 void *data, unsigned int size, callback &c,
150 uint64_t cflags, unsigned int ioflags, int type);
151 struct dnet_node *m_node;
152 logger *m_log;
154 friend class session;
157 class session {
158 public:
159 session(node &n);
160 virtual ~session();
162 void transform(const std::string &data, struct dnet_id &id);
164 void add_groups(std::vector<int> &groups);
165 std::vector<int> get_groups() {return groups;};
167 void read_file(struct dnet_id &id, const std::string &file, uint64_t offset, uint64_t size);
168 void read_file(const std::string &remote, const std::string &file,
169 uint64_t offset, uint64_t size, int type);
171 void write_file(struct dnet_id &id, const std::string &file, uint64_t local_offset,
172 uint64_t offset, uint64_t size, uint64_t cflags, unsigned int ioflags);
173 void write_file(const std::string &remote, const std::string &file,
174 uint64_t local_offset, uint64_t offset, uint64_t size,
175 uint64_t cflags, unsigned int ioflags, int type);
177 std::string read_data_wait(struct dnet_id &id, uint64_t offset, uint64_t size,
178 uint64_t cflags, uint32_t ioflags);
179 std::string read_data_wait(const std::string &remote, uint64_t offset, uint64_t size,
180 uint64_t cflags, uint32_t ioflags, int type);
182 void prepare_latest(struct dnet_id &id, uint64_t cflags, std::vector<int> &groups);
184 std::string read_latest(struct dnet_id &id, uint64_t offset, uint64_t size,
185 uint64_t cflags, uint32_t ioflags);
186 std::string read_latest(const std::string &remote, uint64_t offset, uint64_t size,
187 uint64_t cflags, uint32_t ioflags, int type);
189 std::string write_compare_and_swap(const struct dnet_id &id, const std::string &str,
190 const struct dnet_id &old_csum, uint64_t remote_offset, uint64_t cflags, unsigned int ioflags);
191 std::string write_compare_and_swap(const std::string &remote, const std::string &str,
192 const struct dnet_id &old_csum, uint64_t remote_offset, uint64_t cflags, unsigned int ioflags, int type);
194 std::string write_data_wait(struct dnet_id &id, const std::string &str,
195 uint64_t remote_offset, uint64_t cflags, unsigned int ioflags);
196 std::string write_data_wait(const std::string &remote, const std::string &str,
197 uint64_t remote_offset, uint64_t cflags, unsigned int ioflags, int type);
199 std::string write_prepare(const std::string &remote, const std::string &str, uint64_t remote_offset,
200 uint64_t psize, uint64_t cflags, unsigned int ioflags, int type);
201 std::string write_commit(const std::string &remote, const std::string &str, uint64_t remote_offset,
202 uint64_t csize, uint64_t cflags, unsigned int ioflags, int type);
203 std::string write_plain(const std::string &remote, const std::string &str, uint64_t remote_offset,
204 uint64_t cflags, unsigned int ioflags, int type);
206 std::string write_prepare(const struct dnet_id &id, const std::string &str, uint64_t remote_offset,
207 uint64_t psize, uint64_t cflags, unsigned int ioflags);
208 std::string write_commit(const struct dnet_id &id, const std::string &str, uint64_t remote_offset,
209 uint64_t csize, uint64_t cflags, unsigned int ioflags);
210 std::string write_plain(const struct dnet_id &id, const std::string &str, uint64_t remote_offset,
211 uint64_t cflags, unsigned int ioflags);
213 std::string write_cache(struct dnet_id &id, const std::string &str,
214 uint64_t cflags, unsigned int ioflags, long timeout);
215 std::string write_cache(const std::string &key, const std::string &str,
216 uint64_t cflags, unsigned int ioflags, long timeout);
220 std::string lookup_addr(const std::string &remote, const int group_id);
221 std::string lookup_addr(const struct dnet_id &id);
223 std::string create_metadata(const struct dnet_id &id, const std::string &obj,
224 const std::vector<int> &groups, const struct timespec &ts);
225 int write_metadata(const struct dnet_id &id, const std::string &obj,
226 const std::vector<int> &groups, const struct timespec &ts, uint64_t cflags);
228 void lookup(const std::string &data, const callback &c);
229 void lookup(const struct dnet_id &id, const callback &c);
230 std::string lookup(const std::string &data);
231 std::string lookup(const struct dnet_id &id);
233 void remove_raw(struct dnet_id &id, uint64_t cflags, uint64_t ioflags);
234 void remove_raw(const std::string &data, int type, uint64_t cflags, uint64_t ioflags);
235 void remove(struct dnet_id &id);
236 void remove(const std::string &data, int type = EBLOB_TYPE_DATA);
238 std::string stat_log();
240 int state_num();
242 int request_cmd(struct dnet_trans_control &ctl);
244 std::string read_metadata(struct dnet_id &id);
246 void update_status(const char *addr, const int port, const int family, struct dnet_node_status *status);
247 void update_status(struct dnet_id &id, struct dnet_node_status *status);
249 std::vector<std::string> read_data_range(struct dnet_io_attr &io, int group_id, uint64_t cflags = 0);
251 std::vector<struct dnet_io_attr> remove_data_range(struct dnet_io_attr &io, int group_id, uint64_t cflags = 0);
253 std::vector<std::pair<struct dnet_id, struct dnet_addr> > get_routes();
256 * start execution of the given event
258 std::string exec_locked(struct dnet_id *id, const std::string &event, const std::string &data,
259 const std::string &binary);
260 std::string exec_unlocked(struct dnet_id *id, const std::string &event, const std::string &data,
261 const std::string &binary);
264 * execution with saving ID of the original (blocked) caller
266 std::string push_locked(struct dnet_id *id, const struct sph &sph,
267 const std::string &event, const std::string &data, const std::string &binary);
268 std::string push_unlocked(struct dnet_id *id, const struct sph &sph,
269 const std::string &event, const std::string &data, const std::string &binary);
271 /* send reply back to blocked execution client */
272 void reply(const struct sph &sph, const std::string &event, const std::string &data,
273 const std::string &binary);
275 std::vector<std::string> bulk_read(const std::vector<struct dnet_io_attr> &ios, uint64_t cflags = 0);
276 std::vector<std::string> bulk_read(const std::vector<std::string> &keys, uint64_t cflags = 0);
278 std::string bulk_write(const std::vector<struct dnet_io_attr> &ios,
279 const std::vector<std::string> &data, uint64_t cflags);
281 struct dnet_node * get_node();
283 protected:
284 struct dnet_session *m_session;
285 node *m_node;
287 std::vector<int> groups;
289 std::string raw_exec(struct dnet_id *id,
290 const struct sph *sph,
291 const std::string &event,
292 const std::string &data,
293 const std::string &binary,
294 bool lock);
295 std::string request(struct dnet_id *id, struct sph *sph, bool lock);
299 }}; /* namespace ioremap::elliptics */
301 #endif /* __EDEF_H */