~drizzle-trunk/drizzle/development

612.2.4 by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere.
1
#include <drizzled/global.h>
2
#include <drizzled/serialize/binary_log.h>
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
3
4
#include <google/protobuf/io/coded_stream.h>
5
6
using namespace google::protobuf;
7
using namespace google::protobuf::io;
8
9
bool
10
BinaryLog::Event::write(CodedOutputStream* out) const
11
{
12
  // We frame each event in a length encoded in a special manner, and
13
  // end it with a CRC-32 checksum.
14
15
  // Write length and type
16
  unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
17
  unsigned char *end= length_encode(m_message->ByteSize(), buf);
18
  *end++= m_type;
19
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
24
    return false;
25
26
  return true;
27
}
28
29
30
bool
31
BinaryLog::Event::read(CodedInputStream *in)
32
{
33
  unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
34
35
  // Read length peek byte to figure out length
36
  if (!in->ReadRaw(buf, 1))
37
    return false;
38
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))
42
    return false;
43
44
  size_t length;
45
  (void) length_decode(buf, &length);
46
47
  // Fetch type from read buffer
48
  m_type= static_cast<EventType>(buf[bytes]);
49
50
  // Create the right event based on the type code (is there something
51
  // better in the protobuf library?)
52
  Message *message= NULL;
53
  switch (m_type) {
54
  case QUERY:
55
    message= new BinaryLog::Query;
56
    break;
57
58
  case COMMIT:
59
    message= new BinaryLog::Commit;
60
    break;
61
62
  case ROLLBACK:
63
    message= new BinaryLog::Rollback;
64
    break;
65
66
  case START:
67
    message= new BinaryLog::Start;
68
    break;
69
70
  case CHAIN:
71
    message= new BinaryLog::Chain;
72
    break;
779.3.10 by Monty Taylor
Turned on -Wshadow.
73
74
  case COUNT:
75
  case UNDEF:
76
    break;
324.1.1 by Mats Kindahl
Adding specification of a simple protobuf-based binary log format,
77
  }
78
79
  if (!message)
80
    return false;
81
82
  // Read the event body as length bytes. It is necessary to limit the
83
  // stream since otherwise ParseFromCodedStream reads all bytes of
84
  // the stream.
85
  CodedInputStream::Limit limit= in->PushLimit(length);
86
  if (!message->ParseFromCodedStream(in))
87
    return false;
88
  in->PopLimit(limit);
89
  delete m_message;
90
  m_message= message;
91
92
  // Read checksum (none here yet)
93
  char checksum[4];
94
  if (!in->ReadRaw(checksum, sizeof(checksum)))
95
    return false;
96
  return true;
97
}
98
99
template <class EventClass>
100
void print_common(std::ostream& out, EventClass* event)
101
{
102
  out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
103
}
104
105
106
void
107
BinaryLog::Event::print(std::ostream& out) const
108
{
109
  using namespace google::protobuf;
110
111
  switch (m_type) {
112
  case QUERY:
113
  {
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() ;
118
         ++ii)
119
    {
120
      out << "set @" << ii->name() << " = '" << ii->value() << "'\n";
121
    }
122
    out << event->query() << std::endl;
123
    break;
124
  }
125
126
  case COMMIT:
127
  {
128
    Commit *event= static_cast<Commit*>(m_message);
129
    print_common(out, event);
130
    // NYI !!!
131
    break;
132
  }
133
134
  case ROLLBACK:
135
  {
136
    Rollback *event= static_cast<Rollback*>(m_message);
137
    print_common(out, event);
138
    // NYI !!!
139
    break;
140
  }
141
142
  case START:
143
  {
144
    Start *event= static_cast<Start*>(m_message);
145
    print_common(out, event);
146
    // NYI !!!
147
    break;
148
  }
149
150
  case CHAIN:
151
  {
152
    Chain *event= static_cast<Chain*>(m_message);
153
    print_common(out, event);
154
    // NYI !!!
155
    break;
156
  }
157
158
  default:
159
    break;                                      /* Nothing */
160
  }
161
}