From 5a3f105afce5b496a832ec6178e922937a1d4e24 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 18 Apr 2018 00:26:14 -0700 Subject: [PATCH] ircd::m: Optimize away the event_seq column. --- include/ircd/m/dbs.h | 4 -- include/ircd/m/vm.h | 13 ++++-- ircd/m/dbs.cc | 54 ---------------------- ircd/m/vm.cc | 107 +++++++++++++++++-------------------------- 4 files changed, 49 insertions(+), 129 deletions(-) diff --git a/include/ircd/m/dbs.h b/include/ircd/m/dbs.h index 455b9e81a..597bfd1a8 100644 --- a/include/ircd/m/dbs.h +++ b/include/ircd/m/dbs.h @@ -26,7 +26,6 @@ namespace ircd::m::dbs extern std::array 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; diff --git a/include/ircd/m/vm.h b/include/ircd/m/vm.h index cbef28198..a65d1af2f 100644 --- a/include/ircd/m/vm.h +++ b/include/ircd/m/vm.h @@ -28,20 +28,22 @@ namespace ircd::m::vm extern ctx::shared_view 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; - using closure_bool = std::function; + using id_closure_bool = std::function; + using closure_bool = std::function; // 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 &); diff --git a/ircd/m/dbs.cc b/ircd/m/dbs.cc index 5f23d356c..243218fc5 100644 --- a/ircd/m/dbs.cc +++ b/ircd/m/dbs.cc @@ -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, diff --git a/ircd/m/vm.cc b/ircd/m/vm.cc index 6d1882c3b..88b9ca5e6 100644 --- a/ircd/m/vm.cc +++ b/ircd/m/vm.cc @@ -10,12 +10,6 @@ #include -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(sequence_number), - byte_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() + }; + 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(it->first), byte_view(it->second))) + if(!closure(byte_view(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(it->first), byte_view(it->second))) + if(!closure(byte_view(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() + }; + 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(it->first), byte_view(it->second))) + if(!closure(byte_view(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 seqnum - { - std::get(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::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(it->first) }; + event_id = it->second; return ret; }