Merge Monty
authorBrian Aker <brian@gaz>
Thu, 30 Jul 2009 06:04:28 +0000 (23:04 -0700)
committerBrian Aker <brian@gaz>
Thu, 30 Jul 2009 06:04:28 +0000 (23:04 -0700)
libmemcached/memcached.hh
libmemcached/memcached_hash.c
tests/plus.cpp

index c6c3ef5e60f2a0e58e0f10b1bc2a27b38125ff71..f1ca9d9ad7cb5fd0e5fdf1197323fe6a472c035a 100644 (file)
@@ -1,8 +1,31 @@
+/*
+ * Summary: C++ interface for memcached server
+ *
+ * Copy: See Copyright for the status of this software.
+ *
+ * Authors: Padraig O'Sullivan <osullivan.padraig@gmail.com>
+ *          Patrick Galbraith <patg@patg.net>
+ */
+
+/**
+ * @file memcached.hh
+ * @brief Libmemcached C++ interface
+ */
+
+#ifndef LIBMEMCACHEDPP_H
+#define LIBMEMCACHEDPP_H
+
 #include <libmemcached/memcached.h>
 
+#include <string.h>
+
 #include <string>
 #include <vector>
 
+/**
+ * This is the core memcached library (if later, other objects
+ * are needed, they will be created from this class).
+ */
 class Memcached
 {
 public:
@@ -23,59 +46,100 @@ public:
     memcached_clone(&memc, clone);
   }
 
+  Memcached(const Memcached &rhs)
+    :
+      memc(),
+      result()
+  {
+    memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
+  }
+
   ~Memcached()
   {
     memcached_free(&memc);
   }
 
+  /**
+   * Get the internal memcached_st *
+   */
+  memcached_st &getImpl()
+  {
+    return memc;
+  }
+
+  /**
+   * Get the internal memcached_st *
+   */
+  const memcached_st &getImpl() const
+  {
+    return memc;
+  }
+
+  /**
+   * Return an error string for the given return structure.
+   *
+   * @param[in] rc a memcached_return structure
+   * @return error string corresponding to given return code in the library.
+   */
+  const std::string getError(memcached_return rc) const
+  {
+    /* first parameter to strerror is unused */
+    return memcached_strerror(NULL, rc);
+  }
+
   bool fetch(std::string &key, 
-             std::string &ret_val,
-             size_t *key_length, 
-             size_t *value_length,
+             std::vector<char> &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<char> &get(const std::string &key, 
+                         std::vector<char> &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<char> &getByKey(const std::string &master_key, 
+                              const std::string &key, 
+                              std::vector<char> &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;
   }
@@ -117,21 +181,21 @@ public:
   }
 
   bool set(const std::string &key, 
-           const std::string &value,
+           const std::vector<char> &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<std::string> &keys,
-               std::vector<std::string> &values,
-               time_t expiration,
-               uint32_t flags)
+  bool setAll(std::vector<std::string> &keys,
+              std::vector< std::vector<char> > &values,
+              time_t expiration,
+              uint32_t flags)
   {
     if (keys.size() != values.size())
     {
@@ -139,7 +203,7 @@ public:
     }
     bool retval= true;
     std::vector<std::string>::iterator key_it= keys.begin();
-    std::vector<std::string>::iterator val_it= values.begin();
+    std::vector< std::vector<char> >::iterator val_it= values.begin();
     while (key_it != keys.end())
     {
       retval= set((*key_it), (*val_it), expiration, flags);
@@ -153,16 +217,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<char> &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);
@@ -184,135 +248,134 @@ public:
   }
 
 
-  bool add(const std::string &key, const std::string &value)
+  bool add(const std::string &key, const std::vector<char> &value)
   {
     memcached_return rc= memcached_add(&memc, key.c_str(), key.length(), 
-                                       value.c_str(), value.length(), 0, 0);
+                                       &value[0], value.size(), 0, 0);
     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::vector<char> &value)
   {
     memcached_return rc= memcached_add_by_key(&memc, 
                                               master_key.c_str(),
                                               master_key.length(),
                                               key.c_str(),
                                               key.length(),
-                                              value.c_str()
-                                              value.length(),
+                                              &value[0]
+                                              value.size(),
                                               0, 0);
     return (rc == MEMCACHED_SUCCESS);
   }
 
