Cleaned up silly usage of memory in C++ fetch method.
authorBrian Aker <brian@tangent.org>
Sat, 18 Jun 2011 00:08:20 +0000 (17:08 -0700)
committerBrian Aker <brian@tangent.org>
Sat, 18 Jun 2011 00:08:20 +0000 (17:08 -0700)
libmemcached/memcached.hpp
tests/plus.cpp

index 70d3df07af06bf719afa39731b37a743f39b8d55..0b27291b095ca3f76dd908759900fe2325baf1f4 100644 (file)
@@ -163,30 +163,41 @@ public:
    * @return a memcached return structure
    */
   memcached_return_t fetch(std::string &key,
-                           std::vector<char> &ret_val)
+                           std::vector<char> &ret_val,
+                           uint32_t &flags,
+                           uint64_t &cas_value)
   {
-    char ret_key[MEMCACHED_MAX_KEY];
-    size_t value_length= 0;
-    size_t key_length= 0;
     memcached_return_t rc;
-    uint32_t flags= 0;
-    char *value= memcached_fetch(memc, ret_key, &key_length,
-                                 &value_length, &flags, &rc);
-    if (value && ret_val.empty())
-    {
-      ret_val.reserve(value_length);
-      ret_val.assign(value, value + value_length);
-      key.assign(ret_key, key_length);
-      free(value);
-    }
-    else if (value)
+
+    memcached_result_st *result;
+    if ((result= memcached_fetch_result(memc, NULL, &rc)))
     {
-      free(value);
+      // Key
+      key.assign(memcached_result_key_value(result), memcached_result_key_length(result));
+
+      // Actual value, null terminated
+      ret_val.reserve(memcached_result_length(result) +1);
+      ret_val.assign(memcached_result_value(result), 
+                     memcached_result_value(result) +memcached_result_length(result));
+
+      // Misc
+      flags= memcached_result_flags(result);
+      cas_value= memcached_result_cas(result);
     }
+    memcached_result_free(result);
 
     return rc;
   }
 
+  memcached_return_t fetch(std::string &key,
+                           std::vector<char> &ret_val)
+  {
+    uint32_t flags= 0;
+    uint64_t cas_value= 0;
+
+    return fetch(key, ret_val, flags, cas_value);
+  }
+
   /**
    * Fetches an individual value from the server.
    *
@@ -210,6 +221,7 @@ public:
       free(value);
       return true;
     }
+
     return false;
   }
 
index 818c908a64dca0fb9e55865052fc2220483a27d7..59ebf555def8193c44df885be73403de1c4eb429 100644 (file)
@@ -174,7 +174,6 @@ test_return_t mget_test(memcached_st *original)
   test_true(memc.setAll(keys, values, 50, 9));
 
   test_true(memc.mget(keys));
-
   size_t count= 0;
   while ((mc_rc= memc.fetch(return_key, return_value)) == MEMCACHED_SUCCESS)
   {