0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-14 16:46:50 +01:00

ircd:Ⓜ️:events: Split and reorganize API sections into specific namespaces.

This commit is contained in:
Jason Volk 2019-08-20 18:44:58 -07:00
parent d320ce0b47
commit 204989802d
5 changed files with 208 additions and 180 deletions

View file

@ -14,42 +14,61 @@
namespace ircd::m::events namespace ircd::m::events
{ {
struct range; struct range;
using closure_bool = std::function<bool (const event::idx &, const event &)>; using closure = std::function<bool (const event::idx &, const event &)>;
using closure_type_bool = std::function<bool (const string_view &, const event::idx &)>;
using closure_sender_bool = std::function<bool (const id::user &, const event::idx &)>;
using closure_type_name_bool = std::function<bool (const string_view &)>;
using closure_sender_name_bool = std::function<bool (const id::user &)>;
using closure_origin_name_bool = std::function<bool (const string_view &)>;
// Iterate types starting with some prefix, or all types.
bool for_each_type(const closure_type_name_bool &);
bool for_each_type(const string_view &prefix, const closure_type_name_bool &);
// Iterate servers starting at the hostname equal to or greater than provided
bool for_each_origin(const closure_origin_name_bool &);
bool for_each_origin(const string_view &hostlb, const closure_origin_name_bool &);
// Iterate senders of events (users).
bool for_each_sender(const closure_sender_name_bool &);
bool for_each_sender(const string_view &key, const closure_sender_name_bool &);
// Iterate event indexes matching the argument
bool for_each_in_type(const string_view &, const closure_type_bool &);
bool for_each_in_sender(const id::user &, const closure_sender_bool &);
bool for_each_in_origin(const string_view &, const closure_sender_bool &);
// Iterate viable event indexes in a range // Iterate viable event indexes in a range
bool for_each(const range &, const event::closure_idx_bool &); bool for_each(const range &, const event::closure_idx_bool &);
bool for_each(const range &, const event_filter &, const event::closure_idx_bool &); bool for_each(const range &, const event_filter &, const event::closure_idx_bool &);
// Iterate events in an index range // Iterate events in an index range
bool for_each(const range &, const closure_bool &); bool for_each(const range &, const closure &);
bool for_each(const range &, const event_filter &, const closure_bool &); bool for_each(const range &, const event_filter &, const closure &);
// util // util
void dump__file(const string_view &filename); void dump__file(const string_view &filename);
} }
/// Interface to the types of all events known to this server.
namespace ircd::m::events::type
{
using closure = std::function<bool (const string_view &, const event::idx &)>;
using closure_name = std::function<bool (const string_view &)>;
// Iterate the names of all event types.
bool for_each(const closure_name &);
bool for_each(const string_view &prefix, const closure_name &);
// Iterate the events for a specific type.
bool for_each_in(const string_view &, const closure &);
}
/// Interface to the senders of all events known to the server.
namespace ircd::m::events::sender
{
using closure = std::function<bool (const id::user &, const event::idx &)>;
using closure_name = std::function<bool (const id::user &)>;
// Iterate all of the sender mxids known to the server.
bool for_each(const closure_name &);
bool for_each(const string_view &key, const closure_name &);
// Iterate all of the events for a specific sender mxid.
bool for_each_in(const id::user &, const closure &);
}
/// Interface to the servers of the senders of all events known to this server.
namespace ircd::m::events::origin
{
using closure_name = std::function<bool (const string_view &)>;
// Iterate all server names known to this server.
bool for_each(const closure_name &);
bool for_each(const string_view &hostlb, const closure_name &);
// Iterate all senders mxids on a specific server.
bool for_each_in(const string_view &, const sender::closure &);
}
/// Range to start (inclusive) and stop (exclusive). If start is greater than /// Range to start (inclusive) and stop (exclusive). If start is greater than
/// stop a reverse iteration will occur. -1 (or unsigned max value) can be used /// stop a reverse iteration will occur. -1 (or unsigned max value) can be used
/// to start or stop at the end. 0 can be used to start or stop at the beginning. /// to start or stop at the end. 0 can be used to start or stop at the beginning.
@ -67,3 +86,21 @@ struct ircd::m::events::range
,fopts{fopts} ,fopts{fopts}
{} {}
}; };
inline bool
ircd::m::events::origin::for_each(const closure_name &closure)
{
return for_each(string_view{}, closure);
}
inline bool
ircd::m::events::sender::for_each(const closure_name &closure)
{
return for_each(string_view{}, closure);
}
inline bool
ircd::m::events::type::for_each(const closure_name &closure)
{
return for_each(string_view{}, closure);
}

View file

