~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>
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
8
9
#include <sys/time.h>
10
11
#include <drizzled/message/transaction.pb.h>
12
13
/** 
14
 * @file Example script for writing transactions to a log file.
15
 */
779.3.18 by Monty Taylor
Cleaned up warnings up through innodb.
16
636 by Brian Aker
First pass with new event API (yeah... it will be better).
17
using namespace std;
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
18
using namespace drizzled::message;
636 by Brian Aker
First pass with new event API (yeah... it will be better).
19
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
20
static uint32_t server_id= 1;
21
static uint64_t transaction_id= 0;
22
23
uint64_t getNanoTimestamp()
24
{
25
#ifdef HAVE_CLOCK_GETTIME
26
  struct timespec tp;
27
  clock_gettime(CLOCK_REALTIME, &tp);
28
  return (uint64_t) tp.tv_sec * 10000000
29
       + (uint64_t) tp.tv_nsec;
30
#else
31
  struct timeval tv;
32
  gettimeofday(&tv,NULL);
33
  return (uint64_t) tv.tv_sec * 10000000
34
       + (uint64_t) tv.tv_usec * 1000;
35
#endif
36
}
37
38
void writeCommit(drizzled::message::Command &record)
39
{
40
  record.set_type(Command::COMMIT);
41
  record.set_timestamp(getNanoTimestamp());
42
43
  drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
44
  trx->set_server_id(server_id);
45
  trx->set_transaction_id(transaction_id);
46
}
47
48
void writeRollback(drizzled::message::Command &record)
49
{
50
  record.set_type(Command::ROLLBACK);
51
  record.set_timestamp(getNanoTimestamp());
52
53
  drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
54
  trx->set_server_id(server_id);
55
  trx->set_transaction_id(transaction_id);
56
}
57
58
void writeStartTransaction(drizzled::message::Command &record)
59
{
60
  record.set_type(Command::START_TRANSACTION);
61
  record.set_timestamp(getNanoTimestamp());
62
63
  drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
64
  trx->set_server_id(server_id);
65
  trx->set_transaction_id(transaction_id);
66
}
67
68
void writeInsert(drizzled::message::Command &record)
69
{
70
  record.set_type(Command::INSERT);
71
  record.set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
72
  record.set_timestamp(getNanoTimestamp());
73
  record.set_schema("test");
74
  record.set_table("t1");
75
76
  drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
77
  trx->set_server_id(server_id);
78
  trx->set_transaction_id(transaction_id);
79
80
  drizzled::message::InsertRecord *irecord= record.mutable_insert_record();
81
82
  /* Add Fields and Values... */
83
84
  Table::Field *field= irecord->add_insert_field();
85
  field->set_name("a");
86
  field->set_type(drizzled::message::Table::Field::VARCHAR);
87
88
  irecord->add_insert_value("1");
89
  irecord->add_insert_value("2");
90
}
91
92
void writeDeleteWithPK(drizzled::message::Command &record)
93
{
94
  record.set_type(Command::DELETE);
95
  record.set_sql("DELETE FROM t1 WHERE a = 1");
96
  record.set_timestamp(getNanoTimestamp());
97
  record.set_schema("test");
98
  record.set_table("t1");
99
100
  drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
101
  trx->set_server_id(server_id);
102
  trx->set_transaction_id(transaction_id);
103
104
  drizzled::message::DeleteRecord *drecord= record.mutable_delete_record();
105
106
  Table::Field *field= drecord->add_where_field();
107
  field->set_name("a");
108
  field->set_type(drizzled::message::Table::Field::VARCHAR);
109
110
  drecord->add_where_value("1");
111
}
112
113
void writeUpdateWithPK(drizzled::message::Command &record)
114
{
115
  record.set_type(Command::UPDATE);
116
  record.set_sql("UPDATE t1 SET a = 5 WHERE a = 1;");
117
  record.set_timestamp(getNanoTimestamp());
118
  record.set_schema("test");
119
  record.set_table("t1");
120
121
  drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
122
  trx->set_server_id(server_id);
123
  trx->set_transaction_id(transaction_id);
124
125
  drizzled::message::UpdateRecord *urecord= record.mutable_update_record();
126
127
  Table::Field *field;
128
  
129
  field= urecord->add_update_field();
130
  field->set_name("a");
131
  field->set_type(drizzled::message::Table::Field::VARCHAR);
132
133
  urecord->add_after_value("5");
134
135
  field= urecord->add_where_field();
136
  field->set_name("a");
137
  field->set_type(drizzled::message::Table::Field::VARCHAR);
138
139
  urecord->add_where_value("1");
140
}
141
142
void writeTransaction(int file, drizzled::message::Transaction &transaction)
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
143
{
144
  std::string buffer;
779.3.18 by Monty Taylor
Cleaned up warnings up through innodb.
145
  size_t length;
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
146
  size_t written;
147
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
148
  drizzled::message::TransactionContext *trx= transaction.mutable_transaction_context();
149
  trx->set_server_id(server_id);
150
  trx->set_transaction_id(transaction_id);
151
152
  transaction.SerializeToString(&buffer);
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
153
154
  length= buffer.length();
155
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
156
  cout << "Writing transaction of " << length << " length." << endl;
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
157
158
  if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
159
  {
160
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
161
    exit(1);
162
  }
163
164
  if ((written= write(file, buffer.c_str(), length)) != length)
165
  {
166
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
167
    exit(1);
168
  }
169
}
170
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
171
int main(int argc, char* argv[])
636 by Brian Aker
First pass with new event API (yeah... it will be better).
172
{
173
  GOOGLE_PROTOBUF_VERIFY_VERSION;
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
174
  int file;
636 by Brian Aker
First pass with new event API (yeah... it will be better).
175
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
176
  if (argc != 2) 
177
  {
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
178
    cerr << "Usage:  " << argv[0] << " TRANSACTION_LOG" << endl;
636 by Brian Aker
First pass with new event API (yeah... it will be better).
179
    return -1;
180
  }
181
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
182
  if ((file= open(argv[1], O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
183
  {
184
    cerr << "Can not open file: " << argv[0] << endl;
185
   exit(0);
186
  }
187
988.1.5 by Jay Pipes
Removal of log.cc (binlog), added Applier plugin and fixed up Replicator
188
  /* Write a series of statements which test each type of record class */
189
  transaction_id++;
190
191
  /* Simple INSERT statement */
192
  Transaction transaction;
193
  transaction.set_start_timestamp(getNanoTimestamp());
194
  writeStartTransaction(*transaction.add_command());
195
  writeInsert(*transaction.add_command());
196
  writeCommit(*transaction.add_command());
197
  transaction.set_end_timestamp(getNanoTimestamp());
198
199
  writeTransaction(file, transaction);
200
201
  transaction.Clear();
202
203
  /* Write a DELETE and an UPDATE in one transaction */
204
  transaction_id++;
205
  transaction.set_start_timestamp(getNanoTimestamp());
206
  writeStartTransaction(*transaction.add_command());
207
  writeDeleteWithPK(*transaction.add_command());
208
  writeUpdateWithPK(*transaction.add_command());
209
  writeCommit(*transaction.add_command());
210
  transaction.set_end_timestamp(getNanoTimestamp());
211
212
  writeTransaction(file, transaction);
671 by Brian Aker
Cleaned up events for writing in replication (using simple file
213
214
  close(file);
636 by Brian Aker
First pass with new event API (yeah... it will be better).
215
216
  return 0;
217
}