-  bool replace(const std::string &key, const std::string &value)
+  bool replace(const std::string &key, const std::vector<char> &value)
   {
     memcached_return rc= memcached_replace(&memc, key.c_str(), key.length(),
-                                           value.c_str(), value.length(),
+                                           &value[0], value.size(),
                                            0, 0);
     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::vector<char> &value)
   {
     memcached_return rc= memcached_replace_by_key(&memc, 
                                                   master_key.c_str(), 
                                                   master_key.length(),
                                                   key.c_str(), 
                                                   key.length(),
-                                                  value.c_str()
-                                                  value.length(), 
+                                                  &value[0]
+                                                  value.size(), 
                                                   0, 0);
     return (rc == MEMCACHED_SUCCESS);
   }
 
-  bool prepend(const std::string &key, const std::string &value)
+  bool prepend(const std::string &key, const std::vector<char> &value)
   {
     memcached_return rc= memcached_prepend(&memc, key.c_str(), key.length(),
-                                           value.c_str(), value.length(), 0, 0);
+                                           &value[0], value.size(), 0, 0);
     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::vector<char> &value)
   {
     memcached_return rc= memcached_prepend_by_key(&memc, 
                                                   master_key.c_str(), 
                                                   master_key.length(),
                                                   key.c_str(), 
                                                   key.length(),
-                                                  value.c_str()
-                                                  value.length(),
+                                                  &value[0]
+                                                  value.size(),
                                                   0,
                                                   0);
     return (rc == MEMCACHED_SUCCESS);
   }
 
-  bool append(const std::string &key, const std::string &value)
+  bool append(const std::string &key, const std::vector<char> &value)
   {
     memcached_return rc= memcached_append(&memc, 
                                           key.c_str(), 
                                           key.length(),
-                                          value.c_str()
-                                          value.length(), 
+                                          &value[0]
+                                          value.size(), 
                                           0, 0);
     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::vector<char> &value)
   {
     memcached_return rc= memcached_append_by_key(&memc,
                                                  master_key.c_str(), 
                                                  master_key.length(),
                                                  key.c_str(), 
                                                  key.length(),
-                                                 value.c_str()
-                                                 value.length(), 
+                                                 &value[0]
+                                                 value.size(), 
                                                  0, 0);
     return (rc == MEMCACHED_SUCCESS);
   }
 
   bool cas(const std::string &key, 
-           const std::string &value, 
+           const std::vector<char> &value, 
            uint64_t cas_arg)
   {
     memcached_return rc= memcached_cas(&memc, key.c_str(), key.length(),
-                                       value.c_str(), value.length(), 
+                                       &value[0], value.size(), 
                                        0, 0, cas_arg);
     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::vector<char> &value, 
+                uint64_t cas_arg)
   {
     memcached_return rc= memcached_cas_by_key(&memc,
                                               master_key.c_str(), 
                                               master_key.length(),
                                               key.c_str(), 
                                               key.length(),
-                                              value.c_str()
-                                              value.length(),
+                                              &value[0]
+                                              value.size(),
                                               0, 0, cas_arg);
     return (rc == MEMCACHED_SUCCESS);
   }
 
-  // using 'remove' vs. 'delete' since 'delete' is a keyword 
   bool remove(const std::string &key)
   {
     memcached_return rc= memcached_delete(&memc, key.c_str(), key.length(), 0);
     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(), 
@@ -329,9 +392,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,
@@ -340,7 +403,11 @@ public:
     return (rc == MEMCACHED_SUCCESS);
   }
 
-  const std::string lib_version() const
+  /**
+   * Get the library version string.
+   * @return std::string containing a copy of the library version string.
+   */
+  const std::string libVersion() const
   {
     const char *ver= memcached_lib_version();
     const std::string version(ver);
@@ -348,6 +415,9 @@ public:
   }
 
 private:
