Drizzled Public API Documentation

filtered_replicator.h

Go to the documentation of this file.
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00030 #pragma once
00031 
00032 #include <drizzled/atomics.h>
00033 #include <drizzled/plugin/transaction_replicator.h>
00034 
00035 #include PCRE_HEADER
00036 
00037 #include <vector>
00038 #include <string>
00039 
00040 namespace drizzle_plugin
00041 {
00042 
00043 class FilteredReplicator :
00044   public drizzled::plugin::TransactionReplicator
00045 {
00046 public:
00047   FilteredReplicator(std::string name_arg,
00048                      const std::string &sch_filter,
00049                      const std::string &tab_filter,
00050                      const std::string &sch_regex,
00051                      const std::string &tab_regex);
00052 
00054   ~FilteredReplicator();
00055 
00073   drizzled::plugin::ReplicationReturnCode
00074   replicate(drizzled::plugin::TransactionApplier *in_applier,
00075             drizzled::Session &in_session,
00076             drizzled::message::Transaction &to_replicate);
00077   
00085   void setSchemaFilter(const std::string &input);
00086 
00090   const std::string &getSchemaFilter() const
00091   {
00092     return _sch_filter;
00093   }
00094 
00102   void setTableFilter(const std::string &input);
00103 
00107   const std::string &getTableFilter() const
00108   {
00109     return _tab_filter;
00110   }
00111 
00118   void updateTableSysvar(const char **var_ptr)
00119   {
00120     *var_ptr= _tab_filter.c_str();
00121     pthread_mutex_unlock(&sysvar_tab_lock);
00122   }
00123 
00130   void updateSchemaSysvar(const char **var_ptr)
00131   {
00132     *var_ptr= _sch_filter.c_str();
00133     pthread_mutex_unlock(&sysvar_sch_lock);
00134   }
00135 
00136 private:
00137  
00146   void populateFilter(std::string input,
00147                       std::vector<std::string> &filter);
00148 
00157   bool isSchemaFiltered(const std::string &schema_name);
00158 
00167   bool isTableFiltered(const std::string &table_name);
00168   
00178   void parseStatementTableMetadata(const drizzled::message::Statement &in_statement,
00179                                    std::string &in_schema_name,
00180                                    std::string &in_table_name) const;
00181 
00193   void parseQuery(const std::string &sql,
00194                   std::string &schema_name,
00195                   std::string &table_name);
00196 
00197   /*
00198    * Vectors of the tables and schemas to filter.
00199    */
00200   std::vector<std::string> schemas_to_filter;
00201   std::vector<std::string> tables_to_filter;
00202 
00203   /*
00204    * Variables to contain the string representation of the
00205    * comma-separated lists of schemas and tables to filter.
00206    */
00207   std::string _sch_filter;
00208   std::string _tab_filter;
00209 
00210   const std::string _sch_regex;
00211   const std::string _tab_regex;
00212 
00213   /*
00214    * We need locks to protect the vectors when they are
00215    * being updated and accessed. It would be nice to use
00216    * r/w locks here since the vectors will mostly be 
00217    * accessed in a read-only fashion and will be only updated
00218    * rarely.
00219    */
00220   pthread_mutex_t sch_vector_lock;
00221   pthread_mutex_t tab_vector_lock;
00222 
00223   /*
00224    * We need a lock to protect the system variables
00225    * that can be updated. We have a lock for each 
00226    * system variable.
00227    */
00228   pthread_mutex_t sysvar_sch_lock;
00229   pthread_mutex_t sysvar_tab_lock;
00230 
00231   pcre *sch_re;
00232   pcre *tab_re;
00233 };
00234 
00235 } /* namespace drizzle_plugin */
00236