Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ set(source_files
src/binsrv/events/common_header.hpp
src/binsrv/events/common_header.cpp

src/binsrv/events/common_header_view_fwd.hpp
src/binsrv/events/common_header_view.hpp
src/binsrv/events/common_header_view.cpp

src/binsrv/events/common_header_flag_type_fwd.hpp
src/binsrv/events/common_header_flag_type.hpp

Expand All @@ -117,10 +121,18 @@ set(source_files
src/binsrv/events/event.hpp
src/binsrv/events/event.cpp

src/binsrv/events/event_view_fwd.hpp
src/binsrv/events/event_view.hpp
src/binsrv/events/event_view.cpp

src/binsrv/events/footer_fwd.hpp
src/binsrv/events/footer.hpp
src/binsrv/events/footer.cpp

src/binsrv/events/footer_view_fwd.hpp
src/binsrv/events/footer_view.hpp
src/binsrv/events/footer_view.cpp

src/binsrv/events/format_description_body_impl_fwd.hpp
src/binsrv/events/format_description_body_impl.hpp
src/binsrv/events/format_description_body_impl.cpp
Expand Down
83 changes: 12 additions & 71 deletions src/binsrv/events/common_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,25 @@

#include "binsrv/events/common_header.hpp"

#include <ctime>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>

#include <boost/align/align_up.hpp>

#include "binsrv/ctime_timestamp.hpp"

#include "binsrv/events/code_type.hpp"
#include "binsrv/events/common_header_flag_type.hpp"
#include "binsrv/events/common_header_view.hpp"

#include "util/byte_span_extractors.hpp"
#include "util/byte_span_fwd.hpp"
#include "util/conversion_helpers.hpp"
#include "util/exception_location_helpers.hpp"
#include "util/flag_set.hpp"

namespace binsrv::events {

common_header::common_header(util::const_byte_span portion) {
// TODO: rework with direct member initialization

/*
https://github.com/mysql/mysql-server/blob/mysql-8.0.43/libbinlogevents/src/binlog_event.cpp#L198
https://github.com/mysql/mysql-server/blob/mysql-8.4.6/libs/mysql/binlog/event/binlog_event.cpp#L242

The first 19 bytes in the header is as follows:
+============================================+
| member_variable offset : len |
+============================================+
| when.tv_sec 0 : 4 |
+--------------------------------------------+
| type_code EVENT_TYPE_OFFSET(4) : 1 |
+--------------------------------------------+
| server_id SERVER_ID_OFFSET(5) : 4 |
+--------------------------------------------+
| data_written EVENT_LEN_OFFSET(9) : 4 |
+--------------------------------------------+
| log_pos LOG_POS_OFFSET(13) : 4 |
+--------------------------------------------+
| flags FLAGS_OFFSET(17) : 2 |
+--------------------------------------------+
| extra_headers 19 : x-19|
+============================================+
*/

common_header::common_header(const common_header_view &view)
: timestamp_{view.get_timestamp_raw()},
server_id_{view.get_server_id_raw()},
event_size_{view.get_event_size_raw()},
next_event_position_{view.get_next_event_position_raw()},
flags_{view.get_flags_raw()}, type_code_{view.get_type_code_raw()} {
// TODO: initialize size_in_bytes directly based on the sum of fields
// widths instead of this static_assert
static_assert(sizeof timestamp_ + sizeof type_code_ + sizeof server_id_ +
Expand All @@ -74,47 +45,17 @@ common_header::common_header(util::const_byte_span portion) {
static_assert(sizeof *this == boost::alignment::align_up(
size_in_bytes, alignof(decltype(*this))),
"inefficient data member reordering in common_header");

if (std::size(portion) != size_in_bytes) {
util::exception_location().raise<std::invalid_argument>(
"invalid event common header length");
}

auto remainder = portion;
util::extract_fixed_int_from_byte_span(remainder, timestamp_);
util::extract_fixed_int_from_byte_span(remainder, type_code_);
util::extract_fixed_int_from_byte_span(remainder, server_id_);
util::extract_fixed_int_from_byte_span(remainder, event_size_);
util::extract_fixed_int_from_byte_span(remainder, next_event_position_);
util::extract_fixed_int_from_byte_span(remainder, flags_);

if (get_type_code_raw() >= util::enum_to_index(code_type::delimiter) ||
to_string_view(get_type_code()).empty()) {
util::exception_location().raise<std::logic_error>(
"invalid event code in event header");
}
// TODO: check if flags are valid (all the bits have corresponding enum)
}

[[nodiscard]] ctime_timestamp common_header::get_timestamp() const noexcept {
return ctime_timestamp{static_cast<std::time_t>(get_timestamp_raw())};
}

[[nodiscard]] std::string common_header::get_readable_timestamp() const {
return get_timestamp().str();
}
common_header::common_header(util::const_byte_span portion)
: common_header{common_header_view{portion}} {}

[[nodiscard]] std::string_view
common_header::get_readable_type_code() const noexcept {
return to_string_view(get_type_code());
[[nodiscard]] ctime_timestamp common_header::get_timestamp() const noexcept {
return common_header_view_base::get_timestamp_from_raw(get_timestamp_raw());
}

[[nodiscard]] common_header_flag_set common_header::get_flags() const noexcept {
return common_header_flag_set{get_flags_raw()};
}

[[nodiscard]] std::string common_header::get_readable_flags() const {
return to_string(get_flags());
return common_header_view_base::get_flags_from_raw(get_flags_raw());
}

std::ostream &operator<<(std::ostream &output, const common_header &obj) {
Expand Down
23 changes: 17 additions & 6 deletions src/binsrv/events/common_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "binsrv/events/code_type_fwd.hpp"
#include "binsrv/events/common_header_flag_type_fwd.hpp"
#include "binsrv/events/common_header_view.hpp"
#include "binsrv/events/protocol_traits_fwd.hpp"

#include "util/byte_span_fwd.hpp"
Expand All @@ -35,24 +36,31 @@ namespace binsrv::events {

class [[nodiscard]] common_header {
public:
static constexpr std::size_t size_in_bytes{default_common_header_length};
static constexpr std::size_t size_in_bytes{
common_header_view_base::size_in_bytes};

explicit common_header(const common_header_view &view);
explicit common_header(util::const_byte_span portion);

[[nodiscard]] std::uint32_t get_timestamp_raw() const noexcept {
return timestamp_;
}
[[nodiscard]] ctime_timestamp get_timestamp() const noexcept;

[[nodiscard]] std::string get_readable_timestamp() const;
[[nodiscard]] std::string get_readable_timestamp() const {
return common_header_view_base::get_readable_timestamp_from_raw(
get_timestamp_raw());
}

[[nodiscard]] std::uint8_t get_type_code_raw() const noexcept {
return type_code_;
}
[[nodiscard]] code_type get_type_code() const noexcept {
return static_cast<code_type>(get_type_code_raw());
return common_header_view_base::get_type_code_from_raw(get_type_code_raw());
}
[[nodiscard]] std::string_view get_readable_type_code() const noexcept {
return common_header_view_base::get_readable_type_code_from_raw(
get_type_code_raw());
}
[[nodiscard]] std::string_view get_readable_type_code() const noexcept;

[[nodiscard]] std::uint32_t get_server_id_raw() const noexcept {
return server_id_;
Expand All @@ -68,7 +76,10 @@ class [[nodiscard]] common_header {

[[nodiscard]] std::uint16_t get_flags_raw() const noexcept { return flags_; }
[[nodiscard]] common_header_flag_set get_flags() const noexcept;
[[nodiscard]] std::string get_readable_flags() const;
[[nodiscard]] std::string get_readable_flags() const {
return common_header_view_base::get_readable_flags_from_raw(
get_flags_raw());
}

private:
// the members are deliberately reordered for better packing
Expand Down
Loading