+
   memcached_st memc;
   memcached_result_st result;
 };
+
+#endif /* LIBMEMCACHEDPP_H */
index 5abc1e0761731627bcc49b1caaf1401f7f32e18b..4956d18e0655e784bbb4e7a4b3c0200d4552bb77 100644 (file)
@@ -201,11 +201,11 @@ uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_
 static uint32_t internal_generate_hash(const char *key, size_t key_length)
 {
   const char *ptr= key;
-  int32_t value= 0;
+  uint32_t value= 0;
 
   while (key_length--) 
   {
-    value += (int32_t) *ptr++;
+    value += (uint32_t) *ptr++;
     value += (value << 10);
     value ^= (value >> 6);
   }
index dc075478c9fdec7b9c4091c6912c2cd311b7b2ce..28d728703aa70dd76d6ddda6fe0e400ae02f9a56 100644 (file)
@@ -33,17 +33,36 @@ extern "C" {
    void world_destroy(void *p);
 }
 
+static void populate_vector(vector<char> &vec, const string &str)
+{
+  vec.reserve(str.length());
+  memcpy(&*vec.begin(), str.c_str(), str.length());
+}
+
+static void copy_vec_to_string(vector<char> &vec, string &str)
+{
+  str.clear();
+  char *tmp= static_cast<char *>(malloc(vec.size() * sizeof(char)));
+  if (!vec.empty())
+  {
+    memcpy(tmp, &vec[0], vec.size());
+    str.assign(tmp);
+  }
+}
+
 test_return basic_test(memcached_st *memc)
 {
   Memcached foo(memc);
   const string value_set("This is some data");
-  string value;
-  size_t value_length;
+  std::vector<char> value;
+  std::vector<char> test_value;
+
+  populate_vector(value, value_set);
 
-  foo.set("mine", value_set, 0, 0);
-  value= foo.get("mine", &value_length);
+  foo.set("mine", value, 0, 0);
+  test_value= foo.get("mine", test_value);
 
-  assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
+  assert((memcmp(&test_value[0], &value[0], test_value.size()) == 0));
 
   return TEST_SUCCESS;
 }
@@ -52,18 +71,32 @@ test_return increment_test(memcached_st *memc)
 {
   Memcached mcach(memc);
   bool rc;
-  const string key("inctest");
+  const string key("blah");
   const string inc_value("1");
-  string ret_value;
+  std::vector<char> inc_val;
+  vector<char> ret_value;
+  string ret_string;
   uint64_t int_inc_value;
   uint64_t int_ret_value;
-  size_t value_length;
 
-  mcach.set(key, inc_value, 0, 0);
-  ret_value= mcach.get(key, &value_length);
-  printf("\nretvalue %s\n",ret_value.c_str());
+  populate_vector(inc_val, inc_value);
+
+  rc= mcach.set(key, inc_val, 0, 0);
+  if (rc == false)
+  {
+    return TEST_FAILURE;
+  }
+  ret_value= mcach.get(key, ret_value);
+  if (ret_value.empty())
+  {
+    return TEST_FAILURE;
+  }
+  copy_vec_to_string(ret_value, ret_string);
+
+  printf("string is: %s\n", ret_string.c_str());
+
   int_inc_value= uint64_t(atol(inc_value.c_str()));
-  int_ret_value= uint64_t(atol(ret_value.c_str()));
+  int_ret_value= uint64_t(atol(ret_string.c_str()));
   assert(int_ret_value == int_inc_value); 
 
   rc= mcach.increment(key, 1, &int_ret_value);
@@ -85,19 +118,23 @@ test_return basic_master_key_test(memcached_st *memc)
 {
   Memcached foo(memc);
   const string value_set("Data for server A");
+  vector<char> value;
+  vector<char> test_value;
   const string master_key_a("server-a");
   const string master_key_b("server-b");
   const string key("xyz");
-  string value;
-  size_t value_length;
 
-  foo.set_by_key(master_key_a, key, value_set, 0, 0);
-  value= foo.get_by_key(master_key_a, key, &value_length);
+  populate_vector(value, value_set);
+
+  foo.setByKey(master_key_a, key, value, 0, 0);
+  test_value= foo.getByKey(master_key_a, key, test_value);
+
+  assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
 
-  assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
+  test_value.clear();
 
-  value= foo.get_by_key(master_key_b, key, &value_length);
-  assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0));
+  test_value= foo.getByKey(master_key_b, key, test_value);
+  assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
 
   return TEST_SUCCESS;
 }
