1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/message/binary_log.h>
23
#include <google/protobuf/io/coded_stream.h>
25
using namespace google;
28
BinaryLog::Event::write(protobuf::io::CodedOutputStream* out) const
30
// We frame each event in a length encoded in a special manner, and
31
// end it with a CRC-32 checksum.
33
// Write length and type
34
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
35
unsigned char *end= length_encode(m_message->ByteSize(), buf);
38
char cs[4] = { 0 }; // !!! No checksum yet
39
#if GOOGLE_PROTOBUF_VERSION >= 2001000
40
out->WriteRaw(buf, static_cast<int>(end - buf)); // Length + Type
42
|| !m_message->SerializeToCodedStream(out)) // Event body
44
out->WriteRaw(cs, sizeof(cs)); // Checksum
48
if (!out->WriteRaw(buf, end - buf) || // Length + Type
49
!m_message->SerializeToCodedStream(out) || // Event body
50
!out->WriteRaw(cs, sizeof(cs))) // Checksum
59
BinaryLog::Event::read(protobuf::io::CodedInputStream *in)
61
unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
63
// Read length peek byte to figure out length
64
if (!in->ReadRaw(buf, 1))
67
// Read in the rest of the length bytes plus the type
68
size_t bytes= length_decode_bytes(*buf);
69
if (! in->ReadRaw(buf + 1, static_cast<int>(bytes)))
73
(void) length_decode(buf, &length);
75
// Fetch type from read buffer
76
m_type= static_cast<EventType>(buf[bytes]);
78
// Create the right event based on the type code (is there something
79
// better in the protobuf library?)
80
protobuf::Message *message= NULL;
83
message= new BinaryLog::Query;
87
message= new BinaryLog::Commit;
91
message= new BinaryLog::Rollback;
95
message= new BinaryLog::Start;
99
message= new BinaryLog::Chain;
110
// Read the event body as length bytes. It is necessary to limit the
111
// stream since otherwise ParseFromCodedStream reads all bytes of
113
protobuf::io::CodedInputStream::Limit limit= in->PushLimit(static_cast<int>(length));
114
if (!message->ParseFromCodedStream(in))
120
// Read checksum (none here yet)
122
if (!in->ReadRaw(checksum, sizeof(checksum)))
127
template <class EventClass>
128
void print_common(std::ostream& out, EventClass* event)
130
out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
135
BinaryLog::Event::print(std::ostream& out) const
140
Query *event= static_cast<Query*>(m_message);
141
print_common(out, event);
142
for (protobuf::RepeatedPtrField<Query::Variable>::const_iterator ii=
143
event->variable().begin() ;
144
ii != event->variable().end() ;
147
out << "set @" << ii->name() << " = '" << ii->val() << "'\n";
149
out << event->query() << std::endl;
155
Commit *event= static_cast<Commit*>(m_message);
156
print_common(out, event);
163
Rollback *event= static_cast<Rollback*>(m_message);
164
print_common(out, event);
171
Start *event= static_cast<Start*>(m_message);
172
print_common(out, event);
179
Chain *event= static_cast<Chain*>(m_message);
180
print_common(out, event);