From ccf9e993ddd48f91529ee664ad7dd261a8c446df Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 6 Apr 2018 21:46:20 -0700 Subject: [PATCH] ircd::m::txn: Elaborate the txn generation stack. --- include/ircd/m/txn.h | 8 +++++- ircd/m/m.cc | 68 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/include/ircd/m/txn.h b/include/ircd/m/txn.h index 8a68db45c..b5eb13882 100644 --- a/include/ircd/m/txn.h +++ b/include/ircd/m/txn.h @@ -29,8 +29,14 @@ struct ircd::m::txn > { using array = vector_view; + using closure = std::function; + static string_view create_id(const mutable_buffer &out, const string_view &txn); - static std::string create(const array &pdu, const array &edu, const array &pdu_failure = {}); + + static void create(const closure &, const array &pdu, const array &edu = {}, const array &pdu_failure = {}); + static string_view create(const mutable_buffer &, const array &pdu, const array &edu = {}, const array &pdu_failure = {}); + static std::string create(const array &pdu, const array &edu = {}, const array &pdu_failure = {}); + static size_t serialized(const array &pdu, const array &edu = {}, const array &pdu_failure = {}); using super_type::tuple; using super_type::operator=; diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 992da22d7..436919a7f 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -1486,10 +1486,73 @@ ircd::m::exists(const id::room_alias &room_alias, // m/txn.h // +/// Returns the serial size of the JSON this txn would consume. Note: this +/// creates a json::iov involving a timestamp to figure out the total size +/// of the txn. When the user creates the actual txn a different timestamp +/// is created which may be a different size. Consider using the lower-level +/// create(closure) or add some pad to be sure. +/// +size_t +ircd::m::txn::serialized(const array &pdu, + const array &edu, + const array &pdu_failure) +{ + size_t ret; + const auto closure{[&ret] + (const json::iov &iov) + { + ret = json::serialized(iov); + }}; + + create(closure, pdu, edu, pdu_failure); + return ret; +} + +/// Stringifies a txn from the inputs into the returned std::string +/// std::string ircd::m::txn::create(const array &pdu, const array &edu, const array &pdu_failure) +{ + std::string ret; + const auto closure{[&ret] + (const json::iov &iov) + { + ret = json::strung(iov); + }}; + + create(closure, pdu, edu, pdu_failure); + return ret; +} + +/// Stringifies a txn from the inputs into the buffer +/// +ircd::string_view +ircd::m::txn::create(const mutable_buffer &buf, + const array &pdu, + const array &edu, + const array &pdu_failure) +{ + string_view ret; + const auto closure{[&buf, &ret] + (const json::iov &iov) + { + ret = json::stringify(mutable_buffer{buf}, iov); + }}; + + create(closure, pdu, edu, pdu_failure); + return ret; +} + +/// Forms a txn from the inputs into a json::iov and presents that iov +/// to the user's closure. +/// +void +ircd::m::txn::create(const closure &closure, + const array &pdu, + const array &edu, + const array &pdu_failure) { using ircd::size; @@ -1524,10 +1587,7 @@ ircd::m::txn::create(const array &pdu, } }; - return json::strung - { - iov - }; + closure(iov); } ircd::string_view