From f2a3e2487c112f91bfcea3819af780d4f559a642 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 23 Feb 2018 19:38:56 -0800 Subject: [PATCH] ircd::m: Support lower_bound state_key queries into room::state. --- include/ircd/m/room.h | 2 ++ include/ircd/m/state.h | 1 + ircd/m/room.cc | 31 +++++++++++++++++++++++++++++++ ircd/m/state.cc | 19 +++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/include/ircd/m/room.h b/include/ircd/m/room.h index c03367217..925e8e160 100644 --- a/include/ircd/m/room.h +++ b/include/ircd/m/room.h @@ -181,6 +181,8 @@ struct ircd::m::room::state void for_each(const event::closure &) const; // Iterate the state; test protocol + bool test(const string_view &type, const string_view &lower_bound, const event::id::closure_bool &view) const; + bool test(const string_view &type, const string_view &lower_bound, const event::closure_bool &view) const; bool test(const string_view &type, const event::id::closure_bool &view) const; bool test(const string_view &type, const event::closure_bool &view) const; bool test(const event::id::closure_bool &view) const; diff --git a/include/ircd/m/state.h b/include/ircd/m/state.h index b853cb85b..7282819ca 100644 --- a/include/ircd/m/state.h +++ b/include/ircd/m/state.h @@ -62,6 +62,7 @@ namespace ircd::m::state bool test(const id &root, const iter_bool_closure &); bool test(const id &root, const string_view &type, const iter_bool_closure &); + bool test(const id &root, const string_view &type, const string_view &state_key_lb, const iter_bool_closure &); void for_each(const id &root, const iter_closure &); void for_each(const id &root, const string_view &type, const iter_closure &); diff --git a/ircd/m/room.cc b/ircd/m/room.cc index bbcd86cee..4916de811 100644 --- a/ircd/m/room.cc +++ b/ircd/m/room.cc @@ -483,6 +483,37 @@ const }); } +bool +ircd::m::room::state::test(const string_view &type, + const string_view &state_key_lb, + const event::closure_bool &closure) +const +{ + event::fetch event; + return test(type, state_key_lb, event::id::closure_bool{[&event, &closure] + (const event::id &event_id) + { + if(seek(event, unquote(event_id), std::nothrow)) + if(closure(event)) + return true; + + return false; + }}); +} + +bool +ircd::m::room::state::test(const string_view &type, + const string_view &state_key_lb, + const event::id::closure_bool &closure) +const +{ + return m::state::test(root_id, type, state_key_lb, [&closure] + (const json::array &key, const string_view &event_id) + { + return closure(unquote(event_id)); + }); +} + void ircd::m::room::state::for_each(const event::closure &closure) const diff --git a/ircd/m/state.cc b/ircd/m/state.cc index 74c4827f1..ac64603f5 100644 --- a/ircd/m/state.cc +++ b/ircd/m/state.cc @@ -170,6 +170,25 @@ ircd::m::state::test(const string_view &root, }); } +bool +ircd::m::state::test(const string_view &root, + const string_view &type, + const string_view &state_key_lb, + const iter_bool_closure &closure) +{ + char buf[KEY_MAX_SZ]; + const json::array key + { + make_key(buf, type, state_key_lb) + }; + + return dfs(root, key, [&closure] + (const json::array &key, const string_view &val, const uint &, const uint &) + { + return closure(key, val); + }); +} + namespace ircd::m::state { bool _dfs_recurse(const search_closure &, const node &, const json::array &key, int &);