MySQL 9.0.0
Source Code Documentation
classic_protocol_codec_session_track.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_ROUTER_CLASSIC_PROTOCOL_CODEC_SESSION_TRACK_H_
27#define MYSQL_ROUTER_CLASSIC_PROTOCOL_CODEC_SESSION_TRACK_H_
28
29// codecs for classic_protocol::session_track:: messages
30
37
38namespace classic_protocol {
39
40/**
41 * codec for session_track::TransactionState.
42 *
43 * part of session_track::Field
44 */
45template <>
47 : public impl::EncodeBase<
48 Codec<borrowable::session_track::TransactionState>> {
49 template <class Accumulator>
50 constexpr auto accumulate_fields(Accumulator &&accu) const {
51 namespace bw = borrowable::wire;
52
53 return accu
54 .step(bw::FixedInt<1>(0x08)) // length
55 .step(bw::FixedInt<1>(v_.trx_type()))
56 .step(bw::FixedInt<1>(v_.read_unsafe()))
57 .step(bw::FixedInt<1>(v_.read_trx()))
58 .step(bw::FixedInt<1>(v_.write_unsafe()))
59 .step(bw::FixedInt<1>(v_.write_trx()))
60 .step(bw::FixedInt<1>(v_.stmt_unsafe()))
61 .step(bw::FixedInt<1>(v_.resultset()))
62 .step(bw::FixedInt<1>(v_.locked_tables()))
63 .result();
64 }
65
66 public:
69
70 friend __base;
71
73 : __base(caps), v_{std::move(v)} {}
74
75 constexpr static uint8_t type_byte() { return 0x05; }
76
77 /**
78 * decode a session_track::TransactionState from a buffer-sequence.
79 *
80 * @param buffer input buffer
81 * @param caps protocol capabilities
82 *
83 * @retval std::pair<size_t, message::server::Ok> on success, with bytes
84 * processed
85 * @retval codec_errc::not_enough_input not enough data to parse the whole
86 * message
87 */
91
92 namespace bw = borrowable::wire;
93
94 const auto payload_length_res = accu.template step<bw::VarInt>();
95 if (!accu.result()) return stdx::unexpected(accu.result().error());
96
97 if (payload_length_res->value() != 0x08) {
98 // length of the payload that follows.
99 return stdx::unexpected(make_error_code(std::errc::bad_message));
100 }
101
102 const auto trx_type_res = accu.template step<bw::FixedInt<1>>();
103 const auto read_unsafe_res = accu.template step<bw::FixedInt<1>>();
104 const auto read_trx_res = accu.template step<bw::FixedInt<1>>();
105 const auto write_unsafe_res = accu.template step<bw::FixedInt<1>>();
106 const auto write_trx_res = accu.template step<bw::FixedInt<1>>();
107 const auto stmt_unsafe_res = accu.template step<bw::FixedInt<1>>();
108 const auto resultset_res = accu.template step<bw::FixedInt<1>>();
109 const auto locked_tables_res = accu.template step<bw::FixedInt<1>>();
110
111 if (!accu.result()) return stdx::unexpected(accu.result().error());
112
113 return std::make_pair(
114 accu.result().value(),
115 value_type(trx_type_res->value(), read_unsafe_res->value(),
116 read_trx_res->value(), write_unsafe_res->value(),
117 write_trx_res->value(), stmt_unsafe_res->value(),
118 resultset_res->value(), locked_tables_res->value()));
119 }
120
121 private:
123};
124
125/**
126 * codec for session_track::TransactionCharacteristics.
127 *
128 * part of session_track::Field
129 */
130template <bool Borrowed>
132 : public impl::EncodeBase<Codec<
133 borrowable::session_track::TransactionCharacteristics<Borrowed>>> {
134 template <class Accumulator>
135 constexpr auto accumulate_fields(Accumulator &&accu) const {
136 namespace bw = borrowable::wire;
137
138 return accu.step(bw::VarString<Borrowed>(v_.characteristics())).result();
139 }
140
141 public:
145
146 friend __base;
147
149 : __base(caps), v_{std::move(v)} {}
150
151 constexpr static uint8_t type_byte() { return 0x04; }
152
153 /**
154 * decode a session_track::TransactionCharacteristics from a buffer-sequence.
155 *
156 * @param buffer input buffer
157 * @param caps protocol capabilities
158 *
159 * @retval std::pair<size_t, session_track::TransactionCharacteristics> on
160 * success, with bytes processed
161 * @retval codec_errc::not_enough_input not enough data to parse the whole
162 * message
163 */
167
168 namespace bw = borrowable::wire;
169
170 auto characteristics_res = accu.template step<bw::VarString<Borrowed>>();
171
172 if (!accu.result()) return stdx::unexpected(accu.result().error());
173
174 return std::make_pair(accu.result().value(),
175 value_type(characteristics_res->value()));
176 }
177
178 private:
180};
181
182/**
183 * codec for session_track::State.
184 *
185 * part of session_track::Field
186 */
187template <>
189 : public impl::EncodeBase<Codec<session_track::State>> {
190 template <class Accumulator>
191 constexpr auto accumulate_fields(Accumulator &&accu) const {
192 namespace bw = borrowable::wire;
193
194 return accu.step(bw::FixedInt<1>(v_.state())).result();
195 }
196
197 public:
200
201 friend __base;
202
204 : __base(caps), v_{std::move(v)} {}
205
206 constexpr static uint8_t type_byte() { return 0x02; }
207
208 /**
209 * decode a session_track::State from a buffer-sequence.
210 *
211 * @param buffer input buffer
212 * @param caps protocol capabilities
213 *
214 * @retval std::pair<size_t, session_track::State> on success, with bytes
215 * processed
216 * @retval codec_errc::not_enough_input not enough data to parse the whole
217 * message
218 */
222
223 namespace bw = borrowable::wire;
224
225 auto state_res = accu.template step<bw::FixedInt<1>>();
226
227 if (!accu.result()) return stdx::unexpected(accu.result().error());
228
229 return std::make_pair(accu.result().value(),
230 value_type(state_res->value()));
231 }
232
233 private:
235};
236
237/**
238 * codec for session_track::Schema.
239 *
240 * part of session_track::Field
241 */
242template <bool Borrowed>
243class Codec<borrowable::session_track::Schema<Borrowed>>
244 : public impl::EncodeBase<
245 Codec<borrowable::session_track::Schema<Borrowed>>> {
246 template <class Accumulator>
247 constexpr auto accumulate_fields(Accumulator &&accu) const {
248 namespace bw = borrowable::wire;
249
250 return accu.step(bw::VarString<Borrowed>(v_.schema())).result();
251 }
252
253 public:
256
257 friend __base;
258
260 : __base(caps), v_{std::move(v)} {}
261
262 constexpr static uint8_t type_byte() { return 0x01; }
263
264 /**
265 * decode a session_track::Schema from a buffer-sequence.
266 *
267 * @param buffer input buffer
268 * @param caps protocol capabilities
269 *
270 * @retval std::pair<size_t, session_track::Schema> on success, with bytes
271 * processed
272 * @retval codec_errc::not_enough_input not enough data to parse the whole
273 * message
274 */
278
279 namespace bw = borrowable::wire;
280
281 auto schema_res = accu.template step<bw::VarString<Borrowed>>();
282
283 if (!accu.result()) return stdx::unexpected(accu.result().error());
284
285 return std::make_pair(accu.result().value(),
286 value_type(schema_res->value()));
287 }
288
289 private:
291};
292
293/**
294 * codec for session_track::SystemVariable.
295 *
296 * part of session_track::Field
297 */
298template <bool Borrowed>
299class Codec<borrowable::session_track::SystemVariable<Borrowed>>
300 : public impl::EncodeBase<
301 Codec<borrowable::session_track::SystemVariable<Borrowed>>> {
302 template <class Accumulator>
303 constexpr auto accumulate_fields(Accumulator &&accu) const {
304 namespace bw = borrowable::wire;
305
306 return accu.step(bw::VarString<Borrowed>(v_.key()))
307 .step(bw::VarString<Borrowed>(v_.value()))
308 .result();
309 }
310
311 public:
314
315 friend __base;
316
318 : __base(caps), v_{std::move(v)} {}
319
320 constexpr static uint8_t type_byte() { return 0x00; }
321
322 /**
323 * decode a session_track::SystemVariable from a buffer-sequence.
324 *
325 * @param buffer input buffer
326 * @param caps protocol capabilities
327 *
328 * @retval std::pair<size_t, session_track::SystemVariable> on success, with
329 * bytes processed
330 * @retval codec_errc::not_enough_input not enough data to parse the whole
331 * message
332 */
336
337 namespace bw = borrowable::wire;
338
339 auto key_res = accu.template step<bw::VarString<Borrowed>>();
340 auto value_res = accu.template step<bw::VarString<Borrowed>>();
341
342 if (!accu.result()) return stdx::unexpected(accu.result().error());
343
344 return std::make_pair(accu.result().value(),
345 value_type(key_res->value(), value_res->value()));
346 }
347
348 private:
350};
351
352/**
353 * codec for session_track::Gtid.
354 *
355 * format:
356 *
357 * - FixedInt<int> spec (only 0 is in use for now)
358 * - VarString payload (payload according to spec).
359 *
360 * payload for spec 0:
361 * - GTID in human-readable form like 4dd0f9d5-3b00-11eb-ad70-003093140e4e:23929
362 *
363 * part of session_track::Field
364 */
365template <bool Borrowed>
366class Codec<borrowable::session_track::Gtid<Borrowed>>
367 : public impl::EncodeBase<
368 Codec<borrowable::session_track::Gtid<Borrowed>>> {
369 template <class Accumulator>
370 constexpr auto accumulate_fields(Accumulator &&accu) const {
371 namespace bw = borrowable::wire;
372
373 return accu.step(bw::FixedInt<1>(v_.spec()))
374 .step(bw::VarString<Borrowed>(v_.gtid()))
375 .result();
376 }
377
378 public:
381
382 friend __base;
383
385 : __base(caps), v_{std::move(v)} {}
386
387 constexpr static uint8_t type_byte() { return 0x03; }
388
389 /**
390 * decode a session_track::Gtid from a buffer-sequence.
391 *
392 * @param buffer input buffer
393 * @param caps protocol capabilities
394 *
395 * @retval std::pair<size_t, session_track::Gtid> on success, with bytes
396 * processed
397 * @retval codec_errc::not_enough_input not enough data to parse the whole
398 * message
399 */
403
404 namespace bw = borrowable::wire;
405
406 auto spec_res = accu.template step<bw::FixedInt<1>>();
407 auto gtid_res = accu.template step<bw::VarString<Borrowed>>();
408
409 if (!accu.result()) return stdx::unexpected(accu.result().error());
410
411 return std::make_pair(accu.result().value(),
412 value_type(spec_res->value(), gtid_res->value()));
413 }
414
415 private:
417};
418
419/**
420 * codec for session-track's Field.
421 *
422 * sent as part of a server::Ok and server::Eof message.
423 *
424 * - FixedInt<1> type
425 * - VarString data
426 *
427 * data is encoded according type:
428 *
429 * - 0x00 session_track::SystemVariable
430 * - 0x01 session_track::Schema
431 * - 0x02 session_track::StateChanged
432 * - 0x03 session_track::Gtid
433 * - 0x04 session_track::TransactionCharacteristics
434 * - 0x05 session_track::TransactionState
435 */
436template <bool Borrowed>
437class Codec<borrowable::session_track::Field<Borrowed>>
438 : public impl::EncodeBase<
439 Codec<borrowable::session_track::Field<Borrowed>>> {
440 template <class Accumulator>
441 constexpr auto accumulate_fields(Accumulator &&accu) const {
442 namespace bw = borrowable::wire;
443
444 return accu.step(bw::FixedInt<1>(v_.type()))
445 .step(bw::VarString<Borrowed>(v_.data()))
446 .result();
447 }
448
449 public:
451
453
454 friend __base;
455
457 : __base(caps), v_{std::move(v)} {}
458
459 /**
460 * decode a session_track::Field from a buffer-sequence.
461 *
462 * @param buffer input buffer
463 * @param caps protocol capabilities
464 *
465 * @retval std::pair<size_t, session_track::Field> on success, with bytes
466 * processed
467 * @retval codec_errc::not_enough_input not enough data to parse the whole
468 * message
469 */
473
474 namespace bw = borrowable::wire;
475
476 auto type_res = accu.template step<bw::FixedInt<1>>();
477 auto data_res = accu.template step<bw::VarString<Borrowed>>();
478
479 if (!accu.result()) return stdx::unexpected(accu.result().error());
480
481 return std::make_pair(accu.result().value(),
482 value_type(type_res->value(), data_res->value()));
483 }
484
485 private:
487};
488
489} // namespace classic_protocol
490
491#endif
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::Field from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:470
const value_type v_
Definition: classic_protocol_codec_session_track.h:486
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:456
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:441
friend __base
Definition: classic_protocol_codec_session_track.h:454
constexpr static uint8_t type_byte()
Definition: classic_protocol_codec_session_track.h:387
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:384
const value_type v_
Definition: classic_protocol_codec_session_track.h:416
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::Gtid from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:400
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:370
friend __base
Definition: classic_protocol_codec_session_track.h:382
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::Schema from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:275
friend __base
Definition: classic_protocol_codec_session_track.h:257
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:259
const value_type v_
Definition: classic_protocol_codec_session_track.h:290
constexpr static uint8_t type_byte()
Definition: classic_protocol_codec_session_track.h:262
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:247
friend __base
Definition: classic_protocol_codec_session_track.h:315
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::SystemVariable from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:333
const value_type v_
Definition: classic_protocol_codec_session_track.h:349
constexpr static uint8_t type_byte()
Definition: classic_protocol_codec_session_track.h:320
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:303
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:317
friend __base
Definition: classic_protocol_codec_session_track.h:146
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:148
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:135
constexpr static uint8_t type_byte()
Definition: classic_protocol_codec_session_track.h:151
const value_type v_
Definition: classic_protocol_codec_session_track.h:179
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::TransactionCharacteristics from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:164
friend __base
Definition: classic_protocol_codec_session_track.h:70
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::TransactionState from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:88
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:72
const value_type v_
Definition: classic_protocol_codec_session_track.h:122
constexpr static uint8_t type_byte()
Definition: classic_protocol_codec_session_track.h:75
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:50
friend __base
Definition: classic_protocol_codec_session_track.h:201
static stdx::expected< std::pair< size_t, value_type >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a session_track::State from a buffer-sequence.
Definition: classic_protocol_codec_session_track.h:219
constexpr Codec(value_type v, capabilities::value_type caps)
Definition: classic_protocol_codec_session_track.h:203
constexpr static uint8_t type_byte()
Definition: classic_protocol_codec_session_track.h:206
const value_type v_
Definition: classic_protocol_codec_session_track.h:234
constexpr auto accumulate_fields(Accumulator &&accu) const
Definition: classic_protocol_codec_session_track.h:191
Codec for a type.
Definition: classic_protocol_codec_base.h:72
Field of a session-track array.
Definition: classic_protocol_session_track.h:45
gtid changed.
Definition: classic_protocol_session_track.h:146
schema changed.
Definition: classic_protocol_session_track.h:99
state changed.
Definition: classic_protocol_session_track.h:122
system-variable changed.
Definition: classic_protocol_session_track.h:72
TransactionCharacteristics changed.
Definition: classic_protocol_session_track.h:259
TransactionState changed.
Definition: classic_protocol_session_track.h:195
Generator of decoded Types of a buffer.
Definition: classic_protocol_codec_base.h:153
result_type result() const
get result of the step().
Definition: classic_protocol_codec_base.h:219
CRTP base for the Codec's encode part.
Definition: classic_protocol_codec_base.h:374
Definition: buffer.h:135
Definition: expected.h:284
constexpr const error_type & error() const &
Definition: expected.h:751
constexpr value_type & value() &
Definition: expected.h:636
Definition: expected.h:112
borrowable::session_track::State State
Definition: classic_protocol_session_track.h:289
borrowable::session_track::TransactionState TransactionState
Definition: classic_protocol_session_track.h:286
constexpr value_type session_track
Definition: classic_protocol_constants.h:61
std::bitset< 32 > value_type
Definition: classic_protocol_constants.h:73
borrowable::session_track::State State
Definition: classic_protocol_session_track.h:301
Definition: classic_protocol_binary.h:39
std::error_code make_error_code(codec_errc e) noexcept
Definition: classic_protocol_codec_error.h:86
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
Definition: gcs_xcom_synode.h:64
unexpected(E) -> unexpected< E >