0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-10-02 21:59:02 +02:00

ircd:Ⓜ️ Add central interface for room state prefetch; add console cmd; apply to client sync.

This commit is contained in:
Jason Volk 2018-10-23 09:54:45 -07:00
parent c83d17396b
commit 242516c853
5 changed files with 114 additions and 10 deletions

View file

@ -298,6 +298,10 @@ struct ircd::m::room::state
event::id::buf get(std::nothrow_t, const string_view &type, const string_view &state_key = "") const;
event::id::buf get(const string_view &type, const string_view &state_key = "") const;
// Initiate a database prefetch on the state to cache for future access.
size_t prefetch(const string_view &type, const event::idx &start = 0, const event::idx &stop = 0) const;
size_t prefetch(const event::idx &start = 0, const event::idx &stop = 0) const;
state(const m::room &room, const event::fetch::opts *const & = nullptr);
state() = default;
state(const state &) = delete;

View file

@ -2466,6 +2466,30 @@ const
// m/room.h
//
size_t
ircd::m::room::state::prefetch(const event::idx &start,
const event::idx &stop)
const
{
return prefetch(string_view{}, start, stop);
}
size_t
ircd::m::room::state::prefetch(const string_view &type,
const event::idx &start,
const event::idx &stop)
const
{
using prototype = size_t (const state &, const string_view &, const std::pair<event::idx, event::idx> &);
static mods::import<prototype> function
{
"m_room", "state__prefetch"
};
return function(*this, type, std::pair<event::idx, event::idx>{start, stop});
}
ircd::m::room
ircd::m::create(const id::room &room_id,
const id::user &creator,

View file

@ -995,16 +995,7 @@ ircd::m::sync::polylog::room_state(shortpoll &sp,
};
if(bool(prefetch_state))
{
state.for_each([&]
(const m::event::idx &event_idx)
{
if(event_idx < sp.since || event_idx >= sp.current)
return;
m::prefetch(event_idx, fopts);
});
}
state.prefetch(sp.since, sp.current);
state.for_each([&]
(const m::event &event)

View file

@ -6090,6 +6090,62 @@ console_cmd__room__state__rebuild__history(opt &out, const string_view &line)
return true;
}
bool
console_cmd__room__state__prefetch(opt &out, const string_view &line)
{
const params param{line, " ",
{
"room_id", "[event_id_or_type]", "[type]"
}};
const auto &room_id
{
m::room_id(param.at("room_id"))
};
const auto &event_id_or_type
{
param.at("[event_id_or_type]", string_view{})
};
const auto is_event_id
{
m::has_sigil(event_id_or_type) && valid(m::id::EVENT, event_id_or_type)
};
const string_view &event_id
{
is_event_id?
event_id_or_type:
string_view{}
};
const auto &type
{
is_event_id?
param.at("[type]", string_view{}):
event_id_or_type
};
const m::room room
{
room_id, event_id
};
const m::room::state state
{
room
};
const size_t prefetched
{
state.prefetch(type)
};
out << "prefetched " << prefetched << std::endl;
return true;
}
bool
console_cmd__room__count(opt &out, const string_view &line)
{

View file

@ -467,6 +467,35 @@ state__rebuild_history(const m::room &room)
return ret;
}
extern "C" size_t
state__prefetch(const m::room::state &state,
const string_view &type,
const std::pair<m::event::idx, m::event::idx> &range)
{
const m::event::fetch::opts &fopts
{
state.fopts?
*state.fopts:
m::event::fetch::default_opts
};
size_t ret{0};
state.for_each(type, m::event::closure_idx{[&ret, &fopts, &range]
(const m::event::idx &event_idx)
{
if(event_idx < range.first)
return;
if(range.second && event_idx > range.second)
return;
m::prefetch(event_idx, fopts);
++ret;
}});
return ret;
}
extern "C" size_t
head__rebuild(const m::room &room)
{