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
|
|
|
|
};
|
|
|
|
|
2023-02-11 04:43:00 +01:00
|
|
|
dbs::opts wopts;
|
2019-09-08 05:32:46 +02:00
|
|
|
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-09-29 06:35:11 +02:00
|
|
|
static const db::gopts gopts
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2022-07-02 20:37:49 +02:00
|
|
|
.cache = false,
|
|
|
|
.checksum = false,
|
2019-06-01 02:48:55 +02:00
|
|
|
};
|
|
|
|
|
2022-06-18 21:05:17 +02:00
|
|
|
const fs::fd::opts fileopts
|
2020-01-10 23:48:16 +01:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
.mode = std::ios::out,
|
|
|
|
.exclusive = true, // error if exists
|
|
|
|
.dontneed = true, // fadvise
|
2020-01-10 23:48:16 +01:00
|
|
|
};
|
|
|
|
|
2020-08-24 13:37:30 +02:00
|
|
|
const fs::fd file
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2022-06-18 21:05:17 +02:00
|
|
|
filename, fileopts
|
2020-08-24 13:37:30 +02:00
|
|
|
};
|
2019-06-01 02:48:55 +02:00
|
|
|
|
2020-08-24 13:37:30 +02:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
size_t(dump_buffer_size), 512
|
|
|
|
};
|
2020-01-09 00:35:40 +01:00
|
|
|
|
2020-09-11 08:51:36 +02:00
|
|
|
util::timer timer;
|
2020-08-24 13:37:30 +02:00
|
|
|
event::idx seq{0};
|
|
|
|
size_t foff{0}, ecount{0}, acount{0}, errcount{0};
|
2020-09-07 21:38:59 +02:00
|
|
|
const auto flusher{[&](const const_buffer &buf)
|
2020-08-24 13:37:30 +02:00
|
|
|
{
|
2020-09-07 21:38:59 +02:00
|
|
|
const const_buffer wrote
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2020-09-07 21:38:59 +02:00
|
|
|
fs::append(file, buf)
|
|
|
|
};
|
2019-06-01 02:48:55 +02:00
|
|
|
|
2020-09-07 21:38:59 +02:00
|
|
|
foff += size(wrote);
|
|
|
|
if(acount++ % 256 == 0)
|
|
|
|
{
|
2020-09-28 02:56:38 +02:00
|
|
|
const auto elapsed
|
|
|
|
{
|
|
|
|
std::max(timer.at<seconds>().count(), 1L)
|
|
|
|
};
|
|
|
|
|
|
|
|
char pbuf[3][48];
|
2020-09-07 21:38:59 +02:00
|
|
|
log::info
|
2019-06-01 02:48:55 +02:00
|
|
|
{
|
2020-09-28 02:56:38 +02:00
|
|
|
"dump[%s] %0.2lf%% @ seq %zu of %zu; %zu events; %zu events/s; wrote %s; %s/s; %s elapsed; errors %zu",
|
2020-09-07 21:38:59 +02:00
|
|
|
filename,
|
|
|
|
(seq / double(m::vm::sequence::retired)) * 100.0,
|
|
|
|
seq,
|
|
|
|
m::vm::sequence::retired,
|
|
|
|
ecount,
|
2020-09-28 02:56:38 +02:00
|
|
|
(ecount / elapsed),
|
2020-09-11 08:51:36 +02:00
|
|
|
pretty(pbuf[0], iec(foff)),
|
2020-09-28 02:56:38 +02:00
|
|
|
pretty(pbuf[1], iec(foff / elapsed), 1),
|
|
|
|
ircd::pretty(pbuf[2], seconds(elapsed)),
|
2020-09-07 21:38:59 +02:00
|
|
|
errcount,
|
|
|
|
};
|
2019-06-01 02:48:55 +02:00
|
|
|
}
|
2020-09-07 21:38:59 +02:00
|
|
|
|
|
|
|
return wrote;
|
|
|
|
}};
|
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
2020-09-11 08:25:46 +02:00
|
|
|
buf,
|
|
|
|
flusher,
|
2020-09-19 07:16:00 +02:00
|
|
|
-1UL, // high watermark
|
|
|
|
size(buf) - 64_KiB // low watermark
|
2020-08-24 13:37:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(dbs::event_json.begin(gopts)); it; ++it) try
|
|
|
|
{
|
|
|
|
seq = byte_view<uint64_t>
|
|
|
|
{
|
|
|
|
it->first
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object source
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
|
|
|
|
2020-09-11 08:25:46 +02:00
|
|
|
const json::stack::checkpoint cp
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
2020-08-24 13:37:30 +02:00
|
|
|
top.append(source);
|
|
|
|
++ecount;
|
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
|
|
|
|
{
|
2020-09-01 01:10:41 +02:00
|
|
|
"dump[%s] %zu events; %zu writes; %zu errors :%s",
|
2020-01-09 00:47:58 +01:00
|
|
|
filename,
|
|
|
|
ecount,
|
|
|
|
acount,
|
|
|
|
errcount,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2019-06-01 02:48:55 +02:00
|
|
|
|
2020-08-24 13:37:30 +02:00
|
|
|
top.~array();
|
|
|
|
out.flush(true);
|
2020-09-11 08:51:36 +02:00
|
|
|
|
|
|
|
char pbuf[48];
|
2019-06-01 02:48:55 +02:00
|
|
|
log::notice
|
|
|
|
{
|
2020-09-11 08:51:36 +02:00
|
|
|
log, "dump[%s] complete events:%zu using %s in writes:%zu errors:%zu; %s elapsed",
|
2019-06-01 02:48:55 +02:00
|
|
|
filename,
|
|
|
|
ecount,
|
|
|
|
pretty(iec(foff)),
|
|
|
|
acount,
|
|
|
|
errcount,
|
2020-09-11 08:51:36 +02:00
|
|
|
timer.pretty(pbuf),
|
2019-06-01 02:48:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-08-25 22:00:31 +02:00
|
|
|
return --limit > 0L;
|
2019-06-01 02:42:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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?
|
2020-08-25 22:00:31 +02:00
|
|
|
std::min(range.first, vm::sequence::retired + 1):
|
2019-06-01 02:42:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-07 13:36:36 +02:00
|
|
|
//
|
|
|
|
// events::source
|
|
|
|
//
|
2019-06-01 02:42:49 +02:00
|
|
|
|
2020-10-07 13:36:36 +02:00
|
|
|
namespace ircd::m::events::source
|
|
|
|
{
|
|
|
|
extern conf::item<size_t> readahead;
|
2019-06-01 02:42:49 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 13:36:36 +02:00
|
|
|
decltype(ircd::m::events::source::readahead)
|
|
|
|
ircd::m::events::source::readahead
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.events.source.readahead" },
|
|
|
|
{ "default", long(4_MiB) },
|
|
|
|
};
|
|
|
|
|
2019-06-01 02:42:49 +02:00
|
|
|
bool
|
2020-10-07 13:36:36 +02:00
|
|
|
ircd::m::events::source::for_each(const range &range,
|
|
|
|
const closure &closure)
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2022-07-02 20:37:49 +02:00
|
|
|
const db::gopts gopts
|
2019-06-01 02:42:49 +02:00
|
|
|
{
|
2022-07-02 20:37:49 +02:00
|
|
|
.cache = false,
|
|
|
|
.checksum = false,
|
|
|
|
.readahead = size_t(readahead) & boolmask<size_t>(ascending),
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
2020-10-07 13:36:36 +02:00
|
|
|
dbs::event_json.lower_bound(byte_view<string_view>(start), gopts)
|
2019-06-01 02:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-10-07 13:36:36 +02:00
|
|
|
const json::object &event
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!closure(event_idx, event))
|
2019-06-01 02:42:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-27 03:17:10 +01:00
|
|
|
//
|
2020-10-07 13:36:36 +02:00
|
|
|
// events::content
|
2020-02-27 03:17:10 +01:00
|
|
|
//
|
|
|
|
|
2020-09-06 11:58:49 +02:00
|
|
|
namespace ircd::m::events::content
|
|
|
|
{
|
|
|
|
extern conf::item<size_t> readahead;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::events::content::readahead)
|
|
|
|
ircd::m::events::content::readahead
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.events.content.readahead" },
|
|
|
|
{ "default", long(4_MiB) },
|
|
|
|
};
|
|
|
|
|
2020-09-06 11:35:03 +02:00
|
|
|
bool
|
|
|
|
ircd::m::events::content::for_each(const closure &closure)
|
|
|
|
{
|
|
|
|
constexpr auto content_idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "content"_>()
|
|
|
|
};
|
|
|
|
|
2022-07-02 20:37:49 +02:00
|
|
|
const db::gopts gopts
|
2020-09-06 11:35:03 +02:00
|
|
|
{
|
2022-07-02 20:37:49 +02:00
|
|
|
.cache = false,
|
|
|
|
.checksum = false,
|
|
|
|
.readahead = size_t(readahead),
|
2020-09-06 11:35:03 +02:00
|
|
|
};
|
|
|
|
|
2022-07-02 20:37:49 +02:00
|
|
|
db::column &column
|
2020-09-06 11:35:03 +02:00
|
|
|
{
|
2022-07-02 20:37:49 +02:00
|
|
|
dbs::event_column.at(content_idx)
|
2020-09-06 11:35:03 +02:00
|
|
|
};
|
2020-10-12 02:19:57 +02:00
|
|
|
|
2020-09-06 11:58:49 +02:00
|
|
|
auto it(column.begin(gopts));
|
|
|
|
for(; it; ++it)
|
2020-09-06 11:35:03 +02:00
|
|
|
{
|
|
|
|
const auto &event_idx
|
|
|
|
{
|
|
|
|
byte_view<uint64_t>(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!closure(event_idx, content))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-28 03:40:23 +02:00
|
|
|
//
|
|
|
|
// events::annotates
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::annotates::for_each(const range &range,
|
|
|
|
const string_view &key,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
return relates::for_each(range, [&key, &closure]
|
|
|
|
(const event::idx &src, const m::relates_to &relates, const event::idx &tgt)
|
|
|
|
{
|
|
|
|
if(json::get<"rel_type"_>(relates) != "m.annotation")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const json::string _key
|
|
|
|
{
|
|
|
|
relates.source["key"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(key && key != _key)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return closure(src, _key, tgt);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-28 02:52:57 +02:00
|
|
|
//
|
|
|
|
// events::relates
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::relates::for_each(const range &range,
|
|
|
|
const string_view &rel_type,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
return refs::for_each(range, [&rel_type, &closure]
|
|
|
|
(const event::idx &src, const dbs::ref &type, const event::idx &tgt)
|
|
|
|
{
|
|
|
|
if(type != dbs::ref::M_RELATES)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const m::relates rels
|
|
|
|
{
|
|
|
|
src
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret(true);
|
|
|
|
rels.for_each(rel_type, [&closure, &rel_type, &src, &tgt, &ret]
|
|
|
|
(const event::idx &_tgt, const json::object &, const m::relates_to &relates_to)
|
|
|
|
{
|
|
|
|
if(_tgt != tgt)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret = closure(src, relates_to, tgt);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-12 02:06:33 +02:00
|
|
|
//
|
|
|
|
// events::refs
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::m::events::refs
|
|
|
|
{
|
|
|
|
extern conf::item<size_t> readahead;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::events::refs::readahead)
|
|
|
|
ircd::m::events::refs::readahead
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.events.refs.readahead" },
|
2020-10-14 01:01:08 +02:00
|
|
|
{ "default", long(512_KiB) },
|
2020-10-12 02:06:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::events::refs::for_each(const range &range,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
db::column &event_refs
|
|
|
|
{
|
|
|
|
dbs::event_refs
|
|
|
|
};
|
|
|
|
|
2022-07-02 20:37:49 +02:00
|
|
|
const db::gopts gopts
|
2022-01-23 20:46:24 +01:00
|
|
|
{
|
2022-07-02 20:37:49 +02:00
|
|
|
.cache = false,
|
|
|
|
.checksum = false,
|
|
|
|
.readahead = size_t(readahead),
|
2022-01-23 20:46:24 +01:00
|
|
|
};
|
2020-10-12 02:06:33 +02:00
|
|
|
|
|
|
|
const auto start
|
|
|
|
{
|
|
|
|
std::min(range.first, range.second)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto stop
|
|
|
|
{
|
|
|
|
std::max(range.first, range.second)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
event_refs.lower_bound(byte_view<string_view>(start), gopts)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; it; ++it)
|
|
|
|
{
|
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
it->first
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx src
|
|
|
|
{
|
|
|
|
byte_view<event::idx, false>(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(src >= stop)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const auto &[type, tgt]
|
|
|
|
{
|
|
|
|
dbs::event_refs_key(key.substr(sizeof(event::idx)))
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(tgt != src);
|
|
|
|
if(!closure(src, type, tgt))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:35:03 +02:00
|
|
|
//
|
|
|
|
// events::state
|
|
|
|
//
|
|
|
|
|
2020-02-27 03:17:10 +01:00
|
|
|
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]
|
2022-07-04 22:15:53 +02:00
|
|
|
(const string_view &type_) noexcept
|
2019-08-21 03:48:35 +02:00
|
|
|
{
|
|
|
|
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]
|
2022-07-04 22:15:53 +02:00
|
|
|
(const string_view &type_) noexcept
|
2019-08-21 03:48:35 +02:00
|
|
|
{
|
|
|
|
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;
|
2020-10-16 10:00:41 +02:00
|
|
|
char buf[event::ORIGIN_MAX_SIZE];
|
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;
|
|
|
|
}
|