0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-16 08:58:20 +02:00

ircd:Ⓜ️:room: Refactor out remaining "test protocol" iterations from state interface.

This commit is contained in:
Jason Volk 2018-09-25 16:02:19 -07:00
parent 4788ac4e86
commit 64ccbea79d
6 changed files with 198 additions and 287 deletions

View file

@ -246,8 +246,9 @@ struct ircd::m::room::state
struct opts;
using keys = std::function<void (const string_view &)>;
using types = std::function<bool (const string_view &)>;
using keys_bool = std::function<bool (const string_view &)>;
using types = std::function<void (const string_view &)>;
using types_bool = std::function<bool (const string_view &)>;
room::id room_id;
event::id::buf event_id;
@ -257,29 +258,26 @@ struct ircd::m::room::state
bool present() const;
// Iterate the state; for_each protocol
bool for_each(const types &) const;
bool for_each(const types_bool &) const;
void for_each(const types &) const;
bool for_each(const string_view &type, const keys_bool &view) const;
void for_each(const string_view &type, const keys &) const;
bool for_each(const string_view &type, const string_view &lower_bound, const event::closure_idx_bool &view) const;
bool for_each(const string_view &type, const string_view &lower_bound, const event::id::closure_bool &view) const;
bool for_each(const string_view &type, const string_view &lower_bound, const event::closure_bool &view) const;
bool for_each(const string_view &type, const event::closure_idx_bool &view) const;
void for_each(const string_view &type, const event::closure_idx &) const;
bool for_each(const string_view &type, const event::id::closure_bool &view) const;
void for_each(const string_view &type, const event::id::closure &) const;
bool for_each(const string_view &type, const event::closure_bool &view) const;
void for_each(const string_view &type, const event::closure &) const;
bool for_each(const event::closure_idx_bool &view) const;
void for_each(const event::closure_idx &) const;
bool for_each(const event::id::closure_bool &view) const;
void for_each(const event::id::closure &) const;
bool for_each(const event::closure_bool &view) const;
void for_each(const event::closure &) const;
// Iterate the state; test protocol
bool test(const types &) const;
bool test(const string_view &type, const string_view &lower_bound, const event::closure_idx_bool &view) const;
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 keys_bool &view) const;
bool test(const string_view &type, const event::closure_idx_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::closure_idx_bool &view) const;
bool test(const event::id::closure_bool &view) const;
bool test(const event::closure_bool &view) const;
// Counting / Statistics
size_t count(const string_view &type) const;
size_t count() const;

View file

@ -1510,10 +1510,10 @@ ircd::m::rooms::for_each(const room::id::closure_bool &closure)
my_room
};
state.test("ircd.room", room::state::keys_bool{[&closure]
state.for_each("ircd.room", room::state::keys_bool{[&closure]
(const string_view &key)
{
return !closure(key);
return closure(key);
}});
}
@ -2104,7 +2104,7 @@ const
user_room, &fopts
};
return !state.test("ircd.member", [&membership, &closure]
return state.for_each("ircd.member", event::closure_bool{[&membership, &closure]
(const m::event &event)
{
const string_view &membership_
@ -2113,15 +2113,15 @@ const
};
if(membership && membership_ != membership)
return false;
return true;
const m::room::id &room_id
{
at<"state_key"_>(event)
};
return !closure(room_id, membership);
});
return closure(room_id, membership);
}});
}
//

View file

