From e38371d50c86b126bfa541b49dfb33bcbf8e1dbe Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 18 Aug 2019 08:07:38 -0700 Subject: [PATCH] ircd::m::room: Add basic count() suite. --- include/ircd/m/room/room.h | 3 +++ ircd/m_room.cc | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/ircd/m/room/room.h b/include/ircd/m/room/room.h index 1c217c9d4..b251feb91 100644 --- a/include/ircd/m/room/room.h +++ b/include/ircd/m/room/room.h @@ -156,6 +156,9 @@ struct ircd::m::room void get(const string_view &type, const event::closure &) const; event::idx get(std::nothrow_t, const string_view &type) const; event::idx get(const string_view &type) const; + size_t count(const string_view &type, const string_view &state_key) const; + size_t count(const string_view &type) const; + size_t count() const; room(const id &room_id, const string_view &event_id, diff --git a/ircd/m_room.cc b/ircd/m_room.cc index c4933824f..16c414da3 100644 --- a/ircd/m_room.cc +++ b/ircd/m_room.cc @@ -1400,6 +1400,57 @@ ircd::m::room::index(const room::id &room_id, // room::room // +size_t +ircd::m::room::count() +const +{ + size_t ret(0); + for_each(event::closure_idx_bool{[&ret] + (const event::idx &event_idx) + { + ++ret; + return true; + }}); + + return ret; +} + +size_t +ircd::m::room::count(const string_view &type) +const +{ + size_t ret(0); + for_each(type, event::closure_idx_bool{[&ret] + (const event::idx &event_idx) + { + ++ret; + return true; + }}); + + return ret; +} + +size_t +ircd::m::room::count(const string_view &type, + const string_view &state_key) +const +{ + size_t ret(0); + for_each(type, event::closure_idx_bool{[&state_key, &ret] + (const event::idx &event_idx) + { + ret += query(std::nothrow, event_idx, "state_key", [&state_key] + (const string_view &_state_key) -> bool + { + return state_key == _state_key; + }); + + return true; + }}); + + return ret; +} + bool ircd::m::room::has(const string_view &type) const