~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/replicator.cc

  • Committer: Monty Taylor
  • Date: 2008-12-06 23:17:26 UTC
  • mfrom: (664 drizzle)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206231726-ut9ntnazs3rega56
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <drizzled/server_includes.h>
21
21
#include <drizzled/replicator.h>
22
22
#include <drizzled/gettext.h>
 
23
#include <drizzled/session.h>
23
24
 
24
25
int replicator_initializer(st_plugin_int *plugin)
25
26
{
69
70
  return 0;
70
71
}
71
72
 
72
 
/* The plugin_foreach() iterator requires that we
73
 
   convert all the parameters of a plugin api entry point
74
 
   into just one single void ptr, plus the session.
75
 
   So we will take all the additional paramters of replicator_do1,
76
 
   and marshall them into a struct of this type, and
77
 
   then just pass in a pointer to it.
78
 
*/
79
 
typedef struct replicator_do1_parms_st
80
 
{
81
 
  void *parm1;
82
 
  void *parm2;
83
 
} replicator_do1_parms_t;
84
 
 
85
73
/* This gets called by plugin_foreach once for each loaded replicator plugin */
86
 
static bool replicator_do1_iterate (Session *session, plugin_ref plugin, void *p)
 
74
static bool replicator_session_iterate (Session *session, plugin_ref plugin, void *)
87
75
{
88
 
  replicator_t *l= plugin_data(plugin, replicator_t *);
89
 
  replicator_do1_parms_t *parms= (replicator_do1_parms_t *) p;
 
76
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
77
  void *session_data;
90
78
 
91
79
  /* call this loaded replicator plugin's replicator_func1 function pointer */
92
 
  if (l && l->replicator_func1)
 
80
  if (repl && repl->session_init)
93
81
    {
94
 
      if (l->replicator_func1(session, parms->parm1, parms->parm2))
 
82
      session_data= repl->session_init(session);
 
83
      if (session_data)
95
84
        {
96
85
          /* TRANSLATORS: The leading word "replicator" is the name
97
86
             of the plugin api, and so should not be translated. */
98
 
          sql_print_error(_("replicator plugin '%s' replicator_func1() failed"),
 
87
          sql_print_error(_("replicator plugin '%s' replicator_session_init() failed"),
99
88
                          (char *)plugin_name(plugin));
100
89
          return true;
101
90
        }
102
91
    }
 
92
 
 
93
  session->setReplicationData(session_data);
 
94
 
103
95
  return false;
104
96
}
105
97
 
106
 
/* This is the replicator_do1 entry point.
107
 
   This gets called by the rest of the Drizzle server code */
108
 
bool replicator_do1 (Session *session, void *parm1, void *parm2)
 
98
/*
 
99
  This call is called once at the begining of each transaction.
 
100
*/
 
101
bool replicator_session_init(Session *session)
109
102
{
110
 
  replicator_do1_parms_t parms;
111
103
  bool foreach_rv;
112
104
 
113
 
  /* marshall the parameters so they will fit into the foreach */
114
 
  parms.parm1= parm1;
115
 
  parms.parm2= parm2;
116
 
 
117
 
  /* call replicator_do1_iterate
118
 
     once for each loaded replicator plugin */
119
 
  foreach_rv= plugin_foreach(session,
120
 
                             replicator_do1_iterate,
121
 
                             DRIZZLE_REPLICATOR_PLUGIN,
122
 
                             (void *) &parms);
 
105
  /* 
 
106
    call replicator_session_iterate
 
107
    once for each loaded replicator plugin 
 
108
  */
 
109
  foreach_rv= plugin_foreach(session, replicator_session_iterate,
 
110
                             DRIZZLE_REPLICATOR_PLUGIN, NULL);
123
111
  return foreach_rv;
124
112
}
125
113
 
130
118
   and marshall them into a struct of this type, and
131
119
   then just pass in a pointer to it.
132
120
*/
133
 
typedef struct replicator_do2_parms_st
 
121
enum repl_row_exec_t{
 
122
  repl_insert,
 
123
  repl_update,
 
124
  repl_delete
 
125
};
 
126
 
 
127
typedef struct replicator_row_parms_st
134
128
{
135
 
  void *parm3;
136
 
  void *parm4;
137
 
} replicator_do2_parms_t;
 
129
  repl_row_exec_t type;
 
130
  Table *table;
 
131
  const unsigned char *before;
 
132
  const unsigned char *after;
 
133
} replicator_row_parms_st;
 
