mirror of
https://github.com/matrix-construct/construct
synced 2025-02-16 16:50:12 +01:00
ircd:Ⓜ️ Add event::fetch: db::cell/db::row aggregate; apply to all pattern.
This commit is contained in:
parent
25ce5e76a6
commit
45f165b462
3 changed files with 107 additions and 55 deletions
|
@ -61,9 +61,8 @@ struct ircd::m::event
|
|||
json::property<name::type, json::string>
|
||||
>
|
||||
{
|
||||
struct fetch;
|
||||
struct sync;
|
||||
struct prev;
|
||||
struct fetch;
|
||||
|
||||
// Common convenience aliases
|
||||
using id = m::id::event;
|
||||
|
@ -105,6 +104,20 @@ struct ircd::m::event::prev
|
|||
};
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
struct ircd::m::event::fetch
|
||||
:event
|
||||
{
|
||||
std::array<db::cell, event::size()> cell;
|
||||
db::row row;
|
||||
|
||||
fetch(const event::id &, std::nothrow_t);
|
||||
fetch(const event::id &);
|
||||
fetch();
|
||||
|
||||
friend bool seek(fetch &, const event::id &, std::nothrow_t);
|
||||
friend void seek(fetch &, const event::id &);
|
||||
};
|
||||
|
||||
inline bool
|
||||
ircd::m::my(const event &event)
|
||||
{
|
||||
|
|
|
@ -336,3 +336,77 @@ ircd::m::event::event(const id &id,
|
|||
|
||||
new (this) m::event(obj);
|
||||
}
|
||||
|
||||
//
|
||||
// event::fetch
|
||||
//
|
||||
|
||||
void
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::id &event_id)
|
||||
{
|
||||
if(!seek(fetch, event_id, std::nothrow))
|
||||
throw m::NOT_FOUND
|
||||
{
|
||||
"%s not found in database", event_id
|
||||
};
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::id &event_id,
|
||||
std::nothrow_t)
|
||||
{
|
||||
db::seek(fetch.row, string_view{event_id});
|
||||
if(!fetch.row.valid(event_id))
|
||||
return false;
|
||||
|
||||
auto &event{static_cast<m::event &>(fetch)};
|
||||
assign(event, fetch.row, event_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
// db::row finds the layout of an event tuple because we pass this as a
|
||||
// reference argument to its constructor, rather than making db::row into
|
||||
// a template type.
|
||||
const ircd::m::event
|
||||
_dummy_event_;
|
||||
|
||||
/// Seekless constructor.
|
||||
ircd::m::event::fetch::fetch()
|
||||
:row
|
||||
{
|
||||
*dbs::events, string_view{}, _dummy_event_, cell
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
/// Seek to event_id and populate this event from database.
|
||||
/// Throws if event not in database.
|
||||
ircd::m::event::fetch::fetch(const event::id &event_id)
|
||||
:row
|
||||
{
|
||||
*dbs::events, event_id, _dummy_event_, cell
|
||||
}
|
||||
{
|
||||
if(!row.valid(event_id))
|
||||
throw m::NOT_FOUND
|
||||
{
|
||||
"%s not found in database", event_id
|
||||
};
|
||||
|
||||
assign(*this, row, event_id);
|
||||
}
|
||||
|
||||
/// Seek to event_id and populate this event from database.
|
||||
/// Event is not populated if not found in database.
|
||||
ircd::m::event::fetch::fetch(const event::id &event_id,
|
||||
std::nothrow_t)
|
||||
:row
|
||||
{
|
||||
*dbs::events, event_id, _dummy_event_, cell
|
||||
}
|
||||
{
|
||||
if(row.valid(event_id))
|
||||
assign(*this, row, event_id);
|
||||
}
|
||||
|
|
|
@ -361,7 +361,7 @@ const
|
|||
|
||||
bool
|
||||
ircd::m::room::prev(const event::closure &closure)
|
||||
const
|
||||
const try
|
||||
{
|
||||
auto it
|
||||
{
|
||||
|
@ -377,44 +377,34 @@ const
|
|||
dbs::room_events_key(key)
|
||||
};
|
||||
|
||||
const string_view event_id
|
||||
const auto event_id
|
||||
{
|
||||
std::get<1>(part)
|
||||
};
|
||||
|
||||
std::array<db::cell, dbs::event_columns> cell;
|
||||
static const event _dummy_;
|
||||
db::row row
|
||||
const event::fetch event
|
||||
{
|
||||
*dbs::events, event_id, _dummy_, cell
|
||||
event_id
|
||||
};
|
||||
|
||||
seek(row, event_id);
|
||||
if(!row.valid(event_id))
|
||||
return false;
|
||||
|
||||
m::event event;
|
||||
assign(event, row, event_id);
|
||||
closure(event);
|
||||
return true;
|
||||
}
|
||||
catch(const NOT_FOUND &)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::room::get(const event::closure &closure)
|
||||
const
|
||||
{
|
||||
std::array<db::cell, dbs::event_columns> cell;
|
||||
static const event _dummy_;
|
||||
db::row row
|
||||
{
|
||||
*dbs::events, string_view{}, _dummy_, cell
|
||||
};
|
||||
|
||||
auto it
|
||||
{
|
||||
dbs::room_events.begin(room_id)
|
||||
};
|
||||
|
||||
event::fetch event;
|
||||
if(it) do
|
||||
{
|
||||
const auto &key{it->first};
|
||||
|
@ -423,18 +413,13 @@ const
|
|||
dbs::room_events_key(key)
|
||||
};
|
||||
|
||||
const string_view event_id
|
||||
const auto event_id
|
||||
{
|
||||
std::get<1>(part)
|
||||
};
|
||||
|
||||
seek(row, event_id);
|
||||
if(!row.valid(event_id))
|
||||
return false;
|
||||
|
||||
m::event event;
|
||||
assign(event, row, event_id);
|
||||
closure(event);
|
||||
if(seek(event, event_id, std::nothrow))
|
||||
closure(event);
|
||||
}
|
||||
while(++it); else return false;
|
||||
|
||||
|
@ -475,20 +460,9 @@ const
|
|||
m::state::get(state_root, type, state_key, [&closure, &ret]
|
||||
(const string_view &event_id)
|
||||
{
|
||||
static const event _dummy_;
|
||||
std::array<db::cell, dbs::event_columns> cell;
|
||||
db::row row
|
||||
{
|
||||
*dbs::events, event_id, _dummy_, cell
|
||||
};
|
||||
|
||||
if(!row.valid(event_id))
|
||||
return;
|
||||
|
||||
m::event event;
|
||||
assign(event, row, event_id);
|
||||
ret = true;
|
||||
closure(event);
|
||||
event::fetch event;
|
||||
if(seek(event, event_id, std::nothrow))
|
||||
closure(event);
|
||||
});
|
||||
|
||||
return ret;
|
||||
|
@ -571,14 +545,8 @@ const
|
|||
dbs::state_root(state_root_buf, room.room_id, event_id)
|
||||
};
|
||||
|
||||
std::array<db::cell, dbs::event_columns> cell;
|
||||
static const event _dummy_;
|
||||
db::row row
|
||||
{
|
||||
*dbs::events, string_view{}, _dummy_, cell
|
||||
};
|
||||
|
||||
return m::state::each(state_root, "m.room.member", [&cell, &row, &view]
|
||||
event::fetch event;
|
||||
return m::state::each(state_root, "m.room.member", [&event, &view]
|
||||
(const json::array &key, const string_view &val)
|
||||
{
|
||||
const string_view event_id
|
||||
|
@ -586,12 +554,9 @@ const
|
|||
unquote(val)
|
||||
};
|
||||
|
||||
seek(row, event_id);
|
||||
if(!row.valid(event_id))
|
||||
if(!seek(event, event_id, std::nothrow))
|
||||
return false;
|
||||
|
||||
m::event event;
|
||||
assign(event, row, event_id);
|
||||
return view(event);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue