From 5815fc87c257b408b7566609be0f8630b2f57ced Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 9 Jan 2019 17:57:13 -0800 Subject: [PATCH] ircd::m::events: Simplify interface. --- include/ircd/m/events.h | 9 +++-- ircd/m/m.cc | 79 ++++++++++------------------------------- 2 files changed, 24 insertions(+), 64 deletions(-) diff --git a/include/ircd/m/events.h b/include/ircd/m/events.h index 316393afc..226d48a54 100644 --- a/include/ircd/m/events.h +++ b/include/ircd/m/events.h @@ -14,10 +14,8 @@ namespace ircd::m::events { struct range; - using id_closure_bool = std::function; using closure_bool = std::function; - bool for_each(const range &, const id_closure_bool &); bool for_each(const range &, const closure_bool &); bool for_each(const range &, const event_filter &, const closure_bool &); } @@ -29,7 +27,12 @@ namespace ircd::m::events struct ircd::m::events::range :std::pair { - range(const event::idx &start, const event::idx &stop = -1) + const event::fetch::opts *fopts {nullptr}; + + range(const event::idx &start, + const event::idx &stop = -1, + const event::fetch::opts *const &fopts = nullptr) :std::pair{start, stop} + ,fopts{fopts} {} }; diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 74f6719dd..61a5a7c4b 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -1638,81 +1638,38 @@ bool ircd::m::events::for_each(const range &range, const closure_bool &closure) { - event::fetch event; - return for_each(range, id_closure_bool{[&event, &closure] - (const event::idx &event_idx, const event::id &event_id) + event::fetch event { - if(!seek(event, event_idx, std::nothrow)) - return true; - - return closure(event_idx, event); - }}); -} - -bool -ircd::m::events::for_each(const range &range, - const id_closure_bool &closure) -{ - static const db::gopts opts - { - db::get::NO_CACHE + range.fopts }; - static constexpr auto column_idx - { - json::indexof() - }; + // When the fopts dictate no columns to fetch there is nothing more to do. + if(event.row.empty()) + return true; - auto &column - { - dbs::event_column.at(column_idx) - }; - - const auto &start{range.first}; - const auto &stop{range.second}; const bool ascending { - start < stop + range.first < range.second }; - auto it + auto start { - column.lower_bound(byte_view(start), opts) + ascending? + range.first: + std::min(range.first, vm::current_sequence) }; - // Branch to use a reverse iterator from the end - if(!ascending && !it) + const auto stop { - for(auto it(column.rbegin(opts)); it; ++it) - { - const event::idx &event_idx - { - byte_view(it->first) - }; + ascending? + std::min(range.second, vm::current_sequence + 1): + range.second + }; - if(event_idx <= stop) - break; - - if(!closure(event_idx, it->second)) + for(; start != stop; ascending? ++start : --start) + if(seek(event, start, std::nothrow)) + if(!closure(start, event)) return false; - } - } - else for(; it; ascending? ++it : --it) - { - const event::idx &event_idx - { - byte_view(it->first) - }; - - if(ascending && event_idx >= stop) - break; - - if(!ascending && event_idx <= stop) - break; - - if(!closure(event_idx, it->second)) - return false; - } return true; }