38
static void writeCommit(message::Command &record)
40
record.set_type(message::Command::COMMIT);
41
record.set_timestamp(getNanoTimestamp());
43
message::TransactionContext *trx= record.mutable_transaction_context();
44
trx->set_server_id(server_id);
45
trx->set_transaction_id(transaction_id);
49
static void writeRollback(message::Command &record)
51
record.set_type(message::Command::ROLLBACK);
52
record.set_timestamp(getNanoTimestamp());
54
message::TransactionContext *trx= record.mutable_transaction_context();
55
trx->set_server_id(server_id);
56
trx->set_transaction_id(transaction_id);
60
static void writeStartTransaction(message::Command &record)
62
record.set_type(message::Command::START_TRANSACTION);
63
record.set_timestamp(getNanoTimestamp());
65
message::TransactionContext *trx= record.mutable_transaction_context();
66
trx->set_server_id(server_id);
67
trx->set_transaction_id(transaction_id);
70
static void writeInsert(message::Command &record)
72
record.set_type(message::Command::INSERT);
73
record.set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
74
record.set_timestamp(getNanoTimestamp());
75
record.set_schema("test");
76
record.set_table("t1");
78
message::TransactionContext *trx= record.mutable_transaction_context();
79
trx->set_server_id(server_id);
80
trx->set_transaction_id(transaction_id);
82
message::InsertRecord *irecord= record.mutable_insert_record();
84
/* Add Fields and Values... */
86
message::Table::Field *field= irecord->add_insert_field();
88
field->set_type(message::Table::Field::VARCHAR);
90
irecord->add_insert_value("1");
91
irecord->add_insert_value("2");
94
static void writeDeleteWithPK(message::Command &record)
96
record.set_type(message::Command::DELETE);
97
record.set_sql("DELETE FROM t1 WHERE a = 1");
98
record.set_timestamp(getNanoTimestamp());
99
record.set_schema("test");
100
record.set_table("t1");
102
message::TransactionContext *trx= record.mutable_transaction_context();
103
trx->set_server_id(server_id);
104
trx->set_transaction_id(transaction_id);
106
message::DeleteRecord *drecord= record.mutable_delete_record();
108
message::Table::Field *field= drecord->add_where_field();
109
field->set_name("a");
110
field->set_type(message::Table::Field::VARCHAR);
112
drecord->add_where_value("1");
115
static void writeUpdateWithPK(message::Command &record)
117
record.set_type(message::Command::UPDATE);
118
record.set_sql("UPDATE t1 SET a = 5 WHERE a = 1;");
119
record.set_timestamp(getNanoTimestamp());
120
record.set_schema("test");
121
record.set_table("t1");
123
message::TransactionContext *trx= record.mutable_transaction_context();
124
trx->set_server_id(server_id);
125
trx->set_transaction_id(transaction_id);
127
message::UpdateRecord *urecord= record.mutable_update_record();
129
message::Table::Field *field;
131
field= urecord->add_update_field();
132
field->set_name("a");
133
field->set_type(message::Table::Field::VARCHAR);
135
urecord->add_after_value("5");
137
field= urecord->add_where_field();
138
field->set_name("a");
139
field->set_type(message::Table::Field::VARCHAR);
141
urecord->add_where_value("1");
144
static void writeTransaction(int file, message::Transaction &transaction)
150
message::TransactionContext *trx= transaction.mutable_transaction_context();
151
trx->set_server_id(server_id);
152
trx->set_transaction_id(transaction_id);
63
static void initTransactionContext(message::Transaction &transaction)
65
message::TransactionContext *ctx= transaction.mutable_transaction_context();
66
ctx->set_transaction_id(transaction_id++);
67
ctx->set_start_timestamp(getNanoTimestamp());
68
ctx->set_server_id(server_id);
71
static void finalizeTransactionContext(message::Transaction &transaction)
73
message::TransactionContext *ctx= transaction.mutable_transaction_context();
74
ctx->set_end_timestamp(getNanoTimestamp());
77
static void doCreateTable1(message::Transaction &transaction)
79
message::Statement *statement= transaction.add_statement();
81
statement->set_type(message::Statement::RAW_SQL);
82
statement->set_sql("CREATE TABLE t1 (a VARCHAR(32) NOT NULL, PRIMARY KEY a) ENGINE=InnoDB");
83
statement->set_start_timestamp(getNanoTimestamp());
84
statement->set_end_timestamp(getNanoTimestamp());
87
static void doCreateTable2(message::Transaction &transaction)
89
message::Statement *statement= transaction.add_statement();
91
statement->set_type(message::Statement::RAW_SQL);
92
statement->set_sql("CREATE TABLE t2 (a INTEGER NOT NULL, PRIMARY KEY a) ENGINE=InnoDB");
93
statement->set_start_timestamp(getNanoTimestamp());
94
statement->set_end_timestamp(getNanoTimestamp());
97
static void doSimpleInsert(message::Transaction &transaction)
99
message::Statement *statement= transaction.add_statement();
101
/* Do generic Statement setup */
102
statement->set_type(message::Statement::INSERT);
103
statement->set_sql("INSERT INTO t1 (a) VALUES (\"1\"), (\"2\")");
104
statement->set_start_timestamp(getNanoTimestamp());
106
/* Do INSERT-specific header and setup */
107
message::InsertHeader *header= statement->mutable_insert_header();
109
/* Add table and field metadata for the statement */
110
message::TableMetadata *t_meta= header->mutable_table_metadata();
111
t_meta->set_schema_name("test");
112
t_meta->set_table_name("t1");
114
message::FieldMetadata *f_meta= header->add_field_metadata();
115
f_meta->set_name("a");
116
f_meta->set_type(message::Table::Field::VARCHAR);
118
/* Add new values... */
119
message::InsertData *data= statement->mutable_insert_data();
120
data->set_segment_id(1);
121
data->set_end_segment(true);
123
message::InsertRecord *record1= data->add_record();
124
message::InsertRecord *record2= data->add_record();
126
record1->add_insert_value("1");
127
record2->add_insert_value("2");
129
statement->set_end_timestamp(getNanoTimestamp());
132
static void doNonVarcharInsert(message::Transaction &transaction)
134
message::Statement *statement= transaction.add_statement();
136
/* Do generic Statement setup */
137
statement->set_type(message::Statement::INSERT);
138
statement->set_sql("INSERT INTO t2 (a) VALUES (1), (2)");
139
statement->set_start_timestamp(getNanoTimestamp());
141
/* Do INSERT-specific header and setup */
142
message::InsertHeader *header= statement->mutable_insert_header();
144
/* Add table and field metadata for the statement */
145
message::TableMetadata *t_meta= header->mutable_table_metadata();
146
t_meta->set_schema_name("test");
147
t_meta->set_table_name("t2");
149
message::FieldMetadata *f_meta= header->add_field_metadata();
150
f_meta->set_name("a");
151
f_meta->set_type(message::Table::Field::INTEGER);
153
/* Add new values... */
154
message::InsertData *data= statement->mutable_insert_data();
155
data->set_segment_id(1);
156
data->set_end_segment(true);
158
message::InsertRecord *record1= data->add_record();
159
message::InsertRecord *record2= data->add_record();
161
record1->add_insert_value("1");
162
record2->add_insert_value("2");
164
statement->set_end_timestamp(getNanoTimestamp());
167
static void doSimpleDelete(message::Transaction &transaction)
169
message::Statement *statement= transaction.add_statement();
171
/* Do generic Statement setup */
172
statement->set_type(message::Statement::DELETE);
173
statement->set_sql("DELETE FROM t1 WHERE a = \"1\"");
174
statement->set_start_timestamp(getNanoTimestamp());
176
/* Do DELETE-specific header and setup */
177
message::DeleteHeader *header= statement->mutable_delete_header();
179
/* Add table and field metadata for the statement */
180
message::TableMetadata *t_meta= header->mutable_table_metadata();
181
t_meta->set_schema_name("test");
182
t_meta->set_table_name("t1");
184
message::FieldMetadata *f_meta= header->add_key_field_metadata();
185
f_meta->set_name("a");
186
f_meta->set_type(message::Table::Field::VARCHAR);
188
/* Add new values... */
189
message::DeleteData *data= statement->mutable_delete_data();
190
data->set_segment_id(1);
191
data->set_end_segment(true);
193
message::DeleteRecord *record1= data->add_record();
195
record1->add_key_value("1");
197
statement->set_end_timestamp(getNanoTimestamp());
200
static void doSimpleUpdate(message::Transaction &transaction)
202
message::Statement *statement= transaction.add_statement();
204
/* Do generic Statement setup */
205
statement->set_type(message::Statement::UPDATE);
206
statement->set_sql("UPDATE t1 SET a = \"5\" WHERE a = \"1\"");
207
statement->set_start_timestamp(getNanoTimestamp());
209
/* Do UPDATE-specific header and setup */
210
message::UpdateHeader *header= statement->mutable_update_header();
212
/* Add table and field metadata for the statement */
213
message::TableMetadata *t_meta= header->mutable_table_metadata();
214
t_meta->set_schema_name("test");
215
t_meta->set_table_name("t1");
217
message::FieldMetadata *kf_meta= header->add_key_field_metadata();
218
kf_meta->set_name("a");
219
kf_meta->set_type(message::Table::Field::VARCHAR);
221
message::FieldMetadata *sf_meta= header->add_set_field_metadata();
222
sf_meta->set_name("a");
223
sf_meta->set_type(message::Table::Field::VARCHAR);
225
header->add_set_value("5");
227
/* Add new values... */
228
message::UpdateData *data= statement->mutable_update_data();
229
data->set_segment_id(1);
230
data->set_end_segment(true);
232
message::UpdateRecord *record1= data->add_record();
234
record1->add_key_value("1");
236
statement->set_end_timestamp(getNanoTimestamp());
239
static void doMultiKeyUpdate(message::Transaction &transaction)
241
message::Statement *statement= transaction.add_statement();
243
/* Do generic Statement setup */
244
statement->set_type(message::Statement::UPDATE);
245
statement->set_sql("UPDATE t1 SET a = \"5\"");
246
statement->set_start_timestamp(getNanoTimestamp());
248
/* Do UPDATE-specific header and setup */
249
message::UpdateHeader *header= statement->mutable_update_header();
251
/* Add table and field metadata for the statement */
252
message::TableMetadata *t_meta= header->mutable_table_metadata();
253
t_meta->set_schema_name("test");
254
t_meta->set_table_name("t1");
256
message::FieldMetadata *kf_meta= header->add_key_field_metadata();
257
kf_meta->set_name("a");
258
kf_meta->set_type(message::Table::Field::VARCHAR);
260
message::FieldMetadata *sf_meta= header->add_set_field_metadata();
261
sf_meta->set_name("a");
262
sf_meta->set_type(message::Table::Field::VARCHAR);
264
header->add_set_value("5");
266
/* Add new values... */
267
message::UpdateData *data= statement->mutable_update_data();
268
data->set_segment_id(1);
269
data->set_end_segment(true);
271
message::UpdateRecord *record1= data->add_record();
272
message::UpdateRecord *record2= data->add_record();
274
record1->add_key_value("1");
275
record2->add_key_value("2");
278
statement->set_end_timestamp(getNanoTimestamp());
281
static void writeTransaction(protobuf::io::CodedOutputStream *output, message::Transaction &transaction)
283
std::string buffer("");
284
finalizeTransactionContext(transaction);
154
285
transaction.SerializeToString(&buffer);
156
length= buffer.length();
158
cout << "Writing transaction of " << length << " length." << endl;
160
if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
162
cerr << "Only wrote " << written << " out of " << length << "." << endl;
166
if ((written= write(file, buffer.c_str(), length)) != length)
168
cerr << "Only wrote " << written << " out of " << length << "." << endl;
287
size_t length= buffer.length();
289
output->WriteLittleEndian64(static_cast<uint64_t>(length));
290
output->WriteString(buffer);
173
293
int main(int argc, char* argv[])
180
cerr << "Usage: " << argv[0] << " TRANSACTION_LOG" << endl;
300
fprintf(stderr, _("Usage: %s TRANSACTION_LOG\n"), argv[0]);
184
304
if ((file= open(argv[1], O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
186
cerr << "Can not open file: " << argv[0] << endl;
306
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
190
/* Write a series of statements which test each type of record class */
310
protobuf::io::ZeroCopyOutputStream *raw_output= new protobuf::io::FileOutputStream(file);
311
protobuf::io::CodedOutputStream *coded_output= new protobuf::io::CodedOutputStream(raw_output);
313
/* Write a series of statements which test each type of Statement */
314
message::Transaction transaction;
316
/* Simple CREATE TABLE statements as raw sql */
317
initTransactionContext(transaction);
318
doCreateTable1(transaction);
319
writeTransaction(coded_output, transaction);
322
initTransactionContext(transaction);
323
doCreateTable2(transaction);
324
writeTransaction(coded_output, transaction);
193
327
/* Simple INSERT statement */
194
message::Transaction transaction;
195
transaction.set_start_timestamp(getNanoTimestamp());
196
writeStartTransaction(*transaction.add_command());
197
writeInsert(*transaction.add_command());
198
writeCommit(*transaction.add_command());
199
transaction.set_end_timestamp(getNanoTimestamp());
201
writeTransaction(file, transaction);
328
initTransactionContext(transaction);
329
doSimpleInsert(transaction);
330
writeTransaction(coded_output, transaction);
203
331
transaction.Clear();
205
333
/* Write a DELETE and an UPDATE in one transaction */
207
transaction.set_start_timestamp(getNanoTimestamp());
208
writeStartTransaction(*transaction.add_command());
209
writeDeleteWithPK(*transaction.add_command());
210
writeUpdateWithPK(*transaction.add_command());
211
writeCommit(*transaction.add_command());
212
transaction.set_end_timestamp(getNanoTimestamp());
214
writeTransaction(file, transaction);
334
initTransactionContext(transaction);
335
doSimpleDelete(transaction);
336
doSimpleUpdate(transaction);
337
writeTransaction(coded_output, transaction);
340
/* Test an INSERT into non-varchar columns */
341
initTransactionContext(transaction);
342
doNonVarcharInsert(transaction);
343
writeTransaction(coded_output, transaction);
346
/* Write an UPDATE which affects >1 row */
347
initTransactionContext(transaction);
348
doMultiKeyUpdate(transaction);
349
writeTransaction(coded_output, transaction);