From d96208faaf6720c06aff69fbd79311b5abc91280 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 16 Apr 2019 14:40:12 -0700 Subject: [PATCH] ircd::m::events: Add iteration tools for events in sender/origin. --- include/ircd/m/events.h | 4 ++ ircd/m.cc | 82 +++++++++++++++++++++++++++++++++++++++++ modules/console.cc | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) diff --git a/include/ircd/m/events.h b/include/ircd/m/events.h index 427671222..b203a0a38 100644 --- a/include/ircd/m/events.h +++ b/include/ircd/m/events.h @@ -15,6 +15,10 @@ namespace ircd::m::events { struct range; using closure_bool = std::function; + using closure_sender_bool = std::function; + + bool for_each_in_sender(const id::user &, const closure_sender_bool &); + bool for_each_in_origin(const string_view &, const closure_sender_bool &); bool for_each(const range &, const closure_bool &); bool for_each(const range &, const event_filter &, const closure_bool &); diff --git a/ircd/m.cc b/ircd/m.cc index b3588aa23..dd6e38807 100644 --- a/ircd/m.cc +++ b/ircd/m.cc @@ -2541,6 +2541,88 @@ ircd::m::events::for_each(const range &range, return true; } +bool +ircd::m::events::for_each_in_origin(const string_view &origin, + const closure_sender_bool &closure) +{ + auto &column + { + dbs::event_sender + }; + + char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE]; + const string_view &key + { + dbs::event_sender_key(buf, origin) + }; + + auto it + { + column.begin(key) + }; + + for(; bool(it); ++it) + { + const auto &keyp + { + dbs::event_sender_key(it->first) + }; + + char _buf[m::id::MAX_SIZE]; + mutable_buffer buf{_buf}; + consume(buf, copy(buf, std::get<0>(keyp))); + consume(buf, copy(buf, ":"_sv)); + consume(buf, copy(buf, origin)); + const string_view &user_id + { + _buf, data(buf) + }; + + assert(valid(id::USER, user_id)); + if(!closure(user_id, std::get<1>(keyp))) + return false; + } + + return true; +} + +bool +ircd::m::events::for_each_in_sender(const id::user &user, + const closure_sender_bool &closure) +{ + auto &column + { + dbs::event_sender + }; + + char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE]; + const string_view &key + { + dbs::event_sender_key(buf, user, 0) + }; + + auto it + { + column.begin(key) + }; + + for(; bool(it); ++it) + { + const auto &keyp + { + dbs::event_sender_key(it->first) + }; + + if(std::get<0>(keyp) != user.local()) + break; + + if(!closure(user, std::get<1>(keyp))) + return false; + } + + return true; +} + /////////////////////////////////////////////////////////////////////////////// // // m/filter.h diff --git a/modules/console.cc b/modules/console.cc index 7aca92e17..c9edc3947 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -5722,6 +5722,74 @@ console_cmd__events__filter(opt &out, const string_view &line) return true; } +bool +console_cmd__events__in__sender(opt &out, const string_view &line) +{ + const params param{line, " ", + { + "user_id" + }}; + + const m::user::id &user_id + { + param.at("user_id") + }; + + m::events::for_each_in_sender(user_id, [&out] + (const m::user::id &user_id, const m::event::idx &event_idx) + { + const m::event::fetch event + { + event_idx, std::nothrow + }; + + if(!event.valid) + { + out << event_idx << " " << "NOT FOUND" << std::endl; + return true; + } + + out << event_idx << " " << pretty_oneline(event) << std::endl;; + return true; + }); + + return true; +} + +bool +console_cmd__events__in__origin(opt &out, const string_view &line) +{ + const params param{line, " ", + { + "origin" + }}; + + const string_view &origin + { + param.at("origin") + }; + + m::events::for_each_in_origin(origin, [&out] + (const m::user::id &user_id, const m::event::idx &event_idx) + { + const m::event::fetch event + { + event_idx, std::nothrow + }; + + if(!event.valid) + { + out << event_idx << " " << "NOT FOUND" << std::endl; + return true; + } + + out << event_idx << " " << pretty_oneline(event) << std::endl;; + return true; + }); + + return true; +} + conf::item events_dump_buffer_size {