~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

  • Committer: tdavies
  • Date: 2010-10-10 05:31:41 UTC
  • mto: (1827.1.3 trunk-drizzle)
  • mto: This revision was merged to the branch mainline in revision 1829.
  • Revision ID: tdavies@molly-20101010053141-7rbcb8fe8a6xxrn8
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
24
#include "config.h"
 
25
#include <drizzled/definitions.h>
 
26
#include <drizzled/gettext.h>
 
27
#include <drizzled/replication_services.h>
 
28
#include <drizzled/algorithm/crc32.h>
 
29
#include <sys/types.h>
 
30
#include <sys/stat.h>
 
31
#include <fcntl.h>
 
32
#include <limits.h>
 
33
#include <cstdio>
 
34
#include <cerrno>
 
35
#include <iostream>
 
36
#include <string>
 
37
#include <algorithm>
 
38
#include <vector>
 
39
#include <unistd.h>
 
40
#include <drizzled/message/transaction.pb.h>
 
41
#include <drizzled/message/statement_transform.h>
 
42
#include <drizzled/message/transaction_manager.h>
 
43
#include <drizzled/util/convert.h>
 
44
 
 
45
#include <google/protobuf/io/coded_stream.h>
 
46
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
47
 
 
48
using namespace std;
 
49
using namespace google;
 
50
using namespace drizzled;
 
51
 
 
52
static const char *replace_with_spaces= "\n\r";
 
53
 
 
54
static void printStatement(const message::Statement &statement)
 
55
{
 
56
  vector<string> sql_strings;
 
57
 
 
58
  message::transformStatementToSql(statement,
 
59
                                   sql_strings,
 
60
                                   message::DRIZZLE,
 
61
                                   true /* already in transaction */);
 
62
 
 
63
  for (vector<string>::iterator sql_string_iter= sql_strings.begin();
 
64
       sql_string_iter != sql_strings.end();
 
65
       ++sql_string_iter)
 
66
  {
 
67
    string &sql= *sql_string_iter;
 
68
 
 
69
    /* 
 
70
     * Replace \n and \r with spaces so that SQL statements 
 
71
     * are always on a single line 
 
72
     */
 
73
    {
 
74
      string::size_type found= sql.find_first_of(replace_with_spaces);
 
75
      while (found != string::npos)
 
76
      {
 
77
        sql[found]= ' ';
 
78
        found= sql.find_first_of(replace_with_spaces, found);
 
79
      }
 
80
    }
 
81
 
 
82
    /*
 
83
     * Embedded NUL characters are a pain in the ass.
 
84
     */
 
85
    {
 
86
      string::size_type found= sql.find_first_of('\0');
 
87
      while (found != string::npos)
 
88
      {
 
89
        sql[found]= '\\';
 
90
        sql.insert(found + 1, 1, '0');
 
91
        found= sql.find_first_of('\0', found);
 
92
      }
 
93
    }
 
94
 
 
95
    cout << sql << ';' << endl;
 
96
  }
 
97
}
 
98
 
 
99
static bool isEndStatement(const message::Statement &statement)
 
100
{
 
101
  switch (statement.type())
 
102
  {
 
103
    case (message::Statement::INSERT):
 
104
    {
 
105
      const message::InsertData &data= statement.insert_data();
 
106
      if (not data.end_segment())
 
107
        return false;
 
108
      break;
 
109
    }
 
110
    case (message::Statement::UPDATE):
 
111
    {
 
112
      const message::UpdateData &data= statement.update_data();
 
113
      if (not data.end_segment())
 
114
        return false;
 
115
      break;
 
116
    }
 
117
    case (message::Statement::DELETE):
 
118
    {
 
119
      const message::DeleteData &data= statement.delete_data();
 
120
      if (not data.end_segment())
 
121
        return false;
 
122
      break;
 
123
    }
 
124
    default:
 
125
      return true;
 
126
  }
 
127
  return true;
 
128
}
 
