Merge lp:~posulliv/drizzle/cleanup-replace-typelib into lp:~drizzle-trunk/drizzle/development

Proposed by Padraig O'Sullivan
Status: Merged
Merged at revision: not available
Proposed branch: lp:~posulliv/drizzle/cleanup-replace-typelib
Merge into: lp:~drizzle-trunk/drizzle/development
Diff against target: None lines
To merge this branch: bzr merge lp:~posulliv/drizzle/cleanup-replace-typelib
Reviewer Review Type Date Requested Status
Jay Pipes (community) Approve
Review via email: mp+4737@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Padraig O'Sullivan (posulliv) wrote :

This patch replaces an instance of TYPELIB with std::vector. I looked for a simple, self-contained instance of TYPELIB to start with and I found one in client/drizzleadmin.cc. I wanted to start on a small and simple instance like this one so that I can get feedback on whether I am going about this the right way or not. I'm hoping to work on replacing more instances of TYPELIB throughout the code in the next few weeks.

I'm looking to get comments on whether what I did to replace TYPELIB in this case is the best way to go about it. I wasn't sure if I should have gone with std::map or std::vector in this case. I found it easier to go with std::vector so that was my main motivation for using it here.

959. By Brian Aker

Merge from Stewart.

960. By Brian Aker

Merging Monty

Revision history for this message
Jay Pipes (jaypipes) wrote :

Looks good to me!

review: Approve
Revision history for this message
Monty Taylor (mordred) wrote :

Padraig O'Sullivan wrote:
> Padraig O'Sullivan has proposed merging
> lp:~posulliv/drizzle/cleanup-replace-typelib into lp:drizzle.
>
> Requested reviews: Drizzle-developers (drizzle-developers)
>
> This patch replaces an instance of TYPELIB with std::vector. I looked
> for a simple, self-contained instance of TYPELIB to start with and I
> found one in client/drizzleadmin.cc. I wanted to start on a small and
> simple instance like this one so that I can get feedback on whether I
> am going about this the right way or not. I'm hoping to work on
> replacing more instances of TYPELIB throughout the code in the next
> few weeks.
>
> I'm looking to get comments on whether what I did to replace TYPELIB
> in this case is the best way to go about it. I wasn't sure if I
> should have gone with std::map or std::vector in this case. I found
> it easier to go with std::vector so that was my main motivation for
> using it here.
>

Looks good in general... but now I'm going to nitpick... :)

If I were you, I'd make command_names a string[] and command_vector a
vector of std::string. Then you can get rid of CommandMatch and just do:

static int get_command_type(const char *name)
{
  int type= ADMIN_ERROR;
  string comp_string= lower_string(name);
  vector<string>::iterator it=
    std::find(command_vector.begin(), command_vector.end(),
              comp_string);
  if (it != command_vector.end())
  {
    /* add 1 due to the way the commands ENUM is defined */
    type= distance(command_vector.begin(), it) + 1;
  }
  return type;
}

Monty

Revision history for this message
Padraig O'Sullivan (posulliv) wrote :

Yep, good point. I updated the branch and was able to remove the function object like you mentioned. Ran the test suite also and its all good so I pushed the changes to the branch.

Thanks guys for the comments! Btw, I created a blueprint for the task of replacing TYPELIB and linked it to the code-cleanup-c++ blueprint. I also added the TYPELIB blueprint to the low-hanging fruit milestone in case anyone else wants to work on it before I get started with it.

-Padraig

961. By Brian Aker

Merge Monty

962. By Brian Aker

Merge in Brian's work on processlist locks.

963. By Brian Aker

Merge in factoring out sql_plugin work.

964. By Brian Aker

Merge of Monty

965. By Brian Aker

Merge with Jay

966. By Brian Aker

Merge Stewart.

967. By Brian Aker

Merge Brian

968. By Brian Aker

Merge from Brian

969. By Brian Aker

Merge of Eric's libdrizzle work.

970. By Brian Aker

Merge Monty.

971. By Brian Aker

