Merge lp://staging/~posulliv/libmemcached/cpp-interface into lp://staging/~tangent-org/libmemcached/trunk

Proposed by Padraig O'Sullivan
Status: Merged
Merged at revision: not available
Proposed branch: lp://staging/~posulliv/libmemcached/cpp-interface
Merge into: lp://staging/~tangent-org/libmemcached/trunk
Diff against target: 157 lines
2 files modified
libmemcached/memcached.hpp (+57/-0)
tests/cpp_example.cc (+21/-29)
To merge this branch: bzr merge lp://staging/~posulliv/libmemcached/cpp-interface
Reviewer Review Type Date Requested Status
Libmemcached-developers Pending
Review via email: mp+13658@code.staging.launchpad.net
To post a comment you must log in.
Revision history for this message
Padraig O'Sullivan (posulliv) wrote :

Added a getStats() method to the C++ interface. I basically added the same functionality as I saw in the spymemcached java client. The interface to this getStats() method is:

getStats(map< string, map<string, string> > &stats_map);

Thus, each server will have an entry in the stats_map that will map to another std::map with the stats for that server. We could also easily add a method to simply retrieve the stats for a specific server if that would be desirable?

Also fixed 2 valgrind warnings that were popping up because I forgot to free memory in 2 locations.

-Padraig

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libmemcached/memcached.hpp'
2--- libmemcached/memcached.hpp 2009-10-07 01:18:17 +0000
3+++ libmemcached/memcached.hpp 2009-10-20 18:25:19 +0000
4@@ -236,6 +236,7 @@
5 ret_val.reserve(value_length);
6 ret_val.assign(value, value + value_length);
7 key.assign(ret_key);
8+ free(value);
9 }
10 return rc;
11 }
12@@ -265,6 +266,7 @@
13 {
14 ret_val.reserve(value_length);
15 ret_val.assign(value, value + value_length);
16+ free(value);
17 return true;
18 }
19 return false;
20@@ -302,6 +304,7 @@
21 {
22 ret_val.reserve(value_length);
23 ret_val.assign(value, value + value_length);
24+ free(value);
25 return true;
26 }
27 return false;
28@@ -917,6 +920,60 @@
29 return version;
30 }
31
32+ /**
33+ * Retrieve memcached statistics. Populate a std::map with the retrieved
34+ * stats. Each server will map to another std::map of the key:value stats.
35+ *
36+ * @param[out] stats_map a std::map to be populated with the memcached
37+ * stats
38+ * @return true on success; false otherwise
39+ */
40+ bool getStats(std::map< std::string, std::map<std::string, std::string> >
41+ &stats_map)
42+ {
43+ memcached_return rc;
44+ memcached_stat_st *stats= memcached_stat(&memc, NULL, &rc);
45+
46+ if (rc != MEMCACHED_SUCCESS &&
47+ rc != MEMCACHED_SOME_ERRORS)
48+ {
49+ return false;
50+ }
51+
52+ uint32_t server_count= memcached_server_count(&memc);
53+
54+ /*
55+ * For each memcached server, construct a std::map for its stats and add
56+ * it to the std::map of overall stats.
57+ */
58+ for (uint32_t x= 0; x < server_count; x++)
59+ {
60+ std::ostringstream strstm;
61+ std::string server_name(memcached_server_name(&memc, servers[x]));
62+ server_name.append(":");
63+ strstm << memcached_server_port(&memc, servers[x]);
64+ server_name.append(strstm.str());
65+
66+ std::map<std::string, std::string> server_stats;
67+ char **list= NULL;
68+ char **ptr= NULL;
69+
70+ list= memcached_stat_get_keys(&memc, &stats[x], &rc);
71+ for (ptr= list; *ptr; ptr++)
72+ {
73+ char *value= memcached_stat_get_value(&memc, &stats[x], *ptr, &rc);
74+ server_stats[*ptr]= value;
75+ free(value);
76+ }
77+
78+ stats_map[server_name]= server_stats;
79+ free(list);
80+ }
81+
82+ memcached_stat_free(&memc, stats);
83+ return true;
84+ }
85+
86 private:
87
88 std::string servers_list;
89
90=== modified file 'tests/cpp_example.cc'
91--- tests/cpp_example.cc 2009-09-19 23:36:57 +0000
92+++ tests/cpp_example.cc 2009-10-20 18:25:19 +0000
93@@ -6,6 +6,7 @@
94 #include <string>
95 #include <iostream>
96 #include <algorithm>
97+#include <map>
98
99 #include <string.h>
100
101@@ -167,36 +168,27 @@
102
103 int main()
104 {
105-#if 0
106- Product pad(1, 5.0);
107- const string key("padraig");
108- cout << "Going to set an object in the cache..." << endl;
109- setProduct(key, pad);
110- cout << "Now retrieve that key..." << endl;
111- Product test= getProduct(key);
112- double price= test.getPrice();
113- cout << "Price of retrieve object: " << price << endl;
114- Product next(2, 10.0);
115- vector<Product> products;
116- products.push_back(pad);
117- products.push_back(next);
118- cout << "going to set a vector of products..." << endl;
119- setAllProducts(products);
120- cout << "now retrieve those products..." << endl;
121- vector<Product> got= getAllProducts();
122- cout << "size of retrieved vector: " << got.size() << endl;
123- vector<Product>::iterator iter= got.begin();
124- while (iter != got.end())
125+ Memcache first_client("127.0.0.1:19191");
126+ map< string, map<string, string> > my_stats;
127+ first_client.getStats(my_stats);
128+
129+ /*
130+ * Iterate through the retrieved stats.
131+ */
132+ map< string, map<string, string> >::iterator it=
133+ my_stats.begin();
134+ while (it != my_stats.end())
135 {
136- cout << "product " << (*iter).getId() << " costs " << (*iter).getPrice() << endl;
137- ++iter;
138+ cout << "working with server: " << (*it).first << endl;
139+ map<string, string> serv_stats= (*it).second;
140+ map<string, string>::iterator iter= serv_stats.begin();
141+ while (iter != serv_stats.end())
142+ {
143+ cout << (*iter).first << ":" << (*iter).second << endl;
144+ ++iter;
145+ }
146+ ++it;
147 }
148-#endif
149- Memcache first_client("127.0.0.1:11211");
150- Memcache second_client("127.0.0.1", 11211);
151- //first_client.set("key", some_vector_of_chars, expiry, flags);
152- //first_client.get("key", vector_to_fill_with_data);
153- //first_client.remove("key");
154- first_client.addServer("192.168.1.1", 11211);
155+
156 return 0;
157 }

Subscribers

People subscribed via source and target branches