From d1425da434bda96ca9602d347e93fc0b121af665 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 13 May 2019 10:19:18 -0700 Subject: [PATCH] ircd::m::dbs: Typedef a tuple for room_space key; conditions for truncated key generation. --- include/ircd/m/dbs/room_space.h | 10 ++++++++-- ircd/m_dbs.cc | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/ircd/m/dbs/room_space.h b/include/ircd/m/dbs/room_space.h index 1ef12658c..4a31ea60b 100644 --- a/include/ircd/m/dbs/room_space.h +++ b/include/ircd/m/dbs/room_space.h @@ -13,15 +13,21 @@ namespace ircd::m::dbs { + using room_space_key_parts = std::tuple; + constexpr size_t ROOM_SPACE_KEY_MAX_SIZE { - id::MAX_SIZE + event::TYPE_MAX_SIZE + event::STATE_KEY_MAX_SIZE + sizeof(int64_t) + id::MAX_SIZE + + event::TYPE_MAX_SIZE + + event::STATE_KEY_MAX_SIZE + + sizeof(int64_t) + + sizeof(event::idx) }; string_view room_space_key(const mutable_buffer &out, const id::room &, const string_view &type, const string_view &state_key, const int64_t &depth, const event::idx & = 0); string_view room_space_key(const mutable_buffer &out, const id::room &, const string_view &type, const string_view &state_key); string_view room_space_key(const mutable_buffer &out, const id::room &, const string_view &type); - std::tuple room_space_key(const string_view &amalgam); + room_space_key_parts room_space_key(const string_view &amalgam); // room_id | type, state_key, depth, event_idx => -- extern db::index room_space; diff --git a/ircd/m_dbs.cc b/ircd/m_dbs.cc index 566ae31bd..7c00fcfdd 100644 --- a/ircd/m_dbs.cc +++ b/ircd/m_dbs.cc @@ -3225,17 +3225,31 @@ ircd::m::dbs::room_space_key(const mutable_buffer &out_, consume(out, copy(out, type)); if(!defined(state_key)) + { + assert(depth < 0L && !event_idx); return { data(out_), data(out) }; + } consume(out, copy(out, "\0"_sv)); consume(out, copy(out, state_key)); consume(out, copy(out, "\0"_sv)); + + if(depth < 0) + { + assert(!event_idx); + return { data(out_), data(out) }; + } + consume(out, copy(out, byte_view(depth))); + + if(!event_idx) + return { data(out_), data(out) }; + consume(out, copy(out, byte_view(event_idx))); return { data(out_), data(out) }; } -std::tuple +ircd::m::dbs::room_space_key_parts ircd::m::dbs::room_space_key(const string_view &amalgam) { const auto &key