Merging changes from trunk
authormike tsai <mtsai@mtsai>
Thu, 22 Oct 2009 23:05:00 +0000 (16:05 -0700)
committermike tsai <mtsai@mtsai>
Thu, 22 Oct 2009 23:05:00 +0000 (16:05 -0700)
libmemcached/memcached.hpp
tests/cpp_example.cc

index 46573f4959b2aab0a874da0eae407965fb2360a3..d7086b4ce301ea11a876fdcb3a20f8b0831a009a 100644 (file)
@@ -236,7 +236,13 @@ public:
       ret_val.reserve(value_length);
       ret_val.assign(value, value + value_length);
       key.assign(ret_key);
+      free(value);
     }
+    else if (value)
+    {
+      free(value);
+    }
+
     return rc;
   }
 
@@ -265,6 +271,7 @@ public:
     {
       ret_val.reserve(value_length);
       ret_val.assign(value, value + value_length);
+      free(value);
       return true;
     }
     return false;
@@ -302,6 +309,7 @@ public:
     {
       ret_val.reserve(value_length);
       ret_val.assign(value, value + value_length);
+      free(value);
       return true;
     }
     return false;
@@ -917,6 +925,60 @@ public:
     return version;
   }
 
+  /**
+   * Retrieve memcached statistics. Populate a std::map with the retrieved
+   * stats. Each server will map to another std::map of the key:value stats.
+   *
+   * @param[out] stats_map a std::map to be populated with the memcached
+   *                       stats
+   * @return true on success; false otherwise
+   */
+  bool getStats(std::map< std::string, std::map<std::string, std::string> >
+                &stats_map)
+  {
+    memcached_return rc;
+    memcached_stat_st *stats= memcached_stat(&memc, NULL, &rc);
+
+    if (rc != MEMCACHED_SUCCESS &&
+        rc != MEMCACHED_SOME_ERRORS)
+    {
+      return false;
+    }
+
+    uint32_t server_count= memcached_server_count(&memc);
+
+    /*
+     * For each memcached server, construct a std::map for its stats and add
+     * it to the std::map of overall stats.
+     */
+    for (uint32_t x= 0; x < server_count; x++)
+    {
+      std::ostringstream strstm;
+      std::string server_name(memcached_server_name(&memc, servers[x]));
+      server_name.append(":");
+      strstm << memcached_server_port(&memc, servers[x]);
+      server_name.append(strstm.str());
+
+      std::map<std::string, std::string> server_stats;
+      char **list= NULL;
+      char **ptr= NULL;
+
+      list= memcached_stat_get_keys(&memc, &stats[x], &rc);
+      for (ptr= list; *ptr; ptr++)
+      {
+        char *value= memcached_stat_get_value(&memc, &stats[x], *ptr, &rc);
+        server_stats[*ptr]= value;
+        free(value);
+      }
+      
+      stats_map[server_name]= server_stats;
+      free(list);
+    }
+
+    memcached_stat_free(&memc, stats);
+    return true;
+  }
+
 private:
 
   std::string servers_list;
index b96aaf4e1c23bf7a838f5f7904dc619e3dbe65f2..0b2037240c00b32398cd52d0fb96b46aa21fe78c 100644 (file)
@@ -6,6 +6,7 @@
 #include <string>
 #include <iostream>
 #include <algorithm>
+#include <map>
 
 #include <string.h>
 
@@ -167,36 +168,27 @@ void setProduct(const string &key, const Product &product)
 
 int main()
 {
-#if 0
-  Product pad(1, 5.0);
-  const string key("padraig");
-  cout << "Going to set an object in the cache..." << endl;
-  setProduct(key, pad);
-  cout << "Now retrieve that key..." << endl;
-  Product test= getProduct(key);
-  double price= test.getPrice();
-  cout << "Price of retrieve object: " << price << endl;
-  Product next(2, 10.0);
-  vector<Product> products;
-  products.push_back(pad);
-  products.push_back(next);
-  cout << "going to set a vector of products..." << endl;
-  setAllProducts(products);
-  cout << "now retrieve those products..." << endl;
-  vector<Product> got= getAllProducts();
-  cout << "size of retrieved vector: " << got.size() << endl;
-  vector<Product>::iterator iter= got.begin();
-  while (iter != got.end())
+  Memcache first_client("127.0.0.1:19191");
+  map< string, map<string, string> > my_stats;
+  first_client.getStats(my_stats);
+  
+  /*
+   * Iterate through the retrieved stats.
+   */
+  map< string, map<string, string> >::iterator it=
+    my_stats.begin();
+  while (it != my_stats.end())
   {
-    cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
-    ++iter;
+    cout << "working with server: " << (*it).first << endl;
+    map<string, string> serv_stats= (*it).second;
+    map<string, string>::iterator iter= serv_stats.begin();
+    while (iter != serv_stats.end())
+    {
+      cout << (*iter).first << ":" << (*iter).second << endl;
+      ++iter;
+    }
+    ++it;
   }
-#endif
-  Memcache first_client("127.0.0.1:11211");
-  Memcache second_client("127.0.0.1", 11211);
-  //first_client.set("key", some_vector_of_chars, expiry, flags);
-  //first_client.get("key", vector_to_fill_with_data);
-  //first_client.remove("key");
-  first_client.addServer("192.168.1.1", 11211);
+
   return 0;
 }