From 4d05fe0353d9ca04d51ebd0514059ecd085c16ad Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 18 Jan 2019 11:10:21 -0800 Subject: [PATCH] ircd::m::event::fetch: Reorg / cleanup internal interface. --- include/ircd/m/event/fetch.h | 5 + ircd/m/event.cc | 193 +++++++++++++++++++++-------------- 2 files changed, 124 insertions(+), 74 deletions(-) diff --git a/include/ircd/m/event/fetch.h b/include/ircd/m/event/fetch.h index 12015b8f9..fda57ed7e 100644 --- a/include/ircd/m/event/fetch.h +++ b/include/ircd/m/event/fetch.h @@ -44,6 +44,11 @@ struct ircd::m::event::fetch db::row row; bool valid; + static string_view key(const event::idx *const &); + static bool should_seek_json(const opts &); + bool assign_from_json(const opts &); + bool assign_from_row(const opts &, const string_view &key); + public: fetch(const idx &, std::nothrow_t, const opts & = default_opts); fetch(const id &, std::nothrow_t, const opts & = default_opts); diff --git a/ircd/m/event.cc b/ircd/m/event.cc index 78c014d52..9d74c6734 100644 --- a/ircd/m/event.cc +++ b/ircd/m/event.cc @@ -1524,53 +1524,12 @@ ircd::m::seek(event::fetch &fetch, byte_view(event_idx) }; - const bool query_json - { - !opts.query_json? - false: // User never wants to make the event_json query - - opts.query_json_force? - true: // User always wants to make the event_json query - - fetch.row.size() < fetch.cell.size()? - false: // User is making specific column queries - - fetch.row.size() == fetch.cell.size()? - true: // User is querying all columns - - false - }; - - if(query_json) - { + if(fetch.should_seek_json(opts)) if((fetch.valid = fetch._json.load(key, opts.gopts))) - { - const json::object source - { - fetch._json.val() - }; + return fetch.assign_from_json(opts); - event = opts.selected_only? - m::event{source, opts.keys}: - m::event{source}; - - assert(data(event.source) == data(source)); - assert(fetch.valid); - return fetch.valid; - } - - // graceful fallback to row query if json query failed. - assert(!fetch.valid); - } - - if(!(fetch.valid = db::seek(fetch.row, key, opts.gopts))) - return fetch.valid; - - if((fetch.valid = fetch.row.valid(key))) - { - assign(event, fetch.row, key); - event.source = {}; - } + if((fetch.valid = db::seek(fetch.row, key, opts.gopts))) + fetch.valid = fetch.assign_from_row(opts, key); return fetch.valid; } @@ -1606,30 +1565,6 @@ ircd::m::event::fetch::event_id(const idx &idx, // event::fetch::fetch // -/// Seekless constructor. -ircd::m::event::fetch::fetch(const opts &opts) -:event{} -,_json -{ - m::dbs::event_json, - string_view{}, - opts.gopts -} -,row -{ - *dbs::events, - string_view{}, - opts.keys, - cell, - opts.gopts -} -,valid -{ - false -} -{ -} - /// Seek to event_id and populate this event from database. /// Throws if event not in database. ircd::m::event::fetch::fetch(const event::id &event_id, @@ -1674,14 +1609,124 @@ ircd::m::event::fetch::fetch(const event::idx &event_idx, ircd::m::event::fetch::fetch(const event::idx &event_idx, std::nothrow_t, const opts &opts) -:fetch +:event{} +,_json { - opts + m::dbs::event_json, + event_idx && should_seek_json(opts)? + key(&event_idx): + string_view{}, + opts.gopts +} +,row +{ + *dbs::events, + event_idx && !_json.valid(key(&event_idx))? + key(&event_idx): + string_view{}, + opts.keys, + cell, + opts.gopts +} +,valid +{ + event_idx && _json.valid(key(&event_idx))? + assign_from_json(opts): + assign_from_row(opts, key(&event_idx)) } { - if(likely(event_idx)) - if(!seek(*this, event_idx, std::nothrow, opts)) - assert(!valid); +} + +/// Seekless constructor. +ircd::m::event::fetch::fetch(const opts &opts) +:event{} +,_json +{ + m::dbs::event_json, + string_view{}, + opts.gopts +} +,row +{ + *dbs::events, + string_view{}, + opts.keys, + cell, + opts.gopts +} +,valid +{ + false +} +{ +} + +bool +ircd::m::event::fetch::assign_from_json(const opts &opts) +{ + auto &event + { + static_cast(*this) + }; + + const json::object source + { + _json.val() + }; + + assert(!empty(source)); + event = opts.selected_only? + m::event{source, opts.keys}: + m::event{source}; + + assert(data(event.source) == data(source)); + return true; +} + +bool +ircd::m::event::fetch::assign_from_row(const opts &opts, + const string_view &key) +{ + auto &event + { + static_cast(*this) + }; + + if(!row.valid(key)) + return false; + + assign(event, row, key); + event.source = {}; + return true; +} + +bool +ircd::m::event::fetch::should_seek_json(const opts &opts) +{ + // User never wants to make the event_json query + if(!opts.query_json) + return false; + + // User always wants to make the event_json query + if(opts.query_json_force) + return true; + + // User is making specific column queries + if(opts.keys.count() < event::size()) + return false; + + // User is querying all columns + if(opts.keys.count() == event::size()) + return true; + + return false; +} + +ircd::string_view +ircd::m::event::fetch::key(const event::idx *const &event_idx) +{ + assert(event_idx); + return byte_view(*event_idx); } //