40#include <drizzled/gettext.h>
41#include <drizzled/plugin/transaction_applier.h>
42#include <drizzled/message/transaction.pb.h>
43#include <drizzled/plugin.h>
45#include <drizzled/item/string.h>
47#include <boost/program_options.hpp>
51namespace po= boost::program_options;
58static string sysvar_filtered_replicator_sch_filters;
59static string sysvar_filtered_replicator_tab_filters;
61FilteredReplicator::FilteredReplicator(
string name_arg,
62 const std::string &sch_filter,
63 const std::string &tab_filter,
64 const std::string &sch_regex,
65 const std::string &tab_regex) :
66 plugin::TransactionReplicator(name_arg),
69 _sch_filter(sch_filter),
70 _tab_filter(tab_filter),
71 _sch_regex(sch_regex),
72 _tab_regex(tab_regex),
80 if (not _sch_filter.empty())
82 populateFilter(_sch_filter, schemas_to_filter);
89 if (not _tab_filter.empty())
91 populateFilter(_tab_filter, tables_to_filter);
98 if (not _sch_regex.empty())
101 PCRE2_SIZE error_offset= 0;
102 sch_re= pcre2_compile(reinterpret_cast<PCRE2_SPTR>(_sch_regex.c_str()),
103 PCRE2_ZERO_TERMINATED,
114 if (not _tab_regex.empty())
117 PCRE2_SIZE error_offset= 0;
118 tab_re= pcre2_compile(reinterpret_cast<PCRE2_SPTR>(_tab_regex.c_str()),
119 PCRE2_ZERO_TERMINATED,
126 pthread_mutex_init(&sch_vector_lock, NULL);
127 pthread_mutex_init(&tab_vector_lock, NULL);
128 pthread_mutex_init(&sysvar_sch_lock, NULL);
129 pthread_mutex_init(&sysvar_tab_lock, NULL);
136 pcre2_code_free(sch_re);
140 pcre2_code_free(tab_re);
143 pthread_mutex_destroy(&sch_vector_lock);
144 pthread_mutex_destroy(&tab_vector_lock);
145 pthread_mutex_destroy(&sysvar_sch_lock);
146 pthread_mutex_destroy(&sysvar_tab_lock);
151 string &in_schema_name,
152 string &in_table_name)
const
154 switch (in_statement.type())
156 case message::Statement::INSERT:
158 const message::TableMetadata &metadata= in_statement.insert_header().table_metadata();
159 in_schema_name= metadata.schema_name();
160 in_table_name= metadata.table_name();
163 case message::Statement::UPDATE:
165 const message::TableMetadata &metadata= in_statement.update_header().table_metadata();
166 in_schema_name= metadata.schema_name();
167 in_table_name= metadata.table_name();
170 case message::Statement::DELETE:
172 const message::TableMetadata &metadata= in_statement.delete_header().table_metadata();
173 in_schema_name= metadata.schema_name();
174 in_table_name= metadata.table_name();
177 case message::Statement::CREATE_SCHEMA:
179 in_schema_name= in_statement.create_schema_statement().schema().name();
180 in_table_name.clear();
183 case message::Statement::ALTER_SCHEMA:
185 in_schema_name= in_statement.alter_schema_statement().after().name();
186 in_table_name.clear();
189 case message::Statement::DROP_SCHEMA:
191 in_schema_name= in_statement.drop_schema_statement().schema_name();
192 in_table_name.clear();
195 case message::Statement::CREATE_TABLE:
197 in_schema_name= in_statement.create_table_statement().table().schema();
198 in_table_name= in_statement.create_table_statement().table().name();
201 case message::Statement::ALTER_TABLE:
203 in_schema_name= in_statement.alter_table_statement().after().schema();
204 in_table_name= in_statement.alter_table_statement().after().name();
207 case message::Statement::DROP_TABLE:
209 const message::TableMetadata &metadata= in_statement.drop_table_statement().table_metadata();
210 in_schema_name= metadata.schema_name();
211 in_table_name= metadata.table_name();
217 in_schema_name.clear();
218 in_table_name.clear();
224plugin::ReplicationReturnCode
227 message::Transaction &to_replicate)
232 size_t num_statements= to_replicate.statement_size();
242 message::Transaction filtered_transaction;
244 for (
size_t x= 0; x < num_statements; ++x)
249 const message::Statement &statement= to_replicate.statement(x);
261 if (statement.type() == message::Statement::RAW_SQL)
263 parseQuery(statement.sql(), schema_name, table_name);
276 boost::to_lower(schema_name);
277 boost::to_lower(table_name);
281 message::Statement *s= filtered_transaction.add_statement();
286 if (filtered_transaction.statement_size() > 0)
293 message::TransactionContext *tc= filtered_transaction.mutable_transaction_context();
294 *tc= to_replicate.transaction_context();
295 return in_applier->
apply(in_session, filtered_transaction);
297 return plugin::SUCCESS;
301 std::vector<string> &filter)
307 boost::to_lower(input);
308 string::size_type last_pos= input.find_first_not_of(
',', 0);
309 string::size_type pos= input.find_first_of(
',', last_pos);
311 while (pos != string::npos || last_pos != string::npos)
313 filter.push_back(input.substr(last_pos, pos - last_pos));
314 last_pos= input.find_first_not_of(
',', pos);
315 pos= input.find_first_of(
',', last_pos);
321 pthread_mutex_lock(&sch_vector_lock);
322 std::vector<string>::iterator it= find(schemas_to_filter.begin(), schemas_to_filter.end(), schema_name);
323 if (it != schemas_to_filter.end())
325 pthread_mutex_unlock(&sch_vector_lock);
328 pthread_mutex_unlock(&sch_vector_lock);
335 if (not _sch_regex.empty() && sch_re)
337 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(sch_re, NULL);
338 if (match_data == NULL)
340 int32_t result= pcre2_match(sch_re,
341 reinterpret_cast<PCRE2_SPTR
>(schema_name.c_str()),
342 schema_name.length(), 0, 0, match_data, NULL);
343 pcre2_match_data_free(match_data);
353 pthread_mutex_lock(&tab_vector_lock);
354 std::vector<string>::iterator it= find(tables_to_filter.begin(), tables_to_filter.end(), table_name);
355 if (it != tables_to_filter.end())
357 pthread_mutex_unlock(&tab_vector_lock);
360 pthread_mutex_unlock(&tab_vector_lock);
367 if (not _tab_regex.empty() && tab_re)
369 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(tab_re, NULL);
370 if (match_data == NULL)
372 int32_t result= pcre2_match(tab_re,
373 reinterpret_cast<PCRE2_SPTR
>(table_name.c_str()),
374 table_name.length(), 0, 0, match_data, NULL);
375 pcre2_match_data_free(match_data);
391 string::size_type pos= sql.find_first_of(
' ', 0);
392 string type= sql.substr(0, pos);
398 boost::to_upper(
type);
400 if (
type.compare(
"DROP") == 0)
408 pos= sql.find_first_of(
' ', 11);
409 string cmp_str= sql.substr(11, pos - 11);
410 string target_name(
"");
411 if (cmp_str.compare(
"IF") == 0)
414 pos= sql.find_first_of(
' ', 21);
415 target_name= sql.substr(21, pos - 21);
419 target_name= cmp_str;
426 pos= target_name.find_first_of(
'.', 0);
427 if (pos != string::npos)
432 schema_name= target_name.substr(0, pos);
436 table_name= target_name.substr(pos + 1);
440 table_name= target_name;
443 else if (
type.compare(
"CREATE") == 0)
450 pos= sql.find_first_of(
' ', 13);
451 string target_name= sql.substr(13, pos - 13);
457 pos= target_name.find_first_of(
'.', 0);
458 if (pos != string::npos)
463 schema_name= target_name.substr(0, pos);
467 table_name= target_name.substr(pos + 1);
471 table_name= target_name;
483 pthread_mutex_lock(&sch_vector_lock);
484 pthread_mutex_lock(&sysvar_sch_lock);
486 schemas_to_filter.clear();
488 pthread_mutex_unlock(&sysvar_sch_lock);
489 pthread_mutex_unlock(&sch_vector_lock);
494 pthread_mutex_lock(&tab_vector_lock);
495 pthread_mutex_lock(&sysvar_tab_lock);
497 tables_to_filter.clear();
499 pthread_mutex_unlock(&sysvar_tab_lock);
500 pthread_mutex_unlock(&tab_vector_lock);
507 const char *input=
var->value->str_value.ptr();
508 if (input && filtered_replicator)
510 filtered_replicator->setSchemaFilter(input);
519 const char *input=
var->value->str_value.ptr();
520 if (input && filtered_replicator)
533 filtered_replicator=
new FilteredReplicator(
"filtered_replicator",
534 vm[
"filteredschemas"].as<string>(),
535 vm[
"filteredtables"].as<string>(),
536 vm[
"schemaregex"].as<string>(),
537 vm[
"tableregex"].as<string>());
539 context.add(filtered_replicator);
541 sysvar_filtered_replicator_sch_filters,
542 filtered_schemas_validate));
544 sysvar_filtered_replicator_tab_filters,
545 filtered_tables_validate));
548 vm[
"schemaregex"].as<string>()));
550 vm[
"tableregex"].as<string>()));
557 context(
"filteredschemas",
558 po::value<string>(&sysvar_filtered_replicator_sch_filters)->default_value(
""),
559 N_(
"Comma-separated list of schemas to exclude"));
560 context(
"filteredtables",
561 po::value<string>(&sysvar_filtered_replicator_tab_filters)->default_value(
""),
562 N_(
"Comma-separated list of tables to exclude"));
563 context(
"schemaregex",
564 po::value<string>()->default_value(
""),
565 N_(
"Regular expression to apply to schemas to exclude"));
566 context(
"tableregex",
567 po::value<string>()->default_value(
""),
568 N_(
"Regular expression to apply to tables to exclude"));
573DRIZZLE_DECLARE_PLUGIN
576 "filtered_replicator",
578 "Padraig O Sullivan",
579 N_(
"Replicates events filtered by schema or table name"),
581 drizzle_plugin::init,
583 drizzle_plugin::init_options
585DRIZZLE_DECLARE_PLUGIN_END;
void populateFilter(std::string input, std::vector< std::string > &filter)
bool isTableFiltered(const std::string &table_name)
void setTableFilter(const std::string &input)
void parseStatementTableMetadata(const drizzled::message::Statement &in_statement, std::string &in_schema_name, std::string &in_table_name) const
drizzled::plugin::ReplicationReturnCode replicate(drizzled::plugin::TransactionApplier *in_applier, drizzled::Session &in_session, drizzled::message::Transaction &to_replicate)
void parseQuery(const std::string &sql, std::string &schema_name, std::string &table_name)
bool isSchemaFiltered(const std::string &schema_name)
void setSchemaFilter(const std::string &input)
virtual ReplicationReturnCode apply(Session &in_session, const message::Transaction &to_apply)=0
if(!referred_tab||referred_tab->join !=join)
Convert from decimal to binary representation.
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.