~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/replication_event_writer.cc

  • Committer: Monty Taylor
  • Date: 2009-03-25 21:06:47 UTC
  • mto: This revision was merged to the branch mainline in revision 964.
  • Revision ID: mordred@inaugust.com-20090325210647-7j1tm98gvct3jxsu
Removed legacy_db_type.

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
 
 
12
using namespace std;
 
13
using namespace drizzle;
 
14
 
 
15
static uint64_t query_id= 0;
 
16
char transaction_id[37];
 
17
 
 
18
/*
 
19
  Example script for reader a Drizzle master replication list.
 
20
*/
 
21
 
 
22
void write_ddl(::drizzle::Event *record, const char *sql)
 
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
 
 
38
void write_insert(::drizzle::Event *record, const char *trx)
 
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_val("1");
 
57
 
 
58
  /* Add values (second row) */
 
59
  value= record->add_values();
 
60
  value->add_val("2");
 
61
}
 
62
 
 
63
void write_delete(::drizzle::Event *record, const char *trx)
 
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_val("1");
 
82
  value->add_val("2");
 
83
}
 
84
 
 
85
void write_update(::drizzle::Event *record, const char *trx)
 
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_val("1"); // The first value is always the primary key comparison value
 
105
  value->add_val("5");
 
106
 
 
107
  /* Add values (second row) */
 
108
  value= record->add_values();
 
109
  value->add_val("2");
 
110
  value->add_val("6");
 
111
}
 
112
 
 
113
void write_to_disk(int file, ::drizzle::EventList *list)
 
114
{
 
115
  std::string buffer;
 
116
  size_t length;
 
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
 
 
139
int main(int argc, char* argv[])
 
140
{
 
141
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
142
  int file;
 
143
 
 
144
  if (argc != 2) 
 
145
  {
 
146
    cerr << "Usage:  " << argv[0] << " REPLICATION_EVENT_LOG " << endl;
 
147
    return -1;
 
148
  }
 
149
 
 
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
 
 
156
  EventList list;
 
157
 
 
158
  /* Write first set of records */
 
159
  write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
 
160
  write_insert(list.add_event(), transaction_id);
 
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");
 
166
  write_delete(list.add_event(), transaction_id);
 
167
  write_update(list.add_event(), transaction_id);
 
168
 
 
169
  write_to_disk(file, &list);
 
170
 
 
171
  close(file);
 
172
 
 
173
  return 0;
 
174
}