From ec8f0c1e1c9e92fde33577b469b7b778c60b4c48 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 6 Feb 2019 16:27:48 -0800 Subject: [PATCH] ircd::m::event: Add refs interface. --- include/ircd/m/event.h | 1 + include/ircd/m/event/event.h | 1 + include/ircd/m/event/refs.h | 34 ++++++++++++++++++ ircd/m_event.cc | 69 ++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 include/ircd/m/event/refs.h diff --git a/include/ircd/m/event.h b/include/ircd/m/event.h index ad9ae14b6..e68f4477e 100644 --- a/include/ircd/m/event.h +++ b/include/ircd/m/event.h @@ -33,6 +33,7 @@ namespace ircd::m #include "event/event.h" #include "event/prev.h" +#include "event/refs.h" #include "event/index.h" #include "event/fetch.h" #include "event/get.h" diff --git a/include/ircd/m/event/event.h b/include/ircd/m/event/event.h index d235b15f2..ab7d4b94a 100644 --- a/include/ircd/m/event/event.h +++ b/include/ircd/m/event/event.h @@ -78,6 +78,7 @@ struct ircd::m::event > { struct prev; + struct refs; struct fetch; struct conforms; diff --git a/include/ircd/m/event/refs.h b/include/ircd/m/event/refs.h new file mode 100644 index 000000000..1784185ed --- /dev/null +++ b/include/ircd/m/event/refs.h @@ -0,0 +1,34 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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. + +#pragma once +#define HAVE_IRCD_M_EVENT_REFS_H + +struct ircd::m::event::refs +{ + using closure_bool = event::closure_idx_bool; + + event::idx idx; + + public: + bool for_each(const closure_bool &) const; + bool has(const event::idx &) const noexcept; + size_t count() const noexcept; + + refs(const event::idx &idx) noexcept; +}; + +inline +ircd::m::event::refs::refs(const event::idx &idx) +noexcept +:idx{idx} +{ + assert(idx); +} diff --git a/ircd/m_event.cc b/ircd/m_event.cc index cf0b70e53..d4fb24158 100644 --- a/ircd/m_event.cc +++ b/ircd/m_event.cc @@ -1034,6 +1034,75 @@ ircd::m::index(const event::id &event_id, }); } +/////////////////////////////////////////////////////////////////////////////// +// +// event/refs.h +// + +size_t +ircd::m::event::refs::count() +const noexcept +{ + assert(idx); + size_t ret(0); + for_each([&ret](const auto &) + { + ++ret; + return true; + }); + + return ret; +} + +bool +ircd::m::event::refs::has(const event::idx &idx) +const noexcept +{ + return !for_each([&idx](const event::idx &ref) + { + return ref != idx; // true to continue, false to break + }); +} + +bool +ircd::m::event::refs::for_each(const closure_bool &closure) +const +{ + auto &column + { + dbs::event_refs + }; + + const byte_view &key + { + idx + }; + + auto it + { + column.begin(key) + }; + + for(; it; ++it) + { + const auto parts + { + dbs::event_refs_key(it->first) + }; + + const auto &ref + { + std::get<0>(parts) + }; + + assert(idx != ref); + if(!closure(ref)) + return false; + } + + return true; +} + /////////////////////////////////////////////////////////////////////////////// // // event/prev.h