5
5
#include <sys/stat.h>
10
#include <drizzled/message/replication_event.pb.h>
11
#include <drizzled/message/transaction.pb.h>
14
* @file Example script for writing transactions to a log file.
12
17
using namespace std;
13
18
using namespace drizzled::message;
15
static uint64_t query_id= 0;
16
char transaction_id[37];
19
Example script for reader a Drizzle master replication list.
22
void write_ddl(::drizzled::message::Event *record, const char *sql)
26
uuid_generate_time(uu);
27
uuid_unparse(uu, transaction_id);
29
record->set_type(Event::DDL);
30
record->set_autocommit(true);
31
record->set_server_id("localhost");
32
record->set_query_id(query_id++);
33
record->set_transaction_id(transaction_id);
34
record->set_schema("test");
38
void write_insert(::drizzled::message::Event *record, const char *trx)
42
record->set_type(Event::INSERT);
43
record->set_autocommit(true);
44
record->set_server_id("localhost");
45
record->set_query_id(query_id++);
46
record->set_transaction_id(trx);
47
record->set_schema("test");
48
record->set_table("t1");
49
record->set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
52
record->add_field_names("a");
54
/* Add values (first row) */
55
value= record->add_values();
58
/* Add values (second row) */
59
value= record->add_values();
63
void write_delete(::drizzled::message::Event *record, const char *trx)
67
record->set_type(Event::DELETE);
68
record->set_autocommit(true);
69
record->set_server_id("localhost");
70
record->set_query_id(query_id++);
71
record->set_transaction_id(trx);
72
record->set_schema("test");
73
record->set_table("t1");
74
record->set_sql("DELETE FROM t1 WHERE a IN (1, 2)");
77
record->set_primary_key("a");
79
/* Add values for IN() */
80
value= record->add_values();
85
void write_update(::drizzled::message::Event *record, const char *trx)
89
record->set_type(Event::UPDATE);
90
record->set_autocommit(true);
91
record->set_server_id("localhost");
92
record->set_query_id(query_id++);
93
record->set_transaction_id(trx);
94
record->set_schema("test");
95
record->set_table("t1");
96
record->set_sql("UPDATE t1 SET a=5 WHERE a = 1 ");
97
record->set_primary_key("a");
100
record->add_field_names("a");
102
/* Add values (first row) */
103
value= record->add_values();
104
value->add_val("1"); // The first value is always the primary key comparison value
107
/* Add values (second row) */
108
value= record->add_values();
113
void write_to_disk(int file, ::drizzled::message::EventList *list)
20
static uint32_t server_id= 1;
21
static uint64_t transaction_id= 0;
23
uint64_t getNanoTimestamp()
25
#ifdef HAVE_CLOCK_GETTIME
27
clock_gettime(CLOCK_REALTIME, &tp);
28
return (uint64_t) tp.tv_sec * 10000000
29
+ (uint64_t) tp.tv_nsec;
32
gettimeofday(&tv,NULL);
33
return (uint64_t) tv.tv_sec * 10000000
34
+ (uint64_t) tv.tv_usec * 1000;
38
void writeCommit(drizzled::message::Command &record)
40
record.set_type(Command::COMMIT);
41
record.set_timestamp(getNanoTimestamp());
43
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
44
trx->set_server_id(server_id);
45
trx->set_transaction_id(transaction_id);
48
void writeRollback(drizzled::message::Command &record)
50
record.set_type(Command::ROLLBACK);
51
record.set_timestamp(getNanoTimestamp());
53
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
54
trx->set_server_id(server_id);
55
trx->set_transaction_id(transaction_id);
58
void writeStartTransaction(drizzled::message::Command &record)
60
record.set_type(Command::START_TRANSACTION);
61
record.set_timestamp(getNanoTimestamp());
63
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
64
trx->set_server_id(server_id);
65
trx->set_transaction_id(transaction_id);
68
void writeInsert(drizzled::message::Command &record)
70
record.set_type(Command::INSERT);
71
record.set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
72
record.set_timestamp(getNanoTimestamp());
73
record.set_schema("test");
74
record.set_table("t1");
76
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
77
trx->set_server_id(server_id);
78
trx->set_transaction_id(transaction_id);
80
drizzled::message::InsertRecord *irecord= record.mutable_insert_record();
82
/* Add Fields and Values... */
84
Table::Field *field= irecord->add_insert_field();
86
field->set_type(drizzled::message::Table::Field::VARCHAR);
88
irecord->add_insert_value("1");
89
irecord->add_insert_value("2");
92
void writeDeleteWithPK(drizzled::message::Command &record)
94
record.set_type(Command::DELETE);
95
record.set_sql("DELETE FROM t1 WHERE a = 1");
96
record.set_timestamp(getNanoTimestamp());
97
record.set_schema("test");
98
record.set_table("t1");
100
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
101
trx->set_server_id(server_id);
102
trx->set_transaction_id(transaction_id);
104
drizzled::message::DeleteRecord *drecord= record.mutable_delete_record();
106
Table::Field *field= drecord->add_where_field();
107
field->set_name("a");
108
field->set_type(drizzled::message::Table::Field::VARCHAR);
110
drecord->add_where_value("1");
113
void writeUpdateWithPK(drizzled::message::Command &record)
115
record.set_type(Command::UPDATE);
116
record.set_sql("UPDATE t1 SET a = 5 WHERE a = 1;");
117
record.set_timestamp(getNanoTimestamp());
118
record.set_schema("test");
119
record.set_table("t1");
121
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
122
trx->set_server_id(server_id);
123
trx->set_transaction_id(transaction_id);
125
drizzled::message::UpdateRecord *urecord= record.mutable_update_record();
129
field= urecord->add_update_field();
130
field->set_name("a");
131
field->set_type(drizzled::message::Table::Field::VARCHAR);
133
urecord->add_after_value("5");
135
field= urecord->add_where_field();
136
field->set_name("a");
137
field->set_type(drizzled::message::Table::Field::VARCHAR);
139
urecord->add_where_value("1");
142
void writeTransaction(int file, drizzled::message::Transaction &transaction)
115
144
std::string buffer;
119
list->SerializePartialToString(&buffer);
148
drizzled::message::TransactionContext *trx= transaction.mutable_transaction_context();
149
trx->set_server_id(server_id);
150
trx->set_transaction_id(transaction_id);
152
transaction.SerializeToString(&buffer);
121
154
length= buffer.length();
123
cout << "Writing record of " << length << "." << endl;
156
cout << "Writing transaction of " << length << " length." << endl;
125
158
if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
158
/* Write first set of records */
159
write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
160
write_insert(list.add_event(), transaction_id);
162
write_to_disk(file, &list);
164
/* Write Second set of records */
165
write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
166
write_delete(list.add_event(), transaction_id);
167
write_update(list.add_event(), transaction_id);
169
write_to_disk(file, &list);
188
/* Write a series of statements which test each type of record class */
191
/* Simple INSERT statement */
192
Transaction transaction;
193
transaction.set_start_timestamp(getNanoTimestamp());
194
writeStartTransaction(*transaction.add_command());
195
writeInsert(*transaction.add_command());
196
writeCommit(*transaction.add_command());
197
transaction.set_end_timestamp(getNanoTimestamp());
199
writeTransaction(file, transaction);
203
/* Write a DELETE and an UPDATE in one transaction */
205
transaction.set_start_timestamp(getNanoTimestamp());
206
writeStartTransaction(*transaction.add_command());
207
writeDeleteWithPK(*transaction.add_command());
208
writeUpdateWithPK(*transaction.add_command());
209
writeCommit(*transaction.add_command());
210
transaction.set_end_timestamp(getNanoTimestamp());
212
writeTransaction(file, transaction);