~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/binary_log.cc

  • Committer: Brian Aker
  • Date: 2009-08-18 07:19:56 UTC
  • mfrom: (1116.1.3 stewart)
  • mto: This revision was merged to the branch mainline in revision 1118.
  • Revision ID: brian@gaz-20090818071956-nfpoe9rp3i7p50kx
Merge my branch from Stewart into one branch

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