mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 18:22:50 +01:00
ircd:Ⓜ️ Improve room::messages interface as a bidirectional STL iterator.
This commit is contained in:
parent
6f0ca19df6
commit
d32f990c6a
2 changed files with 113 additions and 64 deletions
|
@ -106,23 +106,41 @@ struct ircd::m::room
|
||||||
|
|
||||||
/// Interface to room messages
|
/// Interface to room messages
|
||||||
///
|
///
|
||||||
/// This interface focuses on room messages (state and ephemeral) from all
|
/// This interface has the form of an STL-style iterator over room messages
|
||||||
/// integrated timelines. Most queries to this interface respond in linear
|
/// which are state and non-state events from all integrated timelines.
|
||||||
/// time.
|
/// Moving the iterator is cheap, but the dereference operators fetch a
|
||||||
|
/// full event. One can iterate just event_id's by using event_id() instead
|
||||||
|
/// of the dereference operators.
|
||||||
///
|
///
|
||||||
struct ircd::m::room::messages
|
struct ircd::m::room::messages
|
||||||
{
|
{
|
||||||
m::room room;
|
m::room room;
|
||||||
|
db::index::const_iterator it;
|
||||||
|
event::id _event_id;
|
||||||
|
event::fetch _event;
|
||||||
|
|
||||||
void for_each(const event::id::closure &) const;
|
public:
|
||||||
void for_each(const event::closure &) const;
|
operator bool() const { return bool(it); }
|
||||||
|
bool operator!() const { return !it; }
|
||||||
|
|
||||||
bool test(const event::id::closure_bool &view) const;
|
const event::id &event_id();
|
||||||
bool test(const event::closure_bool &view) const;
|
const m::event &fetch(std::nothrow_t);
|
||||||
|
const m::event &fetch();
|
||||||
|
|
||||||
messages(const m::room &room)
|
bool seek(const uint64_t &depth);
|
||||||
:room{room}
|
bool seek(const event::id &);
|
||||||
{}
|
bool seek();
|
||||||
|
|
||||||
|
// These are reversed on purpose
|
||||||
|
auto &operator++() { return --it; }
|
||||||
|
auto &operator--() { return ++it; }
|
||||||
|
|
||||||
|
const m::event &operator*();
|
||||||
|
const m::event *operator->() { return &operator*(); }
|
||||||
|
|
||||||
|
messages(const m::room &room, const uint64_t &depth);
|
||||||
|
messages(const m::room &room, const event::id &);
|
||||||
|
messages(const m::room &room);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Interface to room state.
|
/// Interface to room state.
|
||||||
|
|
137
ircd/m/room.cc
137
ircd/m/room.cc
|
@ -369,75 +369,106 @@ const
|
||||||
// room::messages
|
// room::messages
|
||||||
//
|
//
|
||||||
|
|
||||||
void
|
ircd::m::room::messages::messages(const m::room &room)
|
||||||
ircd::m::room::messages::for_each(const event::closure &closure)
|
:room{room}
|
||||||
const
|
|
||||||
{
|
{
|
||||||
event::fetch event;
|
seek();
|
||||||
for_each(event::id::closure{[&closure, &event]
|
|
||||||
(const event::id &event_id)
|
|
||||||
{
|
|
||||||
if(seek(event, event_id, std::nothrow))
|
|
||||||
closure(event);
|
|
||||||
}});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
ircd::m::room::messages::messages(const m::room &room,
|
||||||
ircd::m::room::messages::for_each(const event::id::closure &closure)
|
const event::id &event_id)
|
||||||
const
|
:room{room}
|
||||||
{
|
{
|
||||||
test(event::id::closure_bool{[&closure]
|
seek(event_id);
|
||||||
(const event::id &event_id)
|
|
||||||
{
|
|
||||||
closure(event_id);
|
|
||||||
return false;
|
|
||||||
}});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
ircd::m::room::messages::messages(const m::room &room,
|
||||||
ircd::m::room::messages::test(const event::closure_bool &closure)
|
const uint64_t &depth)
|
||||||
const
|
:room{room}
|
||||||
{
|
{
|
||||||
event::fetch event;
|
seek(depth);
|
||||||
return test(event::id::closure_bool{[&closure, &event]
|
|
||||||
(const m::event::id &event_id)
|
|
||||||
{
|
|
||||||
if(seek(event, event_id, std::nothrow))
|
|
||||||
if(closure(event))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
const ircd::m::event &
|
||||||
ircd::m::room::messages::test(const event::id::closure_bool &closure)
|
ircd::m::room::messages::operator*()
|
||||||
const
|
|
||||||
{
|
{
|
||||||
auto it
|
return fetch(std::nothrow);
|
||||||
{
|
|
||||||
dbs::room_events.begin(room.room_id)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if(it) do
|
bool
|
||||||
|
ircd::m::room::messages::seek()
|
||||||
{
|
{
|
||||||
|
this->it = dbs::room_events.begin(room.room_id);
|
||||||
|
return bool(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
ircd::m::room::messages::seek(const event::id &event_id)
|
||||||
|
{
|
||||||
|
static constexpr auto idx
|
||||||
|
{
|
||||||
|
json::indexof<event, "depth"_>()
|
||||||
|
};
|
||||||
|
|
||||||
|
auto &column
|
||||||
|
{
|
||||||
|
dbs::event_column.at(idx)
|
||||||
|
};
|
||||||
|
|
||||||
|
uint64_t depth;
|
||||||
|
column(event_id, [&](const string_view &binary)
|
||||||
|
{
|
||||||
|
assert(size(binary) == sizeof(depth));
|
||||||
|
depth = byte_view<uint64_t>(binary);
|
||||||
|
});
|
||||||
|
|
||||||
|
char buf[m::state::KEY_MAX_SZ];
|
||||||
|
const auto seek_key
|
||||||
|
{
|
||||||
|
dbs::room_events_key(buf, room.room_id, depth, event_id)
|
||||||
|
};
|
||||||
|
|
||||||
|
this->it = dbs::room_events.begin(seek_key);
|
||||||
|
return bool(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
ircd::m::room::messages::seek(const uint64_t &depth)
|
||||||
|
{
|
||||||
|
char buf[m::state::KEY_MAX_SZ];
|
||||||
|
const auto seek_key
|
||||||
|
{
|
||||||
|
dbs::room_events_key(buf, room.room_id, depth)
|
||||||
|
};
|
||||||
|
|
||||||
|
this->it = dbs::room_events.begin(seek_key);
|
||||||
|
return bool(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ircd::m::event::id &
|
||||||
|
ircd::m::room::messages::event_id()
|
||||||
|
{
|
||||||
|
assert(bool(*this));
|
||||||
const auto &key{it->first};
|
const auto &key{it->first};
|
||||||
const auto part
|
const auto part{dbs::room_events_key(key)};
|
||||||
{
|
_event_id = std::get<1>(part);
|
||||||
dbs::room_events_key(key)
|
return _event_id;
|
||||||
};
|
|
||||||
|
|
||||||
const auto event_id
|
|
||||||
{
|
|
||||||
std::get<1>(part)
|
|
||||||
};
|
|
||||||
|
|
||||||
if(closure(event_id))
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
while(++it);
|
|
||||||
|
|
||||||
return false;
|
const ircd::m::event &
|
||||||
|
ircd::m::room::messages::fetch()
|
||||||
|
{
|
||||||
|
m::seek(_event, event_id());
|
||||||
|
return _event;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ircd::m::event &
|
||||||
|
ircd::m::room::messages::fetch(std::nothrow_t)
|
||||||
|
{
|
||||||
|
if(!m::seek(_event, event_id(), std::nothrow))
|
||||||
|
_event = m::event::fetch{};
|
||||||
|
|
||||||
|
return _event;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in a new issue