Skip to content

About

C++17 library that decodes the MySQL and MariaDB binlog wire format: rows, JSON, GTIDs, compressed events

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

libmybinlog

CI Version License C++17 Platform

libmybinlog turns MySQL and MariaDB binlog bytes into typed events and row values. It reads a binlog file or the event stream of a replication connection that you already hold, so it has no network code, no exceptions and no RTTI. It depends on zlib, and optionally on zstd.

📖 Documentation  ·  Getting started  ·  Supported servers

Pre-1.0: the API may still change between minor releases.

What's inside

  • Streaming decoder — feed bytes in any split and take typed events one at a time. The Decoder
  • Row images — FULL, MINIMAL and NOBLOB images decoded into structured values, with absent columns kept apart from NULL. Rows · Values
  • JSON — MySQL binary JSON rendered as the server's own text, and partial JSON updates applied. JSON
  • Compressed events — MariaDB zlib events and MySQL zstd transaction payloads expanded. Compressed events
  • Checksums and GTIDs — per-file CRC32 handling, MySQL GTID sets with tags, MariaDB GTID positions. Checksums · GTIDs
  • Safety checks — event classification for readers that must not lose data, and static server-setting rules. Event disposition · Server settings
  • Low-level API — every per-event parser the decoder uses is public. Low-level API

Installation

include(FetchContent)
FetchContent_Declare(
  libmybinlog
  GIT_REPOSITORY https://github.com/libraz/libmybinlog.git
  GIT_TAG v0.3.1
)
FetchContent_MakeAvailable(libmybinlog)

target_link_libraries(your_target PRIVATE mybinlog::mybinlog)

An installed copy is found with find_package(mybinlog) or pkg-config mybinlog.

Quick start

#include <mybinlog/mybinlog.h>

#include <cstdio>
#include <variant>

void PrintRowCounts(const uint8_t* data, size_t size) {
  mybinlog::Decoder decoder;
  decoder.Feed(data, size);  // a whole binlog file, or stream bytes in any split
  mybinlog::Event event;
  mybinlog::Status status;
  while ((status = decoder.Next(&event)) == mybinlog::Status::kOk) {
    const auto* rows = std::get_if<mybinlog::TableRowsEvent>(&event.body);
    if (rows != nullptr && rows->decoded) {
      std::printf("%s.%s: %zu rows\n", rows->table->schema.c_str(), rows->table->table.c_str(),
                  rows->rows.rows.size() + rows->rows.updates.size());
    }
  }
  if (status == mybinlog::Status::kNeedMoreData) status = decoder.Finish();
  if (status != mybinlog::Status::kOk) {
    std::fprintf(stderr, "%s\n", decoder.last_error().message.c_str());
  }
}

examples/mybinlog-dump prints every event of a binlog file as JSON Lines. → The Decoder

Build from source

make build         # configure and compile into build/
make test          # build, then run the tests
make format-check  # clang-format check and clang-tidy

Build options (MYBINLOG_WITH_ZSTD, MYBINLOG_BUILD_TESTS, MYBINLOG_INSTALL, MYBINLOG_BUILD_EXAMPLES) are covered in Getting started.

Documentation

The guides live in docs/en.

Non-goals

libmybinlog decodes the format and stops there. It is not a replication client (connecting, authenticating and requesting the dump are the caller's job), not a SQL parser (QUERY events give statement text), and not a change-data-capture policy (it classifies events; the caller decides what to do). Windows is not tested. See Introduction for the details.

License

Apache-2.0

About

C++17 library that decodes the MySQL and MariaDB binlog wire format: rows, JSON, GTIDs, compressed events

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages