~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <uuid/uuid.h>

#include <drizzled/serialize/replication_event.pb.h>

using namespace std;
using namespace drizzle;

static uint64_t query_id= 0;
char transaction_id[37];

/*
  Example script for reader a Drizzle master replication list.
*/

void write_ddl(::drizzle::Event *record, const char *sql)
{
  uuid_t uu;

  uuid_generate_time(uu);
  uuid_unparse(uu, transaction_id);

  record->set_type(Event::DDL);
  record->set_autocommit(true);
  record->set_server_id("localhost");
  record->set_query_id(query_id++);
  record->set_transaction_id(transaction_id);
  record->set_schema("test");
  record->set_sql(sql);
}

void write_insert(::drizzle::Event *record, const char *trx)
{
  Event::Value *value;

  record->set_type(Event::INSERT);
  record->set_autocommit(true);
  record->set_server_id("localhost");
  record->set_query_id(query_id++);
  record->set_transaction_id(trx);
  record->set_schema("test");
  record->set_table("t1");
  record->set_sql("INSERT INTO t1 (a) VALUES (1) (2)");

  /* Add Field Names */
  record->add_field_names("a");

  /* Add values (first row) */
  value= record->add_values();
  value->add_value("1");

  /* Add values (second row) */
  value= record->add_values();
  value->add_value("2");
}

void write_delete(::drizzle::Event *record, const char *trx)
{
  Event::Value *value;

  record->set_type(Event::DELETE);
  record->set_autocommit(true);
  record->set_server_id("localhost");
  record->set_query_id(query_id++);
  record->set_transaction_id(trx);
  record->set_schema("test");
  record->set_table("t1");
  record->set_sql("DELETE FROM t1 WHERE a IN (1, 2)");

  /* Add Field Names */
  record->set_primary_key("a");

  /* Add values for IN() */
  value= record->add_values();
  value->add_value("1");
  value->add_value("2");
}

void write_update(::drizzle::Event *record, const char *trx)
{
  Event::Value *value;

  record->set_type(Event::UPDATE);
  record->set_autocommit(true);
  record->set_server_id("localhost");
  record->set_query_id(query_id++);
  record->set_transaction_id(trx);
  record->set_schema("test");
  record->set_table("t1");
  record->set_sql("UPDATE t1 SET a=5 WHERE a = 1 ");
  record->set_primary_key("a");

  /* Add Field Names */
  record->add_field_names("a");

  /* Add values (first row) */
  value= record->add_values();
  value->add_value("1"); // The first value is always the primary key comparison value
  value->add_value("5");

  /* Add values (second row) */
  value= record->add_values();
  value->add_value("2");
  value->add_value("6");
}

void write_to_disk(int file, ::drizzle::EventList *list)
{
  std::string buffer;
  size_t length;
  size_t written;

  list->SerializePartialToString(&buffer);

  length= buffer.length();

  cout << "Writing record of " << length << "." << endl;

  if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
  {
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
    exit(1);
  }

  if ((written= write(file, buffer.c_str(), length)) != length)
  {
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
    exit(1);
  }
}


int main(int argc, char* argv[])
{
  GOOGLE_PROTOBUF_VERIFY_VERSION;
  int file;

  if (argc != 2) 
  {
    cerr << "Usage:  " << argv[0] << " REPLICATION_EVENT_LOG " << endl;
    return -1;
  }

  if ((file= open(argv[1], O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
  {
    cerr << "Can not open file: " << argv[0] << endl;
   exit(0);
  }

  EventList list;

  /* Write first set of records */
  write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
  write_insert(list.add_event(), transaction_id);

  write_to_disk(file, &list);

  /* Write Second set of records */
  write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
  write_delete(list.add_event(), transaction_id);
  write_update(list.add_event(), transaction_id);

  write_to_disk(file, &list);

  close(file);

  return 0;
}