From 0b0d20884d61062772f68ebd92b0117adb2a1ff7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 29 Nov 2018 15:05:51 -0800 Subject: [PATCH] ircd::m::state: Eliminate remaining test-protocol iterations. --- include/ircd/m/state.h | 7 +++-- ircd/m/room.cc | 28 ++++++++++---------- ircd/m/state.cc | 58 +++++++++++++++++++++--------------------- modules/console.cc | 4 +-- 4 files changed, 48 insertions(+), 49 deletions(-) diff --git a/include/ircd/m/state.h b/include/ircd/m/state.h index 16823e1a1..bfd46ccfe 100644 --- a/include/ircd/m/state.h +++ b/include/ircd/m/state.h @@ -73,12 +73,11 @@ namespace ircd::m::state size_t count(const id &root, const string_view &type); size_t count(const id &root); - 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 &); - + bool for_each(const id &root, const iter_bool_closure &); void for_each(const id &root, const iter_closure &); + bool for_each(const id &root, const string_view &type, const iter_bool_closure &); void for_each(const id &root, const string_view &type, const iter_closure &); + bool for_each(const id &root, const string_view &type, const string_view &state_key_lb, const iter_bool_closure &); size_t accumulate(const id &root, const iter_bool_closure &); diff --git a/ircd/m/room.cc b/ircd/m/room.cc index 59a623696..8127b6542 100644 --- a/ircd/m/room.cc +++ b/ircd/m/room.cc @@ -1162,11 +1162,11 @@ ircd::m::room::state::for_each(const event::id::closure_bool &closure) const { if(!present()) - return !m::state::test(root_id, [&closure] + return m::state::for_each(root_id, m::state::iter_bool_closure{[&closure] (const json::array &key, const string_view &event_id) { - return !closure(unquote(event_id)); - }); + return closure(unquote(event_id)); + }}); return for_each(event::closure_idx_bool{[&closure] (const event::idx &idx) @@ -1199,11 +1199,11 @@ ircd::m::room::state::for_each(const event::closure_idx_bool &closure) const { if(!present()) - return !m::state::test(root_id, [&closure] + return m::state::for_each(root_id, m::state::iter_bool_closure{[&closure] (const json::array &key, const string_view &event_id) { - return !closure(index(unquote(event_id), std::nothrow)); - }); + return closure(index(unquote(event_id), std::nothrow)); + }}); db::gopts opts { @@ -1319,10 +1319,10 @@ ircd::m::room::state::for_each(const string_view &type, const { if(!present()) - return !m::state::test(root_id, type, state_key_lb, [&closure] + return m::state::for_each(root_id, type, state_key_lb, [&closure] (const json::array &key, const string_view &event_id) { - return !closure(unquote(event_id)); + return closure(unquote(event_id)); }); return for_each(type, state_key_lb, event::closure_idx_bool{[&closure] @@ -1346,10 +1346,10 @@ ircd::m::room::state::for_each(const string_view &type, const { if(!present()) - return !m::state::test(root_id, type, state_key_lb, [&closure] + return m::state::for_each(root_id, type, state_key_lb, [&closure] (const json::array &key, const string_view &event_id) { - return !closure(index(unquote(event_id), std::nothrow)); + return closure(index(unquote(event_id), std::nothrow)); }); char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE]; @@ -1406,11 +1406,11 @@ ircd::m::room::state::for_each(const string_view &type, const { if(!present()) - return !m::state::test(root_id, type, state_key_lb, [&closure] + return m::state::for_each(root_id, type, state_key_lb, [&closure] (const json::array &key, const string_view &event_id) { assert(size(key) >= 2); - return !closure(unquote(key.at(1))); + return closure(unquote(key.at(1))); }); char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE]; @@ -1466,7 +1466,7 @@ const char lastbuf[256]; //TODO: type maxlen if(!present()) { - m::state::for_each(root_id, [&closure, &last, &lastbuf] + m::state::for_each(root_id, m::state::iter_bool_closure{[&closure, &last, &lastbuf] (const json::array &key, const string_view &) { assert(size(key) >= 2); @@ -1480,7 +1480,7 @@ const last = strlcpy(lastbuf, type); return closure(type); - }); + }}); return true; } diff --git a/ircd/m/state.cc b/ircd/m/state.cc index cab65c1cc..ea234475d 100644 --- a/ircd/m/state.cc +++ b/ircd/m/state.cc @@ -112,11 +112,22 @@ void ircd::m::state::for_each(const string_view &root, const iter_closure &closure) { - test(root, [&closure] + for_each(root, iter_bool_closure{[&closure] (const json::array &key, const string_view &val) { closure(key, val); - return false; + return true; + }}); +} + +bool +ircd::m::state::for_each(const string_view &root, + const iter_bool_closure &closure) +{ + return !dfs(root, [&closure] + (const json::array &key, const string_view &val, const uint &, const uint &) + { + return !closure(key, val); }); } @@ -125,29 +136,18 @@ ircd::m::state::for_each(const string_view &root, const string_view &type, const iter_closure &closure) { - test(root, type, [&closure] + for_each(root, type, iter_bool_closure{[&closure] (const json::array &key, const string_view &val) { closure(key, val); - return false; - }); + return true; + }}); } bool -ircd::m::state::test(const string_view &root, - const iter_bool_closure &closure) -{ - return dfs(root, [&closure] - (const json::array &key, const string_view &val, const uint &, const uint &) - { - return closure(key, val); - }); -} - -bool -ircd::m::state::test(const string_view &root, - const string_view &type, - const iter_bool_closure &closure) +ircd::m::state::for_each(const string_view &root, + const string_view &type, + const iter_bool_closure &closure) { char buf[KEY_MAX_SZ]; const json::array key @@ -155,18 +155,18 @@ ircd::m::state::test(const string_view &root, make_key(buf, type) }; - return dfs(root, key, [&closure] + return !dfs(root, key, [&closure] (const json::array &key, const string_view &val, const uint &, const uint &) { - return closure(key, val); + return !closure(key, val); }); } bool -ircd::m::state::test(const string_view &root, - const string_view &type, - const string_view &state_key_lb, - const iter_bool_closure &closure) +ircd::m::state::for_each(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 @@ -174,10 +174,10 @@ ircd::m::state::test(const string_view &root, make_key(buf, type, state_key_lb) }; - return dfs(root, key, [&closure] + return !dfs(root, key, [&closure] (const json::array &key, const string_view &val, const uint &, const uint &) { - return closure(key, val); + return !closure(key, val); }); } @@ -278,8 +278,8 @@ ircd::m::state::dfs(const string_view &root, const json::array &key, const search_closure &closure) { - bool ret{true}; - get_node(root, [&closure, &key, &ret] + bool ret{false}; + get_node(std::nothrow, root, [&closure, &key, &ret] (const auto &node) { int depth(-1); diff --git a/modules/console.cc b/modules/console.cc index ae463f490..d76f5bcad 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -5259,10 +5259,10 @@ console_cmd__state__find(opt &out, const string_view &line) (const auto &key, const string_view &val) { out << key << " => " << val << std::endl; - return false; + return true; }}; - m::state::test(root, type, state_key, closure); + m::state::for_each(root, type, state_key, closure); return true; }