@ -926,7 +926,7 @@ bool
ircd::m::room::state::has(const string_view &type)
const
{
return test(type, event::id::closure_bool{[](const m::event::id &)
return for_each(type, event::id::closure_bool{[](const m::event::id &)
{
return true;
}});
@ -1002,8 +1002,20 @@ const
return ret;
}
void
ircd::m::room::state::for_each(const event::closure &closure)
const
{
for_each(event::closure_bool{[&closure]
(const m::event &event)
{
closure(event);
return true;
}});
}
bool
ircd::m::room::state::test(const event::closure_bool &closure)
ircd::m::room::state::for_each(const event::closure_bool &closure)
const
{
event::fetch event
@ -1011,32 +1023,44 @@ const
fopts
};
return test(event::closure_idx_bool{[&event, &closure]
return for_each(event::closure_idx_bool{[&event, &closure]
(const event::idx &event_idx)
{
if(seek(event, event_idx, std::nothrow))
if(closure(event))
return true;
if(!closure(event))
return false;
return false;
return true;
}});
}
void
ircd::m::room::state::for_each(const event::id::closure &closure)
const
{
for_each(event::id::closure_bool{[&closure]
(const event::id &event_id)
{
closure(event_id);
return true;
}});
}
bool
ircd::m::room::state::test(const event::id::closure_bool &closure)
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::test(root_id, [&closure]
(const json::array &key, const string_view &event_id)
{
return closure(unquote(event_id));
return !closure(unquote(event_id));
});
return test(event::closure_idx_bool{[&closure]
return for_each(event::closure_idx_bool{[&closure]
(const event::idx &idx)
{
bool ret{false};
bool ret{true};
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
(const event::id &id)
{
@ -1047,15 +1071,27 @@ const
}});
}
void
ircd::m::room::state::for_each(const event::closure_idx &closure)
const
{
for_each(event::closure_idx_bool{[&closure]
(const event::idx &event_idx)
{
closure(event_idx);
return true;
}});
}
bool
ircd::m::room::state::test(const event::closure_idx_bool &closure)
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::test(root_id, [&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
@ -1068,15 +1104,28 @@ const
auto &column{dbs::room_state};
for(auto it{column.begin(room_id, opts)}; bool(it); ++it)
if(closure(byte_view<event::idx>(it->second)))
return true;
if(!closure(byte_view<event::idx>(it->second)))
return false;
return false;
return true;
}
void
ircd::m::room::state::for_each(const string_view &type,
const event::closure &closure)
const
{
for_each(type, event::closure_bool{[&closure]
(const m::event &event)
{
closure(event);
return true;
}});
}
bool
ircd::m::room::state::test(const string_view &type,
const event::closure_bool &closure)
ircd::m::room::state::for_each(const string_view &type,
const event::closure_bool &closure)
const
{
event::fetch event
@ -1084,33 +1133,46 @@ const
fopts
};
return test(type, event::closure_idx_bool{[&event, &closure]
return for_each(type, event::closure_idx_bool{[&event, &closure]
(const event::idx &event_idx)
{
if(seek(event, event_idx, std::nothrow))
if(closure(event))
return true;
if(!closure(event))
return false;
return false;
return true;
}});
}
void
ircd::m::room::state::for_each(const string_view &type,
const event::id::closure &closure)
const
{
for_each(type, event::id::closure_bool{[&closure]
(const event::id &event_id)
{
closure(event_id);
return true;
}});
}
bool
ircd::m::room::state::test(const string_view &type,
const event::id::closure_bool &closure)
ircd::m::room::state::for_each(const string_view &type,
const event::id::closure_bool &closure)
const
{
if(!present())
return m::state::test(root_id, type, [&closure]
return !m::state::test(root_id, type, [&closure]
(const json::array &key, const string_view &event_id)
{
return closure(unquote(event_id));
return !closure(unquote(event_id));
});
return test(type, event::closure_idx_bool{[&closure]
return for_each(type, event::closure_idx_bool{[&closure]
(const event::idx &idx)
{
bool ret{false};
bool ret{true};
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
(const event::id &id)
{
@ -1121,16 +1183,29 @@ const
}});
}
void
ircd::m::room::state::for_each(const string_view &type,
const event::closure_idx &closure)
const
{
for_each(type, event::closure_idx_bool{[&closure]
(const event::idx &event_idx)
{
closure(event_idx);
return true;
}});
}
bool
ircd::m::room::state::test(const string_view &type,
const event::closure_idx_bool &closure)
ircd::m::room::state::for_each(const string_view &type,
const event::closure_idx_bool &closure)
const
{
if(!present())
return m::state::test(root_id, type, [&closure]
return !m::state::test(root_id, type, [&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];
@ -1151,64 +1226,18 @@ const
for(auto it{column.begin(key, opts)}; bool(it); ++it)
if(std::get<0>(dbs::room_state_key(it->first)) == type)
{
if(closure(byte_view<event::idx>(it->second)))
return true;
if(!closure(byte_view<event::idx>(it->second)))
return false;
}
else break;
return false;
return true;
}
bool
ircd::m::room::state::test(const string_view &type,
const keys_bool &closure)
const
{
if(!present())
return m::state::test(root_id, type, [&closure]
(const json::array &key, const string_view &event_id)
{
assert(size(key) >= 2);
return closure(unquote(key.at(1)));
});
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
const auto &key
{
dbs::room_state_key(keybuf, room_id, type)
};
db::gopts opts
{
this->fopts? this->fopts->gopts : db::gopts{}
};
if(!opts.readahead)
opts.readahead = 0_KiB;
auto &column{dbs::room_state};
for(auto it{column.begin(key, opts)}; bool(it); ++it)
{
const auto part
{
dbs::room_state_key(it->first)
};
if(std::get<0>(part) == type)
{
if(closure(std::get<1>(part)))
return true;
}
else break;
}
return false;
}
bool
ircd::m::room::state::test(const string_view &type,
const string_view &state_key_lb,
const event::closure_bool &closure)
ircd::m::room::state::for_each(const string_view &type,
const string_view &state_key_lb,
const event::closure_bool &closure)
const
{
event::fetch event
@ -1216,34 +1245,34 @@ const
fopts
};
return test(type, state_key_lb, event::closure_idx_bool{[&event, &closure]
return for_each(type, state_key_lb, event::closure_idx_bool{[&event, &closure]
(const event::idx &event_idx)
{
if(seek(event, event_idx, std::nothrow))
if(closure(event))
return true;
if(!closure(event))
return false;
return false;
return true;
}});
}
bool
ircd::m::room::state::test(const string_view &type,
const string_view &state_key_lb,
const event::id::closure_bool &closure)
ircd::m::room::state::for_each(const string_view &type,
const string_view &state_key_lb,
const event::id::closure_bool &closure)
const
{
if(!present())
return m::state::test(root_id, type, state_key_lb, [&closure]
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));
return !closure(unquote(event_id));
});
return test(type, state_key_lb, event::closure_idx_bool{[&closure]
return for_each(type, state_key_lb, event::closure_idx_bool{[&closure]
(const event::idx &idx)
{
bool ret{false};
bool ret{true};
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
(const event::id &id)
{
@ -1255,16 +1284,16 @@ const
}
bool
ircd::m::room::state::test(const string_view &type,
const string_view &state_key_lb,
const event::closure_idx_bool &closure)
ircd::m::room::state::for_each(const string_view &type,
const string_view &state_key_lb,
const event::closure_idx_bool &closure)
const
{
if(!present())
return m::state::test(root_id, type, state_key_lb, [&closure]
return !m::state::test(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];
@ -1285,166 +1314,38 @@ const
for(auto it{column.begin(key, opts)}; bool(it); ++it)
if(std::get<0>(dbs::room_state_key(it->first)) == type)
{
if(closure(byte_view<event::idx>(it->second)))
return true;
if(!closure(byte_view<event::idx>(it->second)))
return false;
}
else break;
return false;
}
bool
ircd::m::room::state::test(const types &closure)
const
{
return !for_each(types{[&closure]
(const string_view &type)
{
return !closure(type);
}});
}
void
ircd::m::room::state::for_each(const event::closure &closure)
const
{
event::fetch event
{
fopts
};
for_each(event::closure_idx{[&event, &closure]
(const event::idx &event_idx)
{
if(seek(event, event_idx, std::nothrow))
closure(event);
}});
}
void
ircd::m::room::state::for_each(const event::id::closure &closure)
const
{
if(!present())
return m::state::for_each(root_id, [&closure]
(const json::array &key, const string_view &event_id)
{
closure(unquote(event_id));
});
for_each(event::closure_idx{[&closure]
(const event::idx &idx)
{
event::fetch::event_id(idx, std::nothrow, closure);
}});
}
void
ircd::m::room::state::for_each(const event::closure_idx &closure)
const
{
if(!present())
return m::state::for_each(root_id, [&closure]
(const json::array &key, const string_view &event_id)
{
closure(index(unquote(event_id), std::nothrow));
});
db::gopts opts
{
this->fopts? this->fopts->gopts : db::gopts{}
};
if(!opts.readahead)
opts.readahead = 0_KiB;
auto &column{dbs::room_state};
for(auto it{column.begin(room_id, opts)}; bool(it); ++it)
closure(byte_view<event::idx>(it->second));
}
void
ircd::m::room::state::for_each(const string_view &type,
const event::closure &closure)
const
{
event::fetch event
{
fopts
};
for_each(type, event::closure_idx{[&event, &closure]
(const event::idx &event_idx)
{
if(seek(event, event_idx, std::nothrow))
closure(event);
}});
}
void
ircd::m::room::state::for_each(const string_view &type,
const event::id::closure &closure)
const
{
if(!present())
return m::state::for_each(root_id, type, [&closure]
(const json::array &key, const string_view &event_id)
{
closure(unquote(event_id));
});
for_each(type, event::closure_idx{[&closure]
(const event::idx &idx)
{
event::fetch::event_id(idx, std::nothrow, closure);
}});
}
void
ircd::m::room::state::for_each(const string_view &type,
const event::closure_idx &closure)
const
{
if(!present())
return m::state::for_each(root_id, type, [&closure]
(const json::array &key, const string_view &event_id)
{
closure(index(unquote(event_id), std::nothrow));
});
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
const auto &key
{
dbs::room_state_key(keybuf, room_id, type)
};
db::gopts opts
{
this->fopts? this->fopts->gopts : db::gopts{}
};
if(!opts.readahead)
opts.readahead = 0_KiB;
auto &column{dbs::room_state};
for(auto it{column.begin(key, opts)}; bool(it); ++it)
if(std::get<0>(dbs::room_state_key(it->first)) == type)
closure(byte_view<event::idx>(it->second));
else
break;
return true;
}
void
ircd::m::room::state::for_each(const string_view &type,
const keys &closure)
const
{
for_each(type, keys_bool{[&closure]
(const string_view &key)
{
closure(key);
return true;
}});
}
bool
ircd::m::room::state::for_each(const string_view &type,
const keys_bool &closure)
const
{
if(!present())
return m::state::for_each(root_id, type, [&closure]
(const json::array &key, const string_view &)
return !m::state::test(root_id, type, [&closure]
(const json::array &key, const string_view &event_id)
{
assert(size(key) >= 2);
closure(unquote(key.at(1)));
return !closure(unquote(key.at(1)));
});
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
@ -1470,14 +1371,30 @@ const
};
if(std::get<0>(part) == type)
closure(std::get<1>(part));
else
break;
{
if(!closure(std::get<1>(part)))
return false;
}
else break;
}
return true;
}
void
ircd::m::room::state::for_each(const types &closure)
const
{
for_each(types_bool{[&closure]
(const string_view &type)
{
closure(type);
return true;
}});
}
bool
ircd::m::room::state::for_each(const types &closure)
ircd::m::room::state::for_each(const types_bool &closure)
const
{
string_view last;
@ -1575,10 +1492,10 @@ ircd::m::room::members::test(const event::closure_bool &closure)
const
{
const room::state state{room};
return state.test("m.room.member", event::closure_bool{[&closure]
return !state.for_each("m.room.member", event::closure_bool{[&closure]
(const m::event &event)
{
return closure(event);
return !closure(event);
}});
}

View file

@ -56,7 +56,7 @@ post__search(client &client,
bool limited{true};
std::vector<json::value> results;
users.test("ircd.user", query, [&results, &limit, &limited]
users.for_each("ircd.user", query, [&results, &limit, &limited]
(const m::event &event)
{
results.emplace_back(json::members
@ -65,7 +65,7 @@ post__search(client &client,
});
limited = results.size() >= limit;
return limited; // return true to break
return !limited; // returns true to continue
});
return resource::response

View file

@ -114,7 +114,7 @@ post__publicrooms(client &client,
{
json::stack::member chunk_m{top, "chunk"};
json::stack::array chunk{chunk_m};
publix.test("ircd.room", since, [&](const m::event &event)
publix.for_each("ircd.room", since, [&](const m::event &event)
{
const m::room::id room_id{at<"state_key"_>(event)};
const m::room::state state{room_id};
@ -221,7 +221,7 @@ post__publicrooms(client &client,
});
next_batch_buf = room_id;
return count >= limit;
return count < limit;
});
}

View file

@ -6795,13 +6795,9 @@ console_cmd__node__keys(opt &out, const string_view &line)
param.at(1, size_t(1))
};
const m::node::room node_room
{
node
};
const m::node::room node_room{node};
const m::room::state state{node_room};
state.for_each("ircd.key", [&out, &limit]
state.for_each("ircd.key", m::event::closure_bool{[&out, &limit]
(const m::event &event)
{
const m::keys keys
@ -6811,7 +6807,7 @@ console_cmd__node__keys(opt &out, const string_view &line)
out << keys << std::endl;
return --limit;
});
}});
return true;
}