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/gettext.h>
26
#include <drizzled/replication_services.h>
27
#include <drizzled/algorithm/crc32.h>
1
#include <drizzled/global.h>
28
2
#include <sys/types.h>
29
3
#include <sys/stat.h>
38
9
#include <drizzled/message/transaction.pb.h>
39
#include <drizzled/message/statement_transform.h>
40
#include <drizzled/util/convert.h>
42
#include <google/protobuf/io/coded_stream.h>
43
#include <google/protobuf/io/zero_copy_stream_impl.h>
45
11
using namespace std;
46
using namespace google;
47
using namespace drizzled;
49
static const char *replace_with_spaces= "\n\r";
51
static void printStatement(const message::Statement &statement)
53
vector<string> sql_strings;
55
message::transformStatementToSql(statement,
58
true /* already in transaction */);
60
for (vector<string>::iterator sql_string_iter= sql_strings.begin();
61
sql_string_iter != sql_strings.end();
64
string &sql= *sql_string_iter;
67
* Replace \n and \r with spaces so that SQL statements
68
* are always on a single line
71
string::size_type found= sql.find_first_of(replace_with_spaces);
72
while (found != string::npos)
75
found= sql.find_first_of(replace_with_spaces, found);
80
* Embedded NUL characters are a pain in the ass.
83
string::size_type found= sql.find_first_of('\0');
84
while (found != string::npos)
87
sql.insert(found + 1, 1, '0');
88
found= sql.find_first_of('\0', found);
92
cout << sql << ';' << endl;
96
static void printTransaction(const message::Transaction &transaction)
98
const message::TransactionContext trx= transaction.transaction_context();
100
size_t num_statements= transaction.statement_size();
103
cout << "START TRANSACTION;" << endl;
104
for (x= 0; x < num_statements; ++x)
106
const message::Statement &statement= transaction.statement(x);
107
printStatement(statement);
109
cout << "COMMIT;" << endl;
12
using namespace drizzled::message;
15
* @file Example application for reading change records and transactions
18
void printInsert(const drizzled::message::Command &container, const drizzled::message::InsertRecord &record)
21
cout << "INSERT INTO `" << container.schema() << "`.`" << container.table() << "` (";
23
int32_t num_fields= record.insert_field_size();
26
for (x= 0; x < num_fields; x++)
31
const Table::Field f= record.insert_field(x);
33
cout << "`" << f.name() << "`";
39
* There may be an INSERT VALUES (),() type statement. We know the
40
* number of records is equal to the field_values array size divided
41
* by the number of fields.
43
* So, we do an inner and an outer loop. Outer loop is on the number
44
* of records and the inner loop on the number of fields. In this way,
45
* we know that record.field_values(outer_loop * num_fields) + inner_loop))
46
* always gives us our correct field value.
48
int32_t num_records= (record.insert_value_size() / num_fields);
50
for (x= 0; x < num_records; x++)
56
for (y= 0; y < num_fields; y++)
61
cout << "\"" << record.insert_value((x * num_fields) + y) << "\"";
69
void printDeleteWithPK(const drizzled::message::Command &container, const drizzled::message::DeleteRecord &record)
71
cout << "DELETE FROM `" << container.schema() << "`.`" << container.table() << "`";
73
int32_t num_where_fields= record.where_field_size();
75
* Make sure we catch anywhere we're not aligning the fields with
76
* the field_values arrays...
78
assert(num_where_fields == record.where_value_size());
82
for (x= 0; x < num_where_fields; x++)
85
cout << " AND "; /* Always AND condition with a multi-column PK */
87
const Table::Field f= record.where_field(x);
89
/* Always equality conditions */
90
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
94
void printUpdateWithPK(const drizzled::message::Command &container, const drizzled::message::UpdateRecord &record)
96
int32_t num_update_fields= record.update_field_size();
99
cout << "UPDATE `" << container.schema() << "`.`" << container.table() << "` SET ";
101
for (x= 0;x < num_update_fields; x++)
103
Table::Field f= record.update_field(x);
108
cout << "`" << f.name() << "` = \"" << record.after_value(x) << "\"";
111
int32_t num_where_fields= record.where_field_size();
113
* Make sure we catch anywhere we're not aligning the fields with
114
* the field_values arrays...
116
assert(num_where_fields == record.where_value_size());
119
for (x= 0;x < num_where_fields; x++)
122
cout << " AND "; /* Always AND condition with a multi-column PK */
124
const Table::Field f= record.where_field(x);
126
/* Always equality conditions */
127
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
131
void printTransaction(const drizzled::message::Transaction &transaction)
135
cout << "/* Start Time: " << transaction.start_timestamp() << " */ START TRANSACTION;"<< endl;
137
for (e_size= 0; e_size < transaction.command_size(); e_size++)
139
const drizzled::message::Command command= transaction.command(e_size);
141
drizzled::message::TransactionContext trx= command.transaction_context();
143
cout << "/* SID: " << trx.server_id() << " XID: " << trx.transaction_id() << " */ ";
145
switch (command.type())
147
case Command::START_TRANSACTION:
148
cout << "START TRANSACTION;";
150
case Command::COMMIT:
153
case Command::ROLLBACK:
156
case Command::INSERT:
158
printInsert(command, command.insert_record());
161
case Command::DELETE:
163
printDeleteWithPK(command, command.delete_record());
166
case Command::UPDATE:
168
printUpdateWithPK(command, command.update_record());
176
cout << "/* Commit Time: " << transaction.end_timestamp() << " */ COMMIT;" << endl;
112
179
int main(int argc, char* argv[])
114
181
GOOGLE_PROTOBUF_VERIFY_VERSION;
117
if (argc < 2 || argc > 3)
119
fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
123
message::Transaction transaction;
125
file= open(argv[1], O_RDONLY);
128
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
132
bool do_checksum= false;
136
string checksum_arg(argv[2]);
137
transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
139
if ("--checksum" == checksum_arg)
143
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
144
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
186
cerr << "Usage: " << argv[0] << " TRANSACTION_LOG" << endl;
190
Transaction transaction;
192
if ((file= open(argv[1], O_RDONLY)) == -1)
194
cerr << "Can not open file: " << argv[1] << endl;
146
197
char *buffer= NULL;
147
char *temp_buffer= NULL;
149
uint32_t previous_length= 0;
150
uint32_t checksum= 0;
152
uint32_t message_type= 0;
154
/* Read in the length of the command */
155
while (result == true &&
156
coded_input->ReadLittleEndian32(&message_type) == true &&
157
coded_input->ReadLittleEndian32(&length) == true)
159
if (message_type != ReplicationServices::TRANSACTION)
161
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
165
if (length > INT_MAX)
167
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
174
* First time around...just malloc the length. This block gets rid
175
* of a GCC warning about uninitialized temp_buffer.
177
temp_buffer= (char *) malloc(static_cast<size_t>(length));
179
/* No need to allocate if we have a buffer big enough... */
180
else if (length > previous_length)
182
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
205
if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
208
if (length > SIZE_MAX)
210
cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
214
temp_buffer= (char *)realloc(buffer, (size_t)length);
185
215
if (temp_buffer == NULL)
187
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
188
static_cast<uint64_t>(length));
194
/* Read the Command */
195
result= coded_input->ReadRaw(buffer, (int) length);
198
fprintf(stderr, _("Could not read transaction message.\n"));
199
fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
201
hexdump.reserve(length * 4);
202
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
203
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
207
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
210
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
214
hexdump.reserve(length * 4);
215
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
216
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
217
cerr << "Memory allocation failure trying to allocate " << length << " bytes." << endl;
220
memset(temp_buffer, 0, length);
222
size_t read_bytes= 0;
224
/* Read the transaction */
225
if ((read_bytes= read(file, buffer, (uint64_t)length)) != (uint64_t)length)
227
cerr << "Could not read entire transaction. Read " << read_bytes << " bytes instead of " << length << " bytes." << endl;
230
transaction.ParseFromArray(buffer, (int) length);
221
232
/* Print the transaction */
222
233
printTransaction(transaction);
224
/* Skip 4 byte checksum */
225
coded_input->ReadLittleEndian32(&checksum);
229
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
231
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
235
previous_length= length;
243
return (result == true ? 0 : 1);