@@ -122,16 +159,27 @@ test_return mget_result_function(memcached_st *memc)
   string key2("son");
   string key3("food");
   vector<string> keys;
+  vector< vector<char> > values;
+  vector<char> val1;
+  vector<char> val2;
+  vector<char> val3;
+  populate_vector(val1, key1);
+  populate_vector(val2, key2);
+  populate_vector(val3, key3);
   keys.reserve(3);
   keys.push_back(key1);
   keys.push_back(key2);
   keys.push_back(key3);
+  values.reserve(3);
+  values.push_back(val1);
+  values.push_back(val2);
+  values.push_back(val3);
   unsigned int counter;
   memcached_execute_function callbacks[1];
 
   /* We need to empty the server before we continue the test */
   rc= mc.flush(0);
-  rc= mc.set_all(keys, keys, 50, 9);
+  rc= mc.setAll(keys, values, 50, 9);
   assert(rc == true);
 
   rc= mc.mget(keys);
@@ -139,7 +187,7 @@ test_return mget_result_function(memcached_st *memc)
 
   callbacks[0]= &callback_counter;
   counter= 0;
-  rc= mc.fetch_execute(callbacks, static_cast<void *>(&counter), 1); 
+  rc= mc.fetchExecute(callbacks, static_cast<void *>(&counter), 1); 
 
   assert(counter == 3);
 
@@ -152,16 +200,25 @@ test_return mget_test(memcached_st *memc)
   bool rc;
   memcached_return mc_rc;
   vector<string> keys;
+  vector< vector<char> > values;
   keys.reserve(3);
   keys.push_back("fudge");
   keys.push_back("son");
   keys.push_back("food");
+  vector<char> val1;
+  vector<char> val2;
+  vector<char> val3;
+  populate_vector(val1, "fudge");
+  populate_vector(val2, "son");
+  populate_vector(val3, "food");
+  values.reserve(3);
+  values.push_back(val1);
+  values.push_back(val2);
+  values.push_back(val3);
   uint32_t flags;
 
   string return_key;
-  size_t return_key_length;
-  string return_value;
-  size_t return_value_length;
+  vector<char> return_value;
 
   /* We need to empty the server before we continue the test */
   rc= mc.flush(0);
@@ -170,27 +227,26 @@ test_return mget_test(memcached_st *memc)
   rc= mc.mget(keys);
   assert(rc == true);
 
-  while (mc.fetch(return_key, return_value, &return_key_length, 
-                  &return_value_length, &flags, &mc_rc))
+  while (mc.fetch(return_key, return_value, 
+                  &flags, &mc_rc))
   {
-    assert(return_value.length() != 0);
+    assert(return_value.size() != 0);
+    return_value.clear();
   }
-  assert(return_value_length == 0);
   assert(mc_rc == MEMCACHED_END);
 
-  rc= mc.set_all(keys, keys, 50, 9);
+  rc= mc.setAll(keys, values, 50, 9);
   assert(rc == true);
 
   rc= mc.mget(keys);
   assert(rc == true);
 
-  while ((mc.fetch(return_key, return_value, &return_key_length, 
-                   &return_value_length, &flags, &mc_rc)))
+  while ((mc.fetch(return_key, return_value, 
+                   &flags, &mc_rc)))
   {
-    assert(return_value.length() != 0);
     assert(mc_rc == MEMCACHED_SUCCESS);
-    assert(return_key_length == return_value_length);
-    assert(!memcmp(return_value.c_str(), return_key.c_str(), return_value_length));
+    assert(return_key.length() == return_value.size());
+    assert(!memcmp(&return_value[0], return_key.c_str(), return_value.size()));
   }
 
   return TEST_SUCCESS;