summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-05-21 08:43:43 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-05-21 08:43:43 +0100
commitbf55ecaeb6849eab89fdb9bc5da51ff32dd1ea33 (patch)
tree3e5d8bd554aff8f455bf55c38d0cb63505beb4b9
parentDefault intialize structs (diff)
downloadmygrate-bf55ecaeb6849eab89fdb9bc5da51ff32dd1ea33.tar.bz2
mygrate-bf55ecaeb6849eab89fdb9bc5da51ff32dd1ea33.tar.xz
mygrate-bf55ecaeb6849eab89fdb9bc5da51ff32dd1ea33.zip
Unpack row data into a vector or vector pair
-rw-r--r--lib/row.cpp34
-rw-r--r--lib/row.h18
-rw-r--r--main/main.cpp16
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 <string>
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<void(MySQL::FieldValue)> & 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<std::span<const std::byte>>(flagBytes)};
MyGrate::BitSet columnFlags {{reinterpret_cast<const std::byte *>(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<T>::read(md, data)); \
+ emplace_back(MySQL::Type<T>::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> {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 <functional>
namespace MyGrate {
- class Row {
+ class Row : public std::vector<MySQL::FieldValue> {
public:
Row(const st_mariadb_rpl_rows_event &, const st_mariadb_rpl_table_map_event &);
- void forEachField(const std::function<void(MySQL::FieldValue)> & 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<Row, Row> {
+ 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