~drizzle-trunk/drizzle/development

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