129
 
 
130
static bool isEndTransaction(const message::Transaction &transaction)
 
131
{
 
132
  const message::TransactionContext trx= transaction.transaction_context();
 
133
 
 
134
  size_t num_statements= transaction.statement_size();
 
135
 
 
136
  /*
 
137
   * If any Statement is partial, then we can expect another Transaction
 
138
   * message.
 
139
   */
 
140
  for (size_t x= 0; x < num_statements; ++x)
 
141
  {
 
142
    const message::Statement &statement= transaction.statement(x);
 
143
 
 
144
    if (not isEndStatement(statement))
 
145
      return false;
 
146
  }
 
147
 
 
148
  return true;
 
149
}
 
150
 
 
151
static void printTransaction(const message::Transaction &transaction)
 
152
{
 
153
  static uint64_t last_trx_id= 0;
 
154
  bool should_commit= true;
 
155
  const message::TransactionContext trx= transaction.transaction_context();
 
156
 
 
157
  size_t num_statements= transaction.statement_size();
 
158
  size_t x;
 
159
 
 
160
  /*
 
161
   * One way to determine when a new transaction begins is when the
 
162
   * transaction id changes (if all transactions have their GPB messages
 
163
   * grouped together, which this program will). We check that here.
 
164
   */
 
165
  if (trx.transaction_id() != last_trx_id)
 
166
    cout << "START TRANSACTION;" << endl;
 
167
 
 
168
  last_trx_id= trx.transaction_id();
 
169
 
 
170
  for (x= 0; x < num_statements; ++x)
 
171
  {
 
172
    const message::Statement &statement= transaction.statement(x);
 
173
 
 
174
    if (should_commit)
 
175
      should_commit= isEndStatement(statement);
 
176
 
 
177
    /* A ROLLBACK would be the only Statement within the Transaction
 
178
     * since all other Statements will have been deleted from the
 
179
     * Transaction message, so we should fall out of this loop immediately.
 
180
     * We don't want to issue an unnecessary COMMIT, so we change
 
181
     * should_commit to false here.
 
182
     */
 
183
    if (statement.type() == message::Statement::ROLLBACK)
 
184
      should_commit= false;
 
185
 
 
186
    printStatement(statement);
 
187
  }
 
188
 
 
189
  /*
 
190
   * If ALL Statements are end segments, we can commit this Transaction.
 
191
   * We can also check to see if the transaction_id changed, but this
 
192
   * wouldn't work for the last Transaction in the transaction log since
 
193
   * we don't have another Transaction to compare to. Checking for all
 
194
   * end segments (like we do above) covers this case.
 
195
   */
 
196
  if (should_commit)
 
197
    cout << "COMMIT;" << endl;
 
198
}
 
199
 
 
200
int main(int argc, char* argv[])
 
