1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
8
* Jay Pipes <joinfu@sun.com>
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.
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.
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
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>
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>
45
#include <google/protobuf/io/coded_stream.h>
46
#include <google/protobuf/io/zero_copy_stream_impl.h>
49
using namespace google;
50
using namespace drizzled;
52
static const char *replace_with_spaces= "\n\r";
54
static void printStatement(const message::Statement &statement)
56
vector<string> sql_strings;
58
message::transformStatementToSql(statement,
61
true /* already in transaction */);
63
for (vector<string>::iterator sql_string_iter= sql_strings.begin();
64
sql_string_iter != sql_strings.end();
67
string &sql= *sql_string_iter;
70
* Replace \n and \r with spaces so that SQL statements
71
* are always on a single line
74
string::size_type found= sql.find_first_of(replace_with_spaces);
75
while (found != string::npos)
78
found= sql.find_first_of(replace_with_spaces, found);
83
* Embedded NUL characters are a pain in the ass.
86
string::size_type found= sql.find_first_of('\0');
87
while (found != string::npos)
90
sql.insert(found + 1, 1, '0');
91
found= sql.find_first_of('\0', found);
95
cout << sql << ';' << endl;
99
static bool isEndStatement(const message::Statement &statement)
101
switch (statement.type())
103
case (message::Statement::INSERT):
105
const message::InsertData &data= statement.insert_data();
106
if (not data.end_segment())
110
case (message::Statement::UPDATE):
112
const message::UpdateData &data= statement.update_data();
113
if (not data.end_segment())
117
case (message::Statement::DELETE):
119
const message::DeleteData &data= statement.delete_data();
120
if (not data.end_segment())
130
static bool isEndTransaction(const message::Transaction &transaction)
132
const message::TransactionContext trx= transaction.transaction_context();
134
size_t num_statements= transaction.statement_size();
137
* If any Statement is partial, then we can expect another Transaction
140
for (size_t x= 0; x < num_statements; ++x)
142
const message::Statement &statement= transaction.statement(x);
144
if (not isEndStatement(statement))
151
static void printTransaction(const message::Transaction &transaction)
153
static uint64_t last_trx_id= 0;
154
bool should_commit= true;
155
const message::TransactionContext trx= transaction.transaction_context();
157
size_t num_statements= transaction.statement_size();
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.
165
if (trx.transaction_id() != last_trx_id)
166
cout << "START TRANSACTION;" << endl;
168
last_trx_id= trx.transaction_id();
170
for (x= 0; x < num_statements; ++x)
172
const message::Statement &statement= transaction.statement(x);
175
should_commit= isEndStatement(statement);
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.
183
if (statement.type() == message::Statement::ROLLBACK)
184
should_commit= false;
186
printStatement(statement);
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.
197
cout << "COMMIT;" << endl;
200
int main(int argc, char* argv[])
202
GOOGLE_PROTOBUF_VERIFY_VERSION;
205
if (argc < 2 || argc > 3)
207
fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
211
message::Transaction transaction;
213
file= open(argv[1], O_RDONLY);
216
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
220
bool do_checksum= false;
224
string checksum_arg(argv[2]);
225
transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
227
if ("--checksum" == checksum_arg)
231
message::TransactionManager trx_mgr;
233
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
234
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
237
char *temp_buffer= NULL;
239
uint32_t previous_length= 0;
240
uint32_t checksum= 0;
242
uint32_t message_type= 0;
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)
249
if (message_type != ReplicationServices::TRANSACTION)
251
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
255
if (length > INT_MAX)
257
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
264
* First time around...just malloc the length. This block gets rid
265
* of a GCC warning about uninitialized temp_buffer.
267
temp_buffer= (char *) malloc(static_cast<size_t>(length));
269
/* No need to allocate if we have a buffer big enough... */
270
else if (length > previous_length)
272
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
275
if (temp_buffer == NULL)
277
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
278
static_cast<uint64_t>(length));
284
/* Read the Command */
285
result= coded_input->ReadRaw(buffer, (int) length);
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);
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());
299
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
302
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
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());
313
if (not isEndTransaction(transaction))
315
trx_mgr.store(transaction);
319
const message::TransactionContext trx= transaction.transaction_context();
320
uint64_t transaction_id= trx.transaction_id();
323
* If there are any previous Transaction messages for this transaction,
324
* store this one, then output all of them together.
326
if (trx_mgr.contains(transaction_id))
328
trx_mgr.store(transaction);
330
uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
335
message::Transaction new_trx;
336
trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
337
printTransaction(new_trx);
341
/* No longer need this transaction */
342
trx_mgr.remove(transaction_id);
346
printTransaction(transaction);
350
/* Skip 4 byte checksum */
351
coded_input->ReadLittleEndian32(&checksum);
355
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
357
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
361
previous_length= length;
370
return (result == true ? 0 : 1);