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