~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems
 *
 *  Authors:
 *
 *    Jay Pipes <joinfu@sun.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "drizzled/hash/crc32.h"
#include "drizzled/gettext.h"
#include "drizzled/replication_services.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <fstream>
#include <unistd.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <drizzled/message/transaction.pb.h>

#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

#include "drizzled/gettext.h"

/** 
 * @file Example script for writing transactions to a log file.
 */

using namespace std;
using namespace drizzled;
using namespace google;

static uint32_t server_id= 1;
static uint64_t transaction_id= 1;

static uint64_t getNanoTimestamp()
{
#ifdef HAVE_CLOCK_GETTIME
  struct timespec tp;
  clock_gettime(CLOCK_REALTIME, &tp);
  return (uint64_t) tp.tv_sec * 10000000
       + (uint64_t) tp.tv_nsec;
#else
  struct timeval tv;
  gettimeofday(&tv,NULL);
  return (uint64_t) tv.tv_sec * 10000000
       + (uint64_t) tv.tv_usec * 1000;
#endif
}

static void initTransactionContext(message::Transaction &transaction)
{
  message::TransactionContext *ctx= transaction.mutable_transaction_context();
  ctx->set_transaction_id(transaction_id++);
  ctx->set_start_timestamp(getNanoTimestamp());
  ctx->set_server_id(server_id);
}

static void finalizeTransactionContext(message::Transaction &transaction)
{
  message::TransactionContext *ctx= transaction.mutable_transaction_context();
  ctx->set_end_timestamp(getNanoTimestamp());
}

static void doCreateTable1(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  statement->set_type(message::Statement::RAW_SQL);
  statement->set_sql("CREATE TABLE t1 (a VARCHAR(32) NOT NULL, PRIMARY KEY a) ENGINE=InnoDB");
  statement->set_start_timestamp(getNanoTimestamp());
  statement->set_end_timestamp(getNanoTimestamp());
}

static void doCreateTable2(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  statement->set_type(message::Statement::RAW_SQL);
  statement->set_sql("CREATE TABLE t2 (a INTEGER NOT NULL, PRIMARY KEY a) ENGINE=InnoDB");
  statement->set_start_timestamp(getNanoTimestamp());
  statement->set_end_timestamp(getNanoTimestamp());
}

static void doCreateTable3(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  statement->set_type(message::Statement::RAW_SQL);
  statement->set_sql("CREATE TABLE t3 (a INTEGER NOT NULL, b BLOB NOT NULL, PRIMARY KEY a) ENGINE=InnoDB");
  statement->set_start_timestamp(getNanoTimestamp());
  statement->set_end_timestamp(getNanoTimestamp());
}

static void doSimpleInsert(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  /* Do generic Statement setup */
  statement->set_type(message::Statement::INSERT);
  statement->set_sql("INSERT INTO t1 (a) VALUES (\"1\"), (\"2\")");
  statement->set_start_timestamp(getNanoTimestamp());

  /* Do INSERT-specific header and setup */
  message::InsertHeader *header= statement->mutable_insert_header();

  /* Add table and field metadata for the statement */
  message::TableMetadata *t_meta= header->mutable_table_metadata();
  t_meta->set_schema_name("test");
  t_meta->set_table_name("t1");

  message::FieldMetadata *f_meta= header->add_field_metadata();
  f_meta->set_name("a");
  f_meta->set_type(message::Table::Field::VARCHAR);

  /* Add new values... */
  message::InsertData *data= statement->mutable_insert_data();
  data->set_segment_id(1);
  data->set_end_segment(true);

  message::InsertRecord *record1= data->add_record();
  message::InsertRecord *record2= data->add_record();

  record1->add_insert_value("1");
  record2->add_insert_value("2");

  statement->set_end_timestamp(getNanoTimestamp());
}

