~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/transaction_reader.cc

  • Committer: Jay Pipes
  • Date: 2009-04-10 17:06:58 UTC
  • mto: (971.1.47 mordred)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: jpipes@serialcoder-20090410170658-d3azdnas1fn8v68l
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
plugin.  New transaction_services.cc class implementation of the API for
converting between internal formats and GPB Command Messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#include <fstream>
7
7
#include <string>
8
8
#include <unistd.h>
9
 
#include <drizzled/message/replication_event.pb.h>
 
9
#include <drizzled/message/transaction.pb.h>
10
10
 
11
11
using namespace std;
12
12
using namespace drizzled::message;
13
13
 
14
 
/*
15
 
  Example reader application for master.info data.
16
 
*/
17
 
 
18
 
void printRecord(const ::drizzled::message::EventList *list)
 
14
/**
 
15
 * @file Example application for reading change records and transactions
 
16
 */
 
17
 
 
18
void printInsert(const drizzled::message::Command &container, const drizzled::message::InsertRecord &record)
 
19
{
 
20
 
 
21
  cout << "INSERT INTO `" << container.schema() << "`.`" << container.table() << "` (";
 
22
 
 
23
  int32_t num_fields= record.insert_field_size();
 
24
 
 
25
  int32_t x;
 
26
  for (x= 0; x < num_fields; x++)
 
27
  {
 
28
    if (x != 0)
 
29
      cout << ", ";
 
30
 
 
31
    const Table::Field f= record.insert_field(x);
 
32
 
 
33
    cout << "`" << f.name() << "`";
 
34
  }
 
35
 
 
36
  cout << ") VALUES ";
 
37
 
 
38
  /* 
 
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.
 
42
   *
 
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.
 
47
   */
 
48
  int32_t num_records= (record.insert_value_size() / num_fields);
 
49
  int32_t y;
 
50
  for (x= 0; x < num_records; x++)
 
51
  {
 
52
    if (x != 0)
 
53
      cout << ", ";
 
54
 
 
55
    cout << "(";
 
56
    for (y= 0; y < num_fields; y++)
 
57
    {
 
58
      if (y != 0)
 
59
        cout << ", ";
 
60
 
 
61
      cout << "\"" << record.insert_value((x * num_fields) + y) << "\"";
 
62
    }
 
63
    cout << ")";
 
64
  }
 
65
 
 
66
  cout << ";";
 
67
}
 
68
 
 
69
void printDeleteWithPK(const drizzled::message::Command &container, const drizzled::message::DeleteRecord &record)
 
70
{
 
71
  cout << "DELETE FROM `" << container.schema() << "`.`" << container.table() << "`";
 
72
 
 
73
  int32_t num_where_fields= record.where_field_size();
 
74
  /* 
 
75
   * Make sure we catch anywhere we're not aligning the fields with
 
76
   * the field_values arrays...
 
77
   */
 
78
  assert(num_where_fields == record.where_value_size());
 
79
 
 
80
  cout << " WHERE ";
 
81
  int32_t x;
 
82
  for (x= 0; x < num_where_fields; x++)
 
83
  {
 
84
    if (x != 0)
 
85
      cout << " AND "; /* Always AND condition with a multi-column PK */
 
86
 
 
87
    const Table::Field f= record.where_field(x);
 
88
 
 
89
    /* Always equality conditions */
 
90
    cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
 
91
  }
 
92
}
 
93
 
 
94
void printUpdateWithPK(const drizzled::message::Command &container, const drizzled::message::UpdateRecord &record)
 
95
{
 
96
  int32_t num_update_fields= record.update_field_size();
 
97
  int32_t x;
 
98
 
 
99
  cout << "UPDATE `" << container.schema() << "`.`" << container.table() << "` SET ";
 
100
 
 
101
  for (x= 0;x < num_update_fields; x++)
 
102
  {
 
103
    Table::Field f= record.update_field(x);
 
104
    
 
105
    if (x != 0)
 
106
      cout << ", ";
 
107
 
 
108
    cout << "`" << f.name() << "` = \"" << record.after_value(x) << "\"";
 
109
  }
 
110
 
 
111
  int32_t num_where_fields= record.where_field_size();
 
112
  /* 
 
113
   * Make sure we catch anywhere we're not aligning the fields with
 
114
   * the field_values arrays...
 
115
   */
 
116
  assert(num_where_fields == record.where_value_size());
 
117
 
 
118
  cout << " WHERE ";
 
119
  for (x= 0;x < num_where_fields; x++)
 
120
  {
 
121
    if (x != 0)
 
122
      cout << " AND "; /* Always AND condition with a multi-column PK */
 
123
 
 
124
    const Table::Field f= record.where_field(x);
 
125
 
 
126
    /* Always equality conditions */
 
127
    cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
 
128
  }
 
129
}
 
