~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-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <sys/types.h>
 
2
#include <sys/stat.h>
 
3
#include <fcntl.h>
 
4
#include <iostream>
 
5
#include <fstream>
 
6
#include <string>
 
7
#include <unistd.h>
 
8
#include <drizzled/serialize/replication_event.pb.h>
 
9
using namespace std;
 
10
 
 
11
/*
 
12
  Example reader application for master.info data.
 
13
*/
 
14
 
 
15
void printRecord(const drizzle::EventList *list)
 
16
{
 
17
  using namespace drizzle;
 
18
  uint32_t x;
 
19
 
 
20
  for (x= 0; x < list->event_size(); x++)
 
21
  {
 
22
    const drizzle::Event event= list->event(x);
 
23
 
 
24
    if (x != 0)
 
25
      cout << endl << "##########################################################################################" << endl << endl;
 
26
 
 
27
    switch (event.type())
 
28
    {
 
29
    case Event::DDL:
 
30
      cout << "DDL ";
 
31
      break;
 
32
    case Event::INSERT:
 
33
      {
 
34
        uint32_t x;
 
35
 
 
36
        cout << "INSERT INTO " << event.table() << " (";
 
37
 
 
38
        for (x= 0; x < event.field_names_size() ; x++)
 
39
        {
 
40
          if (x != 0)
 
41
            cout << ", ";
 
42
 
 
43
          cout << event.field_names(x);
 
44
        }
 
45
 
 
46
        cout << ") VALUES " << endl;
 
47
 
 
48
        for (x= 0; x < event.values_size(); x++)
 
49
        {
 
50
          uint32_t y;
 
51
          Event_Value values= event.values(x);
 
52
 
 
53
          if (x != 0)
 
54
            cout << ", ";
 
55
 
 
56
          cout << "(";
 
57
          for (y= 0; y < values.value_size() ; y++)
 
58
          {
 
59
            if (y != 0)
 
60
              cout << ", ";
 
61
 
 
62
            cout << "\"" << values.value(y) << "\"";
 
63
          }
 
64
          cout << ")";
 
65
        }
 
66
 
 
67
        cout << ";" << endl;
 
68
        break;
 
69
      }
 
70
    case Event::DELETE:
 
71
      {
 
72
        uint32_t x;
 
73
        Event_Value values= event.values(0);
 
74
 
 
75
        cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";
 
76
 
 
77
        for (x= 0; x < values.value_size() ; x++)
 
78
        {
 
79
          if (x != 0)
 
80
            cout << ", ";
 
81
 
 
82
          cout << "\"" << values.value(x) << "\"";
 
83
        }
 
84
 
 
85
        cout << ")" << endl;
 
86
        break;
 
87
      }
 
88
    case Event::UPDATE:
 
89
      {
 
90
        uint32_t count;
 
91
 
 
92
        for (count= 0; count < event.values_size() ; count++)
 
93
        {
 
94
          uint32_t x;
 
95
          Event_Value values= event.values(count);
 
96
 
 
97
          cout << "UPDATE "  << event.table() << " SET ";
 
98
 
 
99
          for (x= 1; x < values.value_size() ; x++)
 
100
          {
 
101
            if (x != 1)
 
102
              cout << ", ";
 
103
 
 
104
            cout << event.field_names(x - 1) << " = \"" << values.value(x) << "\"";
 
105
          }
 
106
 
 
107
          cout << " WHERE " << event.primary_key() << " = " << values.value(0) << endl;
 
108
        }
 
109
 
 
110
        break;
 
111
      }
 
112
    case Event::COMMIT:
 
113
      cout << "COMMIT" << endl;
 
114
      break;
 
115
    }
 
116
    cout << endl;
 
117
 
 
118
    if (event.has_sql())
 
119
      cout << "Original SQL: " << event.sql() << endl;
 
120
 
 
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;
 
128
  }
 
129
}
 
130
 
 
131
int main(int argc, char* argv[])
 
132
{
 
133
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
134
  int file;
 
135
 
 
136
  if (argc != 2)
 
137
  {
 
138
    cerr << "Usage:  " << argv[0] << " replication event log " << endl;
 
139
    return -1;
 
140
  }
 
141
 
 
142
  drizzle::EventList list;
 
143
 
 
144
  if ((file= open(argv[1], O_RDONLY)) == -1)
 
145
  {
 
146
    cerr << "Can not open file: " << argv[0] << endl;
 
147
  }
 
148
 
 
149
  while (1)
 
150
  {
 
151
    uint64_t length;
 
152
    char *buffer= NULL;
 
153
    char *temp_buffer;
 
154
 
 
155
    /* Read the size */
 
156
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
 
157
      break;
 
158
 
 
159
    temp_buffer= (char *)realloc(buffer, length);
 
160
    if (temp_buffer == NULL)
 
161
    {
 
162
      cerr << "Memory allocation failure trying to " << length << "."  << endl;
 
163
      exit(1);
 
164
    }
 
165
    buffer= temp_buffer;
 
166
 
 
167
    /* Read the record */
 
168
    if (read(file, buffer, length) != length)
 
169
    {
 
170
      cerr << "Could not read entire record." << endl;
 
171
      exit(1);
 
172
    }
 
173
    list.ParseFromArray(buffer, length);
 
174
 
 
175
    /* Print the record */
 
176
    printRecord(&list);
 
177
  }
 
178
 
 
179
 
 
180
  return 0;
 
181
}