1
#include "binary_log.h"
3
#include <google/protobuf/io/coded_stream.h>
5
using namespace google::protobuf;
6
using namespace google::protobuf::io;
9
BinaryLog::Event::write(CodedOutputStream* out) const
11
// We frame each event in a length encoded in a special manner, and
12
// end it with a CRC-32 checksum.
14
// Write length and type
15
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
16
unsigned char *end= length_encode(m_message->ByteSize(), buf);
19
char cs[4] = { 0 }; // !!! No checksum yet
20
if (!out->WriteRaw(buf, end - buf) || // Length + Type
21
!m_message->SerializeToCodedStream(out) || // Event body
22
!out->WriteRaw(cs, sizeof(cs))) // Checksum
30
BinaryLog::Event::read(CodedInputStream *in)
32
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
34
// Read length peek byte to figure out length
35
if (!in->ReadRaw(buf, 1))
38
// Read in the rest of the length bytes plus the type
39
size_t bytes= length_decode_bytes(*buf);
40
if (!in->ReadRaw(buf + 1, bytes))
44
(void) length_decode(buf, &length);
46
// Fetch type from read buffer
47
m_type= static_cast<EventType>(buf[bytes]);
49
// Create the right event based on the type code (is there something
50
// better in the protobuf library?)
51
Message *message= NULL;
54
message= new BinaryLog::Query;
58
message= new BinaryLog::Commit;
62
message= new BinaryLog::Rollback;
66
message= new BinaryLog::Start;
70
message= new BinaryLog::Chain;
77
// Read the event body as length bytes. It is necessary to limit the
78
// stream since otherwise ParseFromCodedStream reads all bytes of
80
CodedInputStream::Limit limit= in->PushLimit(length);
81
if (!message->ParseFromCodedStream(in))
87
// Read checksum (none here yet)
89
if (!in->ReadRaw(checksum, sizeof(checksum)))
94
template <class EventClass>
95
void print_common(std::ostream& out, EventClass* event)
97
out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
102
BinaryLog::Event::print(std::ostream& out) const
104
using namespace google::protobuf;
109
Query *event= static_cast<Query*>(m_message);
110
print_common(out, event);
111
for (RepeatedPtrField<Query::Variable>::const_iterator ii= event->variable().begin() ;
112
ii != event->variable().end() ;
115
out << "set @" << ii->name() << " = '" << ii->value() << "'\n";
117
out << event->query() << std::endl;
123
Commit *event= static_cast<Commit*>(m_message);
124
print_common(out, event);
131
Rollback *event= static_cast<Rollback*>(m_message);
132
print_common(out, event);
139
Start *event= static_cast<Start*>(m_message);
140
print_common(out, event);
147
Chain *event= static_cast<Chain*>(m_message);
148
print_common(out, event);