Skip to content

Commit 7924a9f

Browse files
committed
use offsetof replace switch to memcpy to column value
1 parent a489bfd commit 7924a9f

File tree

4 files changed

+89
-101
lines changed

4 files changed

+89
-101
lines changed

src/core/rs_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <signal.h>
2525
#include <pwd.h>
2626
#include <grp.h>
27+
#include <stddef.h>
2728

2829

2930
#define RS_ALIGNMENT sizeof(unsigned long) /* platform word */

src/slave/rs_binlog_row.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,20 @@ static char *rs_binlog_parse_string(char *p, u_char *cm, uint32_t ml,
380380
}
381381

382382

383-
int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
384-
uint32_t len, char type, rs_dml_binlog_row_pt write_handle,
385-
rs_dml_binlog_row_pt before_update_handle,
386-
rs_dml_binlog_row_pt update_handle,
387-
rs_dml_binlog_row_pt delete_handle,
388-
rs_parse_binlog_row_pt parse_handle, void *obj)
383+
int rs_dml_binlog_row(rs_slave_info_t *si, void *data, uint32_t len, char type,
384+
rs_binlog_obj_func before_parse_handle,
385+
rs_binlog_obj_func after_parse_handle,
386+
rs_binlog_dml_func write_handle,
387+
rs_binlog_dml_func before_update_handle,
388+
rs_binlog_dml_func update_handle,
389+
rs_binlog_dml_func delete_handle,
390+
int32_t *offset_arr, void *obj)
389391
{
390392
char *p, *ubp;
391393
u_char *ctp, *cmp;
392394
uint32_t i, j, un, nn, before, ml, cn, cl, cmdn, dl, t;
393395
int cmd;
394-
rs_dml_binlog_row_pt handle;
396+
rs_binlog_dml_func handle;
395397
rs_binlog_column_meta_t *meta;
396398

397399
p = data;
@@ -405,18 +407,12 @@ int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
405407
cmp = NULL;
406408
meta = NULL;
407409

408-
if(si == NULL || data == NULL || obj == NULL || parse_handle == NULL)
409-
{
410-
rs_log_err(0, "rs_dml_binlog_row() failed, args can't be null");
411-
return RS_ERR;
412-
}
413-
414410
/* get column number */
415411
rs_memcpy(&cn, p, 4);
416412
p += 4;
417413

418-
u_char ct[cn];
419414
/* get column type */
415+
u_char ct[cn];
420416
rs_memcpy(ct, p, cn);
421417
p += cn;
422418

@@ -449,6 +445,10 @@ int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
449445
p += un;
450446
}
451447

