This commit contains the following changes:
[m6w6/libmemcached] / libmemcached / memcached.hpp
1 /*
2 * Summary: C++ interface for memcached server
3 *
4 * Copy: See Copyright for the status of this software.
5 *
6 * Authors: Padraig O'Sullivan <osullivan.padraig@gmail.com>
7 * Patrick Galbraith <patg@patg.net>
8 */
9
10 /**
11 * @file memcached.hpp
12 * @brief Libmemcached C++ interface
13 */
14
15 #ifndef LIBMEMCACHEDPP_H
16 #define LIBMEMCACHEDPP_H
17
18 #include <libmemcached/memcached.h>
19
20 #include <string.h>
21
22 #include <string>
23 #include <vector>
24 #include <map>
25
26 namespace memcache
27 {
28
29 /**
30 * This is the core memcached library (if later, other objects
31 * are needed, they will be created from this class).
32 */
33 class Memcache
34 {
35 public:
36
37 Memcache()
38 :
39 memc(),
40 result()
41 {
42 memcached_create(&memc);
43 }
44
45 Memcache(memcached_st *clone)
46 :
47 memc(),
48 result()
49 {
50 memcached_clone(&memc, clone);
51 }
52
53 Memcache(const Memcache &rhs)
54 :
55 memc(),
56 result()
57 {
58 memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
59 }
60
61 ~Memcache()
62 {
63 memcached_free(&memc);
64 }
65
66 /**
67 * Get the internal memcached_st *
68 */
69 memcached_st &getImpl()
70 {
71 return memc;
72 }
73
74 /**
75 * Get the internal memcached_st *
76 */
77 const memcached_st &getImpl() const
78 {
79 return memc;
80 }
81
82 /**
83 * Return an error string for the given return structure.
84 *
85 * @param[in] rc a memcached_return structure
86 * @return error string corresponding to given return code in the library.
87 */
88 const std::string getError(memcached_return rc) const
89 {
90 /* first parameter to strerror is unused */
91 return memcached_strerror(NULL, rc);
92 }
93
94 bool fetch(std::string &key,
95 std::vector<char> &ret_val,
96 uint32_t *flags,
97 memcached_return *rc)
98 {
99 char ret_key[MEMCACHED_MAX_KEY];
100 size_t value_length= 0;
101 size_t key_length= 0;
102 char *value= memcached_fetch(&memc, ret_key, &key_length,
103 &value_length, flags, rc);
104 if (value && ret_val.empty())
105 {
106 ret_val.reserve(value_length);
107 ret_val.assign(value, value + value_length);
108 key.assign(ret_key);
109 return true;
110 }
111 return false;
112 }
113
114 std::vector<char> &get(const std::string &key,
115 std::vector<char> &ret_val)
116 {
117 uint32_t flags= 0;
118 memcached_return rc;
119 size_t value_length= 0;
120
121 if (key.empty())
122 {
123 return ret_val;
124 }
125 char *value= memcached_get(&memc, key.c_str(), key.length(),
126 &value_length, &flags, &rc);
127 if (value != NULL && ret_val.empty())
128 {
129 ret_val.reserve(value_length);
130 ret_val.assign(value, value + value_length);
131 }
132 return ret_val;
133 }
134
135 std::vector<char> &getByKey(const std::string &master_key,
136 const std::string &key,
137 std::vector<char> &ret_val)
138 {
139 uint32_t flags= 0;
140 memcached_return rc;
141 size_t value_length= 0;
142
143 if (master_key.empty() || key.empty())
144 {
145 return ret_val;
146 }
147 char *value= memcached_get_by_key(&memc,
148 master_key.c_str(), master_key.length(),
149 key.c_str(), key.length(),
150 &value_length, &flags, &rc);
151 if (value)
152 {
153 ret_val.reserve(value_length);
154 ret_val.assign(value, value + value_length);
155 }
156 return ret_val;
157 }
158
159 bool mget(std::vector<std::string> &keys)
160 {
161 std::vector<const char *> real_keys;
162 std::vector<size_t> key_len;
163 /*
164 * Construct an array which will contain the length
165 * of each of the strings in the input vector. Also, to
166 * interface with the memcached C API, we need to convert
167 * the vector of std::string's to a vector of char *.
168 */
169 real_keys.reserve(keys.size());
170 key_len.reserve(keys.size());
171
172 std::vector<std::string>::iterator it= keys.begin();
173
174 while (it != keys.end())
175 {
176 real_keys.push_back(const_cast<char *>((*it).c_str()));
177 key_len.push_back((*it).length());
178 ++it;
179 }
180
181 /*
182 * If the std::vector of keys is empty then we cannot
183 * call memcached_mget as we will get undefined behavior.
184 */
185 if (! real_keys.empty())
186 {
187 memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0],
188 real_keys.size());
189 return (rc == MEMCACHED_SUCCESS);
190 }
191
192 return false;
193 }
194
195 bool set(const std::string &key,
196 const std::vector<char> &value,
197 time_t expiration,
198 uint32_t flags)
199 {
200 if (key.empty() || value.empty())
201 {
202 return false;
203 }
204 memcached_return rc= memcached_set(&memc,
205 key.c_str(), key.length(),
206 &value[0], value.size(),
207 expiration, flags);
208 return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
209 }
210
211 bool setAll(std::vector<std::string> &keys,
212 std::vector< std::vector<char> *> &values,
213 time_t expiration,
214 uint32_t flags)
215 {
216 if (keys.size() != values.size())
217 {
218 return false;
219 }
220 bool retval= true;
221 std::vector<std::string>::iterator key_it= keys.begin();
222 std::vector< std::vector<char> *>::iterator val_it= values.begin();
223 while (key_it != keys.end())
224 {
225 retval= set((*key_it), *(*val_it), expiration, flags);
226 if (retval == false)
227 {
228 return retval;
229 }
230 ++key_it;
231 ++val_it;
232 }
233 return retval;
234 }
235
236 bool setAll(std::map<const std::string, std::vector<char> > key_value_map,
237 time_t expiration,
238 uint32_t flags)
239 {
240 if (key_value_map.empty())
241 {
242 return false;
243 }
244 bool retval= true;
245 std::map<const std::string, std::vector<char> >::iterator it=
246 key_value_map.begin();
247 while (it != key_value_map.end())
248 {
249 retval= set(it->first, it->second, expiration, flags);
250 if (retval == false)
251 {
252 return false;
253 }
254 ++it;
255 }
256 return true;
257 }
258
259 bool setByKey(const std::string &master_key,
260 const std::string &key,
261 const std::vector<char> &value,
262 time_t expiration,
263 uint32_t flags)
264 {
265 if (master_key.empty() ||
266 key.empty() ||
267 value.empty())
268 {
269 return false;
270 }
271 memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(),
272 master_key.length(),
273 key.c_str(), key.length(),
274 &value[0], value.size(),
275 expiration,
276 flags);
277 return (rc == MEMCACHED_SUCCESS);
278 }
279
280 bool increment(const std::string &key, unsigned int offset, uint64_t *value)
281 {
282 if (key.empty())
283 {
284 return false;
285 }
286 memcached_return rc= memcached_increment(&memc, key.c_str(), key.length(),
287 offset, value);
288 return (rc == MEMCACHED_SUCCESS);
289 }
290
291 bool decrement(const std::string &key, unsigned int offset, uint64_t *value)
292 {
293 if (key.empty())
294 {
295 return false;
296 }
297 memcached_return rc= memcached_decrement(&memc, key.c_str(),
298 key.length(),
299 offset, value);
300 return (rc == MEMCACHED_SUCCESS);
301 }
302
303
304 bool add(const std::string &key, const std::vector<char> &value)
305 {
306 if (key.empty() || value.empty())
307 {
308 return false;
309 }
310 memcached_return rc= memcached_add(&memc, key.c_str(), key.length(),
311 &value[0], value.size(), 0, 0);
312 return (rc == MEMCACHED_SUCCESS);
313 }
314
315 bool addByKey(const std::string &master_key,
316 const std::string &key,
317 const std::vector<char> &value)
318 {
319 if (master_key.empty() ||
320 key.empty() ||
321 value.empty())
322 {
323 return false;
324 }
325 memcached_return rc= memcached_add_by_key(&memc,
326 master_key.c_str(),
327 master_key.length(),
328 key.c_str(),
329 key.length(),
330 &value[0],
331 value.size(),
332 0, 0);
333 return (rc == MEMCACHED_SUCCESS);
334 }
335
336 bool replace(const std::string &key, const std::vector<char> &value)
337 {
338 if (key.empty() ||
339 value.empty())
340 {
341 return false;
342 }
343 memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
344 &value[0], value.size(),
345 0, 0);
346 return (rc == MEMCACHED_SUCCESS);
347 }
348
349 bool replaceByKey(const std::string &master_key,
350 const std::string &key,
351 const std::vector<char> &value)
352 {
353 if (master_key.empty() ||
354 key.empty() ||
355 value.empty())
356 {
357 return false;
358 }
359 memcached_return rc= memcached_replace_by_key(&memc,
360 master_key.c_str(),
361 master_key.length(),
362 key.c_str(),
363 key.length(),
364 &value[0],
365 value.size(),
366 0, 0);
367 return (rc == MEMCACHED_SUCCESS);
368 }
369
370 bool prepend(const std::string &key, const std::vector<char> &value)
371 {
372 if (key.empty() || value.empty())
373 {
374 return false;
375 }
376 memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
377 &value[0], value.size(), 0, 0);
378 return (rc == MEMCACHED_SUCCESS);
379 }
380
381 bool prependByKey(const std::string &master_key,
382 const std::string &key,
383 const std::vector<char> &value)
384 {
385 if (master_key.empty() ||
386 key.empty() ||
387 value.empty())
388 {
389 return false;
390 }
391 memcached_return rc= memcached_prepend_by_key(&memc,
392 master_key.c_str(),
393 master_key.length(),
394 key.c_str(),
395 key.length(),
396 &value[0],
397 value.size(),
398 0,
399 0);
400 return (rc == MEMCACHED_SUCCESS);
401 }
402
403 bool append(const std::string &key, const std::vector<char> &value)
404 {
405 if (key.empty() || value.empty())
406 {
407 return false;
408 }
409 memcached_return rc= memcached_append(&memc,
410 key.c_str(),
411 key.length(),
412 &value[0],
413 value.size(),
414 0, 0);
415 return (rc == MEMCACHED_SUCCESS);
416 }
417
418 bool appendByKey(const std::string &master_key,
419 const std::string &key,
420 const std::vector<char> &value)
421 {
422 if (master_key.empty() ||
423 key.empty() ||
424 value.empty())
425 {
426 return false;
427 }
428 memcached_return rc= memcached_append_by_key(&memc,
429 master_key.c_str(),
430 master_key.length(),
431 key.c_str(),
432 key.length(),
433 &value[0],
434 value.size(),
435 0, 0);
436 return (rc == MEMCACHED_SUCCESS);
437 }
438
439 bool cas(const std::string &key,
440 const std::vector<char> &value,
441 uint64_t cas_arg)
442 {
443 if (key.empty() || value.empty())
444 {
445 return false;
446 }
447 memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
448 &value[0], value.size(),
449 0, 0, cas_arg);
450 return (rc == MEMCACHED_SUCCESS);
451 }
452
453 bool casByKey(const std::string &master_key,
454 const std::string &key,
455 const std::vector<char> &value,
456 uint64_t cas_arg)
457 {
458 if (master_key.empty() ||
459 key.empty() ||
460 value.empty())
461 {
462 return false;
463 }
464 memcached_return rc= memcached_cas_by_key(&memc,
465 master_key.c_str(),
466 master_key.length(),
467 key.c_str(),
468 key.length(),
469 &value[0],
470 value.size(),
471 0, 0, cas_arg);
472 return (rc == MEMCACHED_SUCCESS);
473 }
474
475 bool remove(const std::string &key)
476 {
477 if (key.empty())
478 {
479 return false;
480 }
481 memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
482 return (rc == MEMCACHED_SUCCESS);
483 }
484
485 bool removeByKey(const std::string &master_key,
486 const std::string &key)
487 {
488 if (master_key.empty() || key.empty())
489 {
490 return false;
491 }
492 memcached_return rc= memcached_delete_by_key(&memc,
493 master_key.c_str(),
494 master_key.length(),
495 key.c_str(),
496 key.length(),
497 0);
498 return (rc == MEMCACHED_SUCCESS);
499 }
500
501 bool flush(time_t expiration)
502 {
503 memcached_return rc= memcached_flush(&memc, expiration);
504 return (rc == MEMCACHED_SUCCESS);
505 }
506
507 bool fetchExecute(memcached_execute_function *callback,
508 void *context,
509 unsigned int num_of_callbacks)
510 {
511 memcached_return rc= memcached_fetch_execute(&memc,
512 callback,
513 context,
514 num_of_callbacks);
515 return (rc == MEMCACHED_SUCCESS);
516 }
517
518 /**
519 * Get the library version string.
520 * @return std::string containing a copy of the library version string.
521 */
522 const std::string libVersion() const
523 {
524 const char *ver= memcached_lib_version();
525 const std::string version(ver);
526 return version;
527 }
528
529 private:
530
531 memcached_st memc;
532 memcached_result_st result;
533 };
534
535 }
536
537 #endif /* LIBMEMCACHEDPP_H */