~drizzle-trunk/drizzle/development

1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2009 Sun Microsystems
5
 *
6
 *  Authors:
7
 *
8
 *    Jay Pipes <joinfu@sun.com>
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; version 2 of the License.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, write to the Free Software
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 */
23
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
24
#include "config.h"
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <fcntl.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
28
#include <limits.h>
29
#include <cerrno>
636 by Brian Aker
First pass with new event API (yeah... it will be better).
30
#include <iostream>
31
#include <string>
1143.2.10 by Jay Pipes
Phase 2 new replication work:
32
#include <algorithm>
1143.2.7 by Jay Pipes
Based on IRC discussions with Brian, this patch reworks the Statement
33
#include <vector>
685.1.32 by Monty Taylor
Added missing header.
34
#include <unistd.h>
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
35
#include "drizzled/definitions.h"
36
#include "drizzled/gettext.h"
37
#include "drizzled/replication_services.h"
38
#include "drizzled/algorithm/crc32.h"
39
#include "drizzled/message/transaction.pb.h"
40
#include "drizzled/message/statement_transform.h"
41
#include "drizzled/message/transaction_manager.h"
42
#include "drizzled/util/convert.h"
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
43
44
#include <google/protobuf/io/coded_stream.h>
45
#include <google/protobuf/io/zero_copy_stream_impl.h>
779.3.18 by Monty Taylor
Cleaned up warnings up through innodb.
46
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
47
#include <boost/program_options.hpp>
48
636 by Brian Aker
First pass with new event API (yeah... it will be better).
49
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
50
using namespace google;
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
51
using namespace drizzled;
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
52
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
53
namespace po= boost::program_options;
54
1143.2.35 by Jay Pipes
Adds support for BLOB fields in the replication stream. Note that
55
static const char *replace_with_spaces= "\n\r";
56
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
57
static void printStatement(const message::Statement &statement)
58
{
1143.2.7 by Jay Pipes
Based on IRC discussions with Brian, this patch reworks the Statement
59
  vector<string> sql_strings;
60
1143.4.25 by Jay Pipes
Correctly puts START TRANSACTION; COMMIT; containers around group-related SQL statements in the transaction_reader and ensures that when a different type of Statement message is started in an existing Transaction message, that the active Statement message is finalized.
61
  message::transformStatementToSql(statement,
62
                                   sql_strings,
63
                                   message::DRIZZLE,
64
                                   true /* already in transaction */);
1143.2.7 by Jay Pipes
Based on IRC discussions with Brian, this patch reworks the Statement
65
1143.2.35 by Jay Pipes
Adds support for BLOB fields in the replication stream. Note that
66
  for (vector<string>::iterator sql_string_iter= sql_strings.begin();
67
       sql_string_iter != sql_strings.end();
68
       ++sql_string_iter)
1143.2.7 by Jay Pipes
Based on IRC discussions with Brian, this patch reworks the Statement
69
  {
70
    string &sql= *sql_string_iter;
1143.2.35 by Jay Pipes
Adds support for BLOB fields in the replication stream. Note that
71
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
72
    /*
73
     * Replace \n and \r with spaces so that SQL statements
74
     * are always on a single line
1143.2.7 by Jay Pipes
Based on IRC discussions with Brian, this patch reworks the Statement
75
     */
1143.2.35 by Jay Pipes
Adds support for BLOB fields in the replication stream. Note that
76
    {
77
      string::size_type found= sql.find_first_of(replace_with_spaces);
78
      while (found != string::npos)
79
      {
80
        sql[found]= ' ';
81
        found= sql.find_first_of(replace_with_spaces, found);
82
      }
83
    }
84
85
    /*
86
     * Embedded NUL characters are a pain in the ass.
87
     */
88
    {
89
      string::size_type found= sql.find_first_of('\0');
90
      while (found != string::npos)
91
      {
92
        sql[found]= '\\';
93
        sql.insert(found + 1, 1, '0');
94
        found= sql.find_first_of('\0', found);
95
      }
96
    }
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
97
1143.2.7 by Jay Pipes
Based on IRC discussions with Brian, this patch reworks the Statement
98
    cout << sql << ';' << endl;
99
  }
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
100
}
101
1719.3.1 by David Shrewsbury
Fix to capture INSERTs from a LOAD DATA command in the replication stream.
102
static bool isEndStatement(const message::Statement &statement)
103
{
104
  switch (statement.type())
105
  {
106
    case (message::Statement::INSERT):
107
    {
108
      const message::InsertData &data= statement.insert_data();
109
      if (not data.end_segment())
110
        return false;
111
      break;
112
    }
113
    case (message::Statement::UPDATE):
114
    {
115
      const message::UpdateData &data= statement.update_data();
116
      if (not data.end_segment())
117
        return false;
118
      break;
119
    }
120
    case (message::Statement::DELETE):
121
    {
122
      const message::DeleteData &data= statement.delete_data();
123
      if (not data.end_segment())
124
        return false;
125
      break;
126
    }
127
    default:
128
      return true;
129
  }
130
  return true;
131
}
132
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
133
static bool isEndTransaction(const message::Transaction &transaction)
134
{
135
  const message::TransactionContext trx= transaction.transaction_context();
136
137
  size_t num_statements= transaction.statement_size();
138
139
  /*
140
   * If any Statement is partial, then we can expect another Transaction
141
   * message.
142
   */
143
  for (size_t x= 0; x < num_statements; ++x)
144
  {
145
    const message::Statement &statement= transaction.statement(x);
146
147
    if (not isEndStatement(statement))
148
      return false;
149
  }
150
151
  return true;
152
}
153
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
154
static void printEvent(const message::Event &event)
155
{
156
  switch (event.type())
157
  {
158
    case message::Event::STARTUP:
159
    {
160
      cout << "-- EVENT: Server startup\n";
161
      break;
162
    }
163
    case message::Event::SHUTDOWN:
164
    {
165
      cout << "-- EVENT: Server shutdown\n";
166
      break;
167
    }
168
    default:
169
    {
170
      cout << "-- EVENT: Unknown event\n";
171
      break;
172
    }
173
  }
174
}
175
176
static void printTransaction(const message::Transaction &transaction,
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
177
                             bool ignore_events,
178
                             bool print_as_raw)
