1
/* Copyright (C) 2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include <drizzled/server_includes.h>
17
#include <drizzled/gettext.h>
18
#include <drizzled/session.h>
19
#include <drizzled/error.h>
20
#include <drizzled/plugin/replicator.h>
21
#include <drizzled/message/all.h>
29
static bool isEnabled;
30
static char *log_directory= NULL;
32
static bool write_to_disk(int file, drizzled::message::EventList *list)
38
list->SerializePartialToString(&buffer);
40
length= buffer.length();
42
cout << "Writing record of " << length << "." << endl;
44
if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
46
cerr << "Only wrote " << written << " out of " << length << "." << endl;
50
if ((written= write(file, buffer.c_str(), length)) != length)
52
cerr << "Only wrote " << written << " out of " << length << "." << endl;
59
class Protobuf_replicator: public Replicator
63
Protobuf_replicator() : log_file(-1)
69
logname.append(log_directory ? log_directory : "/tmp");
70
logname.append("/replication_log");
72
if ((log_file= open(logname.c_str(), O_TRUNC|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
74
cerr << "Can not open file: " << logname.c_str() << endl;
81
~Protobuf_replicator()
87
virtual bool statement_hook(Session *session, const char *query, size_t)
89
using namespace drizzled::message;
91
drizzled::message::EventList list;
93
if (isEnabled == false)
95
cerr << "Got into statement" <<endl;
97
drizzled::message::Event *record= list.add_event();
98
record->set_type(Event::DDL);
99
record->set_autocommit(true);
100
record->set_server_id("localhost");
101
record->set_query_id(10);
102
record->set_transaction_id("junk");
103
record->set_schema(session->db);
104
record->set_sql(query);
106
return write_to_disk(log_file, &list);
109
virtual bool session_init_hook(Session *session)
111
using namespace drizzled::message;
113
if (isEnabled == false)
116
drizzled::message::EventList *list= new drizzled::message::EventList;
117
session->setReplicationData(list);
119
drizzled::message::Event *record= list->add_event();
121
record->set_type(Event::DDL);
122
record->set_autocommit(true);
123
record->set_server_id("localhost");
124
record->set_query_id(10);
125
record->set_transaction_id("junk");
126
record->set_schema(session->db);
127
record->set_sql("BEGIN");
132
virtual bool row_insert_hook(Session *session, Table *)
134
using namespace drizzled::message;
136
if (isEnabled == false)
139
drizzled::message::EventList *list= (drizzled::message::EventList *)session->getReplicationData();
140
drizzled::message::Event *record= list->add_event();
142
record->set_type(Event::INSERT);
143
record->set_autocommit(true);
144
record->set_server_id("localhost");
145
record->set_query_id(10);
146
record->set_transaction_id("junk");
147
record->set_schema(session->db);
148
record->set_sql(session->query);
153
virtual bool row_update_hook(Session *session, Table *,
154
const unsigned char *,
155
const unsigned char *)
157
using namespace drizzled::message;
159
if (isEnabled == false)
162
drizzled::message::EventList *list= (drizzled::message::EventList *)session->getReplicationData();
163
drizzled::message::Event *record= list->add_event();
165
record->set_type(Event::UPDATE);
166
record->set_autocommit(true);
167
record->set_server_id("localhost");
168
record->set_query_id(10);
169
record->set_transaction_id("junk");
170
record->set_schema(session->db);
171
record->set_sql(session->query);
176
virtual bool row_delete_hook(Session *session, Table *)
178
using namespace drizzled::message;
180
if (isEnabled == false)
183
drizzled::message::EventList *list= (drizzled::message::EventList *)session->getReplicationData();
184
drizzled::message::Event *record= list->add_event();
186
record->set_type(Event::DELETE);
187
record->set_autocommit(true);
188
record->set_server_id("localhost");
189
record->set_query_id(10);
190
record->set_transaction_id("junk");
191
record->set_schema(session->db);
192
record->set_sql(session->query);
197
virtual bool end_transaction_hook(Session *session,
198
bool autocommit, bool commit)
201
using namespace drizzled::message;
203
if (isEnabled == false)
206
cerr << "Got into end" <<endl;
208
drizzled::message::EventList *list= (drizzled::message::EventList *)session->getReplicationData();
209
drizzled::message::Event *record= list->add_event();
211
record->set_type(Event::DELETE);
212
record->set_autocommit(true);
213
record->set_server_id("localhost");
214
record->set_query_id(10);
215
record->set_transaction_id("junk");
216
record->set_schema(session->db);
221
record->set_sql("COMMIT");
223
record->set_sql("AUTOCOMMIT");
226
record->set_sql("ROLLBACK");
228
error= write_to_disk(log_file, list);
230
session->setReplicationData(NULL);
237
static int init(void *p)
239
Replicator **repl = static_cast<Replicator **>(p);
241
*repl= new Protobuf_replicator();
246
static int deinit(void *p)
248
Protobuf_replicator *repl = static_cast<Protobuf_replicator *>(p);
255
static DRIZZLE_SYSVAR_BOOL(
259
N_("Enable Replicator"),
260
NULL, /* check func */
261
NULL, /* update func */
262
false /* default */);
264
static DRIZZLE_SYSVAR_STR(
268
N_("Directory to place replication logs."),
269
NULL, /* check func */
270
NULL, /* update func*/
273
static struct st_mysql_sys_var* system_variables[]= {
274
DRIZZLE_SYSVAR(directory),
275
DRIZZLE_SYSVAR(enabled),
279
drizzle_declare_plugin(protobuf_replicator)
281
DRIZZLE_REPLICATOR_PLUGIN,
285
"Basic replication module",
287
init, /* Plugin Init */
288
deinit, /* Plugin Deinit */
289
NULL, /* status variables */
290
system_variables, /* system variables */
291
NULL /* config options */
293
drizzle_declare_plugin_end;