From bf55ecaeb6849eab89fdb9bc5da51ff32dd1ea33 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 21 May 2021 08:43:43 +0100 Subject: Unpack row data into a vector or vector pair --- lib/row.cpp | 34 +++++++++++++++++++++++++++------- lib/row.h | 18 ++++++++++++++---- main/main.cpp | 16 ++++++++++++++-- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/lib/row.cpp b/lib/row.cpp index d59cdba..45e2959 100644 --- a/lib/row.cpp +++ b/lib/row.cpp @@ -3,13 +3,21 @@ #include namespace MyGrate { - Row::Row(const st_mariadb_rpl_rows_event & r, const st_mariadb_rpl_table_map_event & t) : row {r}, tm {t} { } + Row::Row(const st_mariadb_rpl_rows_event & row, const st_mariadb_rpl_table_map_event & tm) : + Row(row, tm, RawDataReader {tm.metadata}, RawDataReader {row.row_data, row.row_data_size}) + { + } + + Row::Row(const st_mariadb_rpl_rows_event & row, const st_mariadb_rpl_table_map_event & tm, + MyGrate::RawDataReader && md, MyGrate::RawDataReader && data) : + Row {row, tm, md, data} + { + } - void - Row::forEachField(const std::function & cb) + Row::Row(const st_mariadb_rpl_rows_event & row, const st_mariadb_rpl_table_map_event & tm, + MyGrate::RawDataReader & md, MyGrate::RawDataReader & data) { - MyGrate::RawDataReader md {tm.metadata}; - MyGrate::RawDataReader data {row.row_data, row.row_data_size}; + reserve(tm.column_count); const size_t flagBytes {(tm.column_count + 7) / 8}; MyGrate::BitSet nullFlags {data.viewValue>(flagBytes)}; MyGrate::BitSet columnFlags {{reinterpret_cast(row.column_bitmap), flagBytes}}; @@ -44,13 +52,13 @@ namespace MyGrate { break; default:; } - cb(nullptr); + emplace_back(nullptr); } else { switch (type) { #define TYPE_CALLBACK(T) \ case T: \ - cb(MySQL::Type::read(md, data)); \ + emplace_back(MySQL::Type::read(md, data)); \ break TYPE_CALLBACK(MYSQL_TYPE_DECIMAL); TYPE_CALLBACK(MYSQL_TYPE_TINY); @@ -98,4 +106,16 @@ namespace MyGrate { ++colIter; } } + + RowPair::RowPair(const st_mariadb_rpl_rows_event & row, const st_mariadb_rpl_table_map_event & tm) : + RowPair(row, tm, RawDataReader {tm.metadata}, RawDataReader {tm.metadata}, + RawDataReader {row.row_data, row.row_data_size}) + { + } + + RowPair::RowPair(const st_mariadb_rpl_rows_event & row, const st_mariadb_rpl_table_map_event & tm, + MyGrate::RawDataReader && md1, MyGrate::RawDataReader && md2, MyGrate::RawDataReader && data) : + std::pair {Row {row, tm, md1, data}, Row {row, tm, md2, data}} + { + } } diff --git a/lib/row.h b/lib/row.h index ac2c135..4191644 100644 --- a/lib/row.h +++ b/lib/row.h @@ -6,15 +6,25 @@ #include namespace MyGrate { - class Row { + class Row : public std::vector { public: Row(const st_mariadb_rpl_rows_event &, const st_mariadb_rpl_table_map_event &); - void forEachField(const std::function & callback); + private: + friend class RowPair; + Row(const st_mariadb_rpl_rows_event &, const st_mariadb_rpl_table_map_event &, MyGrate::RawDataReader && md, + MyGrate::RawDataReader && data); + Row(const st_mariadb_rpl_rows_event &, const st_mariadb_rpl_table_map_event &, MyGrate::RawDataReader & md, + MyGrate::RawDataReader & data); + }; + + class RowPair : public std::pair { + public: + RowPair(const st_mariadb_rpl_rows_event &, const st_mariadb_rpl_table_map_event &); private: - const st_mariadb_rpl_rows_event & row; - const st_mariadb_rpl_table_map_event & tm; + RowPair(const st_mariadb_rpl_rows_event &, const st_mariadb_rpl_table_map_event &, + MyGrate::RawDataReader && md1, MyGrate::RawDataReader && md2, MyGrate::RawDataReader && data); }; } diff --git a/main/main.cpp b/main/main.cpp index d976a0a..b5d5a4c 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -51,7 +51,19 @@ struct write { static void dumpRowData(const st_mariadb_rpl_rows_event & row) { - MyGrate::Row {row, tableMaps.at(row.table_id)->event.table_map}.forEachField([](auto && fv) { + MyGrate::Row r {row, tableMaps.at(row.table_id)->event.table_map}; + std::for_each(r.begin(), r.end(), [](auto && fv) { + std::visit(write {}, fv); + }); +} +static void +dumpRowPairData(const st_mariadb_rpl_rows_event & row) +{ + MyGrate::RowPair rp {row, tableMaps.at(row.table_id)->event.table_map}; + std::for_each(rp.first.begin(), rp.first.end(), [](auto && fv) { + std::visit(write {}, fv); + }); + std::for_each(rp.second.begin(), rp.second.end(), [](auto && fv) { std::visit(write {}, fv); }); } @@ -80,7 +92,7 @@ doUpdate(MariaDB_Event_Ptr event) { const auto & rs = event->event.rows; AdHoc::scprintf<"Update %?\n">(std::cout, rs.table_id); - dumpRowData(event->event.rows); + dumpRowPairData(event->event.rows); } static void -- cgit v1.2.3