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
=== modified file 'client/drizzleadmin.cc'
--- client/drizzleadmin.cc 2009-03-04 22:49:53 +0000
+++ client/drizzleadmin.cc 2009-03-20 21:51:00 +0000
@@ -24,12 +24,16 @@
24#include <signal.h>24#include <signal.h>
25#include <mysys/my_pthread.h> /* because of signal() */25#include <mysys/my_pthread.h> /* because of signal() */
26#include <sys/stat.h>26#include <sys/stat.h>
27#include <vector>
28#include <algorithm>
29#include <string>
2730
28/* Added this for string translation. */31/* Added this for string translation. */
29#include <drizzled/gettext.h>32#include <drizzled/gettext.h>
3033
31#define ADMIN_VERSION "8.42"34#define ADMIN_VERSION "8.42"
32#define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */35#define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */
36#define NUM_COMMAND_NAMES 2
3337
34char *host= NULL, *user= NULL, *opt_password= NULL;38char *host= NULL, *user= NULL, *opt_password= NULL;
35static bool interrupted= false, opt_verbose= false,tty_password= false;39static bool interrupted= false, opt_verbose= false,tty_password= false;
@@ -62,12 +66,11 @@
6266
63static const char *command_names[]= {67static const char *command_names[]= {
64 "shutdown",68 "shutdown",
65 "ping",69 "ping"
66 NULL
67};70};
6871
69static TYPELIB command_typelib=72static vector<const char *>
70{ array_elements(command_names)-1,"commands", command_names, NULL };73 command_vector(command_names, command_names + NUM_COMMAND_NAMES);
7174
72static struct my_option my_long_options[] =75static struct my_option my_long_options[] =
73{76{
@@ -106,12 +109,65 @@
106109
107static const char *load_default_groups[]= { "drizzleadmin","client",0 };110static const char *load_default_groups[]= { "drizzleadmin","client",0 };
108111
112inline string lower_string(const char * from_string)
113{
114 string to_string= from_string;
115 transform(to_string.begin(), to_string.end(),
116 to_string.begin(), ::tolower);
117 return to_string;
118}
119
120/*
121 * Function object to be used to compare a string
122 * against the vector of command strings. This function
123 * object is used in the get_command_type() method.
124 */
125template <class T>
126class CommandMatch :
127 public unary_function<const string&, bool>
128{
129 string match_text;
130 T match_func;
131 public:
132 CommandMatch(string text) : match_text(text) { }
133 inline bool operator()(const string match_against) const
134 {
135 return match_func(match_against, match_text);
136 }
137};
138
139/*
140 * Searches for the given command and determines
141 * its type.
142 *
143 * Returns the type of the command corresponding
144 * to the commands enum defined in drizzleadmin.cc
145 * If the command is not supported, return ADMIN_ERROR.
146 *
147 * @param command name to search for
148 */
149static int get_command_type(const char *name)
150{
151 int type= ADMIN_ERROR;
152 string comp_string= lower_string(name);
153 vector<const char *>::iterator it=
154 std::find_if(command_vector.begin(),
155 command_vector.end(),
156 CommandMatch< equal_to<string> >(comp_string));
157 if (it != command_vector.end())
158 {
159 /* add 1 due to the way the commands ENUM is defined */
160 type= distance(command_vector.begin(), it) + 1;
161 }
162 return type;
163}
164
109bool165bool
110get_one_option(int optid, const struct my_option *, char *argument)166get_one_option(int optid, const struct my_option *, char *argument)
111{167{
112 char *endchar= NULL;168 char *endchar= NULL;
113 uint64_t temp_drizzle_port= 0;169 uint64_t temp_drizzle_port= 0;
114 int error = 0;170 int error= 0;
115171
116 switch(optid) {172 switch(optid) {
117 case 'p':173 case 'p':
@@ -138,7 +194,7 @@
138 case 'P':194 case 'P':
139 if (argument)195 if (argument)
140 {196 {
141 char *start=argument;197 char *start= argument;
142 if (opt_password)198 if (opt_password)
143 free(opt_password);199 free(opt_password);
144200
@@ -174,7 +230,7 @@
174 if (argument)230 if (argument)
175 {231 {
176 if ((option_wait=atoi(argument)) <= 0)232 if ((option_wait=atoi(argument)) <= 0)
177 option_wait=1;233 option_wait= 1;
178 }234 }
179 else235 else
180 option_wait= ~(uint32_t)0;236 option_wait= ~(uint32_t)0;
@@ -202,7 +258,7 @@
202 MY_INIT(argv[0]);258 MY_INIT(argv[0]);
203 drizzleclient_create(&drizzle);259 drizzleclient_create(&drizzle);
204 load_defaults("drizzle",load_default_groups,&argc,&argv);260 load_defaults("drizzle",load_default_groups,&argc,&argv);
205 save_argv = argv; /* Save for free_defaults */261 save_argv= argv; /* Save for free_defaults */
206 if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))262 if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
207 {263 {
208 free_defaults(save_argv);264 free_defaults(save_argv);
@@ -215,7 +271,7 @@
215 exit(1);271 exit(1);
216 }272 }
217273
218 commands = argv;274 commands= argv;
219 if (tty_password)275 if (tty_password)
220 opt_password = drizzleclient_get_tty_password(NULL);276 opt_password = drizzleclient_get_tty_password(NULL);
221277
@@ -224,7 +280,7 @@
224280
225 if (opt_connect_timeout)281 if (opt_connect_timeout)
226 {282 {
227 uint32_t tmp=opt_connect_timeout;283 uint32_t tmp= opt_connect_timeout;
228 drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);284 drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
229 }285 }
230286
@@ -238,7 +294,8 @@
238 /* Return 0 if all commands are PING */294 /* Return 0 if all commands are PING */
239 for (; argc > 0; argv++, argc--)295 for (; argc > 0; argv++, argc--)
240 {296 {
241 if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING)297 int type= get_command_type(argv[0]);
298 if (type != ADMIN_PING)
242 {299 {
243 error= 1;300 error= 1;
244 break;301 break;
@@ -248,7 +305,7 @@
248 }305 }
249 else306 else
250 {307 {
251 error=execute_commands(&drizzle,argc,commands);308 error= execute_commands(&drizzle,argc,commands);
252 drizzleclient_close(&drizzle);309 drizzleclient_close(&drizzle);
253 }310 }
254 free(opt_password);311 free(opt_password);
@@ -260,12 +317,12 @@
260317
261void endprog(int)318void endprog(int)
262{319{
263 interrupted=1;320 interrupted= 1;
264}321}
265322
266static bool sql_connect(DRIZZLE *drizzle, uint32_t wait)323static bool sql_connect(DRIZZLE *drizzle, uint32_t wait)
267{324{
268 bool info=0;325 bool info= 0;
269326
270 for (;;)327 for (;;)
271 {328 {
@@ -313,7 +370,7 @@
313 {370 {
314 if (!info)371 if (!info)
315 {372 {
316 info=1;373 info= 1;
317 fputs(_("Waiting for Drizzle server to answer"),stderr);374 fputs(_("Waiting for Drizzle server to answer"),stderr);
318 (void) fflush(stderr);375 (void) fflush(stderr);
319 }376 }
@@ -343,7 +400,8 @@
343 */400 */
344 for (; argc > 0 ; argv++,argc--)401 for (; argc > 0 ; argv++,argc--)
345 {402 {
346 switch (find_type(argv[0],&command_typelib,2)) {403 int type= get_command_type(argv[0]);
404 switch (type) {
347 case ADMIN_SHUTDOWN:405 case ADMIN_SHUTDOWN:
348 {406 {
349 if (opt_verbose)407 if (opt_verbose)
@@ -360,11 +418,11 @@
360 if (opt_verbose)418 if (opt_verbose)
361 printf(_("done\n"));419 printf(_("done\n"));
362420
363 argc=1; /* Force SHUTDOWN to be the last command */421 argc= 1; /* Force SHUTDOWN to be the last command */
364 break;422 break;
365 }423 }
366 case ADMIN_PING:424 case ADMIN_PING:
367 drizzle->reconnect=0; /* We want to know of reconnects */425 drizzle->reconnect= 0; /* We want to know of reconnects */
368 if (!drizzleclient_ping(drizzle))426 if (!drizzleclient_ping(drizzle))
369 {427 {
370 if (option_silent < 2)428 if (option_silent < 2)
@@ -374,7 +432,7 @@
374 {432 {
375 if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR)433 if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR)
376 {434 {
377 drizzle->reconnect=1;435 drizzle->reconnect= 1;
378 if (!drizzleclient_ping(drizzle))436 if (!drizzleclient_ping(drizzle))
379 puts(_("connection was down, but drizzled is now alive"));437 puts(_("connection was down, but drizzled is now alive"));
380 }438 }
@@ -385,7 +443,7 @@
385 return -1;443 return -1;
386 }444 }
387 }445 }
388 drizzle->reconnect=1; /* Automatic reconnect is default */446 drizzle->reconnect= 1; /* Automatic reconnect is default */
389 break;447 break;
390448
391 default:449 default: