0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-30 04:38:52 +02:00

ircd:Ⓜ️:events: Simplify interface.

This commit is contained in:
Jason Volk 2019-01-09 17:57:13 -08:00
parent 6c96e0d785
commit 5815fc87c2
2 changed files with 24 additions and 64 deletions

View file

@ -14,10 +14,8 @@
namespace ircd::m::events
{
struct range;
using id_closure_bool = std::function<bool (const event::idx &, const event::id &)>;
using closure_bool = std::function<bool (const event::idx &, const event &)>;
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<event::idx, event::idx>
{
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<event::idx, event::idx>{start, stop}
,fopts{fopts}
{}
};

View file

@ -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<event, "event_id"_>()
};
// 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<string_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<event::idx>(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<event::idx>(it->first)
};
if(ascending && event_idx >= stop)
break;
if(!ascending && event_idx <= stop)
break;
if(!closure(event_idx, it->second))
return false;
}
return true;
}