Adding the fetch_execute function to the C++ interface.
[awesomized/libmemcached] / libmemcached / memcached.hh
1 #include <libmemcached/memcached.h>
2
3 #include <string>
4 #include <vector>
5
6 class Memcached
7 {
8 public:
9
10 Memcached()
11 :
12 memc(),
13 result()
14 {
15 memcached_create(&memc);
16 }
17
18 Memcached(memcached_st *clone)
19 :
20 memc(),
21 result()
22 {
23 memcached_clone(&memc, clone);
24 }
25
26 ~Memcached()
27 {
28 memcached_free(&memc);
29 }
30
31 std::string fetch(std::string &key, size_t *key_length, size_t *value_length)
32 {
33 uint32_t flags;
34 memcached_return rc;
35 std::string ret_val;
36
37 char *value= memcached_fetch(&memc, const_cast<char *>(key.c_str()), key_length,
38 value_length, &flags, &rc);
39 if (value)
40 {
41 ret_val.assign(value);
42 }
43 return ret_val;
44 }
45
46 std::string get(const std::string &key, size_t *value_length)
47 {
48 uint32_t flags;
49 memcached_return rc;
50 std::string ret_val;
51
52 char *value= memcached_get(&memc, key.c_str(), key.length(),
53 value_length, &flags, &rc);
54 if (value)
55 {
56 ret_val.assign(value);
57 }
58 return ret_val;
59 }
60
61 std::string get_by_key(const std::string &master_key,
62 const std::string &key,
63 size_t *value_length)
64 {
65 uint32_t flags;
66 memcached_return rc;
67 std::string ret_val;
68
69 char *value= memcached_get_by_key(&memc, master_key.c_str(), master_key.length(),
70 key.c_str(), key.length(),
71 value_length, &flags, &rc);
72 if (value)
73 {
74 ret_val.assign(value);
75 }
76 return ret_val;
77 }
78
79 bool mget(std::vector<std::string> &keys)
80 {
81 /*
82 * Construct an array which will contain the length
83 * of each of the strings in the input vector. Also, to
84 * interface with the memcached C API, we need to convert
85 * the vector of std::string's to a vector of char *.
86 */
87 size_t *key_len= static_cast<size_t *>(malloc(keys.size() * sizeof(size_t)));
88 if (key_len == NULL)
89 {
90 return false;
91 }
92 std::vector<char *> real_keys;
93 std::vector<std::string>::iterator it= keys.begin();
94 int i= 0;
95 while (it != keys.end())
96 {
97 real_keys.push_back(const_cast<char *>((*it).c_str()));
98 key_len[i++]= (*it).length();
99 ++it;
100 }
101
102 /*
103 * If the std::vector of keys is empty then we cannot
104 * call memcached_mget as we will get undefined behavior.
105 */
106 if (!real_keys.empty())
107 {
108 memcached_return rc= memcached_mget(&memc, &real_keys[0], key_len,
109 static_cast<unsigned int>(real_keys.size()));
110 return (rc == MEMCACHED_SUCCESS);
111 }
112
113 return false;
114 }
115
116 bool set(const std::string &key,
117 const std::string &value,
118 time_t expiration,
119 uint32_t flags)
120 {
121 memcached_return rc= memcached_set(&memc,
122 key.c_str(), key.length(),
123 value.c_str(), value.length(),
124 expiration, flags);
125 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
126 }
127
128 bool set_all(std::vector<std::string> &keys,
129 std::vector<std::string> &values,
130 time_t expiration,
131 uint32_t flags)
132 {
133 if (keys.size() != values.size())
134 {
135 return false;
136 }
137 bool retval= true;
138 std::vector<std::string>::iterator key_it= keys.begin();
139 std::vector<std::string>::iterator val_it= values.begin();
140 while (key_it != keys.end())
141 {
142 retval= set((*key_it), (*val_it), expiration, flags);
143 if (retval == false)
144 {
145 return retval;
146 }
147 ++key_it;
148 ++val_it;
149 }
150 return retval;
151 }
152
153 bool set_by_key(const std::string &master_key,
154 const std::string &key,
155 const std::string &value,
156 time_t expiration,
157 uint32_t flags)
158 {
159 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
160 master_key.length(),
161 key.c_str(), key.length(),
162 value.c_str(), value.length(),
163 expiration,
164 flags);
165 return (rc == MEMCACHED_SUCCESS);
166 }
167
168 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
169 {
170 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
171 offset, value);
172 return (rc == MEMCACHED_SUCCESS);
173 }
174
175 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
176 {
177 memcached_return rc= memcached_decrement(&memc, key.c_str(),
178 key.length(),
179 offset, value);
180 return (rc == MEMCACHED_SUCCESS);
181 }
182
183
184 bool add(const std::string &key, const std::string &value)
185 {
186 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
187 value.c_str(), value.length(), 0, 0);
188 return (rc == MEMCACHED_SUCCESS);
189 }
190
191 bool add_by_key(const std::string &master_key,
192 const std::string &key,
193 const std::string &value)
194 {
195 memcached_return rc= memcached_add_by_key(&memc,
196 master_key.c_str(),
197 master_key.length(),
198 key.c_str(),
199 key.length(),
200 value.c_str(),
201 value.length(),
202 0, 0);
203 return (rc == MEMCACHED_SUCCESS);
204 }
205
206 bool replace(const std::string &key, const std::string &value)
207 {
208 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
209 value.c_str(), value.length(),
210 0, 0);
211 return (rc == MEMCACHED_SUCCESS);
212 }
213
214 bool replace_by_key(const std::string &master_key,
215 const std::string &key,
216 const std::string &value)
217 {
218 memcached_return rc= memcached_replace_by_key(&memc,
219 master_key.c_str(),
220 master_key.length(),
221 key.c_str(),
222 key.length(),
223 value.c_str(),
224 value.length(),
225 0, 0);
226 return (rc == MEMCACHED_SUCCESS);
227 }
228
229 bool prepend(const std::string &key, const std::string &value)
230 {
231 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
232 value.c_str(), value.length(), 0, 0);
233 return (rc == MEMCACHED_SUCCESS);
234 }
235
236 bool prepend_by_key(const std::string &master_key,
237 const std::string &key,
238 const std::string &value)
239 {
240 memcached_return rc= memcached_prepend_by_key(&memc,
241 master_key.c_str(),
242 master_key.length(),
243 key.c_str(),
244 key.length(),
245 value.c_str(),
246 value.length(),
247 0,
248 0);
249 return (rc == MEMCACHED_SUCCESS);
250 }
251
252 bool append(const std::string &key, const std::string &value)
253 {
254 memcached_return rc= memcached_append(&memc,
255 key.c_str(),
256 key.length(),
257 value.c_str(),
258 value.length(),
259 0, 0);
260 return (rc == MEMCACHED_SUCCESS);
261 }
262
263 bool append_by_key(const std::string &master_key,
264 const std::string &key,
265 const std::string &value)
266 {
267 memcached_return rc= memcached_append_by_key(&memc,
268 master_key.c_str(),
269 master_key.length(),
270 key.c_str(),
271 key.length(),
272 value.c_str(),
273 value.length(),
274 0, 0);
275 return (rc == MEMCACHED_SUCCESS);
276 }
277
278 bool cas(const std::string &key,
279 const std::string &value,
280 uint64_t cas_arg)
281 {
282 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
283 value.c_str(), value.length(),
284 0, 0, cas_arg);
285 return (rc == MEMCACHED_SUCCESS);
286 }
287
288 bool cas_by_key(const std::string &master_key,
289 const std::string &key,
290 const std::string &value,
291 uint64_t cas_arg)
292 {
293 memcached_return rc= memcached_cas_by_key(&memc,
294 master_key.c_str(),
295 master_key.length(),
296 key.c_str(),
297 key.length(),
298 value.c_str(),
299 value.length(),
300 0, 0, cas_arg);
301 return (rc == MEMCACHED_SUCCESS);
302 }
303
304 // using 'remove' vs. 'delete' since 'delete' is a keyword
305 bool remove(const std::string &key)
306 {
307 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
308 return (rc == MEMCACHED_SUCCESS);
309 }
310
311 bool delete_by_key(const std::string &master_key,
312 const std::string &key)
313 {
314 memcached_return rc= memcached_delete_by_key(&memc,
315 master_key.c_str(),
316 master_key.length(),
317 key.c_str(),
318 key.length(),
319 0);
320 return (rc == MEMCACHED_SUCCESS);
321 }
322
323 bool flush(time_t expiration)
324 {
325 memcached_return rc= memcached_flush(&memc, expiration);
326 return (rc == MEMCACHED_SUCCESS);
327 }
328
329 bool fetch_execute(memcached_execute_function *callback,
330 void *context,
331 unsigned int num_of_callbacks)
332 {
333 memcached_return rc= memcached_fetch_execute(&memc,
334 callback,
335 context,
336 num_of_callbacks);
337 return (rc == MEMCACHED_SUCCESS);
338 }
339
340 const std::string lib_version() const
341 {
342 const char *ver= memcached_lib_version();
343 const std::string version(ver);
344 return version;
345 }
346
347 private:
348 memcached_st memc;
349 memcached_result_st result;
350 };