@ -6335,7 +6335,7 @@ console_cmd__events__in__sender(opt &out, const string_view &line)
param.at("user_id") param.at("user_id")
}; };
m::events::for_each_in_sender(user_id, [&out] m::events::sender::for_each_in(user_id, [&out]
(const m::user::id &user_id, const m::event::idx &event_idx) (const m::user::id &user_id, const m::event::idx &event_idx)
{ {
const m::event::fetch event const m::event::fetch event
@ -6369,7 +6369,7 @@ console_cmd__events__in__origin(opt &out, const string_view &line)
lstrip(param.at("origin"), ':') lstrip(param.at("origin"), ':')
}; };
m::events::for_each_in_origin(origin, [&out] m::events::origin::for_each_in(origin, [&out]
(const m::user::id &user_id, const m::event::idx &event_idx) (const m::user::id &user_id, const m::event::idx &event_idx)
{ {
const m::event::fetch event const m::event::fetch event
@ -6403,7 +6403,7 @@ console_cmd__events__in__type(opt &out, const string_view &line)
param.at("type") param.at("type")
}; };
m::events::for_each_in_type(type, [&out] m::events::type::for_each_in(type, [&out]
(const string_view &type, const m::event::idx &event_idx) (const string_view &type, const m::event::idx &event_idx)
{ {
const m::event::fetch event const m::event::fetch event
@ -6459,7 +6459,7 @@ console_cmd__events__type(opt &out, const string_view &line)
param["prefix"] param["prefix"]
}; };
m::events::for_each_type(prefix, [&out] m::events::type::for_each(prefix, [&out]
(const string_view &type) (const string_view &type)
{ {
out << type << std::endl; out << type << std::endl;
@ -6482,7 +6482,7 @@ console_cmd__events__sender(opt &out, const string_view &line)
param["prefix"] param["prefix"]
}; };
m::events::for_each_sender(prefix, [&out] m::events::sender::for_each(prefix, [&out]
(const m::user::id &user_id) (const m::user::id &user_id)
{ {
out << user_id << std::endl; out << user_id << std::endl;
@ -6505,7 +6505,7 @@ console_cmd__events__origin(opt &out, const string_view &line)
param["prefix"] param["prefix"]
}; };
m::events::for_each_origin(prefix, [&out] m::events::origin::for_each(prefix, [&out]
(const string_view &origin) (const string_view &origin)
{ {
out << origin << std::endl; out << origin << std::endl;

View file

@ -106,7 +106,7 @@ bool
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::events::for_each(const range &range, ircd::m::events::for_each(const range &range,
const event_filter &filter, const event_filter &filter,
const closure_bool &closure) const closure &closure)
{ {
auto limit auto limit
{ {
@ -130,7 +130,7 @@ ircd::m::events::for_each(const range &range,
bool bool
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::events::for_each(const range &range, ircd::m::events::for_each(const range &range,
const closure_bool &closure) const closure &closure)
{ {
event::fetch event event::fetch event
{ {
@ -255,85 +255,14 @@ ircd::m::events::for_each(const range &range,
return true; return true;
} }
bool //
IRCD_MODULE_EXPORT // events::type
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_origin_key(buf, origin)
};
auto it
{
column.begin(key)
};
for(; bool(it); ++it)
{
const auto &keyp
{
dbs::event_sender_origin_key(it->first)
};
const user::id::buf user_id
{
std::get<0>(keyp), origin
};
if(!closure(user_id, std::get<1>(keyp)))
return false;
}
return true;
}
bool bool
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::events::for_each_in_sender(const id::user &user, ircd::m::events::type::for_each_in(const string_view &type,
const closure_sender_bool &closure) const closure &closure)
{
auto &column
{
dbs::event_sender
};
char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE];
const string_view &key
{
dbs::event_sender_key(buf, user)
};
auto it
{
column.begin(key)
};
for(; bool(it); ++it)
{
const auto &keyp
{
dbs::event_sender_key(it->first)
};
if(!closure(user, std::get<event::idx>(keyp)))
return false;
}
return true;
}
bool
IRCD_MODULE_EXPORT
ircd::m::events::for_each_in_type(const string_view &type,
const closure_type_bool &closure)
{ {
auto &column auto &column
{ {
@ -367,15 +296,8 @@ ircd::m::events::for_each_in_type(const string_view &type,
bool bool
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::events::for_each_type(const closure_type_name_bool &closure) ircd::m::events::type::for_each(const string_view &prefix,
{ const closure_name &closure)
return for_each_type(string_view{}, closure);
}
bool
IRCD_MODULE_EXPORT
ircd::m::events::for_each_type(const string_view &prefix,
const closure_type_name_bool &closure)
{ {
db::column &column db::column &column
{ {
@ -414,17 +336,140 @@ ircd::m::events::for_each_type(const string_view &prefix,
return true; return true;
} }
//
// events::origin
//
bool bool
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::events::for_each_sender(const closure_sender_name_bool &closure) ircd::m::events::origin::for_each_in(const string_view &origin,
const sender::closure &closure)
{ {
return for_each_sender(string_view{}, closure); auto &column
{
dbs::event_sender
};
char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE];
const string_view &key
{
dbs::event_sender_origin_key(buf, origin)
};
auto it
{
column.begin(key)
};
for(; bool(it); ++it)
{
const auto &keyp
{
dbs::event_sender_origin_key(it->first)
};
const user::id::buf user_id
{
std::get<0>(keyp), origin
};
if(!closure(user_id, std::get<1>(keyp)))
return false;
}
return true;
} }
bool bool
IRCD_MODULE_EXPORT IRCD_MODULE_EXPORT
ircd::m::events::for_each_sender(const string_view &prefix_, ircd::m::events::origin::for_each(const string_view &prefix,
const closure_sender_name_bool &closure) const closure_name &closure)
{
db::column &column
{
dbs::event_sender
};
const auto &prefixer
{
dbs::desc::events__event_sender__pfx
};
if(unlikely(startswith(prefix, '@')))
throw panic
{
"Prefix argument should be a hostname. It must not start with '@'"
};
string_view last;
char buf[rfc3986::DOMAIN_BUFSIZE];
for(auto it(column.lower_bound(prefix)); bool(it); ++it)
{
if(!m::dbs::is_event_sender_origin_key(it->first))
break;
const string_view &host
{
prefixer.get(it->first)
};
if(host == last)
continue;
if(!startswith(host, prefix))
break;
last = { buf, copy(buf, host) };
if(!closure(host))
return false;
}
return true;
}
//
// events::sender
//
bool
IRCD_MODULE_EXPORT
ircd::m::events::sender::for_each_in(const id::user &user,
const closure &closure)
{
auto &column
{
dbs::event_sender
};
char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE];
const string_view &key
{
dbs::event_sender_key(buf, user)
};
auto it
{
column.begin(key)
};
for(; bool(it); ++it)
{
const auto &keyp
{
dbs::event_sender_key(it->first)
};
if(!closure(user, std::get<event::idx>(keyp)))
return false;
}
return true;
}
bool
IRCD_MODULE_EXPORT
ircd::m::events::sender::for_each(const string_view &prefix_,
const closure_name &closure)
{ {
db::column &column db::column &column
{ {
@ -475,57 +520,3 @@ ircd::m::events::for_each_sender(const string_view &prefix_,
return true; return true;
} }
bool
IRCD_MODULE_EXPORT
ircd::m::events::for_each_origin(const closure_origin_name_bool &closure)
{
return for_each_origin(string_view{}, closure);
}
bool
IRCD_MODULE_EXPORT
ircd::m::events::for_each_origin(const string_view &prefix,
const closure_origin_name_bool &closure)
{
db::column &column
{
dbs::event_sender
};
const auto &prefixer
{
dbs::desc::events__event_sender__pfx
};
if(unlikely(startswith(prefix, '@')))
throw panic
{
"Prefix argument should be a hostname. It must not start with '@'"
};
string_view last;
char buf[rfc3986::DOMAIN_BUFSIZE];
for(auto it(column.lower_bound(prefix)); bool(it); ++it)
{
if(!m::dbs::is_event_sender_origin_key(it->first))
break;
const string_view &host
{
prefixer.get(it->first)
};
if(host == last)
continue;
if(!startswith(host, prefix))
break;
last = { buf, copy(buf, host) };
if(!closure(host))
return false;
}
return true;
}

View file

@ -115,7 +115,7 @@ ircd::m::rooms::for_each(const opts &opts,
}); });
} }
return events::for_each_in_type("m.room.create", [&proffer, &ret] return events::type::for_each_in("m.room.create", [&proffer, &ret]
(const string_view &type, const event::idx &event_idx) (const string_view &type, const event::idx &event_idx)
{ {
assert(type == "m.room.create"); assert(type == "m.room.create");

View file

@ -66,7 +66,7 @@ ircd::m::users::for_each(const opts &opts,
return for_each_host(opts, closure); return for_each_host(opts, closure);
bool ret{true}; bool ret{true};
events::for_each_sender(opts.localpart, [&opts, &ret, &closure] events::sender::for_each(opts.localpart, [&opts, &ret, &closure]
(const id::user &sender) (const id::user &sender)
{ {
if(opts.localpart && !opts.localpart_prefix) if(opts.localpart && !opts.localpart_prefix)
@ -98,7 +98,7 @@ ircd::m::users::for_each_host(const opts &opts,
const user::closure_bool &closure) const user::closure_bool &closure)
{ {
bool ret{true}; bool ret{true};
events::for_each_origin(opts.hostpart, [&ret, &opts, &closure] events::origin::for_each(opts.hostpart, [&ret, &opts, &closure]
(const string_view &origin) (const string_view &origin)
{ {
if(opts.hostpart && !opts.hostpart_prefix) if(opts.hostpart && !opts.hostpart_prefix)
@ -125,7 +125,7 @@ ircd::m::users::for_each_in_host(const opts &opts,
{ {
bool ret{true}; bool ret{true};
m::user::id::buf last; m::user::id::buf last;
events::for_each_in_origin(opts.hostpart, [&opts, &ret, &closure, &last] events::origin::for_each_in(opts.hostpart, [&opts, &ret, &closure, &last]
(const id::user &sender, const auto &event_idx) (const id::user &sender, const auto &event_idx)
{ {
if(sender == last) if(sender == last)