1
#include <drizzled/global.h>
2
#include <drizzled/message/binary_log.h>
4
#include <google/protobuf/io/coded_stream.h>
6
using namespace google::protobuf;
7
using namespace google::protobuf::io;
10
BinaryLog::Event::write(CodedOutputStream* out) const
12
// We frame each event in a length encoded in a special manner, and
13
// end it with a CRC-32 checksum.
15
// Write length and type
16
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
17
unsigned char *end= length_encode(m_message->ByteSize(), buf);
20
char cs[4] = { 0 }; // !!! No checksum yet
21
#if GOOGLE_PROTOBUF_VERSION >= 2001000
22
out->WriteRaw(buf, end - buf); // Length + Type
24
|| !m_message->SerializeToCodedStream(out)) // Event body
26
out->WriteRaw(cs, sizeof(cs)); // Checksum
30
if (!out->WriteRaw(buf, end - buf) || // Length + Type
31
!m_message->SerializeToCodedStream(out) || // Event body
32
!out->WriteRaw(cs, sizeof(cs))) // Checksum
41
BinaryLog::Event::read(CodedInputStream *in)
43
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
45
// Read length peek byte to figure out length
46
if (!in->ReadRaw(buf, 1))
49
// Read in the rest of the length bytes plus the type
50
size_t bytes= length_decode_bytes(*buf);
51
if (!in->ReadRaw(buf + 1, bytes))
55
(void) length_decode(buf, &length);
57
// Fetch type from read buffer
58
m_type= static_cast<EventType>(buf[bytes]);
60
// Create the right event based on the type code (is there something
61
// better in the protobuf library?)
62
Message *message= NULL;
65
message= new BinaryLog::Query;
69
message= new BinaryLog::Commit;
73
message= new BinaryLog::Rollback;
77
message= new BinaryLog::Start;
81
message= new BinaryLog::Chain;
92
// Read the event body as length bytes. It is necessary to limit the
93
// stream since otherwise ParseFromCodedStream reads all bytes of
95
CodedInputStream::Limit limit= in->PushLimit(length);
96
if (!message->ParseFromCodedStream(in))
102
// Read checksum (none here yet)
104
if (!in->ReadRaw(checksum, sizeof(checksum)))
109
template <class EventClass>
110
void print_common(std::ostream& out, EventClass* event)
112
out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
117
BinaryLog::Event::print(std::ostream& out) const
119
using namespace google::protobuf;
124
Query *event= static_cast<Query*>(m_message);
125
print_common(out, event);
126
for (RepeatedPtrField<Query::Variable>::const_iterator ii= event->variable().begin() ;
127
ii != event->variable().end() ;
130
out << "set @" << ii->name() << " = '" << ii->val() << "'\n";
132
out << event->query() << std::endl;
138
Commit *event= static_cast<Commit*>(m_message);
139
print_common(out, event);
146
Rollback *event= static_cast<Rollback*>(m_message);
147
print_common(out, event);
154
Start *event= static_cast<Start*>(m_message);
155
print_common(out, event);
162
Chain *event= static_cast<Chain*>(m_message);
163
print_common(out, event);