18 #include "rpl_filter.h"
22 #define TABLE_RULE_HASH_SIZE 16
23 #define TABLE_RULE_ARR_SIZE 16
25 Rpl_filter::Rpl_filter() :
27 do_table_hash_inited(0), ignore_table_hash_inited(0),
28 do_table_array_inited(0), ignore_table_array_inited(0),
29 wild_do_table_inited(0), wild_ignore_table_inited(0)
37 Rpl_filter::~Rpl_filter()
39 if (do_table_hash_inited)
40 my_hash_free(&do_table_hash);
41 if (ignore_table_hash_inited)
42 my_hash_free(&ignore_table_hash);
43 if (do_table_array_inited)
44 free_string_array(&do_table_array);
45 if (ignore_table_array_inited)
46 free_string_array(&ignore_table_array);
47 if (wild_do_table_inited)
48 free_string_array(&wild_do_table);
49 if (wild_ignore_table_inited)
50 free_string_array(&wild_ignore_table);
52 free_list(&ignore_db);
53 free_list(&rewrite_db);
96 Rpl_filter::tables_ok(
const char* db,
TABLE_LIST* tables)
98 bool some_tables_updating= 0;
99 DBUG_ENTER(
"Rpl_filter::tables_ok");
101 for (; tables; tables= tables->next_global)
103 char hash_key[2*NAME_LEN+2];
107 if (!tables->updating)
109 some_tables_updating= 1;
110 end= strmov(hash_key, tables->db ? tables->db : db);
112 len= (uint) (strmov(end, tables->table_name) - hash_key);
113 if (do_table_hash_inited)
115 if (my_hash_search(&do_table_hash, (uchar*) hash_key, len))
118 if (ignore_table_hash_inited)
120 if (my_hash_search(&ignore_table_hash, (uchar*) hash_key, len))
123 if (wild_do_table_inited &&
124 find_wild(&wild_do_table, hash_key, len))
126 if (wild_ignore_table_inited &&
127 find_wild(&wild_ignore_table, hash_key, len))
137 DBUG_RETURN(some_tables_updating &&
138 !do_table_hash_inited && !wild_do_table_inited);
155 Rpl_filter::db_ok(
const char* db)
157 DBUG_ENTER(
"Rpl_filter::db_ok");
159 if (do_db.is_empty() && ignore_db.is_empty())
172 if (!do_db.is_empty())
184 if (!my_strcasecmp(table_alias_charset, tmp->ptr, db))
201 if (!my_strcasecmp(table_alias_charset, tmp->ptr, db))
240 Rpl_filter::db_ok_with_wild_table(
const char *db)
242 DBUG_ENTER(
"Rpl_filter::db_ok_with_wild_table");
244 char hash_key[NAME_LEN+2];
247 end= strmov(hash_key, db);
249 len= end - hash_key ;
250 if (wild_do_table_inited && find_wild(&wild_do_table, hash_key, len))
252 DBUG_PRINT(
"return",(
"1"));
255 if (wild_ignore_table_inited && find_wild(&wild_ignore_table, hash_key, len))
257 DBUG_PRINT(
"return",(
"0"));
265 DBUG_PRINT(
"return",(
"db=%s,retval=%d", db, !wild_do_table_inited));
266 DBUG_RETURN(!wild_do_table_inited);
273 return table_rules_on;
278 Rpl_filter::is_rewrite_empty()
280 return rewrite_db.is_empty();
285 Rpl_filter::add_do_table_array(
const char* table_spec)
287 DBUG_ENTER(
"Rpl_filter::add_do_table");
288 if (!do_table_array_inited)
289 init_table_rule_array(&do_table_array, &do_table_array_inited);
291 DBUG_RETURN(add_table_rule_to_array(&do_table_array, table_spec));
296 Rpl_filter::add_ignore_table_array(
const char* table_spec)
298 DBUG_ENTER(
"Rpl_filter::add_ignore_table");
299 if (!ignore_table_array_inited)
300 init_table_rule_array(&ignore_table_array, &ignore_table_array_inited);
302 DBUG_RETURN(add_table_rule_to_array(&ignore_table_array, table_spec));
307 Rpl_filter::add_wild_do_table(
const char* table_spec)
309 DBUG_ENTER(
"Rpl_filter::add_wild_do_table");
310 if (!wild_do_table_inited)
311 init_table_rule_array(&wild_do_table, &wild_do_table_inited);
313 DBUG_RETURN(add_table_rule_to_array(&wild_do_table, table_spec));
318 Rpl_filter::add_wild_ignore_table(
const char* table_spec)
320 DBUG_ENTER(
"Rpl_filter::add_wild_ignore_table");
321 if (!wild_ignore_table_inited)
322 init_table_rule_array(&wild_ignore_table, &wild_ignore_table_inited);
324 DBUG_RETURN(add_table_rule_to_array(&wild_ignore_table, table_spec));
329 Rpl_filter::add_db_rewrite(
const char* from_db,
const char* to_db)
345 Rpl_filter::build_do_table_hash()
347 DBUG_ENTER(
"Rpl_filter::build_do_table_hash");
349 if (build_table_hash_from_array(&do_table_array, &do_table_hash,
350 do_table_array_inited, &do_table_hash_inited))
354 if (do_table_array_inited)
356 free_string_array(&do_table_array);
357 do_table_array_inited= FALSE;
372 Rpl_filter::build_ignore_table_hash()
374 DBUG_ENTER(
"Rpl_filter::build_ignore_table_hash");
376 if (build_table_hash_from_array(&ignore_table_array, &ignore_table_hash,
377 ignore_table_array_inited, &ignore_table_hash_inited))
381 if (ignore_table_array_inited)
383 free_string_array(&ignore_table_array);
384 ignore_table_array_inited= FALSE;
407 bool array_inited,
bool *hash_inited)
409 DBUG_ENTER(
"Rpl_filter::build_table_hash");
413 init_table_rule_hash(table_hash, hash_inited);
414 for (uint
i= 0;
i < table_array->elements;
i++)
417 get_dynamic(table_array, (uchar*)&e,
i);
418 if (add_table_rule_to_hash(table_hash, e->db, e->key_len))
439 Rpl_filter::add_table_rule_to_hash(
HASH* h,
const char* table_spec, uint len)
441 const char* dot = strchr(table_spec,
'.');
448 e->tbl_name= e->db + (dot - table_spec) + 1;
450 memcpy(e->db, table_spec, len);
452 if (my_hash_insert(h, (uchar*)e))
466 Rpl_filter::add_table_rule_to_array(
DYNAMIC_ARRAY* a,
const char* table_spec)
468 const char* dot = strchr(table_spec,
'.');
470 uint len = (uint)strlen(table_spec);
475 e->tbl_name= e->db + (dot - table_spec) + 1;
477 memcpy(e->db, table_spec, len);
479 if (insert_dynamic(a, &e))
489 Rpl_filter::add_do_db(
const char* table_spec)
491 DBUG_ENTER(
"Rpl_filter::add_do_db");
499 Rpl_filter::add_ignore_db(
const char* table_spec)
501 DBUG_ENTER(
"Rpl_filter::add_ignore_db");
507 extern "C" uchar *get_table_key(
const uchar *,
size_t *, my_bool);
508 extern "C" void free_table_ent(
void* a);
510 uchar *get_table_key(
const uchar* a,
size_t *len,
511 my_bool __attribute__((unused)))
516 return (uchar*)e->db;
520 void free_table_ent(
void* a)
529 Rpl_filter::init_table_rule_hash(
HASH* h,
bool* h_inited)
531 my_hash_init(h, table_alias_charset, TABLE_RULE_HASH_SIZE,0,0,
532 get_table_key, free_table_ent, 0);
538 Rpl_filter::init_table_rule_array(
DYNAMIC_ARRAY* a,
bool* a_inited)
540 my_init_dynamic_array(a,
sizeof(
TABLE_RULE_ENT*), TABLE_RULE_ARR_SIZE,
541 TABLE_RULE_ARR_SIZE);
547 Rpl_filter::find_wild(
DYNAMIC_ARRAY *a,
const char* key,
int len)
550 const char* key_end= key + len;
552 for (i= 0; i < a->elements; i++)
555 get_dynamic(a, (uchar*)&e, i);
561 if (!my_wildcmp(table_alias_charset, key, key_end,
563 (
const char*)(e->db + e->key_len),
564 '\\',wild_one,wild_many))
576 for (i= 0; i < a->elements; i++)
579 get_dynamic(a, (uchar*) &p, i);
600 Rpl_filter::table_rule_ent_hash_to_str(
String* s,
HASH* h,
bool inited)
605 for (uint i= 0; i < h->records; i++)
610 s->append(e->db,e->key_len);
623 for (uint i= 0; i < a->elements; i++)
626 get_dynamic(a, (uchar*)&e, i);
629 s->append(e->db,e->key_len);
636 Rpl_filter::get_do_table(
String* str)
638 table_rule_ent_hash_to_str(str, &do_table_hash, do_table_hash_inited);
643 Rpl_filter::get_ignore_table(
String* str)
645 table_rule_ent_hash_to_str(str, &ignore_table_hash, ignore_table_hash_inited);
650 Rpl_filter::get_wild_do_table(
String* str)
652 table_rule_ent_dynamic_array_to_str(str, &wild_do_table, wild_do_table_inited);
657 Rpl_filter::get_wild_ignore_table(
String* str)
659 table_rule_ent_dynamic_array_to_str(str, &wild_ignore_table, wild_ignore_table_inited);
664 Rpl_filter::get_rewrite_db(
const char* db,
size_t *new_len)
666 if (rewrite_db.is_empty() || !db)
678 if (!my_strcasecmp(table_alias_charset, tmp->key, db))
680 *new_len= strlen(tmp->val);
689 Rpl_filter::get_do_db()
696 Rpl_filter::get_ignore_db()