1 | |
2 | // vim:sw=2:ai |
3 | |
4 | /* |
5 | * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. |
6 | * See COPYRIGHT.txt for details. |
7 | */ |
8 | |
9 | #include <my_global.h> |
10 | #include <string.h> |
11 | |
12 | #include "database.hpp" |
13 | #include "string_util.hpp" |
14 | #include "escape.hpp" |
15 | #include "mysql_incl.hpp" |
16 | |
17 | #define DBG_KEY(x) |
18 | #define DBG_SHUT(x) |
19 | #define DBG_LOCK(x) |
20 | #define DBG_THR(x) |
21 | #define DBG_CMP(x) |
22 | #define DBG_FLD(x) |
23 | #define DBG_FILTER(x) |
24 | #define DBG_REFCNT(x) |
25 | #define DBG_KEYLEN(x) |
26 | #define DBG_DELETED |
27 | |
28 | /* status variables */ |
29 | unsigned long long int open_tables_count; |
30 | unsigned long long int close_tables_count; |
31 | unsigned long long int lock_tables_count; |
32 | unsigned long long int unlock_tables_count; |
33 | unsigned long long int index_exec_count; |
34 | |
35 | namespace dena { |
36 | |
37 | prep_stmt::prep_stmt() |
38 | : dbctx(0), table_id(static_cast<size_t>(-1)), |
39 | idxnum(static_cast<size_t>(-1)) |
40 | { |
41 | } |
42 | prep_stmt::prep_stmt(dbcontext_i *c, size_t tbl, size_t idx, |
43 | const fields_type& rf, const fields_type& ff) |
44 | : dbctx(c), table_id(tbl), idxnum(idx), ret_fields(rf), filter_fields(ff) |
45 | { |
46 | if (dbctx) { |
47 | dbctx->table_addref(table_id); |
48 | } |
49 | } |
50 | prep_stmt::~prep_stmt() |
51 | { |
52 | if (dbctx) { |
53 | dbctx->table_release(table_id); |
54 | } |
55 | } |
56 | |
57 | prep_stmt::prep_stmt(const prep_stmt& x) |
58 | : dbctx(x.dbctx), table_id(x.table_id), idxnum(x.idxnum), |
59 | ret_fields(x.ret_fields), filter_fields(x.filter_fields) |
60 | { |
61 | if (dbctx) { |
62 | dbctx->table_addref(table_id); |
63 | } |
64 | } |
65 | |
66 | prep_stmt& |
67 | prep_stmt::operator =(const prep_stmt& x) |
68 | { |
69 | if (this != &x) { |
70 | if (dbctx) { |
71 | dbctx->table_release(table_id); |
72 | } |
73 | dbctx = x.dbctx; |
74 | table_id = x.table_id; |
75 | idxnum = x.idxnum; |
76 | ret_fields = x.ret_fields; |
77 | filter_fields = x.filter_fields; |
78 | if (dbctx) { |
79 | dbctx->table_addref(table_id); |
80 | } |
81 | } |
82 | return *this; |
83 | } |
84 | |
85 | struct database : public database_i, private noncopyable { |
86 | database(const config& c); |
87 | virtual ~database(); |
88 | virtual dbcontext_ptr create_context(bool for_write) volatile; |
89 | virtual void stop() volatile; |
90 | virtual const config& get_conf() const volatile; |
91 | public: |
92 | int child_running; |
93 | private: |
94 | config conf; |
95 | }; |
96 | |
97 | struct tablevec_entry { |
98 | TABLE *table; |
99 | size_t refcount; |
100 | bool modified; |
101 | tablevec_entry() : table(0), refcount(0), modified(false) { } |
102 | }; |
103 | |
104 | struct expr_user_lock : private noncopyable { |
105 | expr_user_lock(THD *thd, int timeout) |
106 | : lck_key(thd, "handlersocket_wr" , 16, &my_charset_latin1), |
107 | lck_timeout(thd, timeout), |
108 | lck_func_get_lock(thd, &lck_key, &lck_timeout), |
109 | lck_func_release_lock(thd, &lck_key) |
110 | { |
111 | lck_key.fix_fields(thd, 0); |
112 | lck_timeout.fix_fields(thd, 0); |
113 | lck_func_get_lock.fix_fields(thd, 0); |
114 | lck_func_release_lock.fix_fields(thd, 0); |
115 | } |
116 | long long get_lock() { |
117 | return lck_func_get_lock.val_int(); |
118 | } |
119 | long long release_lock() { |
120 | return lck_func_release_lock.val_int(); |
121 | } |
122 | private: |
123 | Item_string lck_key; |
124 | Item_int lck_timeout; |
125 | Item_func_get_lock lck_func_get_lock; |
126 | Item_func_release_lock lck_func_release_lock; |
127 | }; |
128 | |
129 | struct dbcontext : public dbcontext_i, private noncopyable { |
130 | dbcontext(volatile database *d, bool for_write); |
131 | virtual ~dbcontext(); |
132 | virtual void init_thread(const void *stack_botton, |
133 | volatile int& shutdown_flag); |
134 | virtual void term_thread(); |
135 | virtual bool check_alive(); |
136 | virtual void lock_tables_if(); |
137 | virtual void unlock_tables_if(); |
138 | virtual bool get_commit_error(); |
139 | virtual void clear_error(); |
140 | virtual void close_tables_if(); |
141 | virtual void table_addref(size_t tbl_id); |
142 | virtual void table_release(size_t tbl_id); |
143 | virtual void cmd_open(dbcallback_i& cb, const cmd_open_args& args); |
144 | virtual void cmd_exec(dbcallback_i& cb, const cmd_exec_args& args); |
145 | virtual void set_statistics(size_t num_conns, size_t num_active); |
146 | private: |
147 | int set_thread_message(const char *fmt, ...) |
148 | __attribute__((format (printf, 2, 3))); |
149 | bool parse_fields(TABLE *const table, const char *str, |
150 | prep_stmt::fields_type& flds); |
151 | void cmd_insert_internal(dbcallback_i& cb, const prep_stmt& pst, |
152 | const string_ref *fvals, size_t fvalslen); |
153 | void cmd_sql_internal(dbcallback_i& cb, const prep_stmt& pst, |
154 | const string_ref *fvals, size_t fvalslen); |
155 | void cmd_find_internal(dbcallback_i& cb, const prep_stmt& pst, |
156 | ha_rkey_function find_flag, const cmd_exec_args& args); |
157 | size_t calc_filter_buf_size(TABLE *table, const prep_stmt& pst, |
158 | const record_filter *filters); |
159 | bool fill_filter_buf(TABLE *table, const prep_stmt& pst, |
160 | const record_filter *filters, uchar *filter_buf, size_t len); |
161 | int check_filter(dbcallback_i& cb, TABLE *table, const prep_stmt& pst, |
162 | const record_filter *filters, const uchar *filter_buf); |
163 | void resp_record(dbcallback_i& cb, TABLE *const table, const prep_stmt& pst); |
164 | void dump_record(dbcallback_i& cb, TABLE *const table, const prep_stmt& pst); |
165 | int modify_record(dbcallback_i& cb, TABLE *const table, |
166 | const prep_stmt& pst, const cmd_exec_args& args, char mod_op, |
167 | size_t& modified_count); |
168 | private: |
169 | typedef std::vector<tablevec_entry> table_vec_type; |
170 | typedef std::pair<std::string, std::string> table_name_type; |
171 | typedef std::map<table_name_type, size_t> table_map_type; |
172 | private: |
173 | volatile database *const dbref; |
174 | bool for_write_flag; |
175 | THD *thd; |
176 | MYSQL_LOCK *lock; |
177 | bool lock_failed; |
178 | std::auto_ptr<expr_user_lock> user_lock; |
179 | int user_level_lock_timeout; |
180 | bool user_level_lock_locked; |
181 | bool commit_error; |
182 | std::vector<char> info_message_buf; |
183 | table_vec_type table_vec; |
184 | table_map_type table_map; |
185 | }; |
186 | |
187 | database::database(const config& c) |
188 | : child_running(1), conf(c) |
189 | { |
190 | } |
191 | |
192 | database::~database() |
193 | { |
194 | } |
195 | |
196 | dbcontext_ptr |
197 | database::create_context(bool for_write) volatile |
198 | { |
199 | return dbcontext_ptr(new dbcontext(this, for_write)); |
200 | } |
201 | |
202 | void |
203 | database::stop() volatile |
204 | { |
205 | child_running = false; |
206 | } |
207 | |
208 | const config& |
209 | database::get_conf() const volatile |
210 | { |
211 | return const_cast<const config&>(conf); |
212 | } |
213 | |
214 | database_ptr |
215 | database_i::create(const config& conf) |
216 | { |
217 | return database_ptr(new database(conf)); |
218 | } |
219 | |
220 | dbcontext::dbcontext(volatile database *d, bool for_write) |
221 | : dbref(d), for_write_flag(for_write), thd(0), lock(0), lock_failed(false), |
222 | user_level_lock_timeout(0), user_level_lock_locked(false), |
223 | commit_error(false) |
224 | { |
225 | info_message_buf.resize(8192); |
226 | user_level_lock_timeout = d->get_conf().get_int("wrlock_timeout" , 12); |
227 | } |
228 | |
229 | dbcontext::~dbcontext() |
230 | { |
231 | } |
232 | |
233 | namespace { |
234 | |
235 | int |
236 | wait_server_to_start(THD *thd, volatile int& shutdown_flag) |
237 | { |
238 | int r = 0; |
239 | DBG_SHUT(fprintf(stderr, "HNDSOCK wsts\n" )); |
240 | pthread_mutex_lock(&LOCK_server_started); |
241 | while (!mysqld_server_started) { |
242 | timespec abstime; |
243 | set_timespec(abstime, 1); |
244 | pthread_cond_timedwait(&COND_server_started, &LOCK_server_started, |
245 | &abstime); |
246 | pthread_mutex_unlock(&LOCK_server_started); |
247 | pthread_mutex_lock(&thd->mysys_var->mutex); |
248 | killed_state st = thd->killed; |
249 | pthread_mutex_unlock(&thd->mysys_var->mutex); |
250 | DBG_SHUT(fprintf(stderr, "HNDSOCK wsts kst %d\n" , (int)st)); |
251 | pthread_mutex_lock(&LOCK_server_started); |
252 | if (st != NOT_KILLED) { |
253 | DBG_SHUT(fprintf(stderr, "HNDSOCK wsts kst %d break\n" , (int)st)); |
254 | r = -1; |
255 | break; |
256 | } |
257 | if (shutdown_flag) { |
258 | DBG_SHUT(fprintf(stderr, "HNDSOCK wsts kst shut break\n" )); |
259 | r = -1; |
260 | break; |
261 | } |
262 | } |
263 | pthread_mutex_unlock(&LOCK_server_started); |
264 | DBG_SHUT(fprintf(stderr, "HNDSOCK wsts done\n" )); |
265 | return r; |
266 | } |
267 | |
268 | }; // namespace |
269 | |
270 | #define DENA_THR_OFFSETOF(fld) ((char *)(&thd->fld) - (char *)thd) |
271 | |
272 | void |
273 | dbcontext::init_thread(const void *stack_bottom, volatile int& shutdown_flag) |
274 | { |
275 | DBG_THR(fprintf(stderr, "HNDSOCK init thread\n" )); |
276 | { |
277 | my_thread_init(); |
278 | thd = new THD(0); |
279 | thd->thread_stack = (char *)stack_bottom; |
280 | DBG_THR(fprintf(stderr, |
281 | "thread_stack = %p sizeof(THD)=%zu sizeof(mtx)=%zu " |
282 | "O: %zu %zu %zu %zu %zu %zu %zu\n" , |
283 | thd->thread_stack, sizeof(THD), sizeof(LOCK_thread_count), |
284 | DENA_THR_OFFSETOF(mdl_context), |
285 | DENA_THR_OFFSETOF(net), |
286 | DENA_THR_OFFSETOF(LOCK_thd_data), |
287 | DENA_THR_OFFSETOF(mysys_var), |
288 | DENA_THR_OFFSETOF(stmt_arena), |
289 | DENA_THR_OFFSETOF(limit_found_rows), |
290 | DENA_THR_OFFSETOF(locked_tables_list))); |
291 | thd->store_globals(); |
292 | thd->system_thread = static_cast<enum_thread_type>(1<<30UL); |
293 | memset(&thd->net, 0, sizeof(thd->net)); |
294 | if (for_write_flag) { |
295 | #if MYSQL_VERSION_ID >= 50505 |
296 | thd->variables.option_bits |= OPTION_BIN_LOG; |
297 | #else |
298 | thd->options |= OPTION_BIN_LOG; |
299 | #endif |
300 | safeFree((char*) thd->db.str); |
301 | thd->db.str= my_strdup("handlersocket" , MYF(0)); |
302 | thd->db.length= sizeof("handlersocket" )-1; |
303 | } |
304 | thd->variables.option_bits |= OPTION_TABLE_LOCK; |
305 | my_pthread_setspecific_ptr(THR_THD, thd); |
306 | DBG_THR(fprintf(stderr, "HNDSOCK x0 %p\n" , thd)); |
307 | } |
308 | { |
309 | thd->thread_id = next_thread_id(); |
310 | add_to_active_threads(thd); |
311 | } |
312 | |
313 | DBG_THR(fprintf(stderr, "HNDSOCK init thread wsts\n" )); |
314 | wait_server_to_start(thd, shutdown_flag); |
315 | DBG_THR(fprintf(stderr, "HNDSOCK init thread done\n" )); |
316 | |
317 | thd_proc_info(thd, &info_message_buf[0]); |
318 | set_thread_message("hs:listening" ); |
319 | DBG_THR(fprintf(stderr, "HNDSOCK x1 %p\n" , thd)); |
320 | |
321 | lex_start(thd); |
322 | |
323 | user_lock.reset(new expr_user_lock(thd, user_level_lock_timeout)); |
324 | } |
325 | |
326 | int |
327 | dbcontext::set_thread_message(const char *fmt, ...) |
328 | { |
329 | va_list ap; |
330 | va_start(ap, fmt); |
331 | const int n = vsnprintf(&info_message_buf[0], info_message_buf.size(), |
332 | fmt, ap); |
333 | va_end(ap); |
334 | return n; |
335 | } |
336 | |
337 | void |
338 | dbcontext::term_thread() |
339 | { |
340 | DBG_THR(fprintf(stderr, "HNDSOCK thread end %p\n" , thd)); |
341 | close_tables_if(); |
342 | my_pthread_setspecific_ptr(THR_THD, 0); |
343 | { |
344 | pthread_mutex_lock(&LOCK_thread_count); |
345 | delete thd; |
346 | thd = 0; |
347 | pthread_mutex_unlock(&LOCK_thread_count); |
348 | my_thread_end(); |
349 | } |
350 | } |
351 | |
352 | bool |
353 | dbcontext::check_alive() |
354 | { |
355 | pthread_mutex_lock(&thd->mysys_var->mutex); |
356 | killed_state st = thd->killed; |
357 | pthread_mutex_unlock(&thd->mysys_var->mutex); |
358 | DBG_SHUT(fprintf(stderr, "chk HNDSOCK kst %p %p %d %zu\n" , thd, &thd->killed, |
359 | (int)st, sizeof(*thd))); |
360 | if (st != NOT_KILLED) { |
361 | DBG_SHUT(fprintf(stderr, "chk HNDSOCK kst %d break\n" , (int)st)); |
362 | return false; |
363 | } |
364 | return true; |
365 | } |
366 | |
367 | void |
368 | dbcontext::lock_tables_if() |
369 | { |
370 | if (lock_failed) { |
371 | return; |
372 | } |
373 | if (for_write_flag && !user_level_lock_locked) { |
374 | if (user_lock->get_lock()) { |
375 | user_level_lock_locked = true; |
376 | } else { |
377 | lock_failed = true; |
378 | return; |
379 | } |
380 | } |
381 | if (lock == 0) { |
382 | const size_t num_max = table_vec.size(); |
383 | TABLE **const tables = DENA_ALLOCA_ALLOCATE(TABLE *, num_max + 1); |
384 | size_t num_open = 0; |
385 | for (size_t i = 0; i < num_max; ++i) { |
386 | if (table_vec[i].refcount > 0) { |
387 | tables[num_open++] = table_vec[i].table; |
388 | } |
389 | table_vec[i].modified = false; |
390 | } |
391 | #if MYSQL_VERSION_ID >= 50505 |
392 | lock = thd->lock = mysql_lock_tables(thd, &tables[0], num_open, 0); |
393 | #else |
394 | bool need_reopen= false; |
395 | lock = thd->lock = mysql_lock_tables(thd, &tables[0], num_open, |
396 | MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN, &need_reopen); |
397 | #endif |
398 | statistic_increment(lock_tables_count, &LOCK_status); |
399 | thd_proc_info(thd, &info_message_buf[0]); |
400 | DENA_VERBOSE(100, fprintf(stderr, "HNDSOCK lock tables %p %p %zu %zu\n" , |
401 | thd, lock, num_max, num_open)); |
402 | if (lock == 0) { |
403 | lock_failed = true; |
404 | DENA_VERBOSE(10, fprintf(stderr, "HNDSOCK failed to lock tables %p\n" , |
405 | thd)); |
406 | } |
407 | if (for_write_flag) { |
408 | #if MYSQL_VERSION_ID >= 50505 |
409 | thd->set_current_stmt_binlog_format_row(); |
410 | #else |
411 | thd->current_stmt_binlog_row_based = 1; |
412 | #endif |
413 | } |
414 | DENA_ALLOCA_FREE(tables); |
415 | } |
416 | DBG_LOCK(fprintf(stderr, "HNDSOCK tblnum=%d\n" , (int)tblnum)); |
417 | } |
418 | |
419 | void |
420 | dbcontext::unlock_tables_if() |
421 | { |
422 | if (lock != 0) { |
423 | DENA_VERBOSE(100, fprintf(stderr, "HNDSOCK unlock tables %p %p\n" , |
424 | thd, thd->lock)); |
425 | if (for_write_flag) { |
426 | for (size_t i = 0; i < table_vec.size(); ++i) { |
427 | if (table_vec[i].modified) { |
428 | query_cache_invalidate3(thd, table_vec[i].table, 1); |
429 | table_vec[i].table->file->ha_release_auto_increment(); |
430 | } |
431 | } |
432 | } |
433 | { |
434 | bool suc = true; |
435 | #if MYSQL_VERSION_ID >= 50505 |
436 | suc = (trans_commit_stmt(thd) == 0); |
437 | #else |
438 | suc = (ha_autocommit_or_rollback(thd, 0) == 0); |
439 | #endif |
440 | if (!suc) { |
441 | commit_error = true; |
442 | DENA_VERBOSE(10, fprintf(stderr, |
443 | "HNDSOCK unlock tables: commit failed\n" )); |
444 | } |
445 | } |
446 | mysql_unlock_tables(thd, lock); |
447 | lock = thd->lock = 0; |
448 | statistic_increment(unlock_tables_count, &LOCK_status); |
449 | } |
450 | if (user_level_lock_locked) { |
451 | if (user_lock->release_lock()) { |
452 | user_level_lock_locked = false; |
453 | } |
454 | } |
455 | } |
456 | |
457 | bool |
458 | dbcontext::get_commit_error() |
459 | { |
460 | return commit_error; |
461 | } |
462 | |
463 | void |
464 | dbcontext::clear_error() |
465 | { |
466 | lock_failed = false; |
467 | commit_error = false; |
468 | } |
469 | |
470 | void |
471 | dbcontext::close_tables_if() |
472 | { |
473 | unlock_tables_if(); |
474 | DENA_VERBOSE(100, fprintf(stderr, "HNDSOCK close tables\n" )); |
475 | close_thread_tables(thd); |
476 | #if MYSQL_VERSION_ID >= 50505 |
477 | thd->mdl_context.release_transactional_locks(); |
478 | #endif |
479 | if (!table_vec.empty()) { |
480 | statistic_increment(close_tables_count, &LOCK_status); |
481 | table_vec.clear(); |
482 | table_map.clear(); |
483 | } |
484 | } |
485 | |
486 | void |
487 | dbcontext::table_addref(size_t tbl_id) |
488 | { |
489 | table_vec[tbl_id].refcount += 1; |
490 | DBG_REFCNT(fprintf(stderr, "%p %zu %zu addref\n" , this, tbl_id, |
491 | table_vec[tbl_id].refcount)); |
492 | } |
493 | |
494 | void |
495 | dbcontext::table_release(size_t tbl_id) |
496 | { |
497 | table_vec[tbl_id].refcount -= 1; |
498 | DBG_REFCNT(fprintf(stderr, "%p %zu %zu release\n" , this, tbl_id, |
499 | table_vec[tbl_id].refcount)); |
500 | } |
501 | |
502 | void |
503 | dbcontext::resp_record(dbcallback_i& cb, TABLE *const table, |
504 | const prep_stmt& pst) |
505 | { |
506 | char rwpstr_buf[64]; |
507 | String rwpstr(rwpstr_buf, sizeof(rwpstr_buf), &my_charset_bin); |
508 | const prep_stmt::fields_type& rf = pst.get_ret_fields(); |
509 | const size_t n = rf.size(); |
510 | for (size_t i = 0; i < n; ++i) { |
511 | uint32_t fn = rf[i]; |
512 | Field *const fld = table->field[fn]; |
513 | DBG_FLD(fprintf(stderr, "fld=%p %zu\n" , fld, fn)); |
514 | if (fld->is_null()) { |
515 | /* null */ |
516 | cb.dbcb_resp_entry(0, 0); |
517 | } else { |
518 | fld->val_str(&rwpstr, &rwpstr); |
519 | const size_t len = rwpstr.length(); |
520 | if (len != 0) { |
521 | /* non-empty */ |
522 | cb.dbcb_resp_entry(rwpstr.ptr(), rwpstr.length()); |
523 | } else { |
524 | /* empty */ |
525 | static const char empty_str[] = "" ; |
526 | cb.dbcb_resp_entry(empty_str, 0); |
527 | } |
528 | } |
529 | } |
530 | } |
531 | |
532 | void |
533 | dbcontext::dump_record(dbcallback_i& cb, TABLE *const table, |
534 | const prep_stmt& pst) |
535 | { |
536 | char rwpstr_buf[64]; |
537 | String rwpstr(rwpstr_buf, sizeof(rwpstr_buf), &my_charset_bin); |
538 | const prep_stmt::fields_type& rf = pst.get_ret_fields(); |
539 | const size_t n = rf.size(); |
540 | for (size_t i = 0; i < n; ++i) { |
541 | uint32_t fn = rf[i]; |
542 | Field *const fld = table->field[fn]; |
543 | if (fld->is_null()) { |
544 | /* null */ |
545 | fprintf(stderr, "NULL" ); |
546 | } else { |
547 | fld->val_str(&rwpstr, &rwpstr); |
548 | const std::string s(rwpstr.ptr(), rwpstr.length()); |
549 | fprintf(stderr, "[%s]" , s.c_str()); |
550 | } |
551 | } |
552 | fprintf(stderr, "\n" ); |
553 | } |
554 | |
555 | int |
556 | dbcontext::modify_record(dbcallback_i& cb, TABLE *const table, |
557 | const prep_stmt& pst, const cmd_exec_args& args, char mod_op, |
558 | size_t& modified_count) |
559 | { |
560 | if (mod_op == 'U') { |
561 | /* update */ |
562 | handler *const hnd = table->file; |
563 | uchar *const buf = table->record[0]; |
564 | store_record(table, record[1]); |
565 | const prep_stmt::fields_type& rf = pst.get_ret_fields(); |
566 | const size_t n = rf.size(); |
567 | for (size_t i = 0; i < n; ++i) { |
568 | const string_ref& nv = args.uvals[i]; |
569 | uint32_t fn = rf[i]; |
570 | Field *const fld = table->field[fn]; |
571 | if (nv.begin() == 0) { |
572 | fld->set_null(); |
573 | } else { |
574 | fld->set_notnull(); |
575 | fld->store(nv.begin(), nv.size(), &my_charset_bin); |
576 | } |
577 | } |
578 | table_vec[pst.get_table_id()].modified = true; |
579 | const int r = hnd->ha_update_row(table->record[1], buf); |
580 | if (r != 0 && r != HA_ERR_RECORD_IS_THE_SAME) { |
581 | return r; |
582 | } |
583 | ++modified_count; /* TODO: HA_ERR_RECORD_IS_THE_SAME? */ |
584 | } else if (mod_op == 'D') { |
585 | /* delete */ |
586 | handler *const hnd = table->file; |
587 | table_vec[pst.get_table_id()].modified = true; |
588 | const int r = hnd->ha_delete_row(table->record[0]); |
589 | if (r != 0) { |
590 | return r; |
591 | } |
592 | ++modified_count; |
593 | } else if (mod_op == '+' || mod_op == '-') { |
594 | /* increment/decrement */ |
595 | handler *const hnd = table->file; |
596 | uchar *const buf = table->record[0]; |
597 | store_record(table, record[1]); |
598 | const prep_stmt::fields_type& rf = pst.get_ret_fields(); |
599 | const size_t n = rf.size(); |
600 | size_t i = 0; |
601 | for (i = 0; i < n; ++i) { |
602 | const string_ref& nv = args.uvals[i]; |
603 | uint32_t fn = rf[i]; |
604 | Field *const fld = table->field[fn]; |
605 | if (fld->is_null() || nv.begin() == 0) { |
606 | continue; |
607 | } |
608 | const long long pval = fld->val_int(); |
609 | const long long llv = atoll_nocheck(nv.begin(), nv.end()); |
610 | /* TODO: llv == 0? */ |
611 | long long nval = 0; |
612 | if (mod_op == '+') { |
613 | /* increment */ |
614 | nval = pval + llv; |
615 | } else { |
616 | /* decrement */ |
617 | nval = pval - llv; |
618 | if ((pval < 0 && nval > 0) || (pval > 0 && nval < 0)) { |
619 | break; /* don't modify */ |
620 | } |
621 | } |
622 | fld->store(nval, false); |
623 | } |
624 | if (i == n) { |
625 | /* modify */ |
626 | table_vec[pst.get_table_id()].modified = true; |
627 | const int r = hnd->ha_update_row(table->record[1], buf); |
628 | if (r != 0 && r != HA_ERR_RECORD_IS_THE_SAME) { |
629 | return r; |
630 | } |
631 | ++modified_count; |
632 | } |
633 | } |
634 | return 0; |
635 | } |
636 | |
637 | void |
638 | dbcontext::cmd_insert_internal(dbcallback_i& cb, const prep_stmt& pst, |
639 | const string_ref *fvals, size_t fvalslen) |
640 | { |
641 | if (!for_write_flag) { |
642 | return cb.dbcb_resp_short(2, "readonly" ); |
643 | } |
644 | lock_tables_if(); |
645 | if (lock == 0) { |
646 | return cb.dbcb_resp_short(1, "lock_tables" ); |
647 | } |
648 | if (pst.get_table_id() >= table_vec.size()) { |
649 | return cb.dbcb_resp_short(2, "tblnum" ); |
650 | } |
651 | TABLE *const table = table_vec[pst.get_table_id()].table; |
652 | handler *const hnd = table->file; |
653 | uchar *const buf = table->record[0]; |
654 | empty_record(table); |
655 | memset(buf, 0, table->s->null_bytes); /* clear null flags */ |
656 | const prep_stmt::fields_type& rf = pst.get_ret_fields(); |
657 | const size_t n = std::min(rf.size(), fvalslen); |
658 | for (size_t i = 0; i < n; ++i) { |
659 | uint32_t fn = rf[i]; |
660 | Field *const fld = table->field[fn]; |
661 | if (fvals[i].begin() == 0) { |
662 | fld->set_null(); |
663 | } else { |
664 | fld->store(fvals[i].begin(), fvals[i].size(), &my_charset_bin); |
665 | } |
666 | } |
667 | table->next_number_field = table->found_next_number_field; |
668 | /* FIXME: test */ |
669 | const int r = hnd->ha_write_row(buf); |
670 | const ulonglong insert_id = table->file->insert_id_for_cur_row; |
671 | table->next_number_field = 0; |
672 | table_vec[pst.get_table_id()].modified = true; |
673 | if (r == 0 && table->found_next_number_field != 0) { |
674 | return cb.dbcb_resp_short_num64(0, insert_id); |
675 | } |
676 | if (r != 0) { |
677 | return cb.dbcb_resp_short_num(1, r); |
678 | } |
679 | return cb.dbcb_resp_short(0, "" ); |
680 | } |
681 | |
682 | void |
683 | dbcontext::cmd_sql_internal(dbcallback_i& cb, const prep_stmt& pst, |
684 | const string_ref *fvals, size_t fvalslen) |
685 | { |
686 | if (fvalslen < 1) { |
687 | return cb.dbcb_resp_short(2, "syntax" ); |
688 | } |
689 | return cb.dbcb_resp_short(2, "notimpl" ); |
690 | } |
691 | |
692 | static size_t |
693 | prepare_keybuf(const cmd_exec_args& args, uchar *key_buf, TABLE *table, |
694 | KEY& kinfo, size_t invalues_index) |
695 | { |
696 | size_t kplen_sum = 0; |
697 | DBG_KEY(fprintf(stderr, "SLOW\n" )); |
698 | for (size_t i = 0; i < args.kvalslen; ++i) { |
699 | const KEY_PART_INFO & kpt = kinfo.key_part[i]; |
700 | string_ref kval = args.kvals[i]; |
701 | if (args.invalues_keypart >= 0 && |
702 | static_cast<size_t>(args.invalues_keypart) == i) { |
703 | kval = args.invalues[invalues_index]; |
704 | } |
705 | if (kval.begin() == 0) { |
706 | kpt.field->set_null(); |
707 | } else { |
708 | kpt.field->set_notnull(); |
709 | } |
710 | kpt.field->store(kval.begin(), kval.size(), &my_charset_bin); |
711 | kplen_sum += kpt.store_length; |
712 | DBG_KEYLEN(fprintf(stderr, "l=%u sl=%zu\n" , kpt.length, |
713 | kpt.store_length)); |
714 | } |
715 | key_copy(key_buf, table->record[0], &kinfo, kplen_sum); |
716 | DBG_KEYLEN(fprintf(stderr, "sum=%zu flen=%u\n" , kplen_sum, |
717 | kinfo.key_length)); |
718 | return kplen_sum; |
719 | } |
720 | |
721 | void |
722 | dbcontext::cmd_find_internal(dbcallback_i& cb, const prep_stmt& pst, |
723 | ha_rkey_function find_flag, const cmd_exec_args& args) |
724 | { |
725 | const bool debug_out = (verbose_level >= 100); |
726 | bool need_resp_record = true; |
727 | char mod_op = 0; |
728 | const string_ref& mod_op_str = args.mod_op; |
729 | if (mod_op_str.size() != 0) { |
730 | if (!for_write_flag) { |
731 | return cb.dbcb_resp_short(2, "readonly" ); |
732 | } |
733 | mod_op = mod_op_str.begin()[0]; |
734 | need_resp_record = mod_op_str.size() > 1 && mod_op_str.begin()[1] == '?'; |
735 | switch (mod_op) { |
736 | case 'U': /* update */ |
737 | case 'D': /* delete */ |
738 | case '+': /* increment */ |
739 | case '-': /* decrement */ |
740 | break; |
741 | default: |
742 | if (debug_out) { |
743 | fprintf(stderr, "unknown modop: %c\n" , mod_op); |
744 | } |
745 | return cb.dbcb_resp_short(2, "modop" ); |
746 | } |
747 | } |
748 | lock_tables_if(); |
749 | if (lock == 0) { |
750 | return cb.dbcb_resp_short(1, "lock_tables" ); |
751 | } |
752 | if (pst.get_table_id() >= table_vec.size()) { |
753 | return cb.dbcb_resp_short(2, "tblnum" ); |
754 | } |
755 | TABLE *const table = table_vec[pst.get_table_id()].table; |
756 | /* keys */ |
757 | if (pst.get_idxnum() >= table->s->keys) { |
758 | return cb.dbcb_resp_short(2, "idxnum" ); |
759 | } |
760 | KEY& kinfo = table->key_info[pst.get_idxnum()]; |
761 | if (args.kvalslen > kinfo.user_defined_key_parts) { |
762 | return cb.dbcb_resp_short(2, "kpnum" ); |
763 | } |
764 | uchar *const key_buf = DENA_ALLOCA_ALLOCATE(uchar, kinfo.key_length); |
765 | size_t invalues_idx = 0; |
766 | size_t kplen_sum = prepare_keybuf(args, key_buf, table, kinfo, invalues_idx); |
767 | /* filters */ |
768 | uchar *filter_buf = 0; |
769 | if (args.filters != 0) { |
770 | const size_t filter_buf_len = calc_filter_buf_size(table, pst, |
771 | args.filters); |
772 | filter_buf = DENA_ALLOCA_ALLOCATE(uchar, filter_buf_len); |
773 | if (!fill_filter_buf(table, pst, args.filters, filter_buf, |
774 | filter_buf_len)) { |
775 | return cb.dbcb_resp_short(2, "filterblob" ); |
776 | } |
777 | } |
778 | /* handler */ |
779 | table->read_set = &table->s->all_set; |
780 | handler *const hnd = table->file; |
781 | if (!for_write_flag) { |
782 | hnd->init_table_handle_for_HANDLER(); |
783 | } |
784 | hnd->ha_index_or_rnd_end(); |
785 | hnd->ha_index_init(pst.get_idxnum(), 1); |
786 | if (need_resp_record) { |
787 | cb.dbcb_resp_begin(pst.get_ret_fields().size()); |
788 | } |
789 | const uint32_t limit = args.limit ? args.limit : 1; |
790 | uint32_t skip = args.skip; |
791 | size_t modified_count = 0; |
792 | int r = 0; |
793 | bool is_first = true; |
794 | for (uint32_t cnt = 0; cnt < limit + skip;) { |
795 | if (is_first) { |
796 | is_first = false; |
797 | const key_part_map kpm = (1U << args.kvalslen) - 1; |
798 | r = hnd->ha_index_read_map(table->record[0], key_buf, kpm, find_flag); |
799 | } else if (args.invalues_keypart >= 0) { |
800 | if (++invalues_idx >= args.invalueslen) { |
801 | break; |
802 | } |
803 | kplen_sum = prepare_keybuf(args, key_buf, table, kinfo, invalues_idx); |
804 | const key_part_map kpm = (1U << args.kvalslen) - 1; |
805 | r = hnd->ha_index_read_map(table->record[0], key_buf, kpm, find_flag); |
806 | } else { |
807 | switch (find_flag) { |
808 | case HA_READ_BEFORE_KEY: |
809 | case HA_READ_KEY_OR_PREV: |
810 | r = hnd->ha_index_prev(table->record[0]); |
811 | break; |
812 | case HA_READ_AFTER_KEY: |
813 | case HA_READ_KEY_OR_NEXT: |
814 | r = hnd->ha_index_next(table->record[0]); |
815 | break; |
816 | case HA_READ_KEY_EXACT: |
817 | r = hnd->ha_index_next_same(table->record[0], key_buf, kplen_sum); |
818 | break; |
819 | default: |
820 | r = HA_ERR_END_OF_FILE; /* to finish the loop */ |
821 | break; |
822 | } |
823 | } |
824 | if (debug_out) { |
825 | fprintf(stderr, "r=%d\n" , r); |
826 | if (r == 0 || r == HA_ERR_RECORD_DELETED) { |
827 | dump_record(cb, table, pst); |
828 | } |
829 | } |
830 | int filter_res = 0; |
831 | if (r != 0) { |
832 | /* no-count */ |
833 | } else if (args.filters != 0 && (filter_res = check_filter(cb, table, |
834 | pst, args.filters, filter_buf)) != 0) { |
835 | if (filter_res < 0) { |
836 | break; |
837 | } |
838 | } else if (skip > 0) { |
839 | --skip; |
840 | } else { |
841 | /* hit */ |
842 | if (need_resp_record) { |
843 | resp_record(cb, table, pst); |
844 | } |
845 | if (mod_op != 0) { |
846 | r = modify_record(cb, table, pst, args, mod_op, modified_count); |
847 | } |
848 | ++cnt; |
849 | } |
850 | if (args.invalues_keypart >= 0 && r == HA_ERR_KEY_NOT_FOUND) { |
851 | continue; |
852 | } |
853 | if (r != 0 && r != HA_ERR_RECORD_DELETED) { |
854 | break; |
855 | } |
856 | } |
857 | hnd->ha_index_or_rnd_end(); |
858 | if (r != 0 && r != HA_ERR_RECORD_DELETED && r != HA_ERR_KEY_NOT_FOUND && |
859 | r != HA_ERR_END_OF_FILE) { |
860 | /* failed */ |
861 | if (need_resp_record) { |
862 | /* revert dbcb_resp_begin() and dbcb_resp_entry() */ |
863 | cb.dbcb_resp_cancel(); |
864 | } |
865 | cb.dbcb_resp_short_num(1, r); |
866 | } else { |
867 | /* succeeded */ |
868 | if (need_resp_record) { |
869 | cb.dbcb_resp_end(); |
870 | } else { |
871 | cb.dbcb_resp_short_num(0, modified_count); |
872 | } |
873 | } |
874 | DENA_ALLOCA_FREE(filter_buf); |
875 | DENA_ALLOCA_FREE(key_buf); |
876 | } |
877 | |
878 | size_t |
879 | dbcontext::calc_filter_buf_size(TABLE *table, const prep_stmt& pst, |
880 | const record_filter *filters) |
881 | { |
882 | size_t filter_buf_len = 0; |
883 | for (const record_filter *f = filters; f->op.begin() != 0; ++f) { |
884 | if (f->val.begin() == 0) { |
885 | continue; |
886 | } |
887 | const uint32_t fn = pst.get_filter_fields()[f->ff_offset]; |
888 | filter_buf_len += table->field[fn]->pack_length(); |
889 | } |
890 | ++filter_buf_len; |
891 | /* Field_medium::cmp() calls uint3korr(), which may read 4 bytes. |
892 | Allocate 1 more byte for safety. */ |
893 | return filter_buf_len; |
894 | } |
895 | |
896 | bool |
897 | dbcontext::fill_filter_buf(TABLE *table, const prep_stmt& pst, |
898 | const record_filter *filters, uchar *filter_buf, size_t len) |
899 | { |
900 | memset(filter_buf, 0, len); |
901 | size_t pos = 0; |
902 | for (const record_filter *f = filters; f->op.begin() != 0; ++f) { |
903 | if (f->val.begin() == 0) { |
904 | continue; |
905 | } |
906 | const uint32_t fn = pst.get_filter_fields()[f->ff_offset]; |
907 | Field *const fld = table->field[fn]; |
908 | if ((fld->flags & BLOB_FLAG) != 0) { |
909 | return false; |
910 | } |
911 | fld->store(f->val.begin(), f->val.size(), &my_charset_bin); |
912 | const size_t packlen = fld->pack_length(); |
913 | memcpy(filter_buf + pos, fld->ptr, packlen); |
914 | pos += packlen; |
915 | } |
916 | return true; |
917 | } |
918 | |
919 | int |
920 | dbcontext::check_filter(dbcallback_i& cb, TABLE *table, const prep_stmt& pst, |
921 | const record_filter *filters, const uchar *filter_buf) |
922 | { |
923 | DBG_FILTER(fprintf(stderr, "check_filter\n" )); |
924 | size_t pos = 0; |
925 | for (const record_filter *f = filters; f->op.begin() != 0; ++f) { |
926 | const string_ref& op = f->op; |
927 | const string_ref& val = f->val; |
928 | const uint32_t fn = pst.get_filter_fields()[f->ff_offset]; |
929 | Field *const fld = table->field[fn]; |
930 | const size_t packlen = fld->pack_length(); |
931 | const uchar *const bval = filter_buf + pos; |
932 | int cv = 0; |
933 | if (fld->is_null()) { |
934 | cv = (val.begin() == 0) ? 0 : -1; |
935 | } else { |
936 | cv = (val.begin() == 0) ? 1 : fld->cmp(bval); |
937 | } |
938 | DBG_FILTER(fprintf(stderr, "check_filter cv=%d\n" , cv)); |
939 | bool cond = true; |
940 | if (op.size() == 1) { |
941 | switch (op.begin()[0]) { |
942 | case '>': |
943 | DBG_FILTER(fprintf(stderr, "check_filter op: >\n" )); |
944 | cond = (cv > 0); |
945 | break; |
946 | case '<': |
947 | DBG_FILTER(fprintf(stderr, "check_filter op: <\n" )); |
948 | cond = (cv < 0); |
949 | break; |
950 | case '=': |
951 | DBG_FILTER(fprintf(stderr, "check_filter op: =\n" )); |
952 | cond = (cv == 0); |
953 | break; |
954 | default: |
955 | DBG_FILTER(fprintf(stderr, "check_filter op: unknown\n" )); |
956 | cond = false; /* FIXME: error */ |
957 | break; |
958 | } |
959 | } else if (op.size() == 2 && op.begin()[1] == '=') { |
960 | switch (op.begin()[0]) { |
961 | case '>': |
962 | DBG_FILTER(fprintf(stderr, "check_filter op: >=\n" )); |
963 | cond = (cv >= 0); |
964 | break; |
965 | case '<': |
966 | DBG_FILTER(fprintf(stderr, "check_filter op: <=\n" )); |
967 | cond = (cv <= 0); |
968 | break; |
969 | case '!': |
970 | DBG_FILTER(fprintf(stderr, "check_filter op: !=\n" )); |
971 | cond = (cv != 0); |
972 | break; |
973 | default: |
974 | DBG_FILTER(fprintf(stderr, "check_filter op: unknown\n" )); |
975 | cond = false; /* FIXME: error */ |
976 | break; |
977 | } |
978 | } |
979 | DBG_FILTER(fprintf(stderr, "check_filter cond: %d\n" , (int)cond)); |
980 | if (!cond) { |
981 | return (f->filter_type == record_filter_type_skip) ? 1 : -1; |
982 | } |
983 | if (val.begin() != 0) { |
984 | pos += packlen; |
985 | } |
986 | } |
987 | return 0; |
988 | } |
989 | |
990 | void |
991 | dbcontext::cmd_open(dbcallback_i& cb, const cmd_open_args& arg) |
992 | { |
993 | unlock_tables_if(); |
994 | const table_name_type k = std::make_pair(std::string(arg.dbn), |
995 | std::string(arg.tbl)); |
996 | const table_map_type::const_iterator iter = table_map.find(k); |
997 | uint32_t tblnum = 0; |
998 | if (iter != table_map.end()) { |
999 | tblnum = iter->second; |
1000 | DBG_CMP(fprintf(stderr, "HNDSOCK k=%s tblnum=%d\n" , k.c_str(), |
1001 | (int)tblnum)); |
1002 | } else { |
1003 | TABLE_LIST tables; |
1004 | TABLE *table = 0; |
1005 | bool refresh = true; |
1006 | const thr_lock_type lock_type = for_write_flag ? TL_WRITE : TL_READ; |
1007 | #if MYSQL_VERSION_ID >= 50505 |
1008 | LEX_CSTRING db_name= { arg.dbn, strlen(arg.dbn) }; |
1009 | LEX_CSTRING tbl_name= { arg.tbl, strlen(arg.tbl) }; |
1010 | tables.init_one_table(&db_name, &tbl_name, 0, lock_type); |
1011 | tables.mdl_request.init(MDL_key::TABLE, arg.dbn, arg.tbl, |
1012 | for_write_flag ? MDL_SHARED_WRITE : MDL_SHARED_READ, MDL_TRANSACTION); |
1013 | Open_table_context ot_act(thd, 0); |
1014 | if (!open_table(thd, &tables, &ot_act)) { |
1015 | table = tables.table; |
1016 | } |
1017 | #else |
1018 | tables.init_one_table(arg.dbn, arg.tbl, lock_type); |
1019 | table = open_table(thd, &tables, thd->mem_root, &refresh, |
1020 | OPEN_VIEW_NO_PARSE); |
1021 | #endif |
1022 | if (table == 0) { |
1023 | DENA_VERBOSE(20, fprintf(stderr, |
1024 | "HNDSOCK failed to open %p [%s] [%s] [%d]\n" , |
1025 | thd, arg.dbn, arg.tbl, static_cast<int>(refresh))); |
1026 | return cb.dbcb_resp_short(1, "open_table" ); |
1027 | } |
1028 | statistic_increment(open_tables_count, &LOCK_status); |
1029 | table->reginfo.lock_type = lock_type; |
1030 | table->use_all_columns(); |
1031 | tblnum = table_vec.size(); |
1032 | tablevec_entry e; |
1033 | e.table = table; |
1034 | table_vec.push_back(e); |
1035 | table_map[k] = tblnum; |
1036 | } |
1037 | size_t idxnum = static_cast<size_t>(-1); |
1038 | if (arg.idx[0] >= '0' && arg.idx[0] <= '9') { |
1039 | /* numeric */ |
1040 | TABLE *const table = table_vec[tblnum].table; |
1041 | idxnum = atoi(arg.idx); |
1042 | if (idxnum >= table->s->keys) { |
1043 | return cb.dbcb_resp_short(2, "idxnum" ); |
1044 | } |
1045 | } else { |
1046 | const char *const idx_name_to_open = |
1047 | arg.idx[0] == '\0' ? "PRIMARY" : arg.idx; |
1048 | TABLE *const table = table_vec[tblnum].table; |
1049 | for (uint i = 0; i < table->s->keys; ++i) { |
1050 | KEY& kinfo = table->key_info[i]; |
1051 | if (strcmp(kinfo.name.str, idx_name_to_open) == 0) { |
1052 | idxnum = i; |
1053 | break; |
1054 | } |
1055 | } |
1056 | } |
1057 | if (idxnum == size_t(-1)) { |
1058 | return cb.dbcb_resp_short(2, "idxnum" ); |
1059 | } |
1060 | prep_stmt::fields_type rf; |
1061 | prep_stmt::fields_type ff; |
1062 | if (!parse_fields(table_vec[tblnum].table, arg.retflds, rf)) { |
1063 | return cb.dbcb_resp_short(2, "fld" ); |
1064 | } |
1065 | if (!parse_fields(table_vec[tblnum].table, arg.filflds, ff)) { |
1066 | return cb.dbcb_resp_short(2, "fld" ); |
1067 | } |
1068 | prep_stmt p(this, tblnum, idxnum, rf, ff); |
1069 | cb.dbcb_set_prep_stmt(arg.pst_id, p); |
1070 | return cb.dbcb_resp_short(0, "" ); |
1071 | } |
1072 | |
1073 | bool |
1074 | dbcontext::parse_fields(TABLE *const table, const char *str, |
1075 | prep_stmt::fields_type& flds) |
1076 | { |
1077 | string_ref flds_sr(str, strlen(str)); |
1078 | std::vector<string_ref> fldnms; |
1079 | if (flds_sr.size() != 0) { |
1080 | split(',', flds_sr, fldnms); |
1081 | } |
1082 | for (size_t i = 0; i < fldnms.size(); ++i) { |
1083 | Field **fld = 0; |
1084 | size_t j = 0; |
1085 | for (fld = table->field; *fld; ++fld, ++j) { |
1086 | DBG_FLD(fprintf(stderr, "f %s\n" , (*fld)->field_name.str)); |
1087 | string_ref fn((*fld)->field_name.str, (*fld)->field_name.length); |
1088 | if (fn == fldnms[i]) { |
1089 | break; |
1090 | } |
1091 | } |
1092 | if (*fld == 0) { |
1093 | DBG_FLD(fprintf(stderr, "UNKNOWN FLD %s [%s]\n" , retflds, |
1094 | std::string(fldnms[i].begin(), fldnms[i].size()).c_str())); |
1095 | return false; |
1096 | } |
1097 | DBG_FLD(fprintf(stderr, "FLD %s %zu\n" , (*fld)->field_name.str, j)); |
1098 | flds.push_back(j); |
1099 | } |
1100 | return true; |
1101 | } |
1102 | |
1103 | enum db_write_op { |
1104 | db_write_op_none = 0, |
1105 | db_write_op_insert = 1, |
1106 | db_write_op_sql = 2, |
1107 | }; |
1108 | |
1109 | void |
1110 | dbcontext::cmd_exec(dbcallback_i& cb, const cmd_exec_args& args) |
1111 | { |
1112 | const prep_stmt& p = *args.pst; |
1113 | if (p.get_table_id() == static_cast<size_t>(-1)) { |
1114 | return cb.dbcb_resp_short(2, "stmtnum" ); |
1115 | } |
1116 | ha_rkey_function find_flag = HA_READ_KEY_EXACT; |
1117 | db_write_op wrop = db_write_op_none; |
1118 | if (args.op.size() == 1) { |
1119 | switch (args.op.begin()[0]) { |
1120 | case '=': |
1121 | find_flag = HA_READ_KEY_EXACT; |
1122 | break; |
1123 | case '>': |
1124 | find_flag = HA_READ_AFTER_KEY; |
1125 | break; |
1126 | case '<': |
1127 | find_flag = HA_READ_BEFORE_KEY; |
1128 | break; |
1129 | case '+': |
1130 | wrop = db_write_op_insert; |
1131 | break; |
1132 | case 'S': |
1133 | wrop = db_write_op_sql; |
1134 | break; |
1135 | default: |
1136 | return cb.dbcb_resp_short(2, "op" ); |
1137 | } |
1138 | } else if (args.op.size() == 2 && args.op.begin()[1] == '=') { |
1139 | switch (args.op.begin()[0]) { |
1140 | case '>': |
1141 | find_flag = HA_READ_KEY_OR_NEXT; |
1142 | break; |
1143 | case '<': |
1144 | find_flag = HA_READ_KEY_OR_PREV; |
1145 | break; |
1146 | default: |
1147 | return cb.dbcb_resp_short(2, "op" ); |
1148 | } |
1149 | } else { |
1150 | return cb.dbcb_resp_short(2, "op" ); |
1151 | } |
1152 | if (args.kvalslen <= 0) { |
1153 | return cb.dbcb_resp_short(2, "klen" ); |
1154 | } |
1155 | switch (wrop) { |
1156 | case db_write_op_none: |
1157 | return cmd_find_internal(cb, p, find_flag, args); |
1158 | case db_write_op_insert: |
1159 | return cmd_insert_internal(cb, p, args.kvals, args.kvalslen); |
1160 | case db_write_op_sql: |
1161 | return cmd_sql_internal(cb, p, args.kvals, args.kvalslen); |
1162 | } |
1163 | } |
1164 | |
1165 | void |
1166 | dbcontext::set_statistics(size_t num_conns, size_t num_active) |
1167 | { |
1168 | if (for_write_flag) { |
1169 | set_thread_message("handlersocket: mode=wr, %zu conns, %zu active" , |
1170 | num_conns, num_active); |
1171 | } else { |
1172 | set_thread_message("handlersocket: mode=rd, %zu conns, %zu active" , |
1173 | num_conns, num_active); |
1174 | } |
1175 | /* |
1176 | Don't set message buf if it's already in use. This saves slow call to |
1177 | thd_proc_info() (if profiling is enabled) |
1178 | */ |
1179 | if (thd->proc_info != &info_message_buf[0]) |
1180 | thd_proc_info(thd, &info_message_buf[0]); |
1181 | } |
1182 | |
1183 | }; |
1184 | |
1185 | |