134
 
138
135
 
139
136
/* This gets called by plugin_foreach once for each loaded replicator plugin */
140
 
static bool replicator_do2_iterate (Session *session, plugin_ref plugin, void *p)
 
137
static bool replicator_do_row_iterate (Session *session, plugin_ref plugin, void *p)
141
138
{
142
 
  replicator_t *l= plugin_data(plugin, replicator_t *);
143
 
  replicator_do2_parms_t *parms= (replicator_do2_parms_t *) p;
144
 
 
145
 
  /* call this loaded replicator plugin's replicator_func1 function pointer */
146
 
  if (l && l->replicator_func2)
147
 
    {
148
 
      if (l->replicator_func2(session, parms->parm3, parms->parm4))
149
 
        {
150
 
          /* TRANSLATORS: The leading word "replicator" is the name
151
 
             of the plugin api, and so should not be translated. */
152
 
          sql_print_error(_("replicator plugin '%s' replicator_func2() failed"),
153
 
                          (char *)plugin_name(plugin));
154
 
 
155
 
          return true;
156
 
        }
157
 
    }
 
139
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
140
  replicator_row_parms_st *params= (replicator_row_parms_st *) p;
 
141
 
 
142
  switch (params->type) {
 
143
  case repl_insert:
 
144
    if (repl && repl->row_insert)
 
145
    {
 
146
      if (repl->row_insert(session, params->table))
 
147
      {
 
148
        /* TRANSLATORS: The leading word "replicator" is the name
 
149
          of the plugin api, and so should not be translated. */
 
150
        sql_print_error(_("replicator plugin '%s' row_insert() failed"),
 
151
                        (char *)plugin_name(plugin));
 
152
 
 
153
        return true;
 
154
      }
 
155
    }
 
156
    break;
 
157
  case repl_update:
 
158
    if (repl && repl->row_update)
 
159
    {
 
160
      if (repl->row_update(session, params->table, params->before, params->after))
 
161
      {
 
162
        /* TRANSLATORS: The leading word "replicator" is the name
 
163
          of the plugin api, and so should not be translated. */
 
164
        sql_print_error(_("replicator plugin '%s' row_update() failed"),
 
165
                        (char *)plugin_name(plugin));
 
166
 
 
167
        return true;
 
168
      }
 
169
    }
 
170
    break;
 
171
  case repl_delete:
 
172
    if (repl && repl->row_delete)
 
173
    {
 
174
      if (repl->row_delete(session, params->table))
 
175
      {
 
176
        /* TRANSLATORS: The leading word "replicator" is the name
 
177
          of the plugin api, and so should not be translated. */
 
178
        sql_print_error(_("replicator plugin '%s' row_delete() failed"),
 
179
                        (char *)plugin_name(plugin));
 
180
 
 
181
        return true;
 
182
      }
 
183
    }
 
184
    break;
 
185
  }
158
186
  return false;
159
187
}
160
188
 
161
189
/* This is the replicator_do2 entry point.
162
190
   This gets called by the rest of the Drizzle server code */
163
 
bool replicator_do2 (Session *session, void *parm3, void *parm4)
164
 
