0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-14 16:46:50 +01:00

ircd:Ⓜ️:vm: Sequence iteration interface.

This commit is contained in:
Jason Volk 2018-04-16 15:58:02 -07:00
parent b320d8ece1
commit d5cd5dea09
2 changed files with 95 additions and 3 deletions

View file

@ -21,10 +21,7 @@ namespace ircd::m::vm
struct eval;
struct accepted;
enum fault :uint;
using fault_t = std::underlying_type<fault>::type;
using closure = std::function<void (const event &)>;
using closure_bool = std::function<bool (const event &)>;
extern struct log::log log;
extern uint64_t current_sequence;
@ -34,6 +31,20 @@ namespace ircd::m::vm
uint64_t sequence(const eval &);
}
namespace ircd::m::vm::events
{
using id_closure_bool = std::function<bool (const uint64_t &, const event::id &)>;
using closure_bool = std::function<bool (const uint64_t &, const event &)>;
// counts up from start
bool for_each(const uint64_t &start, const id_closure_bool &);
bool for_each(const uint64_t &start, const closure_bool &);
// -1 starts at newest event; counts down
bool rfor_each(const uint64_t &start, const id_closure_bool &);
bool rfor_each(const uint64_t &start, const closure_bool &);
}
struct ircd::m::vm::init
{
init();

View file

@ -536,6 +536,87 @@ ircd::m::vm::write(eval &eval)
eval.txn();
}
bool
ircd::m::vm::events::rfor_each(const uint64_t &start,
const closure_bool &closure)
{
event::fetch event;
return rfor_each(start, id_closure_bool{[&event, &closure]
(const uint64_t &seq, const event::id &event_id)
{
if(!seek(event, event_id, std::nothrow))
return true;
return closure(seq, event);
}});
}
bool
ircd::m::vm::events::rfor_each(const uint64_t &start,
const id_closure_bool &closure)
{
auto &column
{
dbs::event_seq
};
if(start == uint64_t(-1))
{
for(auto it(column.rbegin()); it; ++it)
if(!closure(byte_view<uint64_t>(it->first), it->second))
return false;
return true;
}
auto it
{
column.lower_bound(byte_view<string_view>(start))
};
for(; it; ++it)
if(!closure(byte_view<uint64_t>(it->first), it->second))
return false;
return true;
}
bool
ircd::m::vm::events::for_each(const uint64_t &start,
const closure_bool &closure)
{
event::fetch event;
return for_each(start, id_closure_bool{[&event, &closure]
(const uint64_t &seq, const event::id &event_id)
{
if(!seek(event, event_id, std::nothrow))
return true;
return closure(seq, event);
}});
}
bool
ircd::m::vm::events::for_each(const uint64_t &start,
const id_closure_bool &closure)
{
auto &column
{
dbs::event_seq
};
auto it
{
column.lower_bound(byte_view<string_view>(start))
};
for(; it; ++it)
if(!closure(byte_view<uint64_t>(it->first), it->second))
return false;
return true;
}
uint64_t
ircd::m::vm::sequence(const eval &eval)
{