Merge lp:~posulliv/drizzle/cleanup-replace-typelib into lp:~drizzle-trunk/drizzle/development
- cleanup-replace-typelib
- Merge into development
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 |
Related bugs: | |
Related blueprints: |
Replace TYPELIB with STL standards
(Medium)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jay Pipes (community) | Approve | ||
Review via email:
|
Commit message
Description of the change
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Padraig O'Sullivan (posulliv) wrote : | # |
- 959. By Brian Aker
-
Merge from Stewart.
- 960. By Brian Aker
-
Merging Monty
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
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-
>
> 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/
> 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_
{
int type= ADMIN_ERROR;
string comp_string= lower_string(name);
vector<
std:
if (it != command_
{
/* add 1 due to the way the commands ENUM is defined */
type= distance(
}
return type;
}
Monty
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
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
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 | 24 | #include <signal.h> | 24 | #include <signal.h> |
6 | 25 | #include <mysys/my_pthread.h> /* because of signal() */ | 25 | #include <mysys/my_pthread.h> /* because of signal() */ |
7 | 26 | #include <sys/stat.h> | 26 | #include <sys/stat.h> |
8 | 27 | #include <vector> | ||
9 | 28 | #include <algorithm> | ||
10 | 29 | #include <string> | ||
11 | 27 | 30 | ||
12 | 28 | /* Added this for string translation. */ | 31 | /* Added this for string translation. */ |
13 | 29 | #include <drizzled/gettext.h> | 32 | #include <drizzled/gettext.h> |
14 | 30 | 33 | ||
15 | 31 | #define ADMIN_VERSION "8.42" | 34 | #define ADMIN_VERSION "8.42" |
16 | 32 | #define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */ | 35 | #define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */ |
17 | 36 | #define NUM_COMMAND_NAMES 2 | ||
18 | 33 | 37 | ||
19 | 34 | char *host= NULL, *user= NULL, *opt_password= NULL; | 38 | char *host= NULL, *user= NULL, *opt_password= NULL; |
20 | 35 | static bool interrupted= false, opt_verbose= false,tty_password= false; | 39 | static bool interrupted= false, opt_verbose= false,tty_password= false; |
21 | @@ -62,12 +66,11 @@ | |||
22 | 62 | 66 | ||
23 | 63 | static const char *command_names[]= { | 67 | static const char *command_names[]= { |
24 | 64 | "shutdown", | 68 | "shutdown", |
27 | 65 | "ping", | 69 | "ping" |
26 | 66 | NULL | ||
28 | 67 | }; | 70 | }; |
29 | 68 | 71 | ||
32 | 69 | static TYPELIB command_typelib= | 72 | static vector<const char *> |
33 | 70 | { array_elements(command_names)-1,"commands", command_names, NULL }; | 73 | command_vector(command_names, command_names + NUM_COMMAND_NAMES); |
34 | 71 | 74 | ||
35 | 72 | static struct my_option my_long_options[] = | 75 | static struct my_option my_long_options[] = |
36 | 73 | { | 76 | { |
37 | @@ -106,12 +109,65 @@ | |||
38 | 106 | 109 | ||
39 | 107 | static const char *load_default_groups[]= { "drizzleadmin","client",0 }; | 110 | static const char *load_default_groups[]= { "drizzleadmin","client",0 }; |
40 | 108 | 111 | ||
41 | 112 | inline string lower_string(const char * from_string) | ||
42 | 113 | { | ||
43 | 114 | string to_string= from_string; | ||
44 | 115 | transform(to_string.begin(), to_string.end(), | ||
45 | 116 | to_string.begin(), ::tolower); | ||
46 | 117 | return to_string; | ||
47 | 118 | } | ||
48 | 119 | |||
49 | 120 | /* | ||
50 | 121 | * Function object to be used to compare a string | ||
51 | 122 | * against the vector of command strings. This function | ||
52 | 123 | * object is used in the get_command_type() method. | ||
53 | 124 | */ | ||
54 | 125 | template <class T> | ||
55 | 126 | class CommandMatch : | ||
56 | 127 | public unary_function<const string&, bool> | ||
57 | 128 | { | ||
58 | 129 | string match_text; | ||
59 | 130 | T match_func; | ||
60 | 131 | public: | ||
61 | 132 | CommandMatch(string text) : match_text(text) { } | ||
62 | 133 | inline bool operator()(const string match_against) const | ||
63 | 134 | { | ||
64 | 135 | return match_func(match_against, match_text); | ||
65 | 136 | } | ||
66 | 137 | }; | ||
67 | 138 | |||
68 | 139 | /* | ||
69 | 140 | * Searches for the given command and determines | ||
70 | 141 | * its type. | ||
71 | 142 | * | ||
72 | 143 | * Returns the type of the command corresponding | ||
73 | 144 | * to the commands enum defined in drizzleadmin.cc | ||
74 | 145 | * If the command is not supported, return ADMIN_ERROR. | ||
75 | 146 | * | ||
76 | 147 | * @param command name to search for | ||
77 | 148 | */ | ||
78 | 149 | static int get_command_type(const char *name) | ||
79 | 150 | { | ||
80 | 151 | int type= ADMIN_ERROR; | ||
81 | 152 | string comp_string= lower_string(name); | ||
82 | 153 | vector<const char *>::iterator it= | ||
83 | 154 | std::find_if(command_vector.begin(), | ||
84 | 155 | command_vector.end(), | ||
85 | 156 | CommandMatch< equal_to<string> >(comp_string)); | ||
86 | 157 | if (it != command_vector.end()) | ||
87 | 158 | { | ||
88 | 159 | /* add 1 due to the way the commands ENUM is defined */ | ||
89 | 160 | type= distance(command_vector.begin(), it) + 1; | ||
90 | 161 | } | ||
91 | 162 | return type; | ||
92 | 163 | } | ||
93 | 164 | |||
94 | 109 | bool | 165 | bool |
95 | 110 | get_one_option(int optid, const struct my_option *, char *argument) | 166 | get_one_option(int optid, const struct my_option *, char *argument) |
96 | 111 | { | 167 | { |
97 | 112 | char *endchar= NULL; | 168 | char *endchar= NULL; |
98 | 113 | uint64_t temp_drizzle_port= 0; | 169 | uint64_t temp_drizzle_port= 0; |
100 | 114 | int error = 0; | 170 | int error= 0; |
101 | 115 | 171 | ||
102 | 116 | switch(optid) { | 172 | switch(optid) { |
103 | 117 | case 'p': | 173 | case 'p': |
104 | @@ -138,7 +194,7 @@ | |||
105 | 138 | case 'P': | 194 | case 'P': |
106 | 139 | if (argument) | 195 | if (argument) |
107 | 140 | { | 196 | { |
109 | 141 | char *start=argument; | 197 | char *start= argument; |
110 | 142 | if (opt_password) | 198 | if (opt_password) |
111 | 143 | free(opt_password); | 199 | free(opt_password); |
112 | 144 | 200 | ||
113 | @@ -174,7 +230,7 @@ | |||
114 | 174 | if (argument) | 230 | if (argument) |
115 | 175 | { | 231 | { |
116 | 176 | if ((option_wait=atoi(argument)) <= 0) | 232 | if ((option_wait=atoi(argument)) <= 0) |
118 | 177 | option_wait=1; | 233 | option_wait= 1; |
119 | 178 | } | 234 | } |
120 | 179 | else | 235 | else |
121 | 180 | option_wait= ~(uint32_t)0; | 236 | option_wait= ~(uint32_t)0; |
122 | @@ -202,7 +258,7 @@ | |||
123 | 202 | MY_INIT(argv[0]); | 258 | MY_INIT(argv[0]); |
124 | 203 | drizzleclient_create(&drizzle); | 259 | drizzleclient_create(&drizzle); |
125 | 204 | load_defaults("drizzle",load_default_groups,&argc,&argv); | 260 | load_defaults("drizzle",load_default_groups,&argc,&argv); |
127 | 205 | save_argv = argv; /* Save for free_defaults */ | 261 | save_argv= argv; /* Save for free_defaults */ |
128 | 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))) |
129 | 207 | { | 263 | { |
130 | 208 | free_defaults(save_argv); | 264 | free_defaults(save_argv); |
131 | @@ -215,7 +271,7 @@ | |||
132 | 215 | exit(1); | 271 | exit(1); |
133 | 216 | } | 272 | } |
134 | 217 | 273 | ||
136 | 218 | commands = argv; | 274 | commands= argv; |
137 | 219 | if (tty_password) | 275 | if (tty_password) |
138 | 220 | opt_password = drizzleclient_get_tty_password(NULL); | 276 | opt_password = drizzleclient_get_tty_password(NULL); |
139 | 221 | 277 | ||
140 | @@ -224,7 +280,7 @@ | |||
141 | 224 | 280 | ||
142 | 225 | if (opt_connect_timeout) | 281 | if (opt_connect_timeout) |
143 | 226 | { | 282 | { |
145 | 227 | uint32_t tmp=opt_connect_timeout; | 283 | uint32_t tmp= opt_connect_timeout; |
146 | 228 | drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp); | 284 | drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp); |
147 | 229 | } | 285 | } |
148 | 230 | 286 | ||
149 | @@ -238,7 +294,8 @@ | |||
150 | 238 | /* Return 0 if all commands are PING */ | 294 | /* Return 0 if all commands are PING */ |
151 | 239 | for (; argc > 0; argv++, argc--) | 295 | for (; argc > 0; argv++, argc--) |
152 | 240 | { | 296 | { |
154 | 241 | if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING) | 297 | int type= get_command_type(argv[0]); |
155 | 298 | if (type != ADMIN_PING) | ||
156 | 242 | { | 299 | { |
157 | 243 | error= 1; | 300 | error= 1; |
158 | 244 | break; | 301 | break; |
159 | @@ -248,7 +305,7 @@ | |||
160 | 248 | } | 305 | } |
161 | 249 | else | 306 | else |
162 | 250 | { | 307 | { |
164 | 251 | error=execute_commands(&drizzle,argc,commands); | 308 | error= execute_commands(&drizzle,argc,commands); |
165 | 252 | drizzleclient_close(&drizzle); | 309 | drizzleclient_close(&drizzle); |
166 | 253 | } | 310 | } |
167 | 254 | free(opt_password); | 311 | free(opt_password); |
168 | @@ -260,12 +317,12 @@ | |||
169 | 260 | 317 | ||
170 | 261 | void endprog(int) | 318 | void endprog(int) |
171 | 262 | { | 319 | { |
173 | 263 | interrupted=1; | 320 | interrupted= 1; |
174 | 264 | } | 321 | } |
175 | 265 | 322 | ||
176 | 266 | static bool sql_connect(DRIZZLE *drizzle, uint32_t wait) | 323 | static bool sql_connect(DRIZZLE *drizzle, uint32_t wait) |
177 | 267 | { | 324 | { |
179 | 268 | bool info=0; | 325 | bool info= 0; |
180 | 269 | 326 | ||
181 | 270 | for (;;) | 327 | for (;;) |
182 | 271 | { | 328 | { |
183 | @@ -313,7 +370,7 @@ | |||
184 | 313 | { | 370 | { |
185 | 314 | if (!info) | 371 | if (!info) |
186 | 315 | { | 372 | { |
188 | 316 | info=1; | 373 | info= 1; |
189 | 317 | fputs(_("Waiting for Drizzle server to answer"),stderr); | 374 | fputs(_("Waiting for Drizzle server to answer"),stderr); |
190 | 318 | (void) fflush(stderr); | 375 | (void) fflush(stderr); |
191 | 319 | } | 376 | } |
192 | @@ -343,7 +400,8 @@ | |||
193 | 343 | */ | 400 | */ |
194 | 344 | for (; argc > 0 ; argv++,argc--) | 401 | for (; argc > 0 ; argv++,argc--) |
195 | 345 | { | 402 | { |
197 | 346 | switch (find_type(argv[0],&command_typelib,2)) { | 403 | int type= get_command_type(argv[0]); |
198 | 404 | switch (type) { | ||
199 | 347 | case ADMIN_SHUTDOWN: | 405 | case ADMIN_SHUTDOWN: |
200 | 348 | { | 406 | { |
201 | 349 | if (opt_verbose) | 407 | if (opt_verbose) |
202 | @@ -360,11 +418,11 @@ | |||
203 | 360 | if (opt_verbose) | 418 | if (opt_verbose) |
204 | 361 | printf(_("done\n")); | 419 | printf(_("done\n")); |
205 | 362 | 420 | ||
207 | 363 | argc=1; /* Force SHUTDOWN to be the last command */ | 421 | argc= 1; /* Force SHUTDOWN to be the last command */ |
208 | 364 | break; | 422 | break; |
209 | 365 | } | 423 | } |
210 | 366 | case ADMIN_PING: | 424 | case ADMIN_PING: |
212 | 367 | drizzle->reconnect=0; /* We want to know of reconnects */ | 425 | drizzle->reconnect= 0; /* We want to know of reconnects */ |
213 | 368 | if (!drizzleclient_ping(drizzle)) | 426 | if (!drizzleclient_ping(drizzle)) |
214 | 369 | { | 427 | { |
215 | 370 | if (option_silent < 2) | 428 | if (option_silent < 2) |
216 | @@ -374,7 +432,7 @@ | |||
217 | 374 | { | 432 | { |
218 | 375 | if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR) | 433 | if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR) |
219 | 376 | { | 434 | { |
221 | 377 | drizzle->reconnect=1; | 435 | drizzle->reconnect= 1; |
222 | 378 | if (!drizzleclient_ping(drizzle)) | 436 | if (!drizzleclient_ping(drizzle)) |
223 | 379 | puts(_("connection was down, but drizzled is now alive")); | 437 | puts(_("connection was down, but drizzled is now alive")); |
224 | 380 | } | 438 | } |
225 | @@ -385,7 +443,7 @@ | |||
226 | 385 | return -1; | 443 | return -1; |
227 | 386 | } | 444 | } |
228 | 387 | } | 445 | } |
230 | 388 | drizzle->reconnect=1; /* Automatic reconnect is default */ | 446 | drizzle->reconnect= 1; /* Automatic reconnect is default */ |
231 | 389 | break; | 447 | break; |
232 | 390 | 448 | ||
233 | 391 | default: | 449 | default: |
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.