{
165
 
  replicator_do2_parms_t parms;
166
 
  bool foreach_rv;
167
 
 
168
 
  /* marshall the parameters so they will fit into the foreach */
169
 
  parms.parm3= parm3;
170
 
  parms.parm4= parm4;
171
 
 
172
 
  /* call replicator_do2_iterate
173
 
     once for each loaded replicator plugin */
174
 
  foreach_rv= plugin_foreach(session,
175
 
                             replicator_do2_iterate,
176
 
                             DRIZZLE_REPLICATOR_PLUGIN,
177
 
                             (void *) &parms);
178
 
  return foreach_rv;
 
191
static bool replicator_do_row (Session *session,  replicator_row_parms_st *params)
 
192
{
 
193
  bool foreach_rv;
 
194
 
 
195
  foreach_rv= plugin_foreach(session, replicator_do_row_iterate,
 
196
                             DRIZZLE_REPLICATOR_PLUGIN, (void *) &params);
 
197
  return foreach_rv;
 
198
}
 
199
 
 
200
bool replicator_write_row(Session *session, Table *table)
 
201
{
 
202
  replicator_row_parms_st param;
 
203
 
 
204
  param.type= repl_insert;
 
205
  param.table= table;
 
206
  param.after= NULL;
 
207
  param.before= NULL;
 
208
 
 
209
  return replicator_do_row(session, &param);
 
210
}
 
211
 
 
212
bool replicator_update_row(Session *session, Table *table, 
 
213
                           const unsigned char *before, 
 
214
                           const unsigned char *after)
 
215
{
 
216
  replicator_row_parms_st param;
 
217
 
 
218
  param.type= repl_update;
 
219
  param.table= table;
 
220
  param.after= after;
 
221
  param.before= before;
 
222
 
 
223
  return replicator_do_row(session, &param);
 
224
}
 
225
 
 
226
bool replicator_delete_row(Session *session, Table *table)
 
227
{
 
228
  replicator_row_parms_st param;
 
229
 
 
230
  param.type= repl_delete;
 
231
  param.table= table;
 
232
  param.after= NULL;
 
233
  param.before= NULL;
 
234
 
 
235
  return replicator_do_row(session, &param);
 
236
}
 
237
 
 
238
/* 
 
239
  Here be Dragons!
 
240
 
 
241
  Ok, not so much dragons, but this is where we handle either commits or rollbacks of 
 
242
  statements. 
 
243
*/
 
244
typedef struct replicator_row_end_st
 
245
{
 
246
  bool autocommit;
 
247
  bool commit;
 
248
} replicator_row_end_st;
 
249
 
 
250
/* We call this to end a statement (on each registered plugin) */
 
251
static bool replicator_do_row_end_iterate (Session *session, plugin_ref plugin, void *p)
 
252
{
 
253
  replicator_t *repl= plugin_data(plugin, replicator_t *);
 
254
  replicator_row_end_st *params= (replicator_row_end_st *)p;
 
255
 
 
256
  /* call this loaded replicator plugin's replicator_func1 function pointer */
 
257
  if (repl && repl->end_transaction)
 
258
  {
 
259
    if (repl->end_transaction(session, params->autocommit, params->commit))
 
260
    {
 
261
      /* TRANSLATORS: The leading word "replicator" is the name
 
262
        of the plugin api, and so should not be translated. */
 
263
      sql_print_error(_("replicator plugin '%s' end_transaction() failed"),
 
264
                      (char *)plugin_name(plugin));
 
265
      return true;
 
266
    }
 
267
  }
 
268
 
 
269
  return false;
 
270
}
 
271
 
 
272
bool replicator_end_transaction(Session *session, bool autocommit, bool commit)
 
273
{
 
274
  bool foreach_rv;
 
275
  replicator_row_end_st params;
 
276
  
 
277
  params.autocommit= autocommit;
 
278
  params.commit= commit;
 
279
 
 
280
  /* We need to free any data we did an init of for the session */
 
281
  foreach_rv= plugin_foreach(session, replicator_do_row_end_iterate,
 
282
                             DRIZZLE_REPLICATOR_PLUGIN, (void *) &params);
 
283
 
 
284
  return foreach_rv;
 
285
}
 
286
 
 
287
/*
 
288
  For a ROLLBACK TO SAVEPOINT we make this call.
 
289
*/
 
290
bool replicator_rollback_to_savepoint(Session *, void *)
 
291
{
 
292
  return false;
 
293
}
 
294
 
 
295
/*
 
296
  If somene makes a call to create a savepoint.
 
297
*/
 
298
bool replicator_savepoint_set(Session *, void *)
 
299
{
 
300
  return false;
 
301
}
 
302
 
 
303
/*
 
304
  If you can do real 2PC this is your hook poing to know that the event is coming.
 
305
 
 
306
  Always true for the moment.
 
307
 
 
308
*/
 
309
bool replicator_prepare(Session *)
 
310
{
 
311
  return false;
179
312
}