~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/replication_event_reader.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <drizzled/global.h>
 
2
#include <sys/types.h>
 
3
#include <sys/stat.h>
 
4
#include <fcntl.h>
 
5
#include <iostream>
 
6
#include <fstream>
 
7
#include <string>
 
8
#include <unistd.h>
 
9
#include <drizzled/serialize/replication_event.pb.h>
 
10
 
 
11
using namespace std;
 
12
using namespace drizzle;
 
13
 
 
14
/*
 
15
  Example reader application for master.info data.
 
16
*/
 
17
 
 
18
void printRecord(const ::drizzle::EventList *list)
 
19
{
 
20
  int32_t e_size;
 
21
 
 
22
  for (e_size= 0; e_size < list->event_size(); e_size++)
 
23
  {
 
24
    const Event event= list->event(e_size);
 
25
 
 
26
    if (e_size != 0)
 
27
      cout << endl << "##########################################################################################" << endl << endl;
 
28
 
 
29
    switch (event.type())
 
30
    {
 
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.value_size() ; y++)
 
60
          {
 
61
            if (y != 0)
 
62
              cout << ", ";
 
63
 
 
64
            cout << "\"" << values.value(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.value_size() ; x++)
 
80
        {
 
81
          if (x != 0)
 
82
            cout << ", ";
 
83
 
 
84
          cout << "\"" << values.value(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.value_size() ; x++)
 
102
          {
 
103
            if (x != 1)
 
104
              cout << ", ";
 
105
 
 
106
            cout << event.field_names(x - 1) << " = \"" << values.value(x) << "\"";
 
107
          }
 
108
 
 
109
          cout << " WHERE " << event.primary_key() << " = " << values.value(0) << endl;
 
110
        }
 
111
 
 
112
        break;
 
113
      }
 
114
    case Event::COMMIT:
 
115
      cout << "COMMIT" << endl;
 
116
      break;
 
117
    }
 
118
    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
  }
 
131
}
 
132
 
 
133
int main(int argc, char* argv[])
 
134
{
 
135
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
136
  int file;
 
137
 
 
138
  if (argc != 2)
 
139
  {
 
140
    cerr << "Usage:  " << argv[0] << " replication event log " << endl;
 
141
    return -1;
 
142
  }
 
143
 
 
144
  EventList list;
 
145
 
 
146
  if ((file= open(argv[1], O_RDONLY)) == -1)
 
147
  {
 
148
    cerr << "Can not open file: " << argv[0] << endl;
 
149
  }
 
150
 
 
151
  while (1)
 
152
  {
 
153
    uint64_t length;
 
154
    char *buffer= NULL;
 
155
    char *temp_buffer;
 
156
 
 
157
    /* Read the size */
 
158
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
 
159
      break;
 
160
 
 
161
    if (length > SIZE_MAX)
 
162
    {
 
163
      cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
 
164
      exit(1);
 
165
    }
 
166
    temp_buffer= (char *)realloc(buffer, (size_t)length);
 
167
    if (temp_buffer == NULL)
 
168
    {
 
169
      cerr << "Memory allocation failure trying to " << length << "."  << endl;
 
170
      exit(1);
 
171
    }
 
172
    buffer= temp_buffer;
 
173
 
 
174
    /* Read the record */
 
175
    if (read(file, buffer, (off_t)length) != (off_t)length)
 
176
    {
 
177
      cerr << "Could not read entire record." << endl;
 
178
      exit(1);
 
179
    }
 
180
    list.ParseFromArray(buffer, length);
 
181
 
 
182
    /* Print the record */
 
183
    printRecord(&list);
 
184
  }
 
185
 
 
186
 
 
187
  return 0;
 
188
}