~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/replication_event_writer.cc

  • Committer: Monty Taylor
  • Date: 2008-08-16 21:06:22 UTC
  • Revision ID: monty@inaugust.com-20080816210622-zpnn13unyinqzn72
Updated po files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <iostream>
2
 
#include <fstream>
3
 
#include <unistd.h>
4
 
#include <sys/types.h>
5
 
#include <sys/stat.h>
6
 
#include <fcntl.h>
7
 
#include <string>
8
 
#include <uuid/uuid.h>
9
 
 
10
 
#include <drizzled/serialize/replication_event.pb.h>
11
 
using namespace std;
12
 
 
13
 
static uint64_t query_id= 0;
14
 
char transaction_id[37];
15
 
 
16
 
/*
17
 
  Example script for reader a Drizzle master replication list.
18
 
*/
19
 
 
20
 
void write_ddl(drizzle::Event *record, const char *sql)
21
 
{
22
 
  uuid_t uu;
23
 
 
24
 
  uuid_generate_time(uu);
25
 
  uuid_unparse(uu, transaction_id);
26
 
 
27
 
  using namespace drizzle;
28
 
  record->set_type(Event::DDL);
29
 
  record->set_autocommit(true);
30
 
  record->set_server_id("localhost");
31
 
  record->set_query_id(query_id++);
32
 
  record->set_transaction_id(transaction_id);
33
 
  record->set_schema("test");
34
 
  record->set_sql(sql);
35
 
}
36
 
 
37
 
void write_insert(drizzle::Event *record, const char *trx)
38
 
{
39
 
  using namespace drizzle;
40
 
  Event::Value *value;
41
 
 
42
 
  record->set_type(Event::INSERT);
43
 
  record->set_autocommit(true);
44
 
  record->set_server_id("localhost");
45
 
  record->set_query_id(query_id++);
46
 
  record->set_transaction_id(trx);
47
 
  record->set_schema("test");
48
 
  record->set_table("t1");
49
 
  record->set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
50
 
 
51
 
  /* Add Field Names */
52
 
  record->add_field_names("a");
53
 
 
54
 
  /* Add values (first row) */
55
 
  value= record->add_values();
56
 
  value->add_value("1");
57
 
 
58
 
  /* Add values (second row) */
59
 
  value= record->add_values();
60
 
  value->add_value("2");
61
 
}
62
 
 
63
 
void write_delete(drizzle::Event *record, const char *trx)
64
 
{
65
 
  using namespace drizzle;
66
 
  uuid_t uu;
67
 
  Event::Value *value;
68
 
 
69
 
  record->set_type(Event::DELETE);
70
 
  record->set_autocommit(true);
71
 
  record->set_server_id("localhost");
72
 
  record->set_query_id(query_id++);
73
 
  record->set_transaction_id(trx);
74
 
  record->set_schema("test");
75
 
  record->set_table("t1");
76
 
  record->set_sql("DELETE FROM t1 WHERE a IN (1, 2)");
77
 
 
78
 
  /* Add Field Names */
79
 
  record->set_primary_key("a");
80
 
 
81
 
  /* Add values for IN() */
82
 
  value= record->add_values();
83
 
  value->add_value("1");
84
 
  value->add_value("2");
85
 
}
86
 
 
87
 
void write_update(drizzle::Event *record, const char *trx)
88
 
{
89
 
  using namespace drizzle;
90
 
  Event::Value *value;
91
 
 
92
 
  record->set_type(Event::UPDATE);
93
 
  record->set_autocommit(true);
94
 
  record->set_server_id("localhost");
95
 
  record->set_query_id(query_id++);
96
 
  record->set_transaction_id(trx);
97
 
  record->set_schema("test");
98
 
  record->set_table("t1");
99
 
  record->set_sql("UPDATE t1 SET a=5 WHERE a = 1 ");
100
 
  record->set_primary_key("a");
101
 
 
102
 
  /* Add Field Names */
103
 
  record->add_field_names("a");
104
 
 
105
 
  /* Add values (first row) */
106
 
  value= record->add_values();
107
 
  value->add_value("1"); // The first value is always the primary key comparison value
108
 
  value->add_value("5");
109
 
 
110
 
  /* Add values (second row) */
111
 
  value= record->add_values();
112
 
  value->add_value("2");
113
 
  value->add_value("6");
114
 
}
115
 
 
116
 
void write_to_disk(int file, drizzle::EventList *list)
117
 
{
118
 
  std::string buffer;
119
 
  uint64_t length;
120
 
  size_t written;
121
 
 
122
 
  list->SerializePartialToString(&buffer);
123
 
 
124
 
  length= buffer.length();
125
 
 
126
 
  cout << "Writing record of " << length << "." << endl;
127
 
 
128
 
  if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
129
 
  {
130
 
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
131
 
    exit(1);
132
 
  }
133
 
 
134
 
  if ((written= write(file, buffer.c_str(), length)) != length)
135
 
  {
136
 
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
137
 
    exit(1);
138
 
  }
139
 
}
140
 
 
141
 
 
142
 
int main(int argc, char* argv[])
143
 
{
144
 
  GOOGLE_PROTOBUF_VERIFY_VERSION;
145
 
  int file;
146
 
 
147
 
  if (argc != 2) 
148
 
  {
149
 
    cerr << "Usage:  " << argv[0] << " REPLICATION_EVENT_LOG " << endl;
150
 
    return -1;
151
 
  }
152
 
 
153
 
  if ((file= open(argv[1], O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
154
 
  {
155
 
    cerr << "Can not open file: " << argv[0] << endl;
156
 
   exit(0);
157
 
  }
158
 
 
159
 
  drizzle::EventList list;
160
 
 
161
 
  /* Write first set of records */
162
 
  write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
163
 
  write_insert(list.add_event(), transaction_id);
164
 
 
165
 
  write_to_disk(file, &list);
166
 
 
167
 
  /* Write Second set of records */
168
 
  write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
169
 
  write_delete(list.add_event(), transaction_id);
170
 
  write_update(list.add_event(), transaction_id);
171
 
 
172
 
  write_to_disk(file, &list);
173
 
 
174
 
  close(file);
175
 
 
176
 
  return 0;
177
 
}