~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

Merge refactored command line using for innodb

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/gettext.h>
 
26
#include <drizzled/replication_services.h>
 
27
#include <drizzled/algorithm/crc32.h>
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <fcntl.h>
 
31
#include <limits.h>
 
32
#include <cstdio>
 
33
#include <cerrno>
 
34
#include <iostream>
 
35
#include <string>
 
36
#include <algorithm>
 
37
#include <vector>
 
38
#include <unistd.h>
 
39
#include <drizzled/message/transaction.pb.h>
 
40
#include <drizzled/message/statement_transform.h>
 
41
#include <drizzled/util/convert.h>
 
42
 
 
43
#include <google/protobuf/io/coded_stream.h>
 
44
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
45
 
 
46
using namespace std;
 
47
using namespace google;
 
48
using namespace drizzled;
 
49
 
 
50
static const char *replace_with_spaces= "\n\r";
 
51
 
 
52
static void printStatement(const message::Statement &statement)
 
53
{
 
54
  vector<string> sql_strings;
 
55
 
 
56
  message::transformStatementToSql(statement,
 
57
                                   sql_strings,
 
58
                                   message::DRIZZLE,
 
59
                                   true /* already in transaction */);
 
60
 
 
61
  for (vector<string>::iterator sql_string_iter= sql_strings.begin();
 
62
       sql_string_iter != sql_strings.end();
 
63
       ++sql_string_iter)
 
64
  {
 
65
    string &sql= *sql_string_iter;
 
66
 
 
67
    /* 
 
68
     * Replace \n and \r with spaces so that SQL statements 
 
69
     * are always on a single line 
 
70
     */
 
71
    {
 
72
      string::size_type found= sql.find_first_of(replace_with_spaces);
 
73
      while (found != string::npos)
 
74
      {
 
75
        sql[found]= ' ';
 
76
        found= sql.find_first_of(replace_with_spaces, found);
 
77
      }
 
78
    }
 
79
 
 
80
    /*
 
81
     * Embedded NUL characters are a pain in the ass.
 
82
     */
 
83
    {
 
84
      string::size_type found= sql.find_first_of('\0');
 
85
      while (found != string::npos)
 
86
      {
 
87
        sql[found]= '\\';
 
88
        sql.insert(found + 1, 1, '0');
 
89
        found= sql.find_first_of('\0', found);
 
90
      }
 
91
    }
 
92
 
 
93
    cout << sql << ';' << endl;
 
94
  }
 
95
}
 
96
 
 
97
static void printTransaction(const message::Transaction &transaction)
 
98
{
 
99
  const message::TransactionContext trx= transaction.transaction_context();
 
100
 
 
101
  size_t num_statements= transaction.statement_size();
 
102
  size_t x;
 
103
 
 
104
  cout << "START TRANSACTION;" << endl;
 
105
  for (x= 0; x < num_statements; ++x)
 
106
  {
 
107
    const message::Statement &statement= transaction.statement(x);
 
108
    printStatement(statement);
 
109
  }
 
110
  cout << "COMMIT;" << endl;
 
111
}
 
112
 
 
113
int main(int argc, char* argv[])
 
114
{
 
115
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
116
  int file;
 
117
 
 
118
  if (argc < 2 || argc > 3)
 
119
  {
 
120
    fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
 
121
    return -1;
 
122
  }
 
123
 
 
124
  message::Transaction transaction;
 
125
 
 
126
  file= open(argv[1], O_RDONLY);
 
127
  if (file == -1)
 
128
  {
 
129
    fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
 
130
    return -1;
 
131
  }
 
132
 
 
133
  bool do_checksum= false;
 
134
 
 
135
  if (argc == 3)
 
136
  {
 
137
    string checksum_arg(argv[2]);
 
138
    transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
 
139
 
 
140
    if ("--checksum" == checksum_arg)
 
141
      do_checksum= true;
 
142
  }
 
143
 
 
144
  protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
 
145
  protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
 
146
 
 
147
  char *buffer= NULL;
 
148
  char *temp_buffer= NULL;
 
149
  uint32_t length= 0;
 
150
  uint32_t previous_length= 0;
 
151
  uint32_t checksum= 0;
 
152
  bool result= true;
 
153
  uint32_t message_type= 0;
 
154
 
 
155
  /* Read in the length of the command */
 
156
  while (result == true && 
 
157
         coded_input->ReadLittleEndian32(&message_type) == true &&
 
158
         coded_input->ReadLittleEndian32(&length) == true)
 
159
  {
 
160
    if (message_type != ReplicationServices::TRANSACTION)
 
161
    {
 
162
      fprintf(stderr, _("Found a non-transaction message in log.  Currently, not supported.\n"));
 
163
      exit(1);
 
164
    }
 
165
 
 
166
    if (length > INT_MAX)
 
167
    {
 
168
      fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
 
169
      exit(1);
 
170
    }
 
171
 
 
172
    if (buffer == NULL)
 
173
    {
 
174
      /* 
 
175
       * First time around...just malloc the length.  This block gets rid
 
176
       * of a GCC warning about uninitialized temp_buffer.
 
177
       */
 
178
      temp_buffer= (char *) malloc(static_cast<size_t>(length));
 
179
    }
 
180
    /* No need to allocate if we have a buffer big enough... */
 
181
    else if (length > previous_length)
 
182
    {
 
183
      temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
 
184
    }
 
185
 
 
186
    if (temp_buffer == NULL)
 
187
    {
 
188
      fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
 
189
              static_cast<uint64_t>(length));
 
190
      break;
 
191
    }
 
192
    else
 
193
      buffer= temp_buffer;
 
194
 
 
195
    /* Read the Command */
 
196
    result= coded_input->ReadRaw(buffer, (int) length);
 
197
    if (result == false)
 
198
    {
 
199
      fprintf(stderr, _("Could not read transaction message.\n"));
 
200
      fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
 
201
      string hexdump;
 
202
      hexdump.reserve(length * 4);
 
203
      bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
204
      fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
205
      break;
 
206
    }
 
207
 
 
208
    result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
 
209
    if (result == false)
 
210
    {
 
211
      fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
 
212
      if (buffer != NULL)
 
213
      {
 
214
        string hexdump;
 
215
        hexdump.reserve(length * 4);
 
216
        bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
 
217
        fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
 
218
      }
 
219
      break;
 
220
    }
 
221
 
 
222
    /* Print the transaction */
 
223
    printTransaction(transaction);
 
224
 
 
225
    /* Skip 4 byte checksum */
 
226
    coded_input->ReadLittleEndian32(&checksum);
 
227
 
 
228
    if (do_checksum)
 
229
    {
 
230
      if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
 
231
      {
 
232
        fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
 
233
      }
 
234
    }
 
235
 
 
236
    previous_length= length;
 
237
  }
 
238
  if (buffer)
 
239
    free(buffer);
 
240
  
 
241
  delete coded_input;
 
242
  delete raw_input;
 
243
 
 
244
  return (result == true ? 0 : 1);
 
245
}
 
246