448+
if(before_parse_handle != NULL) {
449+
before_parse_handle(obj);
450+
}
451+
452452
/* get column value */
453453
while(p < (char *) data + len) {
454454

@@ -483,13 +483,13 @@ int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
483483

484484
meta = (rs_binlog_column_meta_t *) &(rs_column_meta[t]);
485485

486-
if(meta->parse_column_handle == NULL) {
486+
if(meta->parse_handle == NULL) {
487487
rs_log_err(0, "not support mysql type parse handle, please "
488488
"contact the author");
489489
return RS_ERR;
490490
}
491491

492-
p = meta->parse_column_handle(p, cmp, meta->meta_len,
492+
p = meta->parse_handle(p, cmp, meta->meta_len,
493493
meta->fixed_len, (uint32_t *) &dl);
494494

495495
rs_log_debug(0, "column type : %u, data len : %u", t, dl);
@@ -500,7 +500,9 @@ int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
500500
/* not null */
501501
if(!((null_bits[j / 8] >> (j % 8)) & 0x01)) {
502502
/* parse */
503-
parse_handle(p, dl, i, obj);
503+
if(offset_arr[i] > -1) {
504+
rs_memcpy((char *) obj + offset_arr[i], p, dl);
505+
}
504506
p += dl;
505507
}
506508

@@ -515,6 +517,10 @@ int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
515517
cmp += meta->meta_len;
516518
}
517519

520+
if(after_parse_handle != NULL) {
521+
after_parse_handle(obj);
522+
}
523+
518524
/* append redis cmd */
519525
if(type == RS_WRITE_ROWS_EVENT) {
520526
handle = write_handle;
@@ -536,12 +542,10 @@ int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
536542
}
537543

538544
if((cmd = handle(si, obj)) == RS_ERR) {
539-
rs_log_err(rs_errno, "handle() failed");
540545
return RS_ERR;
541546
}
542547

543548
cmdn += (uint32_t) cmd;
544-
545549
}
546550

547551
si->cmdn += cmdn;

src/slave/rs_binlog_row.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,27 @@
3939
#define RS_MYSQL_TYPE_STRING 254
4040
#define RS_MYSQL_TYPE_GEOMETRY 255
4141

42-
typedef char *(*rs_binlog_parse_column_pt)(char *p, u_char *cm, uint32_t ml,
43-
uint32_t fl, uint32_t *dl);
42+
typedef char *(*rs_binlog_parse_func) (char *, u_char *, uint32_t, uint32_t,
43+
uint32_t *);
44+
typedef void (*rs_binlog_obj_func) (void *);
45+
typedef int (*rs_binlog_dml_func)(rs_slave_info_t *si, void *obj);
4446

4547
typedef struct {
46-
uint32_t meta_len;
47-
uint32_t fixed_len;
48-
rs_binlog_parse_column_pt parse_column_handle;
48+
uint32_t meta_len;
49+
uint32_t fixed_len;
50+
rs_binlog_parse_func parse_handle;
4951
} rs_binlog_column_meta_t;
5052

5153

5254

53-
typedef int (*rs_dml_binlog_row_pt)(rs_slave_info_t *si, void *obj);
5455

55-
typedef void (*rs_parse_binlog_row_pt)(char *p, uint32_t dl, uint32_t i,
56-
void *obj);
57-
58-
int rs_dml_binlog_row(rs_slave_info_t *si, void *data,
59-
uint32_t len, char type, rs_dml_binlog_row_pt write_handle,
60-
rs_dml_binlog_row_pt before_update_handle,
61-
rs_dml_binlog_row_pt update_handle,
62-
rs_dml_binlog_row_pt delete_handle,
63-
rs_parse_binlog_row_pt parse_handle, void *obj);
56+
int rs_dml_binlog_row(rs_slave_info_t *si, void *data, uint32_t len, char type,
57+
rs_binlog_obj_func before_parse_handle,
58+
rs_binlog_obj_func after_parse_handle,
59+
rs_binlog_dml_func write_handle,
60+
rs_binlog_dml_func before_update_handle,
61+
rs_binlog_dml_func update_handle,
62+
rs_binlog_dml_func delete_handle,
63+
int32_t *offset_arr, void *obj);
6464

6565
#endif

src/slave/rs_mysql_test_test.c

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ typedef struct {
1717

1818
} rs_mysql_test_t;
1919

20+
int32_t rs_mysql_test_pos[] = {
21+
offsetof(rs_mysql_test_t, col1),
22+
offsetof(rs_mysql_test_t, col2),
23+
offsetof(rs_mysql_test_t, col3),
24+
offsetof(rs_mysql_test_t, col4),
25+
offsetof(rs_mysql_test_t, col5),
26+
offsetof(rs_mysql_test_t, col6),
27+
offsetof(rs_mysql_test_t, col7),
28+
offsetof(rs_mysql_test_t, col8),
29+
offsetof(rs_mysql_test_t, col9),
30+
offsetof(rs_mysql_test_t, col10)
31+
};
32+
2033

2134
#define rs_mysql_test_t_init(test) \
2235
(test)->col1 = 0; \
@@ -31,82 +44,50 @@ typedef struct {
3144
rs_memzero((test)->col10, 30 * 3 + 1)
3245

3346

34-
static void rs_parse_test_test(char *p, uint32_t dl, uint32_t i, void *obj);
47+
void rs_init_test_test(void *obj);
48+
void rs_print_test_test(void *obj);
49+
3550
static int rs_insert_test_test(rs_slave_info_t *si, void *obj);
3651
static int rs_before_update_test_test(rs_slave_info_t *si, void *obj);
3752
static int rs_update_test_test(rs_slave_info_t *si, void *obj);
3853
static int rs_delete_test_test(rs_slave_info_t *si, void *obj);
3954

4055
/* test */
41-
void rs_parse_test_test(char *p, uint32_t dl, uint32_t i, void *obj)
56+
void rs_init_test_test(void *obj)
4257
{
4358
rs_mysql_test_t *test;
44-
4559
test = (rs_mysql_test_t *) obj;
60+
rs_mysql_test_t_init(test);
61+
}
4662

47-
switch(i) {
48-
/* col1 */
49-
case 0:
50-
rs_mysql_test_t_init(test);
51-
rs_memcpy(&(test->col1), p, dl);
52-
break;
53-
/* col2 */
54-
case 1:
55-
rs_memcpy(&(test->col2), p, dl);
56-
break;
57-
/* col3 */
58-
case 2:
59-
rs_memcpy(&(test->col3), p, dl);
60-
break;
61-
case 3:
62-
rs_memcpy(&(test->col4), p, dl);
63-
break;
64-
case 4:
65-
rs_memcpy(&(test->col5), p, dl);
66-
break;
67-
case 5:
68-
rs_memcpy(&(test->col6), p, dl);
69-
break;
70-
case 6:
71-
rs_memcpy(test->col7, p, dl);
72-
break;
73-
case 7:
74-
rs_memcpy(test->col8, p, dl);
75-
break;
76-
case 8:
77-
rs_memcpy(test->col9, p, dl);
78-
break;
79-
case 9:
80-
rs_memcpy(test->col10, p, dl);
81-
rs_log_debug(0,
82-
"\n========== test ==========\n"
83-
"col1 : %d\n"
84-
"col2 : %d\n"
85-
"col3 : %d\n"
86-
"col4 : %d\n"
87-
"col5 : %ld\n"
88-
"col6 : %u\n"
89-
"col7 : %s\n"
90-
"col8 : %s\n"
91-
"col9 : %s\n"
92-
"col10 : %s\n"
93-
"\n==========================\n",
94-
test->col1,
95-
test->col2,
96-
test->col3,
97-
test->col4,
98-
test->col5,
99-
test->col6,
100-
test->col7,
101-
test->col8,
102-
test->col9,
103-
test->col10
104-
);
105-
break;
106-
107-
default:
108-
break;
109-
}
63+
void rs_print_test_test(void *obj)
64+
{
65+
rs_mysql_test_t *test;
66+
test = (rs_mysql_test_t *) obj;
67+
rs_log_debug(0,
68+
"\n========== test ==========\n"
69+
"col1 : %d\n"
70+
"col2 : %d\n"
71+
"col3 : %d\n"
72+
"col4 : %d\n"
73+
"col5 : %ld\n"
74+
"col6 : %u\n"
75+
"col7 : %s\n"
76+
"col8 : %s\n"
77+
"col9 : %s\n"
78+
"col10 : %s\n"
79+
"\n==========================\n",
80+
test->col1,
81+
test->col2,
82+
test->col3,
83+
test->col4,
84+
test->col5,
85+
test->col6,
86+
test->col7,
87+
test->col8,
88+
test->col9,
89+
test->col10
90+
);
11091
}
11192

11293
int rs_insert_test_test(rs_slave_info_t *si, void *obj)
@@ -153,9 +134,11 @@ int rs_dml_test_test(rs_slave_info_t *si, char *r, uint32_t rl, char t) {
153134
rs_mysql_test_t test;
154135

155136
return rs_dml_binlog_row(si, r, rl, t,
137+
rs_init_test_test,
138+
rs_print_test_test,
156139
rs_insert_test_test,
157140
rs_before_update_test_test,
158141
rs_update_test_test,
159142
rs_delete_test_test,
160-
rs_parse_test_test, &test);
143+
rs_mysql_test_pos, &test);
161144
}

0 commit comments

Comments
 (0)