0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-25 05:18:23 +02:00

ircd:Ⓜ️:state: Eliminate remaining test-protocol iterations.

This commit is contained in:
Jason Volk 2018-11-29 15:05:51 -08:00
parent 209aa550b3
commit 0b0d20884d
4 changed files with 48 additions and 49 deletions

View file

@ -73,12 +73,11 @@ namespace ircd::m::state
size_t count(const id &root, const string_view &type); size_t count(const id &root, const string_view &type);
size_t count(const id &root); size_t count(const id &root);
bool test(const id &root, const iter_bool_closure &); bool for_each(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 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 &); 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 &); size_t accumulate(const id &root, const iter_bool_closure &);

View file

@ -1162,11 +1162,11 @@ ircd::m::room::state::for_each(const event::id::closure_bool &closure)
const const
{ {
if(!present()) 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) (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] return for_each(event::closure_idx_bool{[&closure]
(const event::idx &idx) (const event::idx &idx)
@ -1199,11 +1199,11 @@ ircd::m::room::state::for_each(const event::closure_idx_bool &closure)
const const
{ {
if(!present()) 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) (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 db::gopts opts
{ {
@ -1319,10 +1319,10 @@ ircd::m::room::state::for_each(const string_view &type,
const const
{ {
if(!present()) 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) (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] 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 const
{ {
if(!present()) 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) (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]; char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
@ -1406,11 +1406,11 @@ ircd::m::room::state::for_each(const string_view &type,
const const
{ {
if(!present()) 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) (const json::array &key, const string_view &event_id)
{ {
assert(size(key) >= 2); assert(size(key) >= 2);
return !closure(unquote(key.at(1))); return closure(unquote(key.at(1)));
}); });
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE]; char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
@ -1466,7 +1466,7 @@ const
char lastbuf[256]; //TODO: type maxlen char lastbuf[256]; //TODO: type maxlen
if(!present()) 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 &) (const json::array &key, const string_view &)
{ {
assert(size(key) >= 2); assert(size(key) >= 2);
@ -1480,7 +1480,7 @@ const
last = strlcpy(lastbuf, type); last = strlcpy(lastbuf, type);
return closure(type); return closure(type);
}); }});
return true; return true;
} }

View file

@ -112,11 +112,22 @@ void
ircd::m::state::for_each(const string_view &root, ircd::m::state::for_each(const string_view &root,
const iter_closure &closure) const iter_closure &closure)
{ {
test(root, [&closure] for_each(root, iter_bool_closure{[&closure]
(const json::array &key, const string_view &val) (const json::array &key, const string_view &val)
{ {
closure(key, 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 string_view &type,
const iter_closure &closure) const iter_closure &closure)
{ {
test(root, type, [&closure] for_each(root, type, iter_bool_closure{[&closure]
(const json::array &key, const string_view &val) (const json::array &key, const string_view &val)
{ {
closure(key, val); closure(key, val);
return false; return true;
}); }});
} }
bool bool
ircd::m::state::test(const string_view &root, ircd::m::state::for_each(const string_view &root,
const iter_bool_closure &closure) const string_view &type,
{ 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)
{ {
char buf[KEY_MAX_SZ]; char buf[KEY_MAX_SZ];
const json::array key const json::array key
@ -155,18 +155,18 @@ ircd::m::state::test(const string_view &root,
make_key(buf, type) 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 &) (const json::array &key, const string_view &val, const uint &, const uint &)
{ {
return closure(key, val); return !closure(key, val);
}); });
} }
bool bool
ircd::m::state::test(const string_view &root, ircd::m::state::for_each(const string_view &root,
const string_view &type, const string_view &type,
const string_view &state_key_lb, const string_view &state_key_lb,
const iter_bool_closure &closure) const iter_bool_closure &closure)
{ {
char buf[KEY_MAX_SZ]; char buf[KEY_MAX_SZ];
const json::array key const json::array key
@ -174,10 +174,10 @@ ircd::m::state::test(const string_view &root,
make_key(buf, type, state_key_lb) 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 &) (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 json::array &key,
const search_closure &closure) const search_closure &closure)
{ {
bool ret{true}; bool ret{false};
get_node(root, [&closure, &key, &ret] get_node(std::nothrow, root, [&closure, &key, &ret]
(const auto &node) (const auto &node)
{ {
int depth(-1); int depth(-1);

View file

@ -5259,10 +5259,10 @@ console_cmd__state__find(opt &out, const string_view &line)
(const auto &key, const string_view &val) (const auto &key, const string_view &val)
{ {
out << key << " => " << val << std::endl; 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; return true;
} }