130
 
 
131
void printTransaction(const drizzled::message::Transaction &transaction)
19
132
{
20
133
  int32_t e_size;
21
134
 
22
 
  for (e_size= 0; e_size < list->event_size(); e_size++)
 
135
  cout << "/* Start Time: " << transaction.start_timestamp() << " */ START TRANSACTION;"<< endl;
 
136
 
 
137
  for (e_size= 0; e_size < transaction.command_size(); e_size++)
23
138
  {
24
 
    const Event event= list->event(e_size);
25
 
 
26
 
    if (e_size != 0)
27
 
      cout << endl << "##########################################################################################" << endl << endl;
28
 
 
29
 
    switch (event.type())
 
139
    const drizzled::message::Command command= transaction.command(e_size);
 
140
 
 
141
    drizzled::message::TransactionContext trx= command.transaction_context();
 
142
 
 
143
    cout << "/* SID: " << trx.server_id() << " XID: " << trx.transaction_id() << " */ ";
 
144
 
 
145
    switch (command.type())
30
146
    {
31
 
    case Event::DDL:
32
 
      cout << "DDL ";
33
 
      break;
34
 
    case Event::INSERT:
35
 
      {
36
 
        int32_t x;
37
 
 
38
 
        cout << "INSERT INTO " << event.table() << " (";
39
 
 
40
 
        for (x= 0; x < event.field_names_size() ; x++)
41
 
        {
42
 
          if (x != 0)
43
 
            cout << ", ";
44
 
 
45
 
          cout << event.field_names(x);
46
 
        }
47
 
 
48
 
        cout << ") VALUES " << endl;
49
 
 
50
 
        for (x= 0; x < event.values_size(); x++)
51
 
        {
52
 
          int32_t y;
53
 
          Event_Value values= event.values(x);
54
 
 
55
 
          if (x != 0)
56
 
            cout << ", ";
57
 
 
58
 
          cout << "(";
59
 
          for (y= 0; y < values.val_size() ; y++)
60
 
          {
61
 
            if (y != 0)
62
 
              cout << ", ";
63
 
 
64
 
            cout << "\"" << values.val(y) << "\"";
65
 
          }
66
 
          cout << ")";
67
 
        }
68
 
 
69
 
        cout << ";" << endl;
70
 
        break;
71
 
      }
72
 
    case Event::DELETE:
73
 
      {
74
 
        int32_t x;
75
 
        Event_Value values= event.values(0);
76
 
 
77
 
        cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";
78
 
 
79
 
        for (x= 0; x < values.val_size() ; x++)
80
 
        {
81
 
          if (x != 0)
82
 
            cout << ", ";
83
 
 
84
 
          cout << "\"" << values.val(x) << "\"";
85
 
        }
86
 
 
87
 
        cout << ")" << endl;
88
 
        break;
89
 
      }
90
 
    case Event::UPDATE:
91
 
      {
92
 
        int32_t count;
93
 
 
94
 
        for (count= 0; count < event.values_size() ; count++)
95
 
        {
96
 
          int32_t x;
97
 
          Event_Value values= event.values(count);
98
 
 
99
 
          cout << "UPDATE "  << event.table() << " SET ";
100
 
 
101
 
          for (x= 1; x < values.val_size() ; x++)
102
 
          {
103
 
            if (x != 1)
104
 
              cout << ", ";
105
 
 
106
 
            cout << event.field_names(x - 1) << " = \"" << values.val(x) << "\"";
107
 
          }
108
 
 
109
 
          cout << " WHERE " << event.primary_key() << " = " << values.val(0) << endl;
110
 
        }
111
 
 
112
 
        break;
113
 
      }
114
 
    case Event::COMMIT:
115
 
      cout << "COMMIT" << endl;
116
 
      break;
 
147
      case Command::START_TRANSACTION:
 
148
        cout << "START TRANSACTION;";
 
149
        break;
 
150
      case Command::COMMIT:
 
151
        cout << "COMMIT;";
 
152
        break;
 
153
      case Command::ROLLBACK:
 
154
        cout << "ROLLBACK;";
 
155
        break;
 
156
      case Command::INSERT:
 
157
      {
 
158
        printInsert(command, command.insert_record());
 
159
        break;
 
160
      }
 
161
      case Command::DELETE:
 
162
      {
 
163
        printDeleteWithPK(command, command.delete_record());
 
164
        break;
 
165
      }
 
166
      case Command::UPDATE:
 
167
      {
 
168
        printUpdateWithPK(command, command.update_record());
 
169
        break;
 
170
      }
 
171
      default:
 
172
      assert(0);
117
173
    }
118
174
    cout << endl;
119
 
 
120
 
    if (event.has_sql())
121
 
      cout << "Original SQL: " << event.sql() << endl;
122
 
 
123
 
    cout << "AUTOCOMMIT: " << event.autocommit() << endl;
124
 
    cout << "Server id: " << event.server_id() << endl;
125
 
    cout << "Query id: " << event.query_id() << endl;
126
 
    cout << "Transaction id: " << event.transaction_id() << endl;
127
 
    cout << "Schema: " << event.schema() << endl;
128
 
    if (event.type() != Event::DDL)
129
 
      cout << "Table Name: " << event.table() << endl;
130
175
  }
 
176
  cout << "/* Commit Time: " << transaction.end_timestamp() << " */ COMMIT;" << endl;
131
177
}
132
178
 
