2019-06-01 02:42:49 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-06-01 02:48:55 +02:00
|
|
|
namespace ircd::m::events
|
|
|
|
{
|
|
|
|
extern conf::item<size_t> dump_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::events::dump_buffer_size)
|
|
|
|
ircd::m::events::dump_buffer_size
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.events.dump.buffer_size" },
|
2020-01-08 23:38:43 +01:00
|
|
|
{ "default", int64_t(512_KiB) },
|
2019-06-01 02:48:55 +02:00
|
|
|
};
|
|
|
|
|
2019-09-08 05:32:46 +02:00
|
|
|
void
|
|
|
|
ircd::m::events::rebuild()
|
|
|
|
{
|
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
event::keys::include {"type", "sender"}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const m::events::range range
|
|
|
|
{
|
|
|
|
0, -1UL, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
db::txn txn
|
|
|
|
{
|
|
|
|
*m::dbs::events
|
|
|
|
};
|
|
|
|
|
|
|
|
dbs::write_opts wopts;
|
|
|
|
wopts.appendix.reset();
|
|
|
|
wopts.appendix.set(dbs::appendix::EVENT_TYPE);
|
|
|
|
wopts.appendix.set(dbs::appendix::EVENT_SENDER);
|
|
|
|
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(range, [&txn, &wopts, &ret]
|
|
|
|
(const event::idx &event_idx, const m::event &event)
|
|
|
|
{
|
|
|
|
wopts.event_idx = event_idx;
|
|
|
|
dbs::write(txn, event, wopts);
|
|
|
|
++ret;
|
|
|
|
|
|
|
|
if(ret % 8192UL == 0UL)
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Events type/sender table rebuild events %zu of %zu num:%zu txn:%zu %s",
|
|
|
|
event_idx,
|
|
|
|
vm::sequence::retired,
|
|
|
|
ret,
|
|
|
|
txn.size(),
|
|
|
|
pretty(iec(txn.bytes())),
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2020-04-10 01:38:31 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Events type/sender table rebuild events:%zu txn:%zu %s commit...",
|
|
|
|
ret,
|
|
|
|
txn.size(),
|
|
|
|
pretty(iec(txn.bytes())),
|
|
|
|
};
|
2019-09-08 05:32:46 +02:00
|
|
|
|
|
|
|
txn();
|
|
|
|
|
2020-04-10 01:38:31 +02:00
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
log, "Events type/sender table rebuild complete.",
|
|
|
|
ret,
|
|
|
|
txn.size(),
|
|
|
|
pretty(iec(txn.bytes())),
|
|
|
|
};
|
2019-09-08 05:32:46 +02:00
|
|
|
}
|
|
|
|
|
2019-06-01 02:48:55 +02:00
|
|
|
void
|
|
|
|
ircd::m::events::dump__file(const string_view &filename)
|
|
|
|
{
|
2020-05-10 03:58:01 +02:00
|
|
|
fs::fd::opts fileopts(std::ios::out | std::ios::app);
|
|
|
|
fileopts.dontneed = true;
|
2019-06-01 02:48:55 +02:00
|
|
|
const fs::fd file
|
|
|
|
{
|
2020-05-10 03:58:01 +02:00
|
|
|
filename, fileopts
|
2019-06-01 02:48:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
size_t(dump_buffer_size)
|
|
|
|
};
|
|
|
|
|
|
|
|
char *pos{data(buf)};
|
|
|
|
size_t foff{0}, ecount{0}, acount{0}, errcount{0};
|
2020-01-10 23:48:16 +01:00
|
|
|
static const db::gopts gopts
|
|
|
|
{
|
|
|
|
db::get::NO_CACHE, db::get::NO_CHECKSUM
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(dbs::event_json.begin(gopts)); it; ++it) try
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2020-01-09 00:35:40 +01:00
|
|
|
const event::idx seq
|
|
|
|
{
|
|
|
|
byte_view<uint64_t>(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view source
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
|
|
|
|
2019-06-01 02:48:55 +02:00
|
|
|
const auto remain
|
|
|
|
{
|
|
|
|
size_t(data(buf) + size(buf) - pos)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(remain >= event::MAX_SIZE && remain <= size(buf));
|
2020-01-09 00:35:40 +01:00
|
|
|
const mutable_buffer mb
|
|
|
|
{
|
|
|
|
pos, remain
|
|
|
|
};
|
|
|
|
|
|
|
|
pos += copy(mb, source);
|
2019-06-01 02:48:55 +02:00
|
|
|
++ecount;
|
|
|
|
if(pos + event::MAX_SIZE > data(buf) + size(buf))
|
|
|
|
{
|
2020-01-08 23:38:26 +01:00
|
|
|
const const_buffer cb
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2020-01-08 23:38:26 +01:00
|
|
|
data(buf), pos
|
2019-06-01 02:48:55 +02:00
|
|
|
};
|
|
|
|
|
2020-01-08 23:38:26 +01:00
|
|
|
foff += size(fs::append(file, cb));
|
|
|
|
pos = data(buf);
|
|
|
|
if(acount++ % 256 == 0)
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2020-01-08 23:38:26 +01:00
|
|
|
char pbuf[48];
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
"dump[%s] %0.2lf%% @ seq %zu of %zu; %zu events; %s in %zu writes; %zu errors",
|
|
|
|
filename,
|
|
|
|
(seq / double(m::vm::sequence::retired)) * 100.0,
|
|
|
|
seq,
|
|
|
|
m::vm::sequence::retired,
|
|
|
|
ecount,
|
|
|
|
pretty(pbuf, iec(foff)),
|
|
|
|
acount,
|
|
|
|
errcount
|
|
|
|
};
|
|
|
|
}
|
2019-06-01 02:48:55 +02:00
|
|
|
}
|
2020-01-09 00:35:40 +01:00
|
|
|
}
|
2020-01-09 00:47:58 +01:00
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
++errcount;
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"dump[%s] events; %s in %zu writes; %zu errors :%s",
|
|
|
|
filename,
|
|
|
|
ecount,
|
|
|
|
acount,
|
|
|
|
errcount,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2019-06-01 02:48:55 +02:00
|
|
|
|
|
|
|
if(pos > data(buf))
|
|
|
|
{
|
|
|
|
const const_buffer cb{data(buf), pos};
|
|
|
|
foff += size(fs::append(file, cb));
|
|
|
|
++acount;
|
|
|
|
}
|
|
|
|
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
log, "dump[%s] complete events:%zu using %s in writes:%zu errors:%zu",
|
|
|
|
filename,
|
|
|
|
ecount,
|
|
|
|
pretty(iec(foff)),
|
|
|
|
acount,
|
|
|
|
errcount,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:42:49 +02:00
|
|
|
bool
|
|
|
|
ircd::m::events::for_each(const range &range,
|
|
|
|
const event_filter &filter,
|
2019-08-21 03:44:58 +02:00
|
|
|
const closure &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
auto limit
|
|
|
|
{
|
|
|
|
json::get<"limit"_>(filter)?: 32L
|
|
|
|
};
|
|
|
|
|
|
|
|
return for_each(range, [&filter, &closure, &limit]
|
|
|
|
(const event::idx &event_idx, const m::event &event)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
if(!match(filter, event))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!closure(event_idx, event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return --limit;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::for_each(const range &range,
|
2019-08-21 03:44:58 +02:00
|
|
|
const closure &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
event::fetch event
|
|
|
|
{
|
|
|
|
range.fopts? *range.fopts : event::fetch::default_opts
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool ascending
|
|
|
|
{
|
|
|
|
range.first <= range.second
|
|
|
|
};
|
|
|
|
|
|
|
|
auto start
|
|
|
|
{
|
|
|
|
ascending?
|
|
|
|
range.first:
|
|
|
|
std::min(range.first, vm::sequence::retired)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto stop
|
|
|
|
{
|
|
|
|
ascending?
|
|
|
|
std::min(range.second, vm::sequence::retired + 1):
|
|
|
|
range.second
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; start != stop; ascending? ++start : --start)
|
2020-04-03 04:50:00 +02:00
|
|
|
if(seek(std::nothrow, event, start))
|
2019-06-01 02:42:49 +02:00
|
|
|
if(!closure(start, event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
///TODO: This impl is temp. Need better dispatching based on filter before
|
|
|
|
///TODO: fetching event.
|
|
|
|
bool
|
|
|
|
ircd::m::events::for_each(const range &range,
|
|
|
|
const event_filter &filter,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
{
|
|
|
|
auto limit
|
|
|
|
{
|
|
|
|
json::get<"limit"_>(filter)?: 32L
|
|
|
|
};
|
|
|
|
|
|
|
|
return for_each(range, event::closure_idx_bool{[&filter, &closure, &limit, &range]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow, event_idx, range.fopts? *range.fopts : event::fetch::default_opts
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!match(filter, event))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!closure(event_idx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return --limit;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::for_each(const range &range,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
{
|
|
|
|
const bool ascending
|
|
|
|
{
|
|
|
|
range.first <= range.second
|
|
|
|
};
|
|
|
|
|
|
|
|
auto start
|
|
|
|
{
|
|
|
|
ascending?
|
|
|
|
range.first:
|
|
|
|
std::min(range.first, vm::sequence::retired)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto stop
|
|
|
|
{
|
|
|
|
ascending?
|
|
|
|
std::min(range.second, vm::sequence::retired + 1):
|
|
|
|
range.second
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_json
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
column.lower_bound(byte_view<string_view>(start))
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; bool(it); ascending? ++it : --it)
|
|
|
|
{
|
|
|
|
const event::idx event_idx
|
|
|
|
{
|
|
|
|
byte_view<event::idx>(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(ascending && event_idx >= stop)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!ascending && event_idx <= stop)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!closure(event_idx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:17:10 +01:00
|
|
|
//
|
|
|
|
// events::state
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::state::for_each(const closure &closure)
|
|
|
|
{
|
|
|
|
static const tuple none
|
|
|
|
{
|
|
|
|
{}, {}, {}, -1L, 0UL
|
|
|
|
};
|
|
|
|
|
|
|
|
return state::for_each(none, [&closure]
|
|
|
|
(const tuple &key) -> bool
|
|
|
|
{
|
|
|
|
return closure(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::state::for_each(const tuple &query,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
db::column &column
|
|
|
|
{
|
|
|
|
dbs::event_state
|
|
|
|
};
|
|
|
|
|
|
|
|
char buf[dbs::EVENT_STATE_KEY_MAX_SIZE];
|
|
|
|
const string_view query_key
|
|
|
|
{
|
|
|
|
dbs::event_state_key(buf, query)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(column.lower_bound(query_key)); bool(it); ++it)
|
|
|
|
{
|
|
|
|
const auto key
|
|
|
|
{
|
|
|
|
dbs::event_state_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &[state_key, type, room_id, depth, event_idx]
|
|
|
|
{
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool tab[]
|
|
|
|
{
|
|
|
|
!std::get<0>(query) || std::get<0>(query) == std::get<0>(key),
|
|
|
|
!std::get<1>(query) || std::get<1>(query) == std::get<1>(key),
|
|
|
|
!std::get<2>(query) || std::get<2>(query) == std::get<2>(key),
|
|
|
|
std::get<3>(query) <= 0 || std::get<3>(query) == std::get<3>(key),
|
|
|
|
std::get<4>(query) == 0 || std::get<4>(query) == std::get<4>(key),
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!std::all_of(begin(tab), end(tab), identity()))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!closure(key))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
//
|
|
|
|
// events::type
|
|
|
|
//
|
|
|
|
|
2019-08-21 03:48:35 +02:00
|
|
|
bool
|
|
|
|
ircd::m::events::type::has(const string_view &type)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
for_each(type, [&ret, &type]
|
|
|
|
(const string_view &type_)
|
|
|
|
{
|
|
|
|
ret = type == type_;
|
|
|
|
return false; // uncond break out of loop after first result
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::type::has_prefix(const string_view &type)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
for_each(type, [&ret, &type]
|
|
|
|
(const string_view &type_)
|
|
|
|
{
|
|
|
|
ret = startswith(type_, type);
|
|
|
|
return false; // uncond break out of loop after first result
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:42:49 +02:00
|
|
|
bool
|
2019-08-21 03:44:58 +02:00
|
|
|
ircd::m::events::type::for_each_in(const string_view &type,
|
|
|
|
const closure &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_type
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
char buf[dbs::EVENT_TYPE_KEY_MAX_SIZE];
|
2019-06-01 02:42:49 +02:00
|
|
|
const string_view &key
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_type_key(buf, type)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
column.begin(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const auto &keyp
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_type_key(it->first)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
if(!closure(type, std::get<0>(keyp)))
|
2019-06-01 02:42:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-21 03:44:58 +02:00
|
|
|
ircd::m::events::type::for_each(const string_view &prefix,
|
|
|
|
const closure_name &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
db::column &column
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_type
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
const auto &prefixer
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2020-03-25 19:06:29 +01:00
|
|
|
dbs::desc::event_type__pfx
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
string_view last;
|
|
|
|
char lastbuf[event::TYPE_MAX_SIZE];
|
|
|
|
for(auto it(column.lower_bound(prefix)); bool(it); ++it)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
prefixer.get(it->first)
|
|
|
|
};
|
2019-06-01 02:42:49 +02:00
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
if(type == last)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(prefix && !startswith(type, prefix))
|
|
|
|
break;
|
|
|
|
|
|
|
|
last =
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
lastbuf, copy(lastbuf, type)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
if(!closure(type))
|
2019-06-01 02:42:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
//
|
|
|
|
// events::origin
|
|
|
|
//
|
|
|
|
|
2019-06-01 02:42:49 +02:00
|
|
|
bool
|
2019-08-21 03:44:58 +02:00
|
|
|
ircd::m::events::origin::for_each_in(const string_view &origin,
|
|
|
|
const sender::closure &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
auto &column
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_sender
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
char buf[dbs::EVENT_SENDER_KEY_MAX_SIZE];
|
2019-06-01 02:42:49 +02:00
|
|
|
const string_view &key
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_sender_origin_key(buf, origin)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
column.begin(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const auto &keyp
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_sender_origin_key(it->first)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
const user::id::buf user_id
|
|
|
|
{
|
|
|
|
std::get<0>(keyp), origin
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!closure(user_id, std::get<1>(keyp)))
|
2019-06-01 02:42:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-21 03:44:58 +02:00
|
|
|
ircd::m::events::origin::for_each(const string_view &prefix,
|
|
|
|
const closure_name &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
db::column &column
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
dbs::event_sender
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &prefixer
|
|
|
|
{
|
2020-03-25 19:06:29 +01:00
|
|
|
dbs::desc::event_sender__pfx
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
if(unlikely(startswith(prefix, '@')))
|
|
|
|
throw panic
|
|
|
|
{
|
|
|
|
"Prefix argument should be a hostname. It must not start with '@'"
|
|
|
|
};
|
|
|
|
|
2019-06-01 02:42:49 +02:00
|
|
|
string_view last;
|
2019-08-21 03:44:58 +02:00
|
|
|
char buf[rfc3986::DOMAIN_BUFSIZE];
|
2019-06-01 02:42:49 +02:00
|
|
|
for(auto it(column.lower_bound(prefix)); bool(it); ++it)
|
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
if(!m::dbs::is_event_sender_origin_key(it->first))
|
|
|
|
break;
|
|
|
|
|
|
|
|
const string_view &host
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
prefixer.get(it->first)
|
|
|
|
};
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
if(host == last)
|
2019-06-01 02:42:49 +02:00
|
|
|
continue;
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
if(!startswith(host, prefix))
|
2019-06-01 02:42:49 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
last = { buf, copy(buf, host) };
|
|
|
|
if(!closure(host))
|
2019-06-01 02:42:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
//
|
|
|
|
// events::sender
|
|
|
|
//
|
|
|
|
|
2019-06-01 02:42:49 +02:00
|
|
|
bool
|
2019-08-21 03:44:58 +02:00
|
|
|
ircd::m::events::sender::for_each_in(const id::user &user,
|
|
|
|
const closure &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-21 03:44:58 +02:00
|
|
|
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;
|
2019-06-01 02:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-08-21 03:44:58 +02:00
|
|
|
ircd::m::events::sender::for_each(const string_view &prefix_,
|
|
|
|
const closure_name &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
db::column &column
|
|
|
|
{
|
|
|
|
dbs::event_sender
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &prefixer
|
|
|
|
{
|
2020-03-25 19:06:29 +01:00
|
|
|
dbs::desc::event_sender__pfx
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
// We MUST query the column with a key starting with '@' here. For a more
|
|
|
|
// convenient API, if the user did not supply an '@' we prefix it for them.
|
|
|
|
char prebuf[id::user::buf::SIZE] {"@"};
|
|
|
|
const string_view prefix
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-20 03:24:10 +02:00
|
|
|
!startswith(prefix_, '@')?
|
|
|
|
strlcat{prebuf, prefix_}:
|
|
|
|
prefix_
|
|
|
|
};
|
2019-06-01 02:42:49 +02:00
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
m::user::id::buf last;
|
|
|
|
for(auto it(column.lower_bound(prefix)); bool(it); ++it)
|
|
|
|
{
|
|
|
|
// Check if this is an '@' key; otherwise it's in the origin
|
|
|
|
// keyspace (sharing this column) which we don't want here.
|
|
|
|
if(!m::dbs::is_event_sender_key(it->first))
|
2019-06-01 02:42:49 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
// Apply the domain prefixer, since we're iterating as a db::column
|
|
|
|
// rather than db::domain.
|
|
|
|
const m::user::id &user_id
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2019-08-20 03:24:10 +02:00
|
|
|
prefixer.get(it->first)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
if(user_id == last)
|
2019-06-01 02:42:49 +02:00
|
|
|
continue;
|
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
if(!startswith(user_id, prefix))
|
|
|
|
break;
|
2019-06-01 02:42:49 +02:00
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
if(!closure(user_id))
|
2019-06-01 02:42:49 +02:00
|
|
|
return false;
|
2019-08-12 11:33:57 +02:00
|
|
|
|
2019-08-20 03:24:10 +02:00
|
|
|
last = user_id;
|
2019-06-01 02:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|