From a1c3788c380db3ec17d8e36fce3ac8b9f6bd586b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 11 May 2018 19:21:56 -0700 Subject: [PATCH] ircd::m: Add single-property getter interface for event. --- include/ircd/m/event.h | 11 ++++ ircd/m/event.cc | 123 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/include/ircd/m/event.h b/include/ircd/m/event.h index b92a4a437..fbf514fef 100644 --- a/include/ircd/m/event.h +++ b/include/ircd/m/event.h @@ -201,6 +201,17 @@ struct ircd::m::event::fetch friend void seek(fetch &, const idx &); friend bool seek(fetch &, const id &, std::nothrow_t); friend void seek(fetch &, const id &); + + using view_closure = std::function; + friend bool get(std::nothrow_t, const idx &, const string_view &key, const view_closure &); + friend bool get(std::nothrow_t, const id &, const string_view &key, const view_closure &); + friend void get(const idx &, const string_view &key, const view_closure &); + friend void get(const id &, const string_view &key, const view_closure &); + + friend const_buffer get(std::nothrow_t, const idx &, const string_view &key, const mutable_buffer &out); + friend const_buffer get(std::nothrow_t, const id &, const string_view &key, const mutable_buffer &out); + friend const_buffer get(const idx &, const string_view &key, const mutable_buffer &out); + friend const_buffer get(const id &, const string_view &key, const mutable_buffer &out); }; /// Device to evaluate the conformity of an event object. This is an 'in vitro' diff --git a/ircd/m/event.cc b/ircd/m/event.cc index 01121a28f..e846b5ca3 100644 --- a/ircd/m/event.cc +++ b/ircd/m/event.cc @@ -1204,6 +1204,129 @@ const // event::fetch // +ircd::const_buffer +ircd::m::get(const event::id &event_id, + const string_view &key, + const mutable_buffer &out) +{ + const auto &ret + { + get(std::nothrow, index(event_id), key, out) + }; + + if(!ret) + throw m::NOT_FOUND + { + "%s for %s not found in database", + key, + string_view{event_id} + }; + + return ret; +} + +ircd::const_buffer +ircd::m::get(const event::idx &event_idx, + const string_view &key, + const mutable_buffer &out) +{ + const const_buffer ret + { + get(std::nothrow, event_idx, key, out) + }; + + if(!ret) + throw m::NOT_FOUND + { + "%s for event_idx[%lu] not found in database", + key, + event_idx + }; + + return ret; +} + +ircd::const_buffer +ircd::m::get(std::nothrow_t, + const event::id &event_id, + const string_view &key, + const mutable_buffer &buf) +{ + return get(std::nothrow, index(event_id), key, buf); +} + +ircd::const_buffer +ircd::m::get(std::nothrow_t, + const event::idx &event_idx, + const string_view &key, + const mutable_buffer &buf) +{ + const_buffer ret; + get(std::nothrow, event_idx, key, [&buf, &ret] + (const string_view &value) + { + ret = { data(buf), copy(buf, value) }; + }); + + return ret; +} + +void +ircd::m::get(const event::id &event_id, + const string_view &key, + const event::fetch::view_closure &closure) +{ + if(!get(std::nothrow, index(event_id), key, closure)) + throw m::NOT_FOUND + { + "%s for %s not found in database", + key, + string_view{event_id} + }; +} + +void +ircd::m::get(const event::idx &event_idx, + const string_view &key, + const event::fetch::view_closure &closure) +{ + if(!get(std::nothrow, event_idx, key, closure)) + throw m::NOT_FOUND + { + "%s for event_idx[%lu] not found in database", + key, + event_idx + }; +} + +bool +ircd::m::get(std::nothrow_t, + const event::id &event_id, + const string_view &key, + const event::fetch::view_closure &closure) +{ + return get(std::nothrow, index(event_id), key, closure); +} + +bool +ircd::m::get(std::nothrow_t, + const event::idx &event_idx, + const string_view &key, + const event::fetch::view_closure &closure) +{ + const auto &column_idx + { + json::indexof(key) + }; + + auto &column + { + dbs::event_column.at(column_idx) + }; + + return column(byte_view{event_idx}, std::nothrow, closure); +} + void ircd::m::seek(event::fetch &fetch, const event::id &event_id)