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 (!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;
82
// Read the event body as length bytes. It is necessary to limit the
83
// stream since otherwise ParseFromCodedStream reads all bytes of
85
CodedInputStream::Limit limit= in->PushLimit(length);
86
if (!message->ParseFromCodedStream(in))
92
// Read checksum (none here yet)
94
if (!in->ReadRaw(checksum, sizeof(checksum)))
99
template <class EventClass>
100
void print_common(std::ostream& out, EventClass* event)
102
out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
107
BinaryLog::Event::print(std::ostream& out) const
109
using namespace google::protobuf;
114
Query *event= static_cast<Query*>(m_message);
115
print_common(out, event);
116
for (RepeatedPtrField<Query::Variable>::const_iterator ii= event->variable().begin() ;
117
ii != event->variable().end() ;
120
out << "set @" << ii->name() << " = '" << ii->val() << "'\n";
122
out << event->query() << std::endl;
128
Commit *event= static_cast<Commit*>(m_message);
129
print_common(out, event);
136
Rollback *event= static_cast<Rollback*>(m_message);
137
print_common(out, event);
144
Start *event= static_cast<Start*>(m_message);
145
print_common(out, event);
152
Chain *event= static_cast<Chain*>(m_message);
153
print_common(out, event);