0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-26 08:42:34 +01:00

ircd:Ⓜ️:events: Add iteration tools for events in sender/origin.

This commit is contained in:
Jason Volk 2019-04-16 14:40:12 -07:00
parent ecbdf00f22
commit d96208faaf
3 changed files with 154 additions and 0 deletions

View file

@ -15,6 +15,10 @@ namespace ircd::m::events
{
struct range;
using closure_bool = std::function<bool (const event::idx &, const event &)>;
using closure_sender_bool = std::function<bool (const id::user &, const event::idx &)>;
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 &);

View file

@ -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

View file

@ -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<size_t>
events_dump_buffer_size
{