~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/binary_log.cc

  • Committer: Devananda
  • Date: 2009-07-04 01:55:13 UTC
  • mto: (1086.10.1 length-plugin)
  • mto: This revision was merged to the branch mainline in revision 1095.
  • Revision ID: deva@myst-20090704015513-gtqliazxtfm7sdvf
refactored function/length into plugin/length

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <drizzled/global.h>
 
2
#include <drizzled/message/binary_log.h>
 
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 GOOGLE_PROTOBUF_VERSION >= 2001000
 
22
  out->WriteRaw(buf, end - buf); // Length + Type
 
23
  if (out->HadError()
 
24
    || !m_message->SerializeToCodedStream(out)) // Event body
 
25
    return false;
 
26
  out->WriteRaw(cs, sizeof(cs)); // Checksum
 
27
  if (out->HadError())
 
28
    return false;
 
29
#else
 
30
  if (!out->WriteRaw(buf, end - buf) ||         // Length + Type
 
31
      !m_message->SerializeToCodedStream(out) || // Event body
 
32
      !out->WriteRaw(cs, sizeof(cs)))           // Checksum
 
33
    return false;
 
34
#endif
 
35
 
 
36
  return true;
 
37
}
 
38
 
 
39
 
 
40
bool
 
41
BinaryLog::Event::read(CodedInputStream *in)
 
42
{
 
43
  unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
 
44
 
 
45
  // Read length peek byte to figure out length
 
46
  if (!in->ReadRaw(buf, 1))
 
47
    return false;
 
48
 
 
49
  // Read in the rest of the length bytes plus the type
 
50
  size_t bytes= length_decode_bytes(*buf);
 
51
  if (!in->ReadRaw(buf + 1, bytes))
 
52
    return false;
 
53
 
 
54
  size_t length;
 
55
  (void) length_decode(buf, &length);
 
56
 
 
57
  // Fetch type from read buffer
 
58
  m_type= static_cast<EventType>(buf[bytes]);
 
59
 
 
60
  // Create the right event based on the type code (is there something
 
61
  // better in the protobuf library?)
 
62
  Message *message= NULL;
 
63
  switch (m_type) {
 
64
  case QUERY:
 
65
    message= new BinaryLog::Query;
 
66
    break;
 
67
 
 
68
  case COMMIT:
 
69
    message= new BinaryLog::Commit;
 
70
    break;
 
71
 
 
72
  case ROLLBACK:
 
73
    message= new BinaryLog::Rollback;
 
74
    break;
 
75
 
 
76
  case START:
 
77
    message= new BinaryLog::Start;
 
78
    break;
 
79
 
 
80
  case CHAIN:
 
81
    message= new BinaryLog::Chain;
 
82
    break;
 
83
 
 
84
  case COUNT:
 
85
  case UNDEF:
 
86
    break;
 
87
  }
 
88
 
 
89
  if (!message)
 
90
    return false;
 
91
 
 
92
  // Read the event body as length bytes. It is necessary to limit the
 
93
  // stream since otherwise ParseFromCodedStream reads all bytes of
 
94
  // the stream.
 
95
  CodedInputStream::Limit limit= in->PushLimit(length);
 
96
  if (!message->ParseFromCodedStream(in))
 
97
    return false;
 
98
  in->PopLimit(limit);
 
99
  delete m_message;
 
100
  m_message= message;
 
101
 
 
102
  // Read checksum (none here yet)
 
103
  char checksum[4];
 
104
  if (!in->ReadRaw(checksum, sizeof(checksum)))
 
105
    return false;
 
106
  return true;
 
107
}
 
108
 
 
109
template <class EventClass>
 
110
void print_common(std::ostream& out, EventClass* event)
 
111
{
 
112
  out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
 
113
}
 
114
 
 
115
 
 
116
void
 
117
BinaryLog::Event::print(std::ostream& out) const
 
118
{
 
119
  using namespace google::protobuf;
 
120
 
 
121
  switch (m_type) {
 
122
  case QUERY:
 
123
  {
 
124
    Query *event= static_cast<Query*>(m_message);
 
125
    print_common(out, event);
 
126
    for (RepeatedPtrField<Query::Variable>::const_iterator ii= event->variable().begin() ;
 
127
         ii != event->variable().end() ;
 
128
         ++ii)
 
129
    {
 
130
      out << "set @" << ii->name() << " = '" << ii->val() << "'\n";
 
131
    }
 
132
    out << event->query() << std::endl;
 
133
    break;
 
134
  }
 
135
 
 
136
  case COMMIT:
 
137
  {
 
138
    Commit *event= static_cast<Commit*>(m_message);
 
139
    print_common(out, event);
 
140
    // NYI !!!
 
141
    break;
 
142
  }
 
143
 
 
144
  case ROLLBACK:
 
145
  {
 
146
    Rollback *event= static_cast<Rollback*>(m_message);
 
147
    print_common(out, event);
 
148
    // NYI !!!
 
149
    break;
 
150
  }
 
151
 
 
152
  case START:
 
153
  {
 
154
    Start *event= static_cast<Start*>(m_message);
 
155
    print_common(out, event);
 
156
    // NYI !!!
 
157
    break;
 
158
  }
 
159
 
 
160
  case CHAIN:
 
161
  {
 
162
    Chain *event= static_cast<Chain*>(m_message);
 
163
    print_common(out, event);
 
164
    // NYI !!!
 
165
    break;
 
166
  }
 
167
 
 
168
  default:
 
169
    break;                                      /* Nothing */
 
170
  }
 
171
}