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/util/convert.h>
44
#include <google/protobuf/io/coded_stream.h>
45
#include <google/protobuf/io/zero_copy_stream_impl.h>
48
using namespace google;
49
using namespace drizzled;
51
static const char *replace_with_spaces= "\n\r";
53
static void printStatement(const message::Statement &statement)
55
vector<string> sql_strings;
57
message::transformStatementToSql(statement,
60
true /* already in transaction */);
62
for (vector<string>::iterator sql_string_iter= sql_strings.begin();
63
sql_string_iter != sql_strings.end();
66
string &sql= *sql_string_iter;
69
* Replace \n and \r with spaces so that SQL statements
70
* are always on a single line
73
string::size_type found= sql.find_first_of(replace_with_spaces);
74
while (found != string::npos)
77
found= sql.find_first_of(replace_with_spaces, found);
82
* Embedded NUL characters are a pain in the ass.
85
string::size_type found= sql.find_first_of('\0');
86
while (found != string::npos)
89
sql.insert(found + 1, 1, '0');
90
found= sql.find_first_of('\0', found);
94
cout << sql << ';' << endl;
98
static bool isEndStatement(const message::Statement &statement)
100
switch (statement.type())
102
case (message::Statement::INSERT):
104
const message::InsertData &data= statement.insert_data();
105
if (not data.end_segment())
109
case (message::Statement::UPDATE):
111
const message::UpdateData &data= statement.update_data();
112
if (not data.end_segment())
116
case (message::Statement::DELETE):
118
const message::DeleteData &data= statement.delete_data();
119
if (not data.end_segment())
129
static void printTransaction(const message::Transaction &transaction)
131
static uint64_t last_trx_id= 0;
132
bool should_commit= true;
133
const message::TransactionContext trx= transaction.transaction_context();
135
size_t num_statements= transaction.statement_size();
139
* One way to determine when a new transaction begins is when the
140
* transaction id changes. We check that here.
142
if (trx.transaction_id() != last_trx_id)
143
cout << "START TRANSACTION;" << endl;
145
last_trx_id= trx.transaction_id();
147
for (x= 0; x < num_statements; ++x)
149
const message::Statement &statement= transaction.statement(x);
152
should_commit= isEndStatement(statement);
154
printStatement(statement);
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.
165
cout << "COMMIT;" << endl;
168
int main(int argc, char* argv[])
170
GOOGLE_PROTOBUF_VERIFY_VERSION;
173
if (argc < 2 || argc > 3)
175
fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
179
message::Transaction transaction;
181
file= open(argv[1], O_RDONLY);
184
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
188
bool do_checksum= false;
192
string checksum_arg(argv[2]);
193
transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
195
if ("--checksum" == checksum_arg)
199
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
200
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
203
char *temp_buffer= NULL;
205
uint32_t previous_length= 0;
206
uint32_t checksum= 0;
208
uint32_t message_type= 0;
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)
215
if (message_type != ReplicationServices::TRANSACTION)
217
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
221
if (length > INT_MAX)
223
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
230
* First time around...just malloc the length. This block gets rid
231
* of a GCC warning about uninitialized temp_buffer.
233
temp_buffer= (char *) malloc(static_cast<size_t>(length));
235
/* No need to allocate if we have a buffer big enough... */
236
else if (length > previous_length)
238
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
241
if (temp_buffer == NULL)
243
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
244
static_cast<uint64_t>(length));
250
/* Read the Command */
251
result= coded_input->ReadRaw(buffer, (int) length);
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);
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());
265
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
268
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
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());
279
/* Print the transaction */
280
printTransaction(transaction);
282
/* Skip 4 byte checksum */
283
coded_input->ReadLittleEndian32(&checksum);
287
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
289
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
293
previous_length= length;
301
return (result == true ? 0 : 1);