From c676d56dcd08dbdc03747a5bbad43471f62c0844 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 3 Mar 2018 06:21:18 -0800 Subject: [PATCH] ircd::m: Add room::members::count() with membership=join optimized path. --- include/ircd/m/room.h | 3 +++ ircd/m/room.cc | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/include/ircd/m/room.h b/include/ircd/m/room.h index 3165cbdca..f8f75a1fb 100644 --- a/include/ircd/m/room.h +++ b/include/ircd/m/room.h @@ -226,6 +226,9 @@ struct ircd::m::room::members { m::room room; + size_t count() const; + size_t count(const string_view &membership) const; + bool test(const string_view &membership, const event::closure_bool &view) const; bool test(const event::closure_bool &view) const; diff --git a/ircd/m/room.cc b/ircd/m/room.cc index 7b514927e..24b68744a 100644 --- a/ircd/m/room.cc +++ b/ircd/m/room.cc @@ -639,6 +639,54 @@ const }); } +size_t +ircd::m::room::members::count(const string_view &membership) +const +{ + // joined members optimization. Only possible when seeking + // membership="join" on the present state of the room. + if(!room.event_id && membership == "join") + { + size_t ret{0}; + const room::origins origins{room}; + origins._test_([&ret](const string_view &) + { + ++ret; + return false; + }); + + return ret; + } + + const room::state state + { + room + }; + + size_t ret{0}; + static const string_view type{"m.room.member"}; + state.for_each(type, event::closure{[&ret, &membership] + (const m::event &event) + { + ret += m::membership(event) == membership; + }}); + + return ret; +} + +size_t +ircd::m::room::members::count() +const +{ + const room::state state + { + room + }; + + static const string_view type{"m.room.member"}; + return state.count(type); +} + // // room::origins //