-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrs_handler.cpp
More file actions
329 lines (305 loc) · 8.21 KB
/
Copy paththrs_handler.cpp
File metadata and controls
329 lines (305 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <stdio.h>
#include <stdlib.h>
#include "mysql_inc.h"
#include "common.h"
#include "clist.h"
#include "cstr.h"
#include "spinlock.h"
#include "log.h"
#include "dict.h"
#include "thrs_handler.h"
#include "jmalloc.h"
#include "thr_socket_svr.h"
static THD* thd_create(char* db, const void *stack_bottom,
bool writeable) {
THD *thd = NULL;
my_thread_init();
thd = new THD();
if (thd == NULL) {
my_thread_end();
return NULL;
}
thd->thread_stack = (char*) stack_bottom;
DEBUG("THRS:thread_stack = %p sizeof(THD)=%zu sizeof(mtx)=%zu \n",
thd->thread_stack, sizeof(THD), sizeof(LOCK_thread_count));
thd->store_globals();
thd->system_thread = static_cast<enum_thread_type>(1 << 30UL);
const NET v = { 0 };
thd->net = v;
if (writeable) {
//for write
#if MYSQL_VERSION_ID >= 50505
thd->variables.option_bits |= OPTION_BIN_LOG;
#else
thd->options |= OPTION_BIN_LOG;
#endif
}
//for db
safeFree(thd->db);
thd->db = db;
my_pthread_setspecific_ptr(THR_THD, thd);
return thd;
}
static void thd_destroy(THD *thd) {
my_pthread_setspecific_ptr(THR_THD, 0);
delete thd;
--thread_count;
my_thread_end();
}
static void opentabs_key_destroy(void *c) {
cstr s = (cstr)c;
cstr_destroy(s);
}
static unsigned int hash(const void *key) {
cstr cs = (cstr)key;
return dict_generic_hash(cs, cstr_used(cs));
}
static int opentabs_key_compare(const void *k1, const void *k2) {
size_t len;
cstr s1 = (cstr)k1;
cstr s2 = (cstr)k2;
len = cstr_used(s1);
if(len != cstr_used(s2))
return 0;
return memcmp(k1, k2, len) == 0;
}
static void* opentabs_key_dup(const void *k) {
return cstr_dup((cstr)k);
}
dict_opts opentabs_opts = {
hash,
opentabs_key_dup,
NULL,
opentabs_key_compare,
opentabs_key_destroy,
NULL,
};
void* db_ctx_init(void *p) {
db_ctx *ctx = (db_ctx*)jmalloc(sizeof(db_ctx));
ctx->opentabs = dict_create(&opentabs_opts);
ctx->thd = thd_create(my_strdup("TDR_SOCKET",MYF(0)), p, 1);
return ctx;
}
void* db_ctx_uninit(void *p) {
dict_entry *entry = NULL;
db_ctx *ctx = (db_ctx *)p;
thd_destroy(ctx->thd);
dict_destroy(ctx->opentabs);
return NULL;
}
void thrs_close_table(db_ctx *ctx) {
close_thread_tables(ctx->thd);
#if MYSQL_VERSION_ID >= 50505
ctx->thd->mdl_context.release_transactional_locks();
#endif
dict_clear(ctx->opentabs);
}
#define min(a, b) (a<b?a:b)
TABLE* thrs_open_table(THD * thd, cstr req_db, cstr req_table, const int writeable) {
int refresh = 1;
TABLE_LIST tables;
TABLE *table = NULL;
#if MYSQL_VERSION_ID >= 50505
tables.init_one_table(req_db, cstr_used(req_db),
req_table, cstr_used(req_table),
req_table, writeable ? TL_WRITE : TL_READ);
tables.mdl_request.init(MDL_key::TABLE, req_db, req_table,
writeable ? MDL_SHARED_WRITE : MDL_SHARED_READ, MDL_TRANSACTION);
Open_table_context ot_act(thd, 0);
if (!open_table(thd, &tables, thd->mem_root, &ot_act)) {
table = tables.table;
}
#else
tables.init_one_table(req_db, req_table, writeable ? TL_WRITE : TL_READ);
table = open_table(thd, &tables, thd->mem_root, &refresh, OPEN_VIEW_NO_PARSE);
#endif
if (table == NULL) {
ERROR("can't open table, db:%s table%s\n", req_db, req_table);
return NULL;
}
table->reginfo.lock_type = writeable ? TL_WRITE : TL_READ;
table->use_all_columns();
return table;
}
int thrs_parse_fields(TABLE *table, cstr fieldstr, int *result, int len) {
cstr *fields = NULL;
size_t field_num;
size_t i;
int rs = 0;
fields = cstr_split((char*)fieldstr, cstr_used(fieldstr), ",", 1, &field_num);
if(fields == NULL ) {
TRACE("split fields error\n");
return -1;
}
if(len <= field_num) {
TRACE("so many fields\n");
rs = -1;
goto end;
}
for (i = 0; i < field_num; i++) {
Field **fld = 0;
size_t j = 0;
for (fld = table->field; *fld; fld++, j++) {
if (memcmp((*fld)->field_name, fields[i], cstr_used(fields[i])) == 0) {
break;
}
}
if (*fld == 0) {
TRACE("UNKNOWN FLD %s\n", fields[i]);
rs = -1;
goto end;
}
result[i] = j;
}
rs = field_num;
end:
for(i = 0; i < field_num; i++) {
cstr_destroy(fields[i]);
}
jfree(fields);
return rs;
}
unsigned long long thrs_insert_inner(TABLE *table, cstr *fields, size_t fieldnum, int *seq, int seqlen) {
size_t i;
handler *const hnd = table->file;
uchar *const buf = table->record[0];
empty_record(table);
memset(buf, 0, table->s->null_bytes); /* clear null flags */
TRACE("fieldnum:%d seqlen:%d\n", fieldnum, seqlen);
const size_t n = min(fieldnum, seqlen);
for (size_t i = 0; i < n; ++i) {
uint32_t fn = seq[i];
Field *const fld = table->field[fn];
TRACE("field %s:%s\n",fld->field_name, fields[i]);
if (fields[i] == NULL) {
fld->set_null();
} else {
fld->store(fields[i], cstr_used(fields[i]), &my_charset_bin);
}
}
table->next_number_field = table->found_next_number_field;
const int r = hnd->ha_write_row(buf);
const ulonglong insert_id = table->file->insert_id_for_cur_row;
TRACE("ha_write_now result:%d table->next_number_field:%llu\n", r, insert_id);
table->next_number_field = 0;
if (r == 0 && table->found_next_number_field != 0) {
return insert_id;
}
if (r != 0) {
return 1;
}
return 0;
}
static MYSQL_LOCK *_thrs_lock_tables(THD *thd, TABLE** tables, int count, int writeable) {
MYSQL_LOCK *lock;
if (!writeable) {
thd->lex->sql_command = SQLCOM_SELECT;
}
#if MYSQL_VERSION_ID >= 50505
lock = thd->lock = mysql_lock_tables(thd, tables, count, 0);
#else
bool need_reopen = false;
lock = thd->lock = mysql_lock_tables(thd, tables, count,
MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN, &need_reopen);
#endif
if (lock == NULL) {
ERROR( "lock table failed! thd [%p]\n", thd);
return NULL;
}
if (writeable) {
#if MYSQL_VERSION_ID >= 50505
thd->set_current_stmt_binlog_format_row();
#else
thd->current_stmt_binlog_row_based = 1;
#endif
}
DEBUG("tdhs_lock_table! table count[%d]\n", count);
return lock;
}
MYSQL_LOCK *thrs_lock_tables(db_ctx *ctx, int writeable) {
size_t i;
MYSQL_LOCK *lock;
size_t tabnum = DICT_USED(ctx->opentabs);
TABLE **tables = (TABLE **)jmalloc(tabnum*sizeof(TABLE*));
dict_iterator *iter = dict_get_iterator(ctx->opentabs);
dict_entry *entry;
i = 0;
while((entry = dict_iterator_next(iter)) != NULL) {
tables[i] = (TABLE*)entry->value;
}
dict_iterator_destroy(iter);
lock = _thrs_lock_tables(ctx->thd, tables, tabnum, writeable);
jfree(tables);
return lock;
}
int thrs_unlock_table(db_ctx *ctx, MYSQL_LOCK* lock, int writeable, int rollback) {
int rs = 0;
THD *thd = ctx->thd;
DEBUG("thrs_unlock_table! lock [%p]\n", *lock);
if (lock != NULL) {
if (writeable && DICT_USED(ctx->opentabs) > 0) {
TABLE* tab;
dict_iterator *iter = dict_get_iterator(ctx->opentabs);
dict_entry *entry;
while((entry = dict_iterator_next(iter)) != NULL) {
tab = (TABLE*)entry->value;
query_cache_invalidate3(thd, tab, 1);
tab->file->ha_release_auto_increment();
}
dict_iterator_destroy(iter);
}
{
bool suc = true;
#if MYSQL_VERSION_ID >= 50505
if(rollback) {
suc = trans_rollback_stmt(thd);
} else {
suc = (trans_commit_stmt(thd) == FALSE);
}
#else
suc = (ha_autocommit_or_rollback(thd, rollback) == 0);
#endif
if (!suc) {
WARN("commit failed, it's rollback! thd [%p] write_error [%d] \n", thd, rollback);
rs = -1;
}
}
mysql_unlock_tables(thd, lock);
thd->lock = NULL;
}
return rs;
}
int thrs_open_index(TABLE *table, cstr idx) {
const char *ptr = idx;
long long idxnum;
if(ptr[0] >= '0' && ptr[0] <= '9') {
if(str2ll(idx, cstr_used(idx), &idxnum) < 0) return -1;
return idxnum;
}
ptr[0] == '\0'? ptr = "PRIMARY": ptr = idx;
for (uint i = 0; i < table->s->keys; ++i) {
KEY& kinfo = table->key_info[i];
if (strncmp(kinfo.name, ptr, cstr_used(idx)) == 0) {
return idxnum;
}
}
return -1;
}
size_t prepare_keybuf(cstr* keys, const uint32_t key_num, uchar *key_buf, TABLE *table, KEY& kinfo) {
size_t key_len_sum = 0;
for (size_t i = 0; i < key_num; i++) {
const KEY_PART_INFO & kpt = kinfo.key_part[i];
const cstr key = keys[i];
if (cstr_used(key) == 0) {
kpt.field->set_null();
} else {
kpt.field->set_notnull();
kpt.field->store(key, cstr_used(key), &my_charset_bin);
}
key_len_sum += kpt.store_length;
TRACE("key len=%u store len=%zu\n", kpt.length, kpt.store_length);
}
key_copy(key_buf, table->record[0], &kinfo, key_len_sum);
TRACE("keys sum=%zu flen=%u\n", key_len_sum, kinfo.key_length);
return key_len_sum;
}