mirror of
https://github.com/matrix-construct/construct
synced 2025-01-14 00:34:18 +01:00
ircd:Ⓜ️:event::fetch: Improve fetch options related; minor cleanup.
This commit is contained in:
parent
2171cc23e8
commit
5ae495031f
5 changed files with 198 additions and 146 deletions
|
@ -11,6 +11,24 @@
|
|||
#pragma once
|
||||
#define HAVE_IRCD_M_EVENT_FETCH_H
|
||||
|
||||
/// Event Fetcher (local).
|
||||
///
|
||||
/// Fetches event data from the local database and populates an m::event which
|
||||
/// is a parent of this class; an instance of this object can be used as an
|
||||
/// `m::event` via that upcast. The data backing this `m::event` is a zero-copy
|
||||
/// reference into the database and its lifetime is governed by the internals
|
||||
/// of this object.
|
||||
///
|
||||
/// This can be constructed from either an event::idx or an `event_id`; the
|
||||
/// latter will incur an extra m::index() lookup. The constructor will throw
|
||||
/// for a missing event; the nothrow overloads will set a boolean indicator
|
||||
/// instead. A default constructor can also be used; after construction the
|
||||
/// seek() ADL suite can be used to the above effect.
|
||||
///
|
||||
/// The data is populated by one of two query types to the database; this is
|
||||
/// determined automatically by default, but can be configured further with
|
||||
/// the options structure.
|
||||
///
|
||||
struct ircd::m::event::fetch
|
||||
:event
|
||||
{
|
||||
|
@ -27,11 +45,15 @@ struct ircd::m::event::fetch
|
|||
bool valid;
|
||||
|
||||
public:
|
||||
fetch(const idx &, std::nothrow_t, const opts *const & = nullptr);
|
||||
fetch(const idx &, const opts *const & = nullptr);
|
||||
fetch(const id &, std::nothrow_t, const opts *const & = nullptr);
|
||||
fetch(const id &, const opts *const & = nullptr);
|
||||
fetch(const opts *const & = nullptr);
|
||||
fetch(const idx &, std::nothrow_t, const opts & = default_opts);
|
||||
fetch(const id &, std::nothrow_t, const opts & = default_opts);
|
||||
fetch(const id &, const opts & = default_opts);
|
||||
fetch(const idx &, const opts & = default_opts);
|
||||
fetch(const opts & = default_opts);
|
||||
fetch(fetch &&) = delete;
|
||||
fetch(const fetch &) = delete;
|
||||
fetch &operator=(fetch &&) = default;
|
||||
fetch &operator=(const fetch &) = delete;
|
||||
|
||||
static bool event_id(const idx &, std::nothrow_t, const id::closure &);
|
||||
static void event_id(const idx &, const id::closure &);
|
||||
|
@ -39,13 +61,35 @@ struct ircd::m::event::fetch
|
|||
|
||||
namespace ircd::m
|
||||
{
|
||||
bool seek(event::fetch &, const event::idx &, std::nothrow_t, const event::fetch::opts *const & = nullptr);
|
||||
void seek(event::fetch &, const event::idx &, const event::fetch::opts *const & = nullptr);
|
||||
bool seek(event::fetch &, const event::idx &, std::nothrow_t, const event::fetch::opts & = event::fetch::default_opts);
|
||||
void seek(event::fetch &, const event::idx &, const event::fetch::opts & = event::fetch::default_opts);
|
||||
|
||||
bool seek(event::fetch &, const event::id &, std::nothrow_t, const event::fetch::opts *const & = nullptr);
|
||||
void seek(event::fetch &, const event::id &, const event::fetch::opts *const & = nullptr);
|
||||
bool seek(event::fetch &, const event::id &, std::nothrow_t, const event::fetch::opts & = event::fetch::default_opts);
|
||||
void seek(event::fetch &, const event::id &, const event::fetch::opts & = event::fetch::default_opts);
|
||||
}
|
||||
|
||||
/// Event Fetch Options.
|
||||
///
|
||||
/// Refer to the individual member documentations for details. Notes:
|
||||
///
|
||||
/// - The default keys selection is *all keys*. This is unnecessarily
|
||||
/// expensive I/O for most uses of event::fetch; consider narrowing the keys
|
||||
/// selection based on what properties of the `m::event` will be accessed.
|
||||
///
|
||||
/// - Row Query: The event is populated by conducting a set of point lookups
|
||||
/// for the selected keys. The point lookups are parallelized so the latency
|
||||
/// of a lookup is only limited to the slowest key. The benefit of this type
|
||||
/// is that very efficient I/O and caching can be conducted, but the cost is
|
||||
/// that each lookup in the row occupies a hardware I/O lane which is a limited
|
||||
/// resource shared by the whole system.
|
||||
///
|
||||
/// - JSON Query: The event is populated by conducting a single point lookup
|
||||
/// to a database value containing the full JSON string of the event. This
|
||||
/// query is made when all keys are selected. The benefit of this query is that
|
||||
/// it only occupies one hardware I/O lane in contrast with the row query. The
|
||||
/// cost is that the full event JSON is read from storage (up to 64_KiB) and
|
||||
/// maintained in cache.
|
||||
///
|
||||
struct ircd::m::event::fetch::opts
|
||||
{
|
||||
/// Event property selector
|
||||
|
@ -54,16 +98,16 @@ struct ircd::m::event::fetch::opts
|
|||
/// Database get options passthru
|
||||
db::gopts gopts;
|
||||
|
||||
/// Whether to allowing querying the event_json to populate the event if
|
||||
/// it would be more efficient based on the keys being sought. Ex. If all
|
||||
/// keys are being sought then we can make a single query to event_json
|
||||
/// rather than a concurrent row query. This is enabled by default.
|
||||
bool query_json_maybe {true};
|
||||
/// Whether to allow querying the event_json to populate the event. A value
|
||||
/// of true only allows this type of query to be made; a value of false
|
||||
/// prevents this query from ever being made.
|
||||
bool query_json {true};
|
||||
|
||||
/// Whether to force only querying for event_json to populate the event,
|
||||
/// regardless of what keys are sought in the property selector. This is
|
||||
/// not enabled by default.
|
||||
bool query_json_only {false};
|
||||
/// Whether to force an attempt at populating the event from event_json
|
||||
/// first, bypassing any decision-making. This is useful if a key selection
|
||||
/// is used which would trigger a row query but the developer wants the
|
||||
/// json query anyway.
|
||||
bool query_json_force {false};
|
||||
|
||||
opts(const event::keys &, const db::gopts & = {});
|
||||
opts(const event::keys::selection &, const db::gopts & = {});
|
||||
|
|
220
ircd/m/event.cc
220
ircd/m/event.cc
|
@ -1402,113 +1402,6 @@ ircd::m::get(std::nothrow_t,
|
|||
return column(column_key, std::nothrow, closure);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::id &event_id,
|
||||
const event::fetch::opts *const &opts)
|
||||
{
|
||||
if(!seek(fetch, event_id, std::nothrow, opts))
|
||||
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,
|
||||
const event::fetch::opts *const &opts)
|
||||
{
|
||||
const auto &event_idx
|
||||
{
|
||||
index(event_id, std::nothrow)
|
||||
};
|
||||
|
||||
return event_idx?
|
||||
seek(fetch, event_idx, std::nothrow, opts):
|
||||
false;
|
||||
}
|
||||
|
||||
void
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::idx &event_idx,
|
||||
const event::fetch::opts *const &opts)
|
||||
{
|
||||
if(!seek(fetch, event_idx, std::nothrow, opts))
|
||||
throw m::NOT_FOUND
|
||||
{
|
||||
"%lu not found in database", event_idx
|
||||
};
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::idx &event_idx,
|
||||
std::nothrow_t,
|
||||
const event::fetch::opts *const &opts)
|
||||
{
|
||||
auto &event
|
||||
{
|
||||
static_cast<m::event &>(fetch)
|
||||
};
|
||||
|
||||
const string_view &key
|
||||
{
|
||||
byte_view<string_view>(event_idx)
|
||||
};
|
||||
|
||||
const db::gopts &gopts
|
||||
{
|
||||
opts?
|
||||
opts->gopts:
|
||||
db::gopts{}
|
||||
};
|
||||
|
||||
const bool query_json
|
||||
{
|
||||
opts && opts->query_json_only?
|
||||
true: // User only wants to make the event_json query
|
||||
|
||||
opts && !opts->query_json_maybe?
|
||||
false: // User never wants to make the event_json query
|
||||
|
||||
fetch.row.size() < fetch.cell.size()?
|
||||
false: // User is making specific column queries
|
||||
|
||||
fetch.row.size() == fetch.cell.size()?
|
||||
true: // User is querying all columns
|
||||
|
||||
false
|
||||
};
|
||||
|
||||
if(query_json)
|
||||
{
|
||||
if((fetch.valid = fetch._json.load(key, gopts)))
|
||||
event = m::event
|
||||
{
|
||||
fetch._json.val()
|
||||
};
|
||||
|
||||
if(fetch.valid)
|
||||
return fetch.valid;
|
||||
|
||||
if(opts && opts->query_json_only)
|
||||
return fetch.valid;
|
||||
|
||||
// graceful fallback to row query if json query failed.
|
||||
assert(!fetch.valid);
|
||||
}
|
||||
|
||||
if(!(fetch.valid = db::seek(fetch.row, key, gopts)))
|
||||
return fetch.valid;
|
||||
|
||||
if((fetch.valid = fetch.row.valid(key)))
|
||||
assign(event, fetch.row, key);
|
||||
|
||||
return fetch.valid;
|
||||
}
|
||||
|
||||
ircd::m::event::idx
|
||||
ircd::m::index(const event &event)
|
||||
try
|
||||
|
@ -1590,6 +1483,103 @@ ircd::m::index(const event::id &event_id,
|
|||
});
|
||||
}
|
||||
|
||||
void
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::id &event_id,
|
||||
const event::fetch::opts &opts)
|
||||
{
|
||||
if(!seek(fetch, event_id, std::nothrow, opts))
|
||||
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,
|
||||
const event::fetch::opts &opts)
|
||||
{
|
||||
const auto &event_idx
|
||||
{
|
||||
index(event_id, std::nothrow)
|
||||
};
|
||||
|
||||
return event_idx?
|
||||
seek(fetch, event_idx, std::nothrow, opts):
|
||||
false;
|
||||
}
|
||||
|
||||
void
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::idx &event_idx,
|
||||
const event::fetch::opts &opts)
|
||||
{
|
||||
if(!seek(fetch, event_idx, std::nothrow, opts))
|
||||
throw m::NOT_FOUND
|
||||
{
|
||||
"%lu not found in database", event_idx
|
||||
};
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::seek(event::fetch &fetch,
|
||||
const event::idx &event_idx,
|
||||
std::nothrow_t,
|
||||
const event::fetch::opts &opts)
|
||||
{
|
||||
auto &event
|
||||
{
|
||||
static_cast<m::event &>(fetch)
|
||||
};
|
||||
|
||||
const string_view &key
|
||||
{
|
||||
byte_view<string_view>(event_idx)
|
||||
};
|
||||
|
||||
const bool query_json
|
||||
{
|
||||
!opts.query_json?
|
||||
false: // User never wants to make the event_json query
|
||||
|
||||
opts.query_json_force?
|
||||
true: // User always wants to make the event_json query
|
||||
|
||||
fetch.row.size() < fetch.cell.size()?
|
||||
false: // User is making specific column queries
|
||||
|
||||
fetch.row.size() == fetch.cell.size()?
|
||||
true: // User is querying all columns
|
||||
|
||||
false
|
||||
};
|
||||
|
||||
if(query_json)
|
||||
{
|
||||
if((fetch.valid = fetch._json.load(key, opts.gopts)))
|
||||
event = m::event
|
||||
{
|
||||
fetch._json.val()
|
||||
};
|
||||
|
||||
if(fetch.valid)
|
||||
return fetch.valid;
|
||||
|
||||
// graceful fallback to row query if json query failed.
|
||||
assert(!fetch.valid);
|
||||
}
|
||||
|
||||
if(!(fetch.valid = db::seek(fetch.row, key, opts.gopts)))
|
||||
return fetch.valid;
|
||||
|
||||
if((fetch.valid = fetch.row.valid(key)))
|
||||
assign(event, fetch.row, key);
|
||||
|
||||
return fetch.valid;
|
||||
}
|
||||
|
||||
void
|
||||
ircd::m::event::fetch::event_id(const idx &idx,
|
||||
const id::closure &closure)
|
||||
|
@ -1610,20 +1600,20 @@ ircd::m::event::fetch::event_id(const idx &idx,
|
|||
}
|
||||
|
||||
/// Seekless constructor.
|
||||
ircd::m::event::fetch::fetch(const opts *const &opts)
|
||||
ircd::m::event::fetch::fetch(const opts &opts)
|
||||
:_json
|
||||
{
|
||||
m::dbs::event_json,
|
||||
string_view{},
|
||||
opts? opts->gopts : default_opts.gopts
|
||||
opts.gopts
|
||||
}
|
||||
,row
|
||||
{
|
||||
*dbs::events,
|
||||
string_view{},
|
||||
opts? opts->keys : default_opts.keys,
|
||||
opts.keys,
|
||||
cell,
|
||||
opts? opts->gopts : default_opts.gopts
|
||||
opts.gopts
|
||||
}
|
||||
,valid
|
||||
{
|
||||
|
@ -1635,7 +1625,7 @@ ircd::m::event::fetch::fetch(const opts *const &opts)
|
|||
/// 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,
|
||||
const opts *const &opts)
|
||||
const opts &opts)
|
||||
:fetch
|
||||
{
|
||||
index(event_id), opts
|
||||
|
@ -1647,7 +1637,7 @@ ircd::m::event::fetch::fetch(const event::id &event_id,
|
|||
/// Event is not populated if not found in database.
|
||||
ircd::m::event::fetch::fetch(const event::id &event_id,
|
||||
std::nothrow_t,
|
||||
const opts *const &opts)
|
||||
const opts &opts)
|
||||
:fetch
|
||||
{
|
||||
index(event_id, std::nothrow), std::nothrow, opts
|
||||
|
@ -1658,7 +1648,7 @@ ircd::m::event::fetch::fetch(const event::id &event_id,
|
|||
/// Seek to event_idx and populate this event from database.
|
||||
/// Throws if event not in database.
|
||||
ircd::m::event::fetch::fetch(const event::idx &event_idx,
|
||||
const opts *const &opts)
|
||||
const opts &opts)
|
||||
:fetch
|
||||
{
|
||||
event_idx, std::nothrow, opts
|
||||
|
@ -1675,7 +1665,7 @@ ircd::m::event::fetch::fetch(const event::idx &event_idx,
|
|||
/// Event is not populated if not found in database.
|
||||
ircd::m::event::fetch::fetch(const event::idx &event_idx,
|
||||
std::nothrow_t,
|
||||
const opts *const &opts)
|
||||
const opts &opts)
|
||||
:fetch
|
||||
{
|
||||
opts
|
||||
|
|
|
@ -1596,7 +1596,7 @@ ircd::m::events::for_each(const range &range,
|
|||
{
|
||||
event::fetch event
|
||||
{
|
||||
range.fopts
|
||||
range.fopts? *range.fopts : event::fetch::default_opts
|
||||
};
|
||||
|
||||
// When the fopts dictate no columns to fetch there is nothing more to do.
|
||||
|
|
|
@ -555,7 +555,7 @@ const
|
|||
{
|
||||
event::fetch event
|
||||
{
|
||||
fopts
|
||||
fopts? *fopts : event::fetch::default_opts
|
||||
};
|
||||
|
||||
return for_each(type, event::closure_idx_bool{[&closure, &event]
|
||||
|
@ -666,8 +666,10 @@ ircd::m::room::messages::messages(const m::room &room,
|
|||
,_event
|
||||
{
|
||||
fopts?
|
||||
fopts:
|
||||
room.fopts
|
||||
*fopts:
|
||||
room.fopts?
|
||||
*room.fopts:
|
||||
event::fetch::default_opts
|
||||
}
|
||||
{
|
||||
if(room.event_id)
|
||||
|
@ -683,8 +685,10 @@ ircd::m::room::messages::messages(const m::room &room,
|
|||
,_event
|
||||
{
|
||||
fopts?
|
||||
fopts:
|
||||
room.fopts
|
||||
*fopts:
|
||||
room.fopts?
|
||||
*room.fopts:
|
||||
event::fetch::default_opts
|
||||
}
|
||||
{
|
||||
seek(event_id);
|
||||
|
@ -696,7 +700,11 @@ ircd::m::room::messages::messages(const m::room &room,
|
|||
:room{room}
|
||||
,_event
|
||||
{
|
||||
fopts? fopts : room.fopts
|
||||
fopts?
|
||||
*fopts:
|
||||
room.fopts?
|
||||
*room.fopts:
|
||||
event::fetch::default_opts
|
||||
}
|
||||
{
|
||||
seek(depth);
|
||||
|
@ -834,8 +842,18 @@ ircd::m::room::messages::fetch()
|
|||
const ircd::m::event &
|
||||
ircd::m::room::messages::fetch(std::nothrow_t)
|
||||
{
|
||||
const event::fetch::opts &fopts
|
||||
{
|
||||
room.fopts?
|
||||
*room.fopts:
|
||||
event::fetch::default_opts
|
||||
};
|
||||
|
||||
if(!m::seek(_event, event_idx(), std::nothrow))
|
||||
_event = m::event::fetch{room.fopts};
|
||||
_event = m::event::fetch
|
||||
{
|
||||
fopts
|
||||
};
|
||||
|
||||
return _event;
|
||||
}
|
||||
|
@ -931,7 +949,7 @@ const
|
|||
{
|
||||
const event::fetch event
|
||||
{
|
||||
event_idx, fopts
|
||||
event_idx, fopts? *fopts : event::fetch::default_opts
|
||||
};
|
||||
|
||||
closure(event);
|
||||
|
@ -1014,7 +1032,7 @@ const
|
|||
{
|
||||
const event::fetch event
|
||||
{
|
||||
event_idx, std::nothrow, fopts
|
||||
event_idx, std::nothrow, fopts? *fopts : event::fetch::default_opts
|
||||
};
|
||||
|
||||
closure(event);
|
||||
|
@ -1132,7 +1150,7 @@ const
|
|||
{
|
||||
event::fetch event
|
||||
{
|
||||
fopts
|
||||
fopts? *fopts : event::fetch::default_opts
|
||||
};
|
||||
|
||||
return for_each(event::closure_idx_bool{[&event, &closure]
|
||||
|
@ -1299,7 +1317,7 @@ const
|
|||
{
|
||||
event::fetch event
|
||||
{
|
||||
fopts
|
||||
fopts? *fopts : event::fetch::default_opts
|
||||
};
|
||||
|
||||
return for_each(type, state_key_lb, event::closure_idx_bool{[&event, &closure]
|
||||
|
|
|
@ -94,7 +94,7 @@ ircd::m::sync::room_state_polylog_events(data &data)
|
|||
|
||||
const event::fetch event
|
||||
{
|
||||
event_idx, std::nothrow, &fopts
|
||||
event_idx, std::nothrow, fopts
|
||||
};
|
||||
|
||||
if(!event.valid)
|
||||
|
|
Loading…
Reference in a new issue