133
179
int main(int argc, char* argv[])
137
183
 
138
184
  if (argc != 2)
139
185
  {
140
 
    cerr << "Usage:  " << argv[0] << " replication event log " << endl;
 
186
    cerr << "Usage:  " << argv[0] << " TRANSACTION_LOG" << endl;
141
187
    return -1;
142
188
  }
143
189
 
144
 
  EventList list;
 
190
  Transaction transaction;
145
191
 
146
192
  if ((file= open(argv[1], O_RDONLY)) == -1)
147
193
  {
148
 
    cerr << "Can not open file: " << argv[0] << endl;
 
194
    cerr << "Can not open file: " << argv[1] << endl;
149
195
  }
150
196
 
 
197
  char *buffer= NULL;
 
198
  char *temp_buffer;
 
199
 
151
200
  while (1)
152
201
  {
153
202
    uint64_t length;
154
 
    char *buffer= NULL;
155
 
    char *temp_buffer;
156
203
 
157
204
    /* Read the size */
158
205
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
163
210
      cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
164
211
      exit(1);
165
212
    }
 
213
 
166
214
    temp_buffer= (char *)realloc(buffer, (size_t)length);
167
215
    if (temp_buffer == NULL)
168
216
    {
169
 
      cerr << "Memory allocation failure trying to " << length << "."  << endl;
 
217
      cerr << "Memory allocation failure trying to allocate " << length << " bytes."  << endl;
170
218
      exit(1);
171
219
    }
 
220
    memset(temp_buffer, 0, length);
172
221
    buffer= temp_buffer;
 
222
    size_t read_bytes= 0;
173
223
 
174
 
    /* Read the record */
175
 
    if (read(file, buffer, (size_t)length) != (ssize_t)length)
 
224
    /* Read the transaction */
 
225
    if ((read_bytes= read(file, buffer, (uint64_t)length)) != (uint64_t)length)
176
226
    {
177
 
      cerr << "Could not read entire record." << endl;
 
227
      cerr << "Could not read entire transaction. Read " << read_bytes << " bytes instead of " << length << " bytes." << endl;
178
228
      exit(1);
179
229
    }
180
 
    list.ParseFromArray(buffer, (int)length);
 
230
    transaction.ParseFromArray(buffer, (int) length);
181
231
 
182
 
    /* Print the record */
183
 
    printRecord(&list);
 
232
    /* Print the transaction */
 
233
    printTransaction(transaction);
184
234
  }
185
 
 
186
 
 
187
235
  return 0;
188
236
}