1
#include <drizzled/global.h>
2
#include <drizzled/message/binary_log.h>
4
#include <google/protobuf/io/coded_stream.h>
6
using namespace google;
9
BinaryLog::Event::write(protobuf::io::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 GOOGLE_PROTOBUF_VERSION >= 2001000
21
out->WriteRaw(buf, end - buf); // Length + Type
23
|| !m_message->SerializeToCodedStream(out)) // Event body
25
out->WriteRaw(cs, sizeof(cs)); // Checksum
29
if (!out->WriteRaw(buf, end - buf) || // Length + Type
30
!m_message->SerializeToCodedStream(out) || // Event body
31
!out->WriteRaw(cs, sizeof(cs))) // Checksum
40
BinaryLog::Event::read(protobuf::io::CodedInputStream *in)
42
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
44
// Read length peek byte to figure out length
45
if (!in->ReadRaw(buf, 1))
48
// Read in the rest of the length bytes plus the type
49
size_t bytes= length_decode_bytes(*buf);
50
if (!in->ReadRaw(buf + 1, bytes))
54
(void) length_decode(buf, &length);
56
// Fetch type from read buffer
57
m_type= static_cast<EventType>(buf[bytes]);
59
// Create the right event based on the type code (is there something
60
// better in the protobuf library?)
61
protobuf::Message *message= NULL;
64
message= new BinaryLog::Query;
68
message= new BinaryLog::Commit;
72
message= new BinaryLog::Rollback;
76
message= new BinaryLog::Start;
80
message= new BinaryLog::Chain;
91
// Read the event body as length bytes. It is necessary to limit the
92
// stream since otherwise ParseFromCodedStream reads all bytes of
94
protobuf::io::CodedInputStream::Limit limit= in->PushLimit(length);
95
if (!message->ParseFromCodedStream(in))
101
// Read checksum (none here yet)
103
if (!in->ReadRaw(checksum, sizeof(checksum)))
108
template <class EventClass>
109
void print_common(std::ostream& out, EventClass* event)
111
out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
116
BinaryLog::Event::print(std::ostream& out) const
121
Query *event= static_cast<Query*>(m_message);
122
print_common(out, event);
123
for (protobuf::RepeatedPtrField<Query::Variable>::const_iterator ii=
124
event->variable().begin() ;
125
ii != event->variable().end() ;
128
out << "set @" << ii->name() << " = '" << ii->val() << "'\n";
130
out << event->query() << std::endl;
136
Commit *event= static_cast<Commit*>(m_message);
137
print_common(out, event);
144
Rollback *event= static_cast<Rollback*>(m_message);
145
print_common(out, event);
152
Start *event= static_cast<Start*>(m_message);
153
print_common(out, event);
160
Chain *event= static_cast<Chain*>(m_message);
161
print_common(out, event);