~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/binary_log.cc

  • Committer: Brian Aker
  • Date: 2010-12-08 22:35:56 UTC
  • mfrom: (1819.9.158 update-innobase)
  • Revision ID: brian@tangent.org-20101208223556-37mi4omqg7lkjzf3
Merge in Stewart's changes, 1.3 changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <drizzled/global.h>
2
 
#include <drizzled/serialize/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 (!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;
73
 
  }
74
 
 
75
 
  if (!message)
76
 
    return false;
77
 
 
78
 
  // Read the event body as length bytes. It is necessary to limit the
79
 
  // stream since otherwise ParseFromCodedStream reads all bytes of
80
 
  // the stream.
81
 
  CodedInputStream::Limit limit= in->PushLimit(length);
82
 
  if (!message->ParseFromCodedStream(in))
83
 
    return false;
84
 
  in->PopLimit(limit);
85
 
  delete m_message;
86
 
  m_message= message;
87
 
 
88
 
  // Read checksum (none here yet)
89
 
  char checksum[4];
90
 
  if (!in->ReadRaw(checksum, sizeof(checksum)))
91
 
    return false;
92
 
  return true;
93
 
}
94
 
 
95
 
template <class EventClass>
96
 
void print_common(std::ostream& out, EventClass* event)
97
 
{
98
 
  out << "# Global Id: (" << event->header().server_id() << "," << event->header().trans_id() << ")\n";
99
 
}
100
 
 
101
 
 
102
 
void
103
 
BinaryLog::Event::print(std::ostream& out) const
104
 
{
105
 
  using namespace google::protobuf;
106
 
 
107
 
  switch (m_type) {
108
 
  case QUERY:
109
 
  {
110
 
    Query *event= static_cast<Query*>(m_message);
111
 
    print_common(out, event);
112
 
    for (RepeatedPtrField<Query::Variable>::const_iterator ii= event->variable().begin() ;
113
 
         ii != event->variable().end() ;
114
 
         ++ii)
115
 
    {
116
 
      out << "set @" << ii->name() << " = '" << ii->value() << "'\n";
117
 
    }
118
 
    out << event->query() << std::endl;
119
 
    break;
120
 
  }
121
 
 
122
 
  case COMMIT:
123
 
  {
124
 
    Commit *event= static_cast<Commit*>(m_message);
125
 
    print_common(out, event);
126
 
    // NYI !!!
127
 
    break;
128
 
  }
129
 
 
130
 
  case ROLLBACK:
131
 
  {
132
 
    Rollback *event= static_cast<Rollback*>(m_message);
133
 
    print_common(out, event);
134
 
    // NYI !!!
135
 
    break;
136
 
  }
137
 
 
138
 
  case START:
139
 
  {
140
 
    Start *event= static_cast<Start*>(m_message);
141
 
    print_common(out, event);
142
 
    // NYI !!!
143
 
    break;
144
 
  }
145
 
 
146
 
  case CHAIN:
147
 
  {
148
 
    Chain *event= static_cast<Chain*>(m_message);
149
 
    print_common(out, event);
150
 
    // NYI !!!
151
 
    break;
152
 
  }
153
 
 
154
 
  default:
155
 
    break;                                      /* Nothing */
156
 
  }
157
 
}