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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <sys/time.h>
#include <drizzled/message/transaction.pb.h>
/**
* @file Example script for writing transactions to a log file.
*/
using namespace std;
using namespace drizzled::message;
static uint32_t server_id= 1;
static uint64_t transaction_id= 0;
uint64_t getNanoTimestamp()
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
return (uint64_t) tp.tv_sec * 10000000
+ (uint64_t) tp.tv_nsec;
#else
struct timeval tv;
gettimeofday(&tv,NULL);
return (uint64_t) tv.tv_sec * 10000000
+ (uint64_t) tv.tv_usec * 1000;
#endif
}
void writeCommit(drizzled::message::Command &record)
{
record.set_type(Command::COMMIT);
record.set_timestamp(getNanoTimestamp());
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
}
void writeRollback(drizzled::message::Command &record)
{
record.set_type(Command::ROLLBACK);
record.set_timestamp(getNanoTimestamp());
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
}
void writeStartTransaction(drizzled::message::Command &record)
{
record.set_type(Command::START_TRANSACTION);
record.set_timestamp(getNanoTimestamp());
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
}
void writeInsert(drizzled::message::Command &record)
{
record.set_type(Command::INSERT);
record.set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
record.set_timestamp(getNanoTimestamp());
record.set_schema("test");
record.set_table("t1");
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
drizzled::message::InsertRecord *irecord= record.mutable_insert_record();
/* Add Fields and Values... */
Table::Field *field= irecord->add_insert_field();
field->set_name("a");
field->set_type(drizzled::message::Table::Field::VARCHAR);
irecord->add_insert_value("1");
irecord->add_insert_value("2");
}
void writeDeleteWithPK(drizzled::message::Command &record)
{
record.set_type(Command::DELETE);
record.set_sql("DELETE FROM t1 WHERE a = 1");
record.set_timestamp(getNanoTimestamp());
record.set_schema("test");
record.set_table("t1");
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
drizzled::message::DeleteRecord *drecord= record.mutable_delete_record();
Table::Field *field= drecord->add_where_field();
field->set_name("a");
field->set_type(drizzled::message::Table::Field::VARCHAR);
drecord->add_where_value("1");
}
void writeUpdateWithPK(drizzled::message::Command &record)
{
record.set_type(Command::UPDATE);
record.set_sql("UPDATE t1 SET a = 5 WHERE a = 1;");
record.set_timestamp(getNanoTimestamp());
record.set_schema("test");
record.set_table("t1");
drizzled::message::TransactionContext *trx= record.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
drizzled::message::UpdateRecord *urecord= record.mutable_update_record();
Table::Field *field;
field= urecord->add_update_field();
field->set_name("a");
field->set_type(drizzled::message::Table::Field::VARCHAR);
urecord->add_after_value("5");
field= urecord->add_where_field();
field->set_name("a");
field->set_type(drizzled::message::Table::Field::VARCHAR);
urecord->add_where_value("1");
}
void writeTransaction(int file, drizzled::message::Transaction &transaction)
{
std::string buffer;
size_t length;
size_t written;
drizzled::message::TransactionContext *trx= transaction.mutable_transaction_context();
trx->set_server_id(server_id);
trx->set_transaction_id(transaction_id);
transaction.SerializeToString(&buffer);
length= buffer.length();
cout << "Writing transaction of " << length << " 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] << " TRANSACTION_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);
}
/* Write a series of statements which test each type of record class */
transaction_id++;
/* Simple INSERT statement */
Transaction transaction;
transaction.set_start_timestamp(getNanoTimestamp());
writeStartTransaction(*transaction.add_command());
writeInsert(*transaction.add_command());
writeCommit(*transaction.add_command());
transaction.set_end_timestamp(getNanoTimestamp());
writeTransaction(file, transaction);
transaction.Clear();
/* Write a DELETE and an UPDATE in one transaction */
transaction_id++;
transaction.set_start_timestamp(getNanoTimestamp());
writeStartTransaction(*transaction.add_command());
writeDeleteWithPK(*transaction.add_command());
writeUpdateWithPK(*transaction.add_command());
writeCommit(*transaction.add_command());
transaction.set_end_timestamp(getNanoTimestamp());
writeTransaction(file, transaction);
close(file);
return 0;
}
|