Uncrustify
[m6w6/libmemcached] / libmemcached / memcached.hpp
index bb8a9732d9b8614614f2b501650bf67a570018df..d7086b4ce301ea11a876fdcb3a20f8b0831a009a 100644 (file)
@@ -164,6 +164,7 @@ public:
   {
     servers_list.assign(in_servers_list);
     servers= memcached_servers_parse(in_servers_list.c_str());
+    memcached_server_push(&memc, servers);
     return (servers == NULL);
   }
 
@@ -171,7 +172,7 @@ public:
    * Add a server to the list of memcached servers to use.
    *
    * @param[in] server_name name of the server to add
-   * @param[in[ port port number of server to add
+   * @param[in] port port number of server to add
    * @return true on success; false otherwise
    */
   bool addServer(const std::string &server_name, unsigned int port)
@@ -187,6 +188,28 @@ public:
                                           server_name.c_str(),
                                           port,
                                           &rc);
+    memcached_server_push(&memc, servers);
+    return (rc == MEMCACHED_SUCCESS);
+  }
+
+  /**
+   * Remove a server from the list of memcached servers to use.
+   *
+   * @param[in] server_name name of the server to remove
+   * @param[in] port port number of server to remove
+   * @return true on success; false otherwise
+   */
+  bool removeServer(const std::string &server_name, size_t port)
+  {
+    std::string tmp_str;
+    std::ostringstream strstm;
+    tmp_str.append(",");
+    tmp_str.append(server_name);
+    tmp_str.append(":");
+    strstm << port;
+    tmp_str.append(strstm.str());
+    memcached_server_st *server= memcached_servers_parse(tmp_str.c_str());
+    memcached_return rc= memcached_server_remove(server);
     return (rc == MEMCACHED_SUCCESS);
   }
 
@@ -213,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;
   }
 
@@ -242,6 +271,7 @@ public:
     {
       ret_val.reserve(value_length);
       ret_val.assign(value, value + value_length);
+      free(value);
       return true;
     }
     return false;
@@ -279,6 +309,7 @@ public:
     {
       ret_val.reserve(value_length);
       ret_val.assign(value, value + value_length);
+      free(value);
       return true;
     }
     return false;
@@ -446,9 +477,8 @@ public:
       retval= set(it->first, it->second, expiration, flags);
       if (retval == false)
       {
-        char err_buff[64];
-        sprintf(err_buff,  "There was an error setting the key %s",
-          it->first.c_str());
+        std::string err_buff("There was an error setting the key ");
+        err_buff.append(it->first);
         throw(Error(err_buff, false));
       }
       ++it;
@@ -895,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;