0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-25 05:18:23 +02:00

ircd:Ⓜ️:room::timeline: Improve iteration.

This commit is contained in:
Jason Volk 2019-07-22 16:06:22 -07:00
parent cc9ce9c8a5
commit eb38a9ce15
3 changed files with 87 additions and 44 deletions

View file

@ -21,15 +21,18 @@ namespace ircd::m
struct ircd::m::room::timeline
{
struct coord;
using closure = std::function<bool (const coord &, const event::idx &)>;
using closure = std::function<coord &(coord &, const event::idx &)>;
m::room room;
public:
static event::idx next(const event::idx &, const int64_t &x = 0);
bool has_past(const event::id &) const;
bool has_future(const event::id &) const;
bool for_each(const closure &, const coord &branch) const;
bool for_each(coord &, const closure &) const;
bool for_each(const closure &) const;
timeline(const m::room &);
timeline() = default;

View file

@ -9273,19 +9273,29 @@ console_cmd__room__timeline(opt &out, const string_view &line)
room
};
int64_t last_depth(0);
const auto closure{[&out, &last_depth]
(const auto &coord, const auto &event_idx)
const auto closure{[&out, &room]
(auto &coord, const auto &event_idx)
-> m::room::timeline::coord &
{
if(coord.y > m::depth(std::nothrow, room))
return coord;
if(!event_idx)
{
++coord.y;
return coord;
}
const m::event::fetch event
{
event_idx, std::nothrow
};
if(json::get<"depth"_>(event) > last_depth + 1)
out << std::endl;
last_depth = json::get<"depth"_>(event);
if(!event.valid)
{
++coord.x;
return coord;
}
out << "("
<< std::left
@ -9300,14 +9310,17 @@ console_cmd__room__timeline(opt &out, const string_view &line)
<< pretty_oneline(event, false)
<< std::endl;
return true;
++coord.x;
return coord;
}};
timeline.for_each(closure,
m::room::timeline::coord coord
{
param.at<int64_t>("x", 0),
param.at<int64_t>("y", 0)
});
param.at<long>("x", 0L),
param.at<long>("y", 0L),
};
timeline.for_each(coord, closure);
return true;
}

View file

@ -31,49 +31,48 @@ ircd::m::room::timeline::timeline(const m::room &room)
}
bool
ircd::m::room::timeline::for_each(const closure &closure,
const coord &branch)
ircd::m::room::timeline::for_each(const closure &closure)
const
{
m::event::refs refs
struct coord coord;
return for_each(coord, closure);
}
bool
ircd::m::room::timeline::for_each(coord &coord,
const closure &closure)
const
{
messages it
{
room.event_id?
index(room.event_id):
room::index(room)
this->room, uint64_t(coord.y)
};
if(!refs.idx)
if(!it)
return true;
timeline::coord coord;
if(!closure(coord, refs.idx))
return false;
for(++coord.y; coord.y <= branch.y; ++coord.y, coord.x = 0)
event::idx next(it.event_idx()); do
{
auto idx(0);
refs.for_each(dbs::ref::NEXT, [&coord, &branch, &idx]
(const auto &event_idx, const auto &)
{
if(coord.x <= branch.x)
idx = event_idx;
if(coord.x < branch.x)
{
++coord.x;
return true;
}
else return false;
});
if(!idx)
const auto last(coord);
coord = closure(coord, next);
if(coord.x == last.x && coord.y == last.y)
return true;
if(!closure(coord, idx))
if(coord.y != last.y)
if(!it.seek(coord.y))
return true;
next = timeline::next(it.event_idx(), coord.x);
if(next == 0 && coord.x == 0)
continue;
if(next == it.event_idx())
return false;
refs.idx = idx;
if(next == 0)
coord.x = 0;
}
while(1);
return true;
}
@ -93,6 +92,10 @@ const
return true;
}
//
// static util
//
void
ircd::m::room::timeline::rebuild(const m::room &room)
{
@ -123,3 +126,27 @@ ircd::m::room::timeline::rebuild(const m::room &room)
txn();
}
ircd::m::event::idx
ircd::m::room::timeline::next(const event::idx &event_idx,
const int64_t &x)
{
const m::event::refs refs
{
event_idx
};
int64_t _x(0);
event::idx ret(0);
refs.for_each(dbs::ref::NEXT, [&_x, &x, &ret]
(const auto &event_idx, const auto &)
{
if(_x++ < x)
return true;
ret = event_idx;
return false;
});
return ret;
}