From 69917f153fb8d8f17f570066dada3cff0afdf59f Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 30 Aug 2017 14:22:19 -0700 Subject: [PATCH] ircd::db: Add json::tuple related and utils to row. --- include/ircd/db/row.h | 61 ++++++++++++++++++++++++++++++++++++++----- ircd/db.cc | 21 +++++++++++++++ 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/include/ircd/db/row.h b/include/ircd/db/row.h index 3c2625d3b..52403e981 100644 --- a/include/ircd/db/row.h +++ b/include/ircd/db/row.h @@ -51,13 +51,14 @@ struct ircd::db::row using pointer = cell *; using difference_type = size_t; - private: + private: public: std::vector its; - template friend void seek(row &, const pos &); - friend void seek(row &, const string_view &key); - public: + auto empty() const { return its.empty(); } + auto size() const { return its.size(); } + bool valid() const; // true on any cell valid; false on empty + // [GET] Iterations const_iterator begin() const; const_iterator end() const; @@ -68,16 +69,20 @@ struct ircd::db::row const_iterator find(const string_view &column) const; iterator find(const string_view &column); - auto empty() const { return its.empty(); } - auto size() const { return its.size(); } - // [GET] Get cell const cell &operator[](const string_view &column) const; cell &operator[](const string_view &column); + // [GET] Object conversion + template explicit operator tuple() const; + template explicit operator tuple(); + // [SET] Perform operation void operator()(const op &, const string_view &col, const string_view &val = {}, const sopts & = {}); + // All cells + void load(const gopts & = {}); // !DANGER! not atomic + row(std::vector cells = {}) :its{std::move(cells)} {} @@ -87,6 +92,12 @@ struct ircd::db::row const vector_view &columns = {}, gopts opts = {}); + template + row(database &, + const string_view &key = {}, + const json::tuple & = {}, + gopts opts = {}); + template friend bool seeka(row &, const pos &); template friend bool seek(row &, const pos &); friend size_t trim(row &, const std::function &); @@ -180,6 +191,42 @@ struct ircd::db::row::delta {} }; +template +ircd::db::row::row(database &d, + const string_view &key, + const json::tuple &t, + gopts opts) +:row{[&d, &key, &t, &opts] +() -> row +{ + std::array cols; + json::_key_transform(t, std::begin(cols), std::end(cols)); + return { d, key, cols, opts }; +}()} +{ +} + +template +ircd::db::row::operator tuple() +{ + tuple ret; + for(auto &cell : *this) + json::set(ret, cell.col(), cell.val()); + + return ret; +} + +template +ircd::db::row::operator tuple() +const +{ + tuple ret; + for(const auto &cell : *this) + json::set(ret, cell.col(), cell.val()); + + return ret; +} + inline ircd::db::cell & ircd::db::row::operator[](const string_view &column) { diff --git a/ircd/db.cc b/ircd/db.cc index 9da91b6d9..8025eb51d 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -1733,6 +1733,27 @@ const return ret; } +bool +ircd::db::row::valid() +const +{ + return std::any_of(std::begin(its), std::end(its), [] + (const auto &cell) + { + return cell.valid(); + }); +} + +void +ircd::db::row::load(const gopts &opts) +{ + std::for_each(std::begin(its), std::end(its), [&opts] + (auto &cell) + { + cell.load(opts); + }); +} + /////////////////////////////////////////////////////////////////////////////// // // db/column.h