201
{
 
202
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
203
  int file;
 
204
 
 
205
  if (argc < 2 || argc > 3)
 
206
  {
 
207
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
 
208
    return -1;
 
209
  }
 
210
 
 
211
  message::Transaction transaction;
 
212
 
 
213
  file= open(argv[1], O_RDONLY);
 
214
  if (file == -1)
 
215
  {
 
216
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
 
217
    return -1;
 
218
  }
 
219
 
 
220
  bool do_checksum= false;
 
221
 
 
222
  if (argc == 3)
 
223
  {
 
224
    string checksum_arg(argv[2]);
 
225
    transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
 
226
 
 
227
    if ("--checksum" == checksum_arg)
 
228
      do_checksum= true;
 
229
  }
 
230
 
 
231
  message::TransactionManager trx_mgr;
 
232
 
 
233
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
 
234
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
 
235
 
 
236
  char *buffer= NULL;
 
237
  char *temp_buffer= NULL;
 
238
  uint32_t length= 0;
 
239
  uint32_t previous_length= 0;
 
240
  uint32_t checksum= 0;
 
241
  bool result= true;
 
242
  uint32_t message_type= 0;
 
243
 
 
244
  /* Read in the length of the command */
 
245
  while (result == true && 
 
246
         coded_input->ReadLittleEndian32(&message_type) == true &&
 
247
         coded_input->ReadLittleEndian32(&length) == true)
 
248
  {
 
249
    if (message_type != ReplicationServices::TRANSACTION)
 
250
    {
 
251
      fprintf(stderr, _("Found a non-transaction message in log.  Currently, not supported.\n"));
 
252
      exit(1);
 
253
    }
 
254
 
 
255
    if (length > INT_MAX)
 
256
    {
 
257
      fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
 
258
      exit(1);
 
259
    }
 
260
 
 
261
    if (buffer == NULL)
 
262
    {
 
263
      /* 
 
264
       * First time around...just malloc the length.  This block gets rid
 
265
       * of a GCC warning about uninitialized temp_buffer.
 
266
       */
 
267
      temp_buffer= (char *) malloc(static_cast<size_t>(length));
 
268
    }
 
269
    /* No need to allocate if we have a buffer big enough... */
 
270
    else if (length > previous_length)
 
271
    {
 
272
      temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
 
273
    }
 
274
 
 
275
    if (temp_buffer == NULL)
 
276
    {
 
277
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
 
278
              static_cast<uint64_t>(length));
 
279
      break;
 
280
    }
 
281
    else
 
282
      buffer= temp_buffer;
 
283
 
 
284
    /* Read the Command */
 
285
    result= coded_input->ReadRaw(buffer, (int) length);
 
286
    if (result == false)
 
287
    {
 
288
      char errmsg[STRERROR_MAX];
 
289
      strerror_r(errno, errmsg, sizeof(errmsg));
 
290
      fprintf(stderr, _("Could not read transaction message.\n"));
 
291
      fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
 
292
      string hexdump;
 
293
      hexdump.reserve(length * 4);
 
294
      bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
295
      fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
296
      break;
 
297
    }
 
298
 
 
299
    result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
 
300
    if (result == false)
 
301
    {
 
302
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
 
303
      if (buffer != NULL)
 
304
      {
 
305
        string hexdump;
 
306
        hexdump.reserve(length * 4);
 
307
        bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
308
        fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
309
      }
 
310
      break;
 
311
    }
 
312
 
 
313
    if (not isEndTransaction(transaction))
 
314
    {
 
315
      trx_mgr.store(transaction);
 
316
    }
 
317
    else
 
318
    {
 
319
      const message::TransactionContext trx= transaction.transaction_context();
 
320
      uint64_t transaction_id= trx.transaction_id();
 
321
 
 
322
      /*
 
323
       * If there are any previous Transaction messages for this transaction,
 
324
       * store this one, then output all of them together.
 
325
       */
 
326
      if (trx_mgr.contains(transaction_id))
 
327
      {
 
328
        trx_mgr.store(transaction);
 
329
 
 
330
        uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
 
331
        uint32_t idx= 0;
 
332
 
 
333
        while (idx != size)
 
334
        {
 
335
          message::Transaction new_trx;
 
336
          trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
 
337
          printTransaction(new_trx);
 
338
          idx++;
 
339
        }
 
340
 
 
341
        /* No longer need this transaction */
 
342
        trx_mgr.remove(transaction_id);
 
343
      }
 
344
      else
 
345
      {
 
346
        printTransaction(transaction);
 
347
      }
 
348
    }
 
349
 
 
350
    /* Skip 4 byte checksum */
 
351
    coded_input->ReadLittleEndian32(&checksum);
 
352
 
 
353
    if (do_checksum)
 
354
    {
 
355
      if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
 
356
      {
 
357
        fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
 
358
      }
 
359
    }
 
360
 
 
361
    previous_length= length;
 
362
  } /* end while */
 
363
 
 
364
  if (buffer)
 
365
    free(buffer);
 
366
 
 
367
  delete coded_input;
 
368
  delete raw_input;
 
369
 
 
370
  return (result == true ? 0 : 1);
 
371
}
 
372