X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=libmemcached%2Fmemcached.hh;h=fa535c0c0e266768d134d89dfd887889d35338dd;hb=c4706160965fc3996b8c6dc25e899ca36b2c3058;hp=689a3bce068f21ff8421a33f264836d2f75f7b6f;hpb=ad00dffc2333bf341028de8962bf17a05e40da18;p=awesomized%2Flibmemcached diff --git a/libmemcached/memcached.hh b/libmemcached/memcached.hh index 689a3bce..fa535c0c 100644 --- a/libmemcached/memcached.hh +++ b/libmemcached/memcached.hh @@ -1,5 +1,18 @@ +/* + * Summary: C++ interface for memcached server + * + * Copy: See Copyright for the status of this software. + * + * Authors: Padraig O'Sullivan, Patrick Galbraith + */ + +#ifndef LIBMEMCACHEDPP_H +#define LIBMEMCACHEDPP_H + #include +#include + #include #include @@ -29,77 +42,81 @@ public: } bool fetch(std::string &key, - std::string &ret_val, - size_t *key_length, - size_t *value_length, + std::vector &ret_val, uint32_t *flags, memcached_return *rc) { char ret_key[MEMCACHED_MAX_KEY]; - char *value= memcached_fetch(&memc, ret_key, key_length, - value_length, flags, rc); + size_t value_length= 0; + size_t key_length= 0; + char *value= memcached_fetch(&memc, ret_key, &key_length, + &value_length, flags, rc); if (value) { - ret_val.assign(value); + ret_val.reserve(value_length); + memcpy(&*ret_val.begin(), value, value_length); key.assign(ret_key); return true; } return false; } - std::string get(const std::string &key, size_t *value_length) + std::vector &get(const std::string &key, + std::vector &ret_val) { - uint32_t flags; + uint32_t flags= 0; memcached_return rc; - std::string ret_val; + size_t value_length= 0; char *value= memcached_get(&memc, key.c_str(), key.length(), - value_length, &flags, &rc); - if (value) + &value_length, &flags, &rc); + if (value != NULL) { - ret_val.assign(value); + ret_val.reserve(value_length); + memcpy(&ret_val[0], value, value_length); } return ret_val; } - std::string get_by_key(const std::string &master_key, - const std::string &key, - size_t *value_length) + std::vector &getByKey(const std::string &master_key, + const std::string &key, + std::vector &ret_val) { - uint32_t flags; + uint32_t flags= 0; memcached_return rc; - std::string ret_val; + size_t value_length= 0; - char *value= memcached_get_by_key(&memc, master_key.c_str(), master_key.length(), + char *value= memcached_get_by_key(&memc, + master_key.c_str(), master_key.length(), key.c_str(), key.length(), - value_length, &flags, &rc); + &value_length, &flags, &rc); if (value) { - ret_val.assign(value); + ret_val.reserve(value_length); + memcpy(&*ret_val.begin(), value, value_length); } return ret_val; } bool mget(std::vector &keys) { + std::vector real_keys; + std::vector key_len; /* * Construct an array which will contain the length * of each of the strings in the input vector. Also, to * interface with the memcached C API, we need to convert * the vector of std::string's to a vector of char *. */ - size_t *key_len= static_cast(malloc(keys.size() * sizeof(size_t))); - if (key_len == NULL) - { - return false; - } - std::vector real_keys; + real_keys.reserve(keys.size()); + key_len.reserve(keys.size()); + std::vector::iterator it= keys.begin(); - int i= 0; + while (it != keys.end()) { real_keys.push_back(const_cast((*it).c_str())); - key_len[i++]= (*it).length(); + key_len.push_back((*it).length()); ++it; } @@ -109,8 +126,8 @@ public: */ if (!real_keys.empty()) { - memcached_return rc= memcached_mget(&memc, &real_keys[0], key_len, - static_cast(real_keys.size())); + memcached_return rc= memcached_mget(&memc, &real_keys[0], &key_len[0], + real_keys.size()); return (rc == MEMCACHED_SUCCESS); } @@ -118,21 +135,21 @@ public: } bool set(const std::string &key, - const std::string &value, + const std::vector &value, time_t expiration, uint32_t flags) { memcached_return rc= memcached_set(&memc, key.c_str(), key.length(), - value.c_str(), value.length(), + &value[0], value.size(), expiration, flags); return (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); } - bool set_all(std::vector &keys, - std::vector &values, - time_t expiration, - uint32_t flags) + bool setAll(std::vector &keys, + std::vector< std::vector > &values, + time_t expiration, + uint32_t flags) { if (keys.size() != values.size()) { @@ -140,7 +157,7 @@ public: } bool retval= true; std::vector::iterator key_it= keys.begin(); - std::vector::iterator val_it= values.begin(); + std::vector< std::vector >::iterator val_it= values.begin(); while (key_it != keys.end()) { retval= set((*key_it), (*val_it), expiration, flags); @@ -154,16 +171,16 @@ public: return retval; } - bool set_by_key(const std::string &master_key, - const std::string &key, - const std::string &value, - time_t expiration, - uint32_t flags) + bool setByKey(const std::string &master_key, + const std::string &key, + const std::vector &value, + time_t expiration, + uint32_t flags) { memcached_return rc= memcached_set_by_key(&memc, master_key.c_str(), master_key.length(), key.c_str(), key.length(), - value.c_str(), value.length(), + &value[0], value.size(), expiration, flags); return (rc == MEMCACHED_SUCCESS); @@ -192,9 +209,9 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool add_by_key(const std::string &master_key, - const std::string &key, - const std::string &value) + bool addByKey(const std::string &master_key, + const std::string &key, + const std::string &value) { memcached_return rc= memcached_add_by_key(&memc, master_key.c_str(), @@ -215,9 +232,9 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool replace_by_key(const std::string &master_key, - const std::string &key, - const std::string &value) + bool replaceByKey(const std::string &master_key, + const std::string &key, + const std::string &value) { memcached_return rc= memcached_replace_by_key(&memc, master_key.c_str(), @@ -237,9 +254,9 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool prepend_by_key(const std::string &master_key, - const std::string &key, - const std::string &value) + bool prependByKey(const std::string &master_key, + const std::string &key, + const std::string &value) { memcached_return rc= memcached_prepend_by_key(&memc, master_key.c_str(), @@ -264,9 +281,9 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool append_by_key(const std::string &master_key, - const std::string &key, - const std::string &value) + bool appendByKey(const std::string &master_key, + const std::string &key, + const std::string &value) { memcached_return rc= memcached_append_by_key(&memc, master_key.c_str(), @@ -289,10 +306,10 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool cas_by_key(const std::string &master_key, - const std::string &key, - const std::string &value, - uint64_t cas_arg) + bool casByKey(const std::string &master_key, + const std::string &key, + const std::string &value, + uint64_t cas_arg) { memcached_return rc= memcached_cas_by_key(&memc, master_key.c_str(), @@ -312,8 +329,8 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool delete_by_key(const std::string &master_key, - const std::string &key) + bool removeByKey(const std::string &master_key, + const std::string &key) { memcached_return rc= memcached_delete_by_key(&memc, master_key.c_str(), @@ -330,9 +347,9 @@ public: return (rc == MEMCACHED_SUCCESS); } - bool fetch_execute(memcached_execute_function *callback, - void *context, - unsigned int num_of_callbacks) + bool fetchExecute(memcached_execute_function *callback, + void *context, + unsigned int num_of_callbacks) { memcached_return rc= memcached_fetch_execute(&memc, callback, @@ -341,7 +358,7 @@ public: return (rc == MEMCACHED_SUCCESS); } - const std::string lib_version() const + const std::string libVersion() const { const char *ver= memcached_lib_version(); const std::string version(ver); @@ -352,3 +369,5 @@ private: memcached_st memc; memcached_result_st result; }; + +#endif /* LIBMEMCACHEDPP_H */