2
#include "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 (!out->WriteRaw(buf, end - buf) || // Length + Type
22
!m_message->SerializeToCodedStream(out) || // Event body
23
!out->WriteRaw(cs, sizeof(cs))) // Checksum
31
BinaryLog::Event::read(CodedInputStream *in)
33
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
35
// Read length peek byte to figure out length
36
if (!in->ReadRaw(buf, 1))
39
// Read in the rest of the length bytes plus the type
40
size_t bytes= length_decode_bytes(*buf);
41
if (!in->ReadRaw(buf + 1, bytes))
45
(void) length_decode(buf, &length);
47
// Fetch type from read buffer
48
m_type= static_cast<EventType>(buf[bytes]);
50
// Create the right event based on the type code (is there something
51
// better in the protobuf library?)
52
Message *message= NULL;
55
message= new BinaryLog::Query;
59
message= new BinaryLog::Commit;
63
message= new BinaryLog::Rollback;
67
message= new BinaryLog::Start;
71
message= new BinaryLog::Chain;
78
// Read the event body as length bytes. It is necessary to limit the
79
// stream since otherwise ParseFromCodedStream reads all bytes of
81
CodedInputStream::Limit limit= in->PushLimit(length);
82
if (!message->ParseFromCodedStream(in))
88
// Read checksum (none here yet)
90
if (!in->ReadRaw(checksum, sizeof(checksum)))
95
template <class EventClass>
96
void print_common(std::ostream& out, EventClass* event)
98
out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
103
BinaryLog::Event::print(std::ostream& out) const
105
using namespace google::protobuf;
110
Query *event= static_cast<Query*>(m_message);
111
print_common(out, event);
112
for (RepeatedPtrField<Query::Variable>::const_iterator ii= event->variable().begin() ;
113
ii != event->variable().end() ;
116
out << "set @" << ii->name() << " = '" << ii->value() << "'\n";
118
out << event->query() << std::endl;
124
Commit *event= static_cast<Commit*>(m_message);
125
print_common(out, event);
132
Rollback *event= static_cast<Rollback*>(m_message);
133
print_common(out, event);
140
Start *event= static_cast<Start*>(m_message);
141
print_common(out, event);
148
Chain *event= static_cast<Chain*>(m_message);
149
print_common(out, event);