mirror of
https://github.com/matrix-construct/construct
synced 2024-11-26 00:32:35 +01:00
ircd:Ⓜ️ Optimize away the event_seq column.
This commit is contained in:
parent
e764747e17
commit
5a3f105afc
4 changed files with 49 additions and 129 deletions
|
@ -26,7 +26,6 @@ namespace ircd::m::dbs
|
|||
extern std::array<db::column, event_columns> event_column;
|
||||
|
||||
// Event metadata columns
|
||||
extern db::column event_seq;
|
||||
extern db::column event_idx;
|
||||
extern db::index room_events;
|
||||
extern db::index room_joined;
|
||||
|
@ -101,9 +100,6 @@ namespace ircd::m::dbs::desc
|
|||
// Metadata columns
|
||||
//
|
||||
|
||||
// events sequence
|
||||
extern const database::descriptor events__event_seq;
|
||||
|
||||
// events index
|
||||
extern const database::descriptor events__event_idx;
|
||||
|
||||
|
|
|
@ -28,20 +28,22 @@ namespace ircd::m::vm
|
|||
extern ctx::shared_view<accepted> accept;
|
||||
extern const opts default_opts;
|
||||
|
||||
uint64_t sequence(const eval &);
|
||||
const uint64_t &sequence(const eval &);
|
||||
uint64_t retired_sequence(id::event::buf &);
|
||||
uint64_t retired_sequence();
|
||||
}
|
||||
|
||||
namespace ircd::m::vm::events
|
||||
{
|
||||
using idx_closure_bool = std::function<bool (const uint64_t &, const event::idx &)>;
|
||||
using closure_bool = std::function<bool (const uint64_t &, const event &)>;
|
||||
using id_closure_bool = std::function<bool (const event::idx &, const event::id &)>;
|
||||
using closure_bool = std::function<bool (const event::idx &, const event &)>;
|
||||
|
||||
// counts up from start
|
||||
bool for_each(const uint64_t &start, const idx_closure_bool &);
|
||||
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 idx_closure_bool &);
|
||||
bool rfor_each(const uint64_t &start, const id_closure_bool &);
|
||||
bool rfor_each(const uint64_t &start, const closure_bool &);
|
||||
}
|
||||
|
||||
|
@ -63,6 +65,7 @@ struct ircd::m::vm::eval
|
|||
{
|
||||
const vm::opts *opts;
|
||||
db::txn *txn;
|
||||
uint64_t sequence {0};
|
||||
|
||||
fault operator()(const event &);
|
||||
|
||||
|
|
|
@ -23,11 +23,6 @@ decltype(ircd::m::dbs::event_column)
|
|||
ircd::m::dbs::event_column
|
||||
{};
|
||||
|
||||
/// Linkage for a reference to the event_seq column.
|
||||
decltype(ircd::m::dbs::event_seq)
|
||||
ircd::m::dbs::event_seq
|
||||
{};
|
||||
|
||||
/// Linkage for a reference to the event_seq column.
|
||||
decltype(ircd::m::dbs::event_idx)
|
||||
ircd::m::dbs::event_idx
|
||||
|
@ -77,7 +72,6 @@ ircd::m::dbs::init::init(std::string dbopts)
|
|||
};
|
||||
|
||||
// Cache the columns for the metadata
|
||||
event_seq = db::column{*events, desc::events__event_seq.name};
|
||||
event_idx = db::column{*events, desc::events__event_idx.name};
|
||||
state_node = db::column{*events, desc::events__state_node.name};
|
||||
room_events = db::index{*events, desc::events__room_events.name};
|
||||
|
@ -91,7 +85,6 @@ ircd::m::dbs::init::~init()
|
|||
noexcept
|
||||
{
|
||||
// Columns should be unrefed before DB closes
|
||||
event_seq = {};
|
||||
state_node = {};
|
||||
room_events = {};
|
||||
room_joined = {};
|
||||
|
@ -466,49 +459,6 @@ ircd::m::dbs::state_root(const mutable_buffer &out,
|
|||
// Database descriptors
|
||||
//
|
||||
|
||||
const ircd::database::descriptor
|
||||
ircd::m::dbs::desc::events__event_seq
|
||||
{
|
||||
// name
|
||||
"_event_seq",
|
||||
|
||||
// explanation
|
||||
R"(### developer note:
|
||||
|
||||
Sequence counter.
|
||||
The key is an integer given by the m::vm. The value is the index number to
|
||||
be used as the key to all the event data columns. At the time of this
|
||||
comment these are actually the same thing.
|
||||
|
||||
)",
|
||||
|
||||
// typing (key, value)
|
||||
{
|
||||
typeid(uint64_t), typeid(uint64_t)
|
||||
},
|
||||
|
||||
// options
|
||||
{},
|
||||
|
||||
// comparator
|
||||
{},
|
||||
|
||||
// prefix transform
|
||||
{},
|
||||
|
||||
// cache size
|
||||
64_MiB, //TODO: conf
|
||||
|
||||
// cache size for compressed assets
|
||||
16_MiB, //TODO: conf
|
||||
|
||||
// bloom filter bits
|
||||
0, // no bloom filter because of possible comparator issues
|
||||
|
||||
// expect queries hit
|
||||
false,
|
||||
};
|
||||
|
||||
const ircd::database::descriptor
|
||||
ircd::m::dbs::desc::events__event_idx
|
||||
{
|
||||
|
@ -1757,10 +1707,6 @@ ircd::m::dbs::desc::events
|
|||
// These columns are metadata oriented around the event data.
|
||||
//
|
||||
|
||||
// uint64_t => uint64_t
|
||||
// Sequence number to event_idx number counted by the m::vm.
|
||||
events__event_seq,
|
||||
|
||||
// event_id => uint64_t
|
||||
// Mapping of event_id to index number.
|
||||
events__event_idx,
|
||||
|
|
107
ircd/m/vm.cc
107
ircd/m/vm.cc
|
@ -10,12 +10,6 @@
|
|||
|
||||
#include <ircd/m/m.h>
|
||||
|
||||
namespace ircd::m::vm
|
||||
{
|
||||
uint64_t last_sequence(id::event::buf &);
|
||||
uint64_t last_sequence();
|
||||
}
|
||||
|
||||
decltype(ircd::m::vm::log)
|
||||
ircd::m::vm::log
|
||||
{
|
||||
|
@ -45,7 +39,7 @@ ircd::m::vm::default_commit_opts
|
|||
ircd::m::vm::init::init()
|
||||
{
|
||||
id::event::buf event_id;
|
||||
current_sequence = last_sequence(event_id);
|
||||
current_sequence = retired_sequence(event_id);
|
||||
|
||||
log.info("Initializing from vm sequence %lu [%s]",
|
||||
current_sequence,
|
||||
|
@ -57,7 +51,7 @@ ircd::m::vm::init::~init()
|
|||
id::event::buf event_id;
|
||||
const auto current_sequence
|
||||
{
|
||||
last_sequence(event_id)
|
||||
retired_sequence(event_id)
|
||||
};
|
||||
|
||||
log.info("Shutting down @ %lu [%s]",
|
||||
|
@ -445,25 +439,12 @@ ircd::m::vm::_eval_pdu(eval &eval,
|
|||
}};
|
||||
|
||||
// Obtain sequence number here
|
||||
const uint64_t sequence_number
|
||||
{
|
||||
++vm::current_sequence
|
||||
};
|
||||
eval.sequence = ++vm::current_sequence;
|
||||
|
||||
m::dbs::write_opts wopts;
|
||||
wopts.present = opts.present;
|
||||
wopts.history = opts.history;
|
||||
wopts.idx = sequence_number;
|
||||
|
||||
db::txn::append
|
||||
{
|
||||
*eval.txn, dbs::event_seq,
|
||||
{
|
||||
db::op::SET,
|
||||
byte_view<string_view>(sequence_number),
|
||||
byte_view<string_view>(sequence_number),
|
||||
}
|
||||
};
|
||||
wopts.idx = eval.sequence;
|
||||
|
||||
eval_hook(event);
|
||||
|
||||
|
@ -552,29 +533,34 @@ ircd::m::vm::events::rfor_each(const uint64_t &start,
|
|||
const closure_bool &closure)
|
||||
{
|
||||
event::fetch event;
|
||||
return rfor_each(start, idx_closure_bool{[&event, &closure]
|
||||
(const uint64_t &seq, const event::idx &event_idx)
|
||||
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(seq, event);
|
||||
return closure(event_idx, event);
|
||||
}});
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::vm::events::rfor_each(const uint64_t &start,
|
||||
const idx_closure_bool &closure)
|
||||
const id_closure_bool &closure)
|
||||
{
|
||||
static constexpr auto column_idx
|
||||
{
|
||||
json::indexof<event, "event_id"_>()
|
||||
};
|
||||
|
||||
auto &column
|
||||
{
|
||||
dbs::event_seq
|
||||
dbs::event_column.at(column_idx)
|
||||
};
|
||||
|
||||
if(start == uint64_t(-1))
|
||||
{
|
||||
for(auto it(column.rbegin()); it; ++it)
|
||||
if(!closure(byte_view<uint64_t>(it->first), byte_view<event::idx>(it->second)))
|
||||
if(!closure(byte_view<event::idx>(it->first), it->second))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
@ -586,7 +572,7 @@ ircd::m::vm::events::rfor_each(const uint64_t &start,
|
|||
};
|
||||
|
||||
for(; it; ++it)
|
||||
if(!closure(byte_view<uint64_t>(it->first), byte_view<event::idx>(it->second)))
|
||||
if(!closure(byte_view<event::idx>(it->first), it->second))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
@ -597,23 +583,28 @@ ircd::m::vm::events::for_each(const uint64_t &start,
|
|||
const closure_bool &closure)
|
||||
{
|
||||
event::fetch event;
|
||||
return for_each(start, idx_closure_bool{[&event, &closure]
|
||||
(const uint64_t &seq, const event::idx &event_idx)
|
||||
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(seq, event);
|
||||
return closure(event_idx, event);
|
||||
}});
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::vm::events::for_each(const uint64_t &start,
|
||||
const idx_closure_bool &closure)
|
||||
const id_closure_bool &closure)
|
||||
{
|
||||
static constexpr auto column_idx
|
||||
{
|
||||
json::indexof<event, "event_id"_>()
|
||||
};
|
||||
|
||||
auto &column
|
||||
{
|
||||
dbs::event_seq
|
||||
dbs::event_column.at(column_idx)
|
||||
};
|
||||
|
||||
auto it
|
||||
|
@ -624,53 +615,36 @@ ircd::m::vm::events::for_each(const uint64_t &start,
|
|||
};
|
||||
|
||||
for(; it; ++it)
|
||||
if(!closure(byte_view<uint64_t>(it->first), byte_view<event::idx>(it->second)))
|
||||
if(!closure(byte_view<event::idx>(it->first), it->second))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
const uint64_t &
|
||||
ircd::m::vm::sequence(const eval &eval)
|
||||
{
|
||||
uint64_t ret;
|
||||
eval.txn->at(db::op::SET, "_event_seq", [&ret]
|
||||
(const auto &delta)
|
||||
{
|
||||
const byte_view<uint64_t> seqnum
|
||||
{
|
||||
std::get<delta.KEY>(delta)
|
||||
};
|
||||
|
||||
ret = seqnum;
|
||||
});
|
||||
|
||||
return ret;
|
||||
return eval.sequence;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
ircd::m::vm::last_sequence(id::event::buf &event_id)
|
||||
ircd::m::vm::retired_sequence()
|
||||
{
|
||||
const auto &ret
|
||||
event::id::buf event_id;
|
||||
return retired_sequence(event_id);
|
||||
}
|
||||
|
||||
uint64_t
|
||||
ircd::m::vm::retired_sequence(event::id::buf &event_id)
|
||||
{
|
||||
static constexpr auto column_idx
|
||||
{
|
||||
last_sequence()
|
||||
json::indexof<event, "event_id"_>()
|
||||
};
|
||||
|
||||
event::fetch::event_id(ret, std::nothrow, [&event_id]
|
||||
(const event::id &event_id_)
|
||||
{
|
||||
event_id = event_id_;
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
ircd::m::vm::last_sequence()
|
||||
{
|
||||
auto &column
|
||||
{
|
||||
dbs::event_seq
|
||||
dbs::event_column.at(column_idx)
|
||||
};
|
||||
|
||||
const auto it
|
||||
|
@ -691,6 +665,7 @@ ircd::m::vm::last_sequence()
|
|||
byte_view<uint64_t>(it->first)
|
||||
};
|
||||
|
||||
event_id = it->second;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue