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>
28
1
#include <sys/types.h>
29
2
#include <sys/stat.h>
38
#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>
8
#include <drizzled/serialize/replication_event.pb.h>
45
9
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
Example reader application for master.info data.
15
void printRecord(const drizzle::EventList *list)
17
using namespace drizzle;
20
for (x= 0; x < list->event_size(); x++)
22
const drizzle::Event event= list->event(x);
25
cout << endl << "##########################################################################################" << endl << endl;
36
cout << "INSERT INTO " << event.table() << " (";
38
for (x= 0; x < event.field_names_size() ; x++)
43
cout << event.field_names(x);
46
cout << ") VALUES " << endl;
48
for (x= 0; x < event.values_size(); x++)
51
Event_Value values= event.values(x);
57
for (y= 0; y < values.value_size() ; y++)
62
cout << "\"" << values.value(y) << "\"";
73
Event_Value values= event.values(0);
75
cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";
77
for (x= 0; x < values.value_size() ; x++)
82
cout << "\"" << values.value(x) << "\"";
92
for (count= 0; count < event.values_size() ; count++)
95
Event_Value values= event.values(count);
97
cout << "UPDATE " << event.table() << " SET ";
99
for (x= 1; x < values.value_size() ; x++)
104
cout << event.field_names(x - 1) << " = \"" << values.value(x) << "\"";
107
cout << " WHERE " << event.primary_key() << " = " << values.value(0) << endl;
113
cout << "COMMIT" << endl;
119
cout << "Original SQL: " << event.sql() << endl;
121
cout << "AUTOCOMMIT: " << event.autocommit() << endl;
122
cout << "Server id: " << event.server_id() << endl;
123
cout << "Query id: " << event.query_id() << endl;
124
cout << "Transaction id: " << event.transaction_id() << endl;
125
cout << "Schema: " << event.schema() << endl;
126
if (event.type() != Event::DDL)
127
cout << "Table Name: " << event.table() << endl;
112
131
int main(int argc, char* argv[])
114
133
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);
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));
138
cerr << "Usage: " << argv[0] << " replication event log " << endl;
142
drizzle::EventList list;
144
if ((file= open(argv[1], O_RDONLY)) == -1)
146
cerr << "Can not open file: " << argv[0] << endl;
156
if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
159
temp_buffer= (char *)realloc(buffer, length);
185
160
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());
221
/* Print the transaction */
222
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;
162
cerr << "Memory allocation failure trying to " << length << "." << endl;
167
/* Read the record */
168
if (read(file, buffer, length) != length)
170
cerr << "Could not read entire record." << endl;
173
list.ParseFromArray(buffer, length);
175
/* Print the record */
243
return (result == true ? 0 : 1);