636 by Brian Aker
First pass with new event API (yeah... it will be better).
179
{
1719.3.1 by David Shrewsbury
Fix to capture INSERTs from a LOAD DATA command in the replication stream.
180
  static uint64_t last_trx_id= 0;
181
  bool should_commit= true;
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
182
  const message::TransactionContext trx= transaction.transaction_context();
183
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
184
  /*
185
   * First check to see if this is an event message.
186
   */
187
  if (transaction.has_event())
188
  {
189
    last_trx_id= trx.transaction_id();
190
    if (not ignore_events)
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
191
    {
192
      if (print_as_raw)
193
        transaction.PrintDebugString();
194
      else
195
        printEvent(transaction.event());
196
    }
197
    return;
198
  }
199
200
  if (print_as_raw)
201
  {
202
    transaction.PrintDebugString();
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
203
    return;
204
  }
205
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
206
  size_t num_statements= transaction.statement_size();
207
  size_t x;
208
1719.3.1 by David Shrewsbury
Fix to capture INSERTs from a LOAD DATA command in the replication stream.
209
  /*
210
   * One way to determine when a new transaction begins is when the
1780.3.3 by David Shrewsbury
Replace std::map with boost::unordered_map
211
   * transaction id changes (if all transactions have their GPB messages
212
   * grouped together, which this program will). We check that here.
1719.3.1 by David Shrewsbury
Fix to capture INSERTs from a LOAD DATA command in the replication stream.
213
   */
214
  if (trx.transaction_id() != last_trx_id)
215
    cout << "START TRANSACTION;" << endl;
216
217
  last_trx_id= trx.transaction_id();
218
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
219
  for (x= 0; x < num_statements; ++x)
636 by Brian Aker
First pass with new event API (yeah... it will be better).
220
  {
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
221
    const message::Statement &statement= transaction.statement(x);
1719.3.1 by David Shrewsbury
Fix to capture INSERTs from a LOAD DATA command in the replication stream.
222
223
    if (should_commit)
224
      should_commit= isEndStatement(statement);
225
1820.2.1 by David Shrewsbury
Fix for transaction_reader to output ROLLBACK statements
226
    /* A ROLLBACK would be the only Statement within the Transaction
227
     * since all other Statements will have been deleted from the
228
     * Transaction message, so we should fall out of this loop immediately.
229
     * We don't want to issue an unnecessary COMMIT, so we change
230
     * should_commit to false here.
231
     */
232
    if (statement.type() == message::Statement::ROLLBACK)
233
      should_commit= false;
234
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
235
    printStatement(statement);
636 by Brian Aker
First pass with new event API (yeah... it will be better).
236
  }
1719.3.1 by David Shrewsbury
Fix to capture INSERTs from a LOAD DATA command in the replication stream.
237
238
  /*
239
   * If ALL Statements are end segments, we can commit this Transaction.
240
   * We can also check to see if the transaction_id changed, but this
241
   * wouldn't work for the last Transaction in the transaction log since
242
   * we don't have another Transaction to compare to. Checking for all
243
   * end segments (like we do above) covers this case.
244
   */
245
  if (should_commit)
246
    cout << "COMMIT;" << endl;
636 by Brian Aker
First pass with new event API (yeah... it will be better).
247
}
248
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
249
int main(int argc, char* argv[])
636 by Brian Aker
First pass with new event API (yeah... it will be better).
250
{
251
  GOOGLE_PROTOBUF_VERIFY_VERSION;
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
252
  int opt_start_pos= 0;
253
  uint64_t opt_transaction_id= 0;
636 by Brian Aker
First pass with new event API (yeah... it will be better).
254
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
255
  /*
256
   * Setup program options
257
   */
258
  po::options_description desc("Program options");
259
  desc.add_options()
260
    ("help", N_("Display help and exit"))
261
    ("checksum", N_("Perform checksum"))
262
    ("ignore-events", N_("Ignore event messages"))
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
263
    ("input-file", po::value< vector<string> >(), N_("Transaction log file"))
264
    ("raw", N_("Print raw Protobuf messages instead of SQL"))
265
    ("start-pos",
266
      po::value<int>(&opt_start_pos),
267
      N_("Start reading from the given file position"))
268
    ("transaction-id",
269
      po::value<uint64_t>(&opt_transaction_id),
270
      N_("Only output for the given transaction ID"));
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
271
272
  /*
273
   * We allow one positional argument that will be transaction file name
274
   */
275
  po::positional_options_description pos;
276
  pos.add("input-file", 1);
277
278
  /*
279
   * Parse the program options
280
   */
281
  po::variables_map vm;
282
  po::store(po::command_line_parser(argc, argv).
283
            options(desc).positional(pos).run(), vm);
284
  po::notify(vm);
285
286
  /*
287
   * If the help option was given, or not input file was supplied,
288
   * print out usage information.
289
   */
290
  if (vm.count("help") || not vm.count("input-file"))
291
  {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
292
    cerr << desc << endl;
293
    return -1;
294
  }
295
296
  /*
297
   * Specifying both a transaction ID and a start position
298
   * is not logical.
299
   */
300
  if (vm.count("start-pos") && vm.count("transaction-id"))
301
  {
302
    cerr << _("Cannot use --start-pos and --transaction-id together\n");
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
303
    return -1;
304
  }
305
306
  bool do_checksum= vm.count("checksum") ? true : false;
307
  bool ignore_events= vm.count("ignore-events") ? true : false;
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
308
  bool print_as_raw= vm.count("raw") ? true : false;
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
309
310
  string filename= vm["input-file"].as< vector<string> >()[0];
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
311
  int file= open(filename.c_str(), O_RDONLY);
1819.4.1 by David Shrewsbury
Add server startup and shutdown events to the replication stream.
312
  if (file == -1)
313
  {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
314
    cerr << _("Cannot open file: ") << filename << endl;
636 by Brian Aker
First pass with new event API (yeah... it will be better).
315
    return -1;
316
  }
317
1101.2.1 by Monty Taylor
Fixed the first set of using namespace
318
  message::Transaction transaction;
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
319
  message::TransactionManager trx_mgr;
320
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
321
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
322
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
323
  /* Skip ahead to user supplied position */
324
  if (opt_start_pos)
325
  {
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
326
    if (not raw_input->Skip(opt_start_pos))
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
327
    {
328
      cerr << _("Could not skip to position ") << opt_start_pos
329
           << _(" in file ") << filename << endl;
330
      exit(-1);
331
    }
332
  }
333
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
334
  char *buffer= NULL;
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
335
  char *temp_buffer= NULL;
1143.2.12 by Jay Pipes
Fixes 64-bit length encoding to be 32-bit, which is what is supported by GPB. We will need to use a segment strategy for large blob records.
336
  uint32_t length= 0;
337
  uint32_t previous_length= 0;
1143.2.10 by Jay Pipes
Phase 2 new replication work:
338
  uint32_t checksum= 0;
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
339
  bool result= true;
1143.2.15 by Jay Pipes
This patch does the following:
340
  uint32_t message_type= 0;
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
341
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
342
  while (result == true)
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
343
  {
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
344
   /*
345
     * Odd thing to note about using CodedInputStream: This class wasn't
346
     * intended to read large amounts of GPB messages. It has an upper
347
     * limit on the number of bytes it will read (see Protobuf docs for
348
     * this class for more info). A warning will be produced as you
349
     * get close to this limit. Since this is a pretty lightweight class,
350
     * we should be able to simply create a new one for each message we
351
     * want to read.
352
     */
353
    protobuf::io::CodedInputStream coded_input(raw_input);
354
355
    /* Read in the type and length of the command */
356
    if (not coded_input.ReadLittleEndian32(&message_type) ||
357
        not coded_input.ReadLittleEndian32(&length))
358
    {
359
      break;  /* EOF */
360
    }
361
1143.2.15 by Jay Pipes
This patch does the following:
362
    if (message_type != ReplicationServices::TRANSACTION)
363
    {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
364
      cerr << _("Found a non-transaction message in log.  Currently, not supported.\n");
365
      exit(-1);
1143.2.15 by Jay Pipes
This patch does the following:
366
    }
367
1143.2.12 by Jay Pipes
Fixes 64-bit length encoding to be 32-bit, which is what is supported by GPB. We will need to use a segment strategy for large blob records.
368
    if (length > INT_MAX)
873.2.20 by Monty Taylor
Fixed 32-bit compile issue.
369
    {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
370
      cerr << _("Attempted to read record bigger than INT_MAX\n");
371
      exit(-1);
873.2.20 by Monty Taylor
Fixed 32-bit compile issue.
372
    }
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
373
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
374
    if (buffer == NULL)
375
    {
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
376
      /*
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
377
       * First time around...just malloc the length.  This block gets rid
378
       * of a GCC warning about uninitialized temp_buffer.
379
       */
1143.2.12 by Jay Pipes
Fixes 64-bit length encoding to be 32-bit, which is what is supported by GPB. We will need to use a segment strategy for large blob records.
380
      temp_buffer= (char *) malloc(static_cast<size_t>(length));
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
381
    }
382
    /* No need to allocate if we have a buffer big enough... */
383
    else if (length > previous_length)
384
    {
1143.2.12 by Jay Pipes
Fixes 64-bit length encoding to be 32-bit, which is what is supported by GPB. We will need to use a segment strategy for large blob records.
385
      temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
386
    }
387
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
388
    if (temp_buffer == NULL)
389
    {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
390
      cerr << _("Memory allocation failure trying to allocate ") << length << _(" bytes\n");
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
391
      break;
392
    }
393
    else
394
      buffer= temp_buffer;
395
396
    /* Read the Command */
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
397
    result= coded_input.ReadRaw(buffer, (int) length);
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
398
    if (result == false)
399
    {
1558.2.3 by Hartmut Holzgraefe
strerror() -> strerror_r() conversion started (bug #606478)
400
      char errmsg[STRERROR_MAX];
401
      strerror_r(errno, errmsg, sizeof(errmsg));
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
402
      cerr << _("Could not read transaction message.\n");
403
      cerr << _("GPB ERROR: ") << errmsg << endl;;
1143.4.8 by Jay Pipes
This commit fixes issues raised by Joe Daly in Bug#489823.
404
      string hexdump;
405
      hexdump.reserve(length * 4);
406
      bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
407
      cerr << _("HEXDUMP:\n\n") << hexdump << endl;
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
408
      break;
409
    }
410
1143.2.12 by Jay Pipes
Fixes 64-bit length encoding to be 32-bit, which is what is supported by GPB. We will need to use a segment strategy for large blob records.
411
    result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
412
    if (result == false)
413
    {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
414
      cerr << _("Unable to parse command. Got error: ")
415
           << transaction.InitializationErrorString() << endl;
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
416
      if (buffer != NULL)
1143.4.8 by Jay Pipes
This commit fixes issues raised by Joe Daly in Bug#489823.
417
      {
418
        string hexdump;
419
        hexdump.reserve(length * 4);
420
        bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
421
        cerr <<  _("HEXDUMP:\n\n") << hexdump << endl;
1143.4.8 by Jay Pipes
This commit fixes issues raised by Joe Daly in Bug#489823.
422
      }
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
423
      break;
424
    }
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
425
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
426
    const message::TransactionContext trx= transaction.transaction_context();
427
    uint64_t transaction_id= trx.transaction_id();
428
429
    /*
430
     * If we are given a transaction ID, we only look for that one and
431
     * print it out.
432
     */
433
    if (vm.count("transaction-id"))
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
434
    {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
435
      if (opt_transaction_id == transaction_id)
436
      {
437
        printTransaction(transaction, ignore_events, print_as_raw);
438
      }
439
      else
440
      {
441
        /* Need to get the checksum bytes out of stream */
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
442
        coded_input.ReadLittleEndian32(&checksum);
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
443
        previous_length = length;
444
        continue;
445
      }
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
446
    }
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
447
448
    /*
449
     * No transaction ID given, so process all messages.
450
     */
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
451
    else
452
    {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
453
      if (not isEndTransaction(transaction))
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
454
      {
455
        trx_mgr.store(transaction);
456
      }
457
      else
458
      {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
459
        /*
460
         * If there are any previous Transaction messages for this transaction,
461
         * store this one, then output all of them together.
462
         */
463
        if (trx_mgr.contains(transaction_id))
464
        {
465
          trx_mgr.store(transaction);
466
467
          uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
468
          uint32_t idx= 0;
469
470
          while (idx != size)
471
          {
472
            message::Transaction new_trx;
473
            trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
474
            printTransaction(new_trx, ignore_events, print_as_raw);
475
            idx++;
476
          }
477
478
          /* No longer need this transaction */
479
          trx_mgr.remove(transaction_id);
480
        }
481
        else
482
        {
483
          printTransaction(transaction, ignore_events, print_as_raw);
484
        }
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
485
      }
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
486
    } /* end ! vm.count("transaction-id") */
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
487
1143.2.10 by Jay Pipes
Phase 2 new replication work:
488
    /* Skip 4 byte checksum */
1887.4.1 by David Shrewsbury
Change transaction_reader to create a new CodedInputStream object for each message.
489
    coded_input.ReadLittleEndian32(&checksum);
1143.2.10 by Jay Pipes
Phase 2 new replication work:
490
491
    if (do_checksum)
492
    {
1411.7.5 by Siddharth Prakash Singh
code refactoring - removing the type when calling crc32
493
      if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
1143.2.10 by Jay Pipes
Phase 2 new replication work:
494
      {
1878.4.1 by David Shrewsbury
Add --raw, --start-pos, and --transaction-id options to transaction_reader utility.
495
        cerr << _("Checksum failed. Wanted ")
496
             << checksum
497
             << _(" got ")
498
             << drizzled::algorithm::crc32(buffer, static_cast<size_t>(length))
499
             << endl;
1143.2.10 by Jay Pipes
Phase 2 new replication work:
500
      }
501
    }
502
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
503
    previous_length= length;
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
504
  } /* end while */
505
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
506
  if (buffer)
507
    free(buffer);
1780.3.1 by David Shrewsbury
Add code to manage multi-GPB-message transactions and output them atomically.
508
1143.2.4 by Jay Pipes
New transaction proto file containing message definitions to be
509
  delete raw_input;
510
511
  return (result == true ? 0 : 1);
636 by Brian Aker
First pass with new event API (yeah... it will be better).
512
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
513