From 7f3919dd17045a11abedfd035c711f2d299deaf7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 18 Apr 2018 00:33:38 -0700 Subject: [PATCH] ircd::m: Split events iterations into namespace. --- include/ircd/m/events.h | 26 +++++++++++ include/ircd/m/m.h | 1 + ircd/m/m.cc | 98 +++++++++++++++++++++++++++++++++++++++++ ircd/m/vm.cc | 93 -------------------------------------- modules/client/sync.cc | 3 +- modules/console.cc | 8 ++-- 6 files changed, 131 insertions(+), 98 deletions(-) create mode 100644 include/ircd/m/events.h diff --git a/include/ircd/m/events.h b/include/ircd/m/events.h new file mode 100644 index 000000000..77fad5129 --- /dev/null +++ b/include/ircd/m/events.h @@ -0,0 +1,26 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_M_EVENTS_H + +namespace ircd::m::events +{ + using id_closure_bool = std::function; + using closure_bool = std::function; + + // counts up from start + bool for_each(const uint64_t &start, const id_closure_bool &); + bool for_each(const uint64_t &start, const closure_bool &); + + // -1 starts at newest event; counts down + bool rfor_each(const uint64_t &start, const id_closure_bool &); + bool rfor_each(const uint64_t &start, const closure_bool &); +} diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index 4a8f88177..c977350ea 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -61,6 +61,7 @@ namespace ircd #include "room.h" #include "user.h" #include "rooms.h" +#include "events.h" #include "node.h" #include "login.h" #include "register.h" diff --git a/ircd/m/m.cc b/ircd/m/m.cc index 865c0c1bf..a8d2f6a73 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -922,6 +922,104 @@ ircd::m::node::room::room(const m::node &node) static_cast(*this) = room_id; } +/////////////////////////////////////////////////////////////////////////////// +// +// m/events.h +// + +bool +ircd::m::events::rfor_each(const uint64_t &start, + const closure_bool &closure) +{ + event::fetch event; + return rfor_each(start, id_closure_bool{[&event, &closure] + (const event::idx &event_idx, const event::id &event_id) + { + if(!seek(event, event_idx, std::nothrow)) + return true; + + return closure(event_idx, event); + }}); +} + +bool +ircd::m::events::rfor_each(const uint64_t &start, + const id_closure_bool &closure) +{ + static constexpr auto column_idx + { + json::indexof() + }; + + auto &column + { + dbs::event_column.at(column_idx) + }; + + if(start == uint64_t(-1)) + { + for(auto it(column.rbegin()); it; ++it) + if(!closure(byte_view(it->first), it->second)) + return false; + + return true; + } + + auto it + { + column.lower_bound(byte_view(start)) + }; + + for(; it; ++it) + if(!closure(byte_view(it->first), it->second)) + return false; + + return true; +} + +bool +ircd::m::events::for_each(const uint64_t &start, + const closure_bool &closure) +{ + event::fetch event; + return for_each(start, id_closure_bool{[&event, &closure] + (const event::idx &event_idx, const event::id &event_id) + { + if(!seek(event, event_idx, std::nothrow)) + return true; + + return closure(event_idx, event); + }}); +} + +bool +ircd::m::events::for_each(const uint64_t &start, + const id_closure_bool &closure) +{ + static constexpr auto column_idx + { + json::indexof() + }; + + auto &column + { + dbs::event_column.at(column_idx) + }; + + auto it + { + start > 0? + column.lower_bound(byte_view(start)): + column.begin() + }; + + for(; it; ++it) + if(!closure(byte_view(it->first), it->second)) + return false; + + return true; +} + /////////////////////////////////////////////////////////////////////////////// // // m/rooms.h diff --git a/ircd/m/vm.cc b/ircd/m/vm.cc index 88b9ca5e6..6e00ac84e 100644 --- a/ircd/m/vm.cc +++ b/ircd/m/vm.cc @@ -528,99 +528,6 @@ ircd::m::vm::write(eval &eval) txn(); } -bool -ircd::m::vm::events::rfor_each(const uint64_t &start, - const closure_bool &closure) -{ - event::fetch event; - return rfor_each(start, id_closure_bool{[&event, &closure] - (const event::idx &event_idx, const event::id &event_id) - { - if(!seek(event, event_idx, std::nothrow)) - return true; - - return closure(event_idx, event); - }}); -} - -bool -ircd::m::vm::events::rfor_each(const uint64_t &start, - const id_closure_bool &closure) -{ - static constexpr auto column_idx - { - json::indexof() - }; - - auto &column - { - dbs::event_column.at(column_idx) - }; - - if(start == uint64_t(-1)) - { - for(auto it(column.rbegin()); it; ++it) - if(!closure(byte_view(it->first), it->second)) - return false; - - return true; - } - - auto it - { - column.lower_bound(byte_view(start)) - }; - - for(; it; ++it) - if(!closure(byte_view(it->first), it->second)) - return false; - - return true; -} - -bool -ircd::m::vm::events::for_each(const uint64_t &start, - const closure_bool &closure) -{ - event::fetch event; - return for_each(start, id_closure_bool{[&event, &closure] - (const event::idx &event_idx, const event::id &event_id) - { - if(!seek(event, event_idx, std::nothrow)) - return true; - - return closure(event_idx, event); - }}); -} - -bool -ircd::m::vm::events::for_each(const uint64_t &start, - const id_closure_bool &closure) -{ - static constexpr auto column_idx - { - json::indexof() - }; - - auto &column - { - dbs::event_column.at(column_idx) - }; - - auto it - { - start > 0? - column.lower_bound(byte_view(start)): - column.begin() - }; - - for(; it; ++it) - if(!closure(byte_view(it->first), it->second)) - return false; - - return true; -} - const uint64_t & ircd::m::vm::sequence(const eval &eval) { diff --git a/modules/client/sync.cc b/modules/client/sync.cc index 319588b04..9126939cc 100644 --- a/modules/client/sync.cc +++ b/modules/client/sync.cc @@ -187,7 +187,8 @@ try std::map, std::less<>> r; - m::vm::events::for_each(since, [&] + bool limited{false}; + m::events::for_each(since, [&] (const uint64_t &sequence, const m::event &event) { if(!r.empty() && (since - _since > 128)) diff --git a/modules/console.cc b/modules/console.cc index 88227c5f1..afac1fafd 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -1454,11 +1454,11 @@ console_cmd__key__fetch(opt &out, const string_view &line) } // -// vm +// events // bool -console_cmd__vm__events(opt &out, const string_view &line) +console_cmd__events(opt &out, const string_view &line) { const params param{line, " ", { @@ -1475,7 +1475,7 @@ console_cmd__vm__events(opt &out, const string_view &line) param.at(1, 32) }; - m::vm::events::rfor_each(start, [&out, &limit] + m::events::rfor_each(start, [&out, &limit] (const uint64_t &seq, const m::event &event) { out << seq << " " << pretty_oneline(event) << std::endl;; @@ -1568,7 +1568,7 @@ console_cmd__event__dump(opt &out, const string_view &line) char *pos{data(buf)}; size_t foff{0}, ecount{0}, acount{0}, errcount{0}; - m::vm::events::for_each(0, [&](const uint64_t &seq, const m::event &event) + m::events::for_each(0, [&](const uint64_t &seq, const m::event &event) { const auto remain {