1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 Sun Microsystems
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <drizzled/global.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <drizzled/message/replication.pb.h>
using namespace std;
using namespace drizzled;
/**
* @file Example application for reading change records and transactions
*/
static void printInsert(const message::Command &container,
const message::InsertRecord &record)
{
cout << "INSERT INTO `" << container.schema() << "`.`"
<< container.table() << "` (";
int32_t num_fields= record.insert_field_size();
int32_t x;
for (x= 0; x < num_fields; x++)
{
if (x != 0)
cout << ", ";
const message::Table::Field f= record.insert_field(x);
cout << "`" << f.name() << "`";
}
cout << ") VALUES ";
/*
* There may be an INSERT VALUES (),() type statement. We know the
* number of records is equal to the field_values array size divided
* by the number of fields.
*
* So, we do an inner and an outer loop. Outer loop is on the number
* of records and the inner loop on the number of fields. In this way,
* we know that record.field_values(outer_loop * num_fields) + inner_loop))
* always gives us our correct field value.
*/
int32_t num_records= (record.insert_value_size() / num_fields);
int32_t y;
for (x= 0; x < num_records; x++)
{
if (x != 0)
cout << ", ";
cout << "(";
for (y= 0; y < num_fields; y++)
{
if (y != 0)
cout << ", ";
cout << "\"" << record.insert_value((x * num_fields) + y) << "\"";
}
cout << ")";
}
cout << ";";
}
static void printDeleteWithPK(const message::Command &container,
const message::DeleteRecord &record)
{
cout << "DELETE FROM `" << container.schema() << "`.`" << container.table() << "`";
int32_t num_where_fields= record.where_field_size();
/*
* Make sure we catch anywhere we're not aligning the fields with
* the field_values arrays...
*/
assert(num_where_fields == record.where_value_size());
cout << " WHERE ";
int32_t x;
for (x= 0; x < num_where_fields; x++)
{
if (x != 0)
cout << " AND "; /* Always AND condition with a multi-column PK */
const message::Table::Field f= record.where_field(x);
/* Always equality conditions */
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
}
}
static void printUpdateWithPK(const message::Command &container,
const message::UpdateRecord &record)
{
int32_t num_update_fields= record.update_field_size();
int32_t x;
cout << "UPDATE `" << container.schema() << "`.`" << container.table() << "` SET ";
for (x= 0;x < num_update_fields; x++)
{
message::Table::Field f= record.update_field(x);
if (x != 0)
cout << ", ";
cout << "`" << f.name() << "` = \"" << record.after_value(x) << "\"";
}
int32_t num_where_fields= record.where_field_size();
/*
* Make sure we catch anywhere we're not aligning the fields with
* the field_values arrays...
*/
assert(num_where_fields == record.where_value_size());
cout << " WHERE ";
for (x= 0;x < num_where_fields; x++)
{
if (x != 0)
cout << " AND "; /* Always AND condition with a multi-column PK */
const message::Table::Field f= record.where_field(x);
/* Always equality conditions */
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
}
}
static void printTransaction(const message::Transaction &transaction)
{
int32_t e_size;
cout << "/* Start Time: " << transaction.start_timestamp() << " */ START TRANSACTION;"<< endl;
for (e_size= 0; e_size < transaction.command_size(); e_size++)
{
const message::Command command= transaction.command(e_size);
message::TransactionContext trx= command.transaction_context();
cout << "/* SID: " << trx.server_id() << " XID: " << trx.transaction_id() << " */ ";
switch (command.type())
{
case message::Command::START_TRANSACTION:
cout << "START TRANSACTION;";
break;
case message::Command::COMMIT:
cout << "COMMIT;";
break;
case message::Command::ROLLBACK:
cout << "ROLLBACK;";
break;
case message::Command::INSERT:
{
printInsert(command, command.insert_record());
break;
}
case message::Command::DELETE:
{
printDeleteWithPK(command, command.delete_record());
break;
}
case message::Command::UPDATE:
{
printUpdateWithPK(command, command.update_record());
break;
}
default:
assert(0);
}
cout << endl;
}
cout << "/* Commit Time: " << transaction.end_timestamp() << " */ COMMIT;" << endl;
}
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
int file;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " TRANSACTION_LOG" << endl;
return -1;
}
message::Transaction transaction;
if ((file= open(argv[1], O_RDONLY)) == -1)
{
cerr << "Can not open file: " << argv[1] << endl;
}
char *buffer= NULL;
char *temp_buffer;
while (1)
{
uint64_t length;
/* Read the size */
if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
break;
if (length > SIZE_MAX)
{
cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
exit(1);
}
temp_buffer= (char *)realloc(buffer, (size_t)length);
if (temp_buffer == NULL)
{
cerr << "Memory allocation failure trying to allocate " << length << " bytes." << endl;
exit(1);
}
memset(temp_buffer, 0, (size_t)length);
buffer= temp_buffer;
size_t read_bytes= 0;
/* Read the transaction */
if ((read_bytes= read(file, buffer, (size_t)length)) != (size_t)length)
{
cerr << "Could not read entire transaction. Read " << read_bytes << " bytes instead of " << length << " bytes." << endl;
exit(1);
}
transaction.ParseFromArray(buffer, (int) length);
/* Print the transaction */
printTransaction(transaction);
}
return 0;
}
|