Merge Monty

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'client/drizzleadmin.cc'
2--- client/drizzleadmin.cc 2009-03-04 22:49:53 +0000
3+++ client/drizzleadmin.cc 2009-03-20 21:51:00 +0000
4@@ -24,12 +24,16 @@
5 #include <signal.h>
6 #include <mysys/my_pthread.h> /* because of signal() */
7 #include <sys/stat.h>
8+#include <vector>
9+#include <algorithm>
10+#include <string>
11
12 /* Added this for string translation. */
13 #include <drizzled/gettext.h>
14
15 #define ADMIN_VERSION "8.42"
16 #define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */
17+#define NUM_COMMAND_NAMES 2
18
19 char *host= NULL, *user= NULL, *opt_password= NULL;
20 static bool interrupted= false, opt_verbose= false,tty_password= false;
21@@ -62,12 +66,11 @@
22
23 static const char *command_names[]= {
24 "shutdown",
25- "ping",
26- NULL
27+ "ping"
28 };
29
30-static TYPELIB command_typelib=
31-{ array_elements(command_names)-1,"commands", command_names, NULL };
32+static vector<const char *>
33+ command_vector(command_names, command_names + NUM_COMMAND_NAMES);
34
35 static struct my_option my_long_options[] =
36 {
37@@ -106,12 +109,65 @@
38
39 static const char *load_default_groups[]= { "drizzleadmin","client",0 };
40
41+inline string lower_string(const char * from_string)
42+{
43+ string to_string= from_string;
44+ transform(to_string.begin(), to_string.end(),
45+ to_string.begin(), ::tolower);
46+ return to_string;
47+}
48+
49+/*
50+ * Function object to be used to compare a string
51+ * against the vector of command strings. This function
52+ * object is used in the get_command_type() method.
53+ */
54+template <class T>
55+class CommandMatch :
56+ public unary_function<const string&, bool>
57+{
58+ string match_text;
59+ T match_func;
60+ public:
61+ CommandMatch(string text) : match_text(text) { }
62+ inline bool operator()(const string match_against) const
63+ {
64+ return match_func(match_against, match_text);
65+ }
66+};
67+
68+/*
69+ * Searches for the given command and determines
70+ * its type.
71+ *
72+ * Returns the type of the command corresponding
73+ * to the commands enum defined in drizzleadmin.cc
74+ * If the command is not supported, return ADMIN_ERROR.
75+ *
76+ * @param command name to search for
77+ */
78+static int get_command_type(const char *name)
79+{
80+ int type= ADMIN_ERROR;
81+ string comp_string= lower_string(name);
82+ vector<const char *>::iterator it=
83+ std::find_if(command_vector.begin(),
84+ command_vector.end(),
85+ CommandMatch< equal_to<string> >(comp_string));
86+ if (it != command_vector.end())
87+ {
88+ /* add 1 due to the way the commands ENUM is defined */
89+ type= distance(command_vector.begin(), it) + 1;
90+ }
91+ return type;
92+}
93+
94 bool
95 get_one_option(int optid, const struct my_option *, char *argument)
96 {
97 char *endchar= NULL;
98 uint64_t temp_drizzle_port= 0;
99- int error = 0;
100+ int error= 0;
101
102 switch(optid) {
103 case 'p':
104@@ -138,7 +194,7 @@
105 case 'P':
106 if (argument)
107 {
108- char *start=argument;
109+ char *start= argument;
110 if (opt_password)
111 free(opt_password);
112
113@@ -174,7 +230,7 @@
114 if (argument)
115 {
116 if ((option_wait=atoi(argument)) <= 0)
117- option_wait=1;
118+ option_wait= 1;
119 }
120 else
121 option_wait= ~(uint32_t)0;
122@@ -202,7 +258,7 @@
123 MY_INIT(argv[0]);
124 drizzleclient_create(&drizzle);
125 load_defaults("drizzle",load_default_groups,&argc,&argv);
126- save_argv = argv; /* Save for free_defaults */
127+ save_argv= argv; /* Save for free_defaults */
128 if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
129 {
130 free_defaults(save_argv);
131@@ -215,7 +271,7 @@
132 exit(1);
133 }
134
135- commands = argv;
136+ commands= argv;
137 if (tty_password)
138 opt_password = drizzleclient_get_tty_password(NULL);
139
140@@ -224,7 +280,7 @@
141
142 if (opt_connect_timeout)
143 {
144- uint32_t tmp=opt_connect_timeout;
145+ uint32_t tmp= opt_connect_timeout;
146 drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
147 }
148
149@@ -238,7 +294,8 @@
150 /* Return 0 if all commands are PING */
151 for (; argc > 0; argv++, argc--)
152 {
153- if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING)
154+ int type= get_command_type(argv[0]);
155+ if (type != ADMIN_PING)
156 {
157 error= 1;
158 break;
159@@ -248,7 +305,7 @@
160 }
161 else
162 {
163- error=execute_commands(&drizzle,argc,commands);
164+ error= execute_commands(&drizzle,argc,commands);
165 drizzleclient_close(&drizzle);
166 }
167 free(opt_password);
168@@ -260,12 +317,12 @@
169
170 void endprog(int)
171 {
172- interrupted=1;
173+ interrupted= 1;
174 }
175
176 static bool sql_connect(DRIZZLE *drizzle, uint32_t wait)
177 {
178- bool info=0;
179+ bool info= 0;
180
181 for (;;)
182 {
183@@ -313,7 +370,7 @@
184 {
185 if (!info)
186 {
187- info=1;
188+ info= 1;
189 fputs(_("Waiting for Drizzle server to answer"),stderr);
190 (void) fflush(stderr);
191 }
192@@ -343,7 +400,8 @@
193 */
194 for (; argc > 0 ; argv++,argc--)
195 {
196- switch (find_type(argv[0],&command_typelib,2)) {
197+ int type= get_command_type(argv[0]);
198+ switch (type) {
199 case ADMIN_SHUTDOWN:
200 {
201 if (opt_verbose)
202@@ -360,11 +418,11 @@
203 if (opt_verbose)
204 printf(_("done\n"));
205
206- argc=1; /* Force SHUTDOWN to be the last command */
207+ argc= 1; /* Force SHUTDOWN to be the last command */
208 break;
209 }
210 case ADMIN_PING:
211- drizzle->reconnect=0; /* We want to know of reconnects */
212+ drizzle->reconnect= 0; /* We want to know of reconnects */
213 if (!drizzleclient_ping(drizzle))
214 {
215 if (option_silent < 2)
216@@ -374,7 +432,7 @@
217 {
218 if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR)
219 {
220- drizzle->reconnect=1;
221+ drizzle->reconnect= 1;
222 if (!drizzleclient_ping(drizzle))
223 puts(_("connection was down, but drizzled is now alive"));
224 }
225@@ -385,7 +443,7 @@
226 return -1;
227 }
228 }
229- drizzle->reconnect=1; /* Automatic reconnect is default */
230+ drizzle->reconnect= 1; /* Automatic reconnect is default */
231 break;
232
233 default: