~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

  • Committer: Patrick Crews
  • Date: 2010-09-14 20:21:03 UTC
  • mto: (1771.1.1 pcrews)
  • mto: This revision was merged to the branch mainline in revision 1772.
  • Revision ID: gleebix@gmail.com-20100914202103-1db2n0bshzafep19
Moved transaction_log tests into updated non-publisher-based tree

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/util/convert.h>
 
43
 
 
44
#include <google/protobuf/io/coded_stream.h>
 
45
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
46
 
 
47
using namespace std;
 
48
using namespace google;
 
49
using namespace drizzled;
 
50
 
 
51
static const char *replace_with_spaces= "\n\r";
 
52
 
 
53
static void printStatement(const message::Statement &statement)
 
54
{
 
55
  vector<string> sql_strings;
 
56
 
 
57
  message::transformStatementToSql(statement,
 
58
                                   sql_strings,
 
59
                                   message::DRIZZLE,
 
60
                                   true /* already in transaction */);
 
61
 
 
62
  for (vector<string>::iterator sql_string_iter= sql_strings.begin();
 
63
       sql_string_iter != sql_strings.end();
 
64
       ++sql_string_iter)
 
65
  {
 
66
    string &sql= *sql_string_iter;
 
67
 
 
68
    /* 
 
69
     * Replace \n and \r with spaces so that SQL statements 
 
70
     * are always on a single line 
 
71
     */
 
72
    {
 
73
      string::size_type found= sql.find_first_of(replace_with_spaces);
 
74
      while (found != string::npos)
 
75
      {
 
76
        sql[found]= ' ';
 
77
        found= sql.find_first_of(replace_with_spaces, found);
 
78
      }
 
79
    }
 
80
 
 
81
    /*
 
82
     * Embedded NUL characters are a pain in the ass.
 
83
     */
 
84
    {
 
85
      string::size_type found= sql.find_first_of('\0');
 
86
      while (found != string::npos)
 
87
      {
 
88
        sql[found]= '\\';
 
89
        sql.insert(found + 1, 1, '0');
 
90
        found= sql.find_first_of('\0', found);
 
91
      }
 
92
    }
 
93
 
 
94
    cout << sql << ';' << endl;
 
95
  }
 
96
}
 
97
 
 
98
static bool isEndStatement(const message::Statement &statement)
 
99
{
 
100
  switch (statement.type())
 
101
  {
 
102
    case (message::Statement::INSERT):
 
103
    {
 
104
      const message::InsertData &data= statement.insert_data();
 
105
      if (not data.end_segment())
 
106
        return false;
 
107
      break;
 
108
    }
 
109
    case (message::Statement::UPDATE):
 
110
    {
 
111
      const message::UpdateData &data= statement.update_data();
 
112
      if (not data.end_segment())
 
113
        return false;
 
114
      break;
 
115
    }
 
116
    case (message::Statement::DELETE):
 
117
    {
 
118
      const message::DeleteData &data= statement.delete_data();
 
119
      if (not data.end_segment())
 
120
        return false;
 
121
      break;
 
122
    }
 
123
    default:
 
124
      return true;
 
125
  }
 
126
  return true;
 
127
}
 
128
 
 
129
static void printTransaction(const message::Transaction &transaction)
 
130
{
 
131
  static uint64_t last_trx_id= 0;
 
132
  bool should_commit= true;
 
133
  const message::TransactionContext trx= transaction.transaction_context();
 
134
 
 
135
  size_t num_statements= transaction.statement_size();
 
136
  size_t x;
 
137
 
 
138
  /*
 
139
   * One way to determine when a new transaction begins is when the
 
140
   * transaction id changes. We check that here.
 
141
   */
 
142
  if (trx.transaction_id() != last_trx_id)
 
143
    cout << "START TRANSACTION;" << endl;
 
144
 
 
145
  last_trx_id= trx.transaction_id();
 
146
 
 
147
  for (x= 0; x < num_statements; ++x)
 
148
  {
 
149
    const message::Statement &statement= transaction.statement(x);
 
150
 
 
151
    if (should_commit)
 
152
      should_commit= isEndStatement(statement);
 
153
 
 
154
    printStatement(statement);
 
155
  }
 
156
 
 
157
  /*
 
158
   * If ALL Statements are end segments, we can commit this Transaction.
 
159
   * We can also check to see if the transaction_id changed, but this
 
160
   * wouldn't work for the last Transaction in the transaction log since
 
161
   * we don't have another Transaction to compare to. Checking for all
 
162
   * end segments (like we do above) covers this case.
 
163
   */
 
164
  if (should_commit)
 
165
    cout << "COMMIT;" << endl;
 
166
}
 
167
 
 
168
int main(int argc, char* argv[])
 
169
{
 
170
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
171
  int file;
 
172
 
 
173
  if (argc < 2 || argc > 3)
 
174
  {
 
175
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
 
176
    return -1;
 
177
  }
 
178
 
 
179
  message::Transaction transaction;
 
180
 
 
181
  file= open(argv[1], O_RDONLY);
 
182
  if (file == -1)
 
183
  {
 
184
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
 
185
    return -1;
 
186
  }
 
187
 
 
188
  bool do_checksum= false;
 
189
 
 
190
  if (argc == 3)
 
191
  {
 
192
    string checksum_arg(argv[2]);
 
193
    transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
 
194
 
 
195
    if ("--checksum" == checksum_arg)
 
196
      do_checksum= true;
 
197
  }
 
198
 
 
199
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
 
200
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
 
201
 
 
202
  char *buffer= NULL;
 
203
  char *temp_buffer= NULL;
 
204
  uint32_t length= 0;
 
205
  uint32_t previous_length= 0;
 
206
  uint32_t checksum= 0;
 
207
  bool result= true;
 
208
  uint32_t message_type= 0;
 
209
 
 
210
  /* Read in the length of the command */
 
211
  while (result == true && 
 
212
         coded_input->ReadLittleEndian32(&message_type) == true &&
 
213
         coded_input->ReadLittleEndian32(&length) == true)
 
214
  {
 
215
    if (message_type != ReplicationServices::TRANSACTION)
 
216
    {
 
217
      fprintf(stderr, _("Found a non-transaction message in log.  Currently, not supported.\n"));
 
218
      exit(1);
 
219
    }
 
220
 
 
221
    if (length > INT_MAX)
 
222
    {
 
223
      fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
 
224
      exit(1);
 
225
    }
 
226
 
 
227
    if (buffer == NULL)
 
228
    {
 
229
      /* 
 
230
       * First time around...just malloc the length.  This block gets rid
 
231
       * of a GCC warning about uninitialized temp_buffer.
 
232
       */
 
233
      temp_buffer= (char *) malloc(static_cast<size_t>(length));
 
234
    }
 
235
    /* No need to allocate if we have a buffer big enough... */
 
236
    else if (length > previous_length)
 
237
    {
 
238
      temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
 
239
    }
 
240
 
 
241
    if (temp_buffer == NULL)
 
242
    {
 
243
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
 
244
              static_cast<uint64_t>(length));
 
245
      break;
 
246
    }
 
247
    else
 
248
      buffer= temp_buffer;
 
249
 
 
250
    /* Read the Command */
 
251
    result= coded_input->ReadRaw(buffer, (int) length);
 
252
    if (result == false)
 
253
    {
 
254
      char errmsg[STRERROR_MAX];
 
255
      strerror_r(errno, errmsg, sizeof(errmsg));
 
256
      fprintf(stderr, _("Could not read transaction message.\n"));
 
257
      fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
 
258
      string hexdump;
 
259
      hexdump.reserve(length * 4);
 
260
      bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
261
      fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
262
      break;
 
263
    }
 
264
 
 
265
    result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
 
266
    if (result == false)
 
267
    {
 
268
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
 
269
      if (buffer != NULL)
 
270
      {
 
271
        string hexdump;
 
272
        hexdump.reserve(length * 4);
 
273
        bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
274
        fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
275
      }
 
276
      break;
 
277
    }
 
278
 
 
279
    /* Print the transaction */
 
280
    printTransaction(transaction);
 
281
 
 
282
    /* Skip 4 byte checksum */
 
283
    coded_input->ReadLittleEndian32(&checksum);
 
284
 
 
285
    if (do_checksum)
 
286
    {
 
287
      if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
 
288
      {
 
289
        fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
 
290
      }
 
291
    }
 
292
 
 
293
    previous_length= length;
 
294
  }
 
295
  if (buffer)
 
296
    free(buffer);
 
297
  
 
298
  delete coded_input;
 
299
  delete raw_input;
 
300
 
 
301
  return (result == true ? 0 : 1);
 
302
}
 
303