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
printStatement(statement);
181
* If ALL Statements are end segments, we can commit this Transaction.
182
* We can also check to see if the transaction_id changed, but this
183
* wouldn't work for the last Transaction in the transaction log since
184
* we don't have another Transaction to compare to. Checking for all
185
* end segments (like we do above) covers this case.
188
cout << "COMMIT;" << endl;
191
int main(int argc, char* argv[])
193
GOOGLE_PROTOBUF_VERIFY_VERSION;
196
if (argc < 2 || argc > 3)
198
fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
202
message::Transaction transaction;
204
file= open(argv[1], O_RDONLY);
207
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
211
bool do_checksum= false;
215
string checksum_arg(argv[2]);
216
transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
218
if ("--checksum" == checksum_arg)
222
message::TransactionManager trx_mgr;
224
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
225
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
228
char *temp_buffer= NULL;
230
uint32_t previous_length= 0;
231
uint32_t checksum= 0;
233
uint32_t message_type= 0;
235
/* Read in the length of the command */
236
while (result == true &&
237
coded_input->ReadLittleEndian32(&message_type) == true &&
238
coded_input->ReadLittleEndian32(&length) == true)
240
if (message_type != ReplicationServices::TRANSACTION)
242
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
246
if (length > INT_MAX)
248
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
255
* First time around...just malloc the length. This block gets rid
256
* of a GCC warning about uninitialized temp_buffer.
258
temp_buffer= (char *) malloc(static_cast<size_t>(length));
260
/* No need to allocate if we have a buffer big enough... */
261
else if (length > previous_length)
263
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
266
if (temp_buffer == NULL)
268
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
269
static_cast<uint64_t>(length));
275
/* Read the Command */
276
result= coded_input->ReadRaw(buffer, (int) length);
279
char errmsg[STRERROR_MAX];
280
strerror_r(errno, errmsg, sizeof(errmsg));
281
fprintf(stderr, _("Could not read transaction message.\n"));
282
fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
284
hexdump.reserve(length * 4);
285
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
286
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
290
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
293
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
297
hexdump.reserve(length * 4);
298
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
299
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
304
if (not isEndTransaction(transaction))
306
trx_mgr.store(transaction);
310
const message::TransactionContext trx= transaction.transaction_context();
311
uint64_t transaction_id= trx.transaction_id();
314
* If there are any previous Transaction messages for this transaction,
315
* store this one, then output all of them together.
317
if (trx_mgr.contains(transaction_id))
319
trx_mgr.store(transaction);
321
uint32_t size= trx_mgr.getTransactionBufferSize(transaction_id);
326
message::Transaction new_trx;
327
trx_mgr.getTransactionMessage(new_trx, transaction_id, idx);
328
printTransaction(new_trx);
332
/* No longer need this transaction */
333
trx_mgr.remove(transaction_id);
337
printTransaction(transaction);
341
/* Skip 4 byte checksum */
342
coded_input->ReadLittleEndian32(&checksum);
346
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
348
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
352
previous_length= length;
361
return (result == true ? 0 : 1);