static void doNonVarcharInsert(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  /* Do generic Statement setup */
  statement->set_type(message::Statement::INSERT);
  statement->set_sql("INSERT INTO t2 (a) VALUES (1), (2)");
  statement->set_start_timestamp(getNanoTimestamp());

  /* Do INSERT-specific header and setup */
  message::InsertHeader *header= statement->mutable_insert_header();

  /* Add table and field metadata for the statement */
  message::TableMetadata *t_meta= header->mutable_table_metadata();
  t_meta->set_schema_name("test");
  t_meta->set_table_name("t2");

  message::FieldMetadata *f_meta= header->add_field_metadata();
  f_meta->set_name("a");
  f_meta->set_type(message::Table::Field::INTEGER);

  /* Add new values... */
  message::InsertData *data= statement->mutable_insert_data();
  data->set_segment_id(1);
  data->set_end_segment(true);

  message::InsertRecord *record1= data->add_record();
  message::InsertRecord *record2= data->add_record();

  record1->add_insert_value("1");
  record2->add_insert_value("2");

  statement->set_end_timestamp(getNanoTimestamp());
}

static void doBlobInsert(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  /* Do generic Statement setup */
  statement->set_type(message::Statement::INSERT);
  statement->set_sql("INSERT INTO t3 (a, b) VALUES (1, 'test\0me')", 43); /* 43 == length including \0 */
  statement->set_start_timestamp(getNanoTimestamp());

  /* Do INSERT-specific header and setup */
  message::InsertHeader *header= statement->mutable_insert_header();

  /* Add table and field metadata for the statement */
  message::TableMetadata *t_meta= header->mutable_table_metadata();
  t_meta->set_schema_name("test");
  t_meta->set_table_name("t3");

  message::FieldMetadata *f_meta= header->add_field_metadata();
  f_meta->set_name("a");
  f_meta->set_type(message::Table::Field::INTEGER);

  f_meta= header->add_field_metadata();
  f_meta->set_name("b");
  f_meta->set_type(message::Table::Field::BLOB);

  /* Add new values... */
  message::InsertData *data= statement->mutable_insert_data();
  data->set_segment_id(1);
  data->set_end_segment(true);

  message::InsertRecord *record1= data->add_record();

  record1->add_insert_value("1");
  record1->add_insert_value("test\0me", 7); /* 7 == length including \0 */

  statement->set_end_timestamp(getNanoTimestamp());
}

static void doSimpleDelete(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  /* Do generic Statement setup */
  statement->set_type(message::Statement::DELETE);
  statement->set_sql("DELETE FROM t1 WHERE a = \"1\"");
  statement->set_start_timestamp(getNanoTimestamp());

  /* Do DELETE-specific header and setup */
  message::DeleteHeader *header= statement->mutable_delete_header();

  /* Add table and field metadata for the statement */
  message::TableMetadata *t_meta= header->mutable_table_metadata();
  t_meta->set_schema_name("test");
  t_meta->set_table_name("t1");

  message::FieldMetadata *f_meta= header->add_key_field_metadata();
  f_meta->set_name("a");
  f_meta->set_type(message::Table::Field::VARCHAR);

  /* Add new values... */
  message::DeleteData *data= statement->mutable_delete_data();
  data->set_segment_id(1);
  data->set_end_segment(true);

  message::DeleteRecord *record1= data->add_record();

  record1->add_key_value("1");

  statement->set_end_timestamp(getNanoTimestamp());
}

static void doSimpleUpdate(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  /* Do generic Statement setup */
  statement->set_type(message::Statement::UPDATE);
  statement->set_sql("UPDATE t1 SET a = \"5\" WHERE a = \"1\"");
  statement->set_start_timestamp(getNanoTimestamp());

  /* Do UPDATE-specific header and setup */
  message::UpdateHeader *header= statement->mutable_update_header();

  /* Add table and field metadata for the statement */
  message::TableMetadata *t_meta= header->mutable_table_metadata();
  t_meta->set_schema_name("test");
  t_meta->set_table_name("t1");

  message::FieldMetadata *kf_meta= header->add_key_field_metadata();
  kf_meta->set_name("a");
  kf_meta->set_type(message::Table::Field::VARCHAR);

  message::FieldMetadata *sf_meta= header->add_set_field_metadata();
  sf_meta->set_name("a");
  sf_meta->set_type(message::Table::Field::VARCHAR);

  /* Add new values... */
  message::UpdateData *data= statement->mutable_update_data();
  data->set_segment_id(1);
  data->set_end_segment(true);

  message::UpdateRecord *record1= data->add_record();

  record1->add_after_value("5");
  record1->add_key_value("1");

  statement->set_end_timestamp(getNanoTimestamp());
}

