~posulliv/drizzle/memcached_applier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <drizzled/global.h>
#include <drizzled/message/binary_log.h>

#include <google/protobuf/io/coded_stream.h>

using namespace google;

bool
BinaryLog::Event::write(protobuf::io::CodedOutputStream* out) const
{
  // We frame each event in a length encoded in a special manner, and
  // end it with a CRC-32 checksum.

  // Write length and type
  unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];
  unsigned char *end= length_encode(m_message->ByteSize(), buf);
  *end++= m_type;

  char cs[4] = { 0 };                           // !!! No checksum yet
#if GOOGLE_PROTOBUF_VERSION >= 2001000
  out->WriteRaw(buf, end - buf); // Length + Type
  if (out->HadError()
    || !m_message->SerializeToCodedStream(out)) // Event body
    return false;
  out->WriteRaw(cs, sizeof(cs)); // Checksum
  if (out->HadError())
    return false;
#else
  if (!out->WriteRaw(buf, end - buf) ||         // Length + Type
      !m_message->SerializeToCodedStream(out) || // Event body
      !out->WriteRaw(cs, sizeof(cs)))           // Checksum
    return false;
#endif

  return true;
}


bool
BinaryLog::Event::read(protobuf::io::CodedInputStream *in)
{
  unsigned char buf[LENGTH_ENCODE_MAX_BYTES + 1];

  // Read length peek byte to figure out length
  if (!in->ReadRaw(buf, 1))
    return false;

  // Read in the rest of the length bytes plus the type
  size_t bytes= length_decode_bytes(*buf);
  if (!in->ReadRaw(buf + 1, bytes))
    return false;

  size_t length;
  (void) length_decode(buf, &length);

  // Fetch type from read buffer
  m_type= static_cast<EventType>(buf[bytes]);

  // Create the right event based on the type code (is there something
  // better in the protobuf library?)
  protobuf::Message *message= NULL;
  switch (m_type) {
  case QUERY:
    message= new BinaryLog::Query;
    break;

  case COMMIT:
    message= new BinaryLog::Commit;
    break;

  case ROLLBACK:
    message= new BinaryLog::Rollback;
    break;

  case START:
    message= new BinaryLog::Start;
    break;

  case CHAIN:
    message= new BinaryLog::Chain;
    break;

  case COUNT:
  case UNDEF:
    break;
  }

  if (!message)
    return false;

  // Read the event body as length bytes. It is necessary to limit the
  // stream since otherwise ParseFromCodedStream reads all bytes of
  // the stream.
  protobuf::io::CodedInputStream::Limit limit= in->PushLimit(length);
  if (!message->ParseFromCodedStream(in))
    return false;
  in->PopLimit(limit);
  delete m_message;
  m_message= message;

  // Read checksum (none here yet)
  char checksum[4];
  if (!in->ReadRaw(checksum, sizeof(checksum)))
    return false;
  return true;
}

template <class EventClass>
void print_common(std::ostream& out, EventClass* event)
{
  out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
}


void
BinaryLog::Event::print(std::ostream& out) const
{
  switch (m_type) {
  case QUERY:
  {
    Query *event= static_cast<Query*>(m_message);
    print_common(out, event);
    for (protobuf::RepeatedPtrField<Query::Variable>::const_iterator ii=
           event->variable().begin() ;
         ii != event->variable().end() ;
         ++ii)
    {
      out << "set @" << ii->name() << " = '" << ii->val() << "'\n";
    }
    out << event->query() << std::endl;
    break;
  }

  case COMMIT:
  {
    Commit *event= static_cast<Commit*>(m_message);
    print_common(out, event);
    // NYI !!!
    break;
  }

  case ROLLBACK:
  {
    Rollback *event= static_cast<Rollback*>(m_message);
    print_common(out, event);
    // NYI !!!
    break;
  }

  case START:
  {
    Start *event= static_cast<Start*>(m_message);
    print_common(out, event);
    // NYI !!!
    break;
  }

  case CHAIN:
  {
    Chain *event= static_cast<Chain*>(m_message);
    print_common(out, event);
    // NYI !!!
    break;
  }

  default:
    break;                                      /* Nothing */
  }
}