static void doMultiKeyUpdate(message::Transaction &transaction)
{
  message::Statement *statement= transaction.add_statement();

  /* Do generic Statement setup */
  statement->set_type(message::Statement::UPDATE);
  statement->set_sql("UPDATE t1 SET a = \"5\"");
  statement->set_start_timestamp(getNanoTimestamp());

  /* Do UPDATE-specific header and setup */
  message::UpdateHeader *header= statement->mutable_update_header();

  /* Add table and field metadata for the statement */
  message::TableMetadata *t_meta= header->mutable_table_metadata();
  t_meta->set_schema_name("test");
  t_meta->set_table_name("t1");

  message::FieldMetadata *kf_meta= header->add_key_field_metadata();
  kf_meta->set_name("a");
  kf_meta->set_type(message::Table::Field::VARCHAR);

  message::FieldMetadata *sf_meta= header->add_set_field_metadata();
  sf_meta->set_name("a");
  sf_meta->set_type(message::Table::Field::VARCHAR);

  /* Add new values... */
  message::UpdateData *data= statement->mutable_update_data();
  data->set_segment_id(1);
  data->set_end_segment(true);

  message::UpdateRecord *record1= data->add_record();
  message::UpdateRecord *record2= data->add_record();

  record1->add_after_value("5");
  record1->add_key_value("1");
  record2->add_after_value("5");
  record2->add_key_value("2");

  statement->set_end_timestamp(getNanoTimestamp());
}

static void writeTransaction(protobuf::io::CodedOutputStream *output, message::Transaction &transaction)
{
  std::string buffer("");
  finalizeTransactionContext(transaction);
  transaction.SerializeToString(&buffer);

  size_t length= buffer.length();

  output->WriteLittleEndian32(static_cast<uint32_t>(ReplicationServices::TRANSACTION));
  output->WriteLittleEndian32(static_cast<uint32_t>(length));
  output->WriteString(buffer);
  output->WriteLittleEndian32(drizzled::hash::crc32(buffer.c_str(), length)); /* checksum */
}

int main(int argc, char* argv[])
{
  GOOGLE_PROTOBUF_VERIFY_VERSION;
  int file;

  if (argc != 2) 
  {
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG\n"), argv[0]);
    return -1;
  }

  if ((file= open(argv[1], O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
  {
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
    return -1;
  }

  protobuf::io::ZeroCopyOutputStream *raw_output= new protobuf::io::FileOutputStream(file);
  protobuf::io::CodedOutputStream *coded_output= new protobuf::io::CodedOutputStream(raw_output);

  /* Write a series of statements which test each type of Statement */
  message::Transaction transaction;

  /* Simple CREATE TABLE statements as raw sql */
  initTransactionContext(transaction);
  doCreateTable1(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  initTransactionContext(transaction);
  doCreateTable2(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  /* Simple INSERT statement */
  initTransactionContext(transaction);
  doSimpleInsert(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  /* Write a DELETE and an UPDATE in one transaction */
  initTransactionContext(transaction);
  doSimpleDelete(transaction);
  doSimpleUpdate(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  /* Test an INSERT into non-varchar columns */
  initTransactionContext(transaction);
  doNonVarcharInsert(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  /* Write an UPDATE which affects >1 row */
  initTransactionContext(transaction);
  doMultiKeyUpdate(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  /* Write an INSERT which writes BLOB data */
  initTransactionContext(transaction);
  doCreateTable3(transaction);
  doBlobInsert(transaction);
  writeTransaction(coded_output, transaction);
  transaction.Clear();

  delete coded_output;
